diff options
author | vince <vincetiu8@gmail.com> | 2020-09-08 20:45:03 +0800 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-09-29 20:42:21 +0200 |
commit | 27e70af23692a70fcb129370235e46eaf33b8cfd (patch) | |
tree | c35dabdcb3380520a7d502850f1be3b5a17c46bd /repository | |
parent | d4f1d5659b9d23ff0768ed584ae320cb657c6f2d (diff) | |
download | git-bug-27e70af23692a70fcb129370235e46eaf33b8cfd.tar.gz |
fix ListCommits implementation
Diffstat (limited to 'repository')
-rw-r--r-- | repository/gogit.go | 34 | ||||
-rw-r--r-- | repository/gogit_test.go | 2 |
2 files changed, 27 insertions, 9 deletions
diff --git a/repository/gogit.go b/repository/gogit.go index 78bae1f9..a93e8576 100644 --- a/repository/gogit.go +++ b/repository/gogit.go @@ -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 } diff --git a/repository/gogit_test.go b/repository/gogit_test.go index 9dcf109f..fba990d3 100644 --- a/repository/gogit_test.go +++ b/repository/gogit_test.go @@ -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) } } } |