Skip to content

Commit 6f35480

Browse files
authored
Merge pull request src-d#1204 from knqyf263/fix/handle_eof_err
Handle EOF error in commitFileIter.ForEach
2 parents 0d1a009 + a2af865 commit 6f35480

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

plumbing/object/commit_walker_file.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ func isParentHash(hash plumbing.Hash, commit *Commit) bool {
128128
func (c *commitFileIter) ForEach(cb func(*Commit) error) error {
129129
for {
130130
commit, nextErr := c.Next()
131+
if nextErr == io.EOF {
132+
break
133+
}
131134
if nextErr != nil {
132135
return nextErr
133136
}
@@ -138,6 +141,7 @@ func (c *commitFileIter) ForEach(cb func(*Commit) error) error {
138141
return err
139142
}
140143
}
144+
return nil
141145
}
142146

143147
func (c *commitFileIter) Close() {

repository_test.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package git
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"fmt"
78
"io"
89
"io/ioutil"
@@ -1507,12 +1508,13 @@ func (s *RepositorySuite) TestLogFileForEach(c *C) {
15071508
}
15081509

15091510
expectedIndex := 0
1510-
cIter.ForEach(func(commit *object.Commit) error {
1511+
err = cIter.ForEach(func(commit *object.Commit) error {
15111512
expectedCommitHash := commitOrder[expectedIndex]
15121513
c.Assert(commit.Hash.String(), Equals, expectedCommitHash.String())
15131514
expectedIndex++
15141515
return nil
15151516
})
1517+
c.Assert(err, IsNil)
15161518
c.Assert(expectedIndex, Equals, 1)
15171519
}
15181520

@@ -1551,12 +1553,13 @@ func (s *RepositorySuite) TestLogAllFileForEach(c *C) {
15511553
}
15521554

15531555
expectedIndex := 0
1554-
cIter.ForEach(func(commit *object.Commit) error {
1556+
err = cIter.ForEach(func(commit *object.Commit) error {
15551557
expectedCommitHash := commitOrder[expectedIndex]
15561558
c.Assert(commit.Hash.String(), Equals, expectedCommitHash.String())
15571559
expectedIndex++
15581560
return nil
15591561
})
1562+
c.Assert(err, IsNil)
15601563
c.Assert(expectedIndex, Equals, 1)
15611564
}
15621565

@@ -1598,12 +1601,13 @@ func (s *RepositorySuite) TestLogFileInitialCommit(c *C) {
15981601
}
15991602

16001603
expectedIndex := 0
1601-
cIter.ForEach(func(commit *object.Commit) error {
1604+
err = cIter.ForEach(func(commit *object.Commit) error {
16021605
expectedCommitHash := commitOrder[expectedIndex]
16031606
c.Assert(commit.Hash.String(), Equals, expectedCommitHash.String())
16041607
expectedIndex++
16051608
return nil
16061609
})
1610+
c.Assert(err, IsNil)
16071611
c.Assert(expectedIndex, Equals, 1)
16081612
}
16091613

@@ -1649,6 +1653,28 @@ func (s *RepositorySuite) TestLogFileWithOtherParamsPass(c *C) {
16491653
c.Assert(iterErr, Equals, io.EOF)
16501654
}
16511655

1656+
type mockErrCommitIter struct{}
1657+
1658+
func (m *mockErrCommitIter) Next() (*object.Commit, error) {
1659+
return nil, errors.New("mock next error")
1660+
}
1661+
func (m *mockErrCommitIter) ForEach(func(*object.Commit) error) error {
1662+
return errors.New("mock foreach error")
1663+
}
1664+
1665+
func (m *mockErrCommitIter) Close() {}
1666+
1667+
func (s *RepositorySuite) TestLogFileWithError(c *C) {
1668+
fileName := "README"
1669+
cIter := object.NewCommitFileIterFromIter(fileName, &mockErrCommitIter{}, false)
1670+
defer cIter.Close()
1671+
1672+
err := cIter.ForEach(func(commit *object.Commit) error {
1673+
return nil
1674+
})
1675+
c.Assert(err, NotNil)
1676+
}
1677+
16521678
func (s *RepositorySuite) TestCommit(c *C) {
16531679
r, _ := Init(memory.NewStorage(), nil)
16541680
err := r.clone(context.Background(), &CloneOptions{

0 commit comments

Comments
 (0)