fix ListCommits implementation

This commit is contained in:
vince 2020-09-08 20:45:03 +08:00 committed by Michael Muré
parent d4f1d5659b
commit 27e70af236
2 changed files with 27 additions and 9 deletions

View File

@ -453,24 +453,42 @@ func (repo *GoGitRepo) RefExist(ref string) (bool, error) {
}
func (repo *GoGitRepo) CopyRef(source string, dest string) error {
return repo.r.Storer.SetReference(plumbing.NewHashReference(plumbing.ReferenceName(dest), plumbing.NewHash(source)))
r, err := repo.r.Reference(plumbing.ReferenceName(source), false)
if err != nil {
return err
}
return repo.r.Storer.SetReference(plumbing.NewHashReference(plumbing.ReferenceName(dest), r.Hash()))
}
func (repo *GoGitRepo) ListCommits(ref string) ([]Hash, error) {
commitIter, err := repo.r.CommitObjects()
r, err := repo.r.Reference(plumbing.ReferenceName(ref), false)
if err != nil {
return nil, err
}
var commits []Hash
err = commitIter.ForEach(func(commit *object.Commit) error {
commits = append(commits, Hash(commit.Hash.String()))
return nil
})
commit, err := repo.r.CommitObject(r.Hash())
if err != nil {
return nil, err
}
commits := []Hash{Hash(commit.Hash.String())}
for {
commit, err = commit.Parent(0)
if err != nil {
if err == object.ErrParentNotFound {
break
}
return nil, err
}
if commit.NumParents() > 1 {
return nil, fmt.Errorf("multiple parents")
}
commits = append(commits, Hash(commit.Hash.String()))
}
return commits, nil
}

View File

@ -58,7 +58,7 @@ func TestNewGoGitRepo(t *testing.T) {
require.Error(t, err, i)
} else {
require.NoError(t, err, i)
assert.Equal(t, tc.outPath, r.GetPath(), i)
assert.Equal(t, filepath.ToSlash(tc.outPath), filepath.ToSlash(r.GetPath()), i)
}
}
}