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

Commit a41491f

Browse files
authored
Merge pull request #319 from ajnavarro/fix/parents-with-path
references.go: fix Parents from commit iterator
2 parents c68ac9e + bc4bbee commit a41491f

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

references.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ func walkGraph(result *[]*object.Commit, seen *map[plumbing.Hash]struct{}, curre
7676

7777
// optimization: don't traverse branches that does not
7878
// contain the path.
79-
parents := parentsContainingPath(path, current)
80-
79+
parents, err := parentsContainingPath(path, current)
80+
if err != nil {
81+
return err
82+
}
8183
switch len(parents) {
8284
// if the path is not found in any of its parents, the path was
8385
// created by this commit; we must add it to the revisions list and
@@ -110,18 +112,18 @@ func walkGraph(result *[]*object.Commit, seen *map[plumbing.Hash]struct{}, curre
110112
return nil
111113
}
112114

113-
func parentsContainingPath(path string, c *object.Commit) []*object.Commit {
115+
func parentsContainingPath(path string, c *object.Commit) ([]*object.Commit, error) {
114116
// TODO: benchmark this method making git.object.Commit.parent public instead of using
115117
// an iterator
116118
var result []*object.Commit
117119
iter := c.Parents()
118120
for {
119121
parent, err := iter.Next()
122+
if err == io.EOF {
123+
return result, nil
124+
}
120125
if err != nil {
121-
if err == io.EOF {
122-
return result
123-
}
124-
panic("unreachable")
126+
return nil, err
125127
}
126128
if _, err := parent.File(path); err == nil {
127129
result = append(result, parent)

references_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/src-d/go-git-fixtures"
88
"gopkg.in/src-d/go-git.v4/plumbing"
99
"gopkg.in/src-d/go-git.v4/plumbing/object"
10+
"gopkg.in/src-d/go-git.v4/storage/memory"
1011

1112
. "gopkg.in/check.v1"
1213
)
@@ -287,6 +288,26 @@ var referencesTests = [...]struct {
287288
*/
288289
}
289290

291+
func (s *ReferencesSuite) TestObjectNotFoundError(c *C) {
292+
h1 := plumbing.NewHash("af2d6a6954d532f8ffb47615169c8fdf9d383a1a")
293+
hParent := plumbing.NewHash("1669dce138d9b841a518c64b10914d88f5e488ea")
294+
295+
url := fixtures.ByURL("https://github.com/git-fixtures/basic.git").One().DotGit().Base()
296+
storer := memory.NewStorage()
297+
r, err := Clone(storer, nil, &CloneOptions{
298+
URL: "file://" + url,
299+
})
300+
c.Assert(err, IsNil)
301+
302+
delete(storer.Objects, hParent)
303+
304+
commit, err := r.CommitObject(h1)
305+
c.Assert(err, IsNil)
306+
307+
_, err = references(commit, "LICENSE")
308+
c.Assert(err, Equals, plumbing.ErrObjectNotFound)
309+
}
310+
290311
func (s *ReferencesSuite) TestRevList(c *C) {
291312
for _, t := range referencesTests {
292313
r := s.NewRepositoryFromPackfile(fixtures.ByURL(t.repo).One())

0 commit comments

Comments
 (0)