Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

references.go: fix Parents from commit iterator #319

Merged
merged 2 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions references.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ func walkGraph(result *[]*object.Commit, seen *map[plumbing.Hash]struct{}, curre

// optimization: don't traverse branches that does not
// contain the path.
parents := parentsContainingPath(path, current)

parents, err := parentsContainingPath(path, current)
if err != nil {
return err
}
switch len(parents) {
// if the path is not found in any of its parents, the path was
// created by this commit; we must add it to the revisions list and
Expand Down Expand Up @@ -110,18 +112,18 @@ func walkGraph(result *[]*object.Commit, seen *map[plumbing.Hash]struct{}, curre
return nil
}

func parentsContainingPath(path string, c *object.Commit) []*object.Commit {
func parentsContainingPath(path string, c *object.Commit) ([]*object.Commit, error) {
// TODO: benchmark this method making git.object.Commit.parent public instead of using
// an iterator
var result []*object.Commit
iter := c.Parents()
for {
parent, err := iter.Next()
if err == io.EOF {
return result, nil
}
if err != nil {
if err == io.EOF {
return result
}
panic("unreachable")
return nil, err
}
if _, err := parent.File(path); err == nil {
result = append(result, parent)
Expand Down
21 changes: 21 additions & 0 deletions references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/src-d/go-git-fixtures"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/storage/memory"

. "gopkg.in/check.v1"
)
Expand Down Expand Up @@ -287,6 +288,26 @@ var referencesTests = [...]struct {
*/
}

func (s *ReferencesSuite) TestObjectNotFoundError(c *C) {
h1 := plumbing.NewHash("af2d6a6954d532f8ffb47615169c8fdf9d383a1a")
hParent := plumbing.NewHash("1669dce138d9b841a518c64b10914d88f5e488ea")

url := fixtures.ByURL("https://github.com/git-fixtures/basic.git").One().DotGit().Base()
storer := memory.NewStorage()
r, err := Clone(storer, nil, &CloneOptions{
URL: "file://" + url,
})
c.Assert(err, IsNil)

delete(storer.Objects, hParent)

commit, err := r.CommitObject(h1)
c.Assert(err, IsNil)

_, err = references(commit, "LICENSE")
c.Assert(err, Equals, plumbing.ErrObjectNotFound)
}

func (s *ReferencesSuite) TestRevList(c *C) {
for _, t := range referencesTests {
r := s.NewRepositoryFromPackfile(fixtures.ByURL(t.repo).One())
Expand Down