Removed file git history (#128)

This commit is contained in:
Domas Monkus 2020-10-09 10:52:05 +03:00 committed by GitHub
parent 57f7843ae1
commit 5eb33f8675
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 171 additions and 44 deletions

View file

@ -8,8 +8,10 @@ import (
"os"
"path/filepath"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/pkg/errors"
)
// ErrNotFound signifies the file was not found.
@ -22,6 +24,9 @@ func FileHistory(path string, repo *git.Repository) ([]string, error) {
FileName: &path,
}
commits, err := repo.Log(&logOpts)
if errors.Is(err, plumbing.ErrReferenceNotFound) {
return nil, ErrNotFound
}
if err != nil {
return nil, fmt.Errorf("failed to get commits for path %q: %v", path, err)
}

View file

@ -3,8 +3,8 @@ package git_test
import (
"testing"
git "github.com/go-git/go-git/v5"
"github.com/stretchr/testify/assert"
git "gopkg.in/src-d/go-git.v4"
gitutil "github.com/mattermost/mattermost-plugin-starter-template/build/sync/plan/git"
)
@ -16,6 +16,7 @@ func TestFileHistory(t *testing.T) {
DetectDotGit: true,
})
assert.Nil(err)
sums, err := gitutil.FileHistory("build/sync/plan/git/testdata/testfile.txt", repo)
assert.Nil(err)
assert.Contains(sums, "ba7192052d7cf77c55d3b7bf40b350b8431b208b")
@ -24,4 +25,9 @@ func TestFileHistory(t *testing.T) {
sums, err = gitutil.FileHistory("build/sync/plan/git/testdata/nosuch_testfile.txt", repo)
assert.Equal(gitutil.ErrNotFound, err)
assert.Nil(sums)
// Calling with a non-existent file that was in git history returns no error.
sums, err = gitutil.FileHistory("build/sync/plan/git/testdata/removedfile.txt", repo)
assert.Nil(err)
assert.Equal([]string{"213df5d04c108c99d3ec9ffe43a53f638f0ede0b"}, sums)
}