Skip to content
Snippets Groups Projects
Commit e0206373 authored by Nick Thomas's avatar Nick Thomas
Browse files

SHA ranges are x..y - add tests and emulate this with go-git

parent e0c6200b
No related branches found
No related tags found
1 merge request!1Initial implementation of an elasticsearch indexer in Go
Pipeline #
Loading
Loading
@@ -38,7 +38,7 @@ func NewGoGitRepository(projectPath string, fromSHA string, toSHA string) (*goGi
} else {
out.FromHash = plumbing.NewHash(fromSHA)
 
commit, err := out.Repository.CommitObject(out.FromHash)
commit, err := repo.CommitObject(out.FromHash)
if err != nil {
return nil, fmt.Errorf("Bad from SHA (%s): %s", out.FromHash, err)
}
Loading
Loading
@@ -149,10 +149,15 @@ func (r *goGitRepository) EachFileChange(ins, mod, del FileFunc) error {
return nil
}
 
// EachCommit runs `f` for each commit within `fromSHA`...`toSHA` (i.e., the
// inclusive set).
// EachCommit runs `f` for each commit within `fromSHA`..`toSHA`
// go-git doesn't directly support ranges of revisions, so we emulate this by
// walking the commit history between from and to
func (r *goGitRepository) EachCommit(f CommitFunc) error {
err := object.WalkCommitHistory(r.ToCommit, func(c *object.Commit) error {
err := object.WalkCommitHistoryPost(r.ToCommit, func(c *object.Commit) error {
if r.FromCommit != nil && c.ID() == r.FromCommit.ID() {
return endError
}
}
 
commit := &Commit{
Message: c.Message,
Loading
Loading
@@ -164,10 +169,6 @@ func (r *goGitRepository) EachCommit(f CommitFunc) error {
return err
}
 
if r.FromCommit != nil && c.ID() == r.FromCommit.ID() {
return endError
}
return nil
})
 
Loading
Loading
Loading
Loading
@@ -29,20 +29,26 @@ func checkDeps(t *testing.T) {
}
}
 
func TestEachCommit(t *testing.T) {
checkDeps(t)
repo, err := git.NewGoGitRepository(*testRepo, "", headSHA)
assert.NoError(t, err)
func runEachCommit(repo git.Repository) (map[string]*git.Commit, []string, error) {
commits := make(map[string]*git.Commit)
commitHashes := []string{}
 
err = repo.EachCommit(func(commit *git.Commit) error {
err := repo.EachCommit(func(commit *git.Commit) error {
commits[commit.Hash] = commit
commitHashes = append(commitHashes, commit.Hash)
return nil
})
return commits, commitHashes, err
}
func TestEachCommit(t *testing.T) {
checkDeps(t)
repo, err := git.NewGoGitRepository(*testRepo, "", headSHA)
assert.NoError(t, err)
commits, commitHashes, err := runEachCommit(repo)
assert.NoError(t, err)
 
expectedCommits := []string{
Loading
Loading
@@ -109,6 +115,45 @@ func TestEachCommit(t *testing.T) {
assert.Equal(t, dmitriy, commit.Author)
}
 
func TestEachCommitGivenRangeOf3Commits(t *testing.T) {
checkDeps(t)
repo, err := git.NewGoGitRepository(*testRepo, "1b12f15a11fc6e62177bef08f47bc7b5ce50b141", headSHA)
assert.NoError(t, err)
_, commitHashes, err := runEachCommit(repo)
assert.NoError(t, err)
expected := []string{"498214de67004b1da3d820901307bed2a68a8ef6", headSHA}
sort.Strings(expected)
sort.Strings(commitHashes)
assert.Equal(t, expected, commitHashes)
}
func TestEachCommitGivenRangeOf2Commits(t *testing.T) {
checkDeps(t)
repo, err := git.NewGoGitRepository(*testRepo, "498214de67004b1da3d820901307bed2a68a8ef6", headSHA)
assert.NoError(t, err)
_, commitHashes, err := runEachCommit(repo)
assert.NoError(t, err)
assert.Equal(t, []string{headSHA}, commitHashes)
}
func TestEachCommitGivenRangeOf1Commit(t *testing.T) {
checkDeps(t)
repo, err := git.NewGoGitRepository(*testRepo, headSHA, headSHA)
assert.NoError(t, err)
_, commitHashes, err := runEachCommit(repo)
assert.NoError(t, err)
assert.Equal(t, []string{}, commitHashes)
}
func TestEachFileChange(t *testing.T) {
checkDeps(t)
 
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment