aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2019-08-04 17:46:36 +0200
committerGitHub <noreply@github.com>2019-08-04 17:46:36 +0200
commit6f354807f63e3479e41e56d72866291eeac9251e (patch)
tree1977bca41aa85e2c57bf48f6c6001ebeec33204b
parent0d1a009cbb604db18be960db5f1525b99a55d727 (diff)
parenta2af865f9dbf292a9804c67abc2727551f7954dd (diff)
downloadgo-git-6f354807f63e3479e41e56d72866291eeac9251e.tar.gz
Merge pull request #1204 from knqyf263/fix/handle_eof_err
Handle EOF error in commitFileIter.ForEach
-rw-r--r--plumbing/object/commit_walker_file.go4
-rw-r--r--repository_test.go32
2 files changed, 33 insertions, 3 deletions
diff --git a/plumbing/object/commit_walker_file.go b/plumbing/object/commit_walker_file.go
index 6f16e61..b73e4ce 100644
--- a/plumbing/object/commit_walker_file.go
+++ b/plumbing/object/commit_walker_file.go
@@ -128,6 +128,9 @@ func isParentHash(hash plumbing.Hash, commit *Commit) bool {
func (c *commitFileIter) ForEach(cb func(*Commit) error) error {
for {
commit, nextErr := c.Next()
+ if nextErr == io.EOF {
+ break
+ }
if nextErr != nil {
return nextErr
}
@@ -138,6 +141,7 @@ func (c *commitFileIter) ForEach(cb func(*Commit) error) error {
return err
}
}
+ return nil
}
func (c *commitFileIter) Close() {
diff --git a/repository_test.go b/repository_test.go
index b87eabb..5fc6aeb 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -3,6 +3,7 @@ package git
import (
"bytes"
"context"
+ "errors"
"fmt"
"io"
"io/ioutil"
@@ -1507,12 +1508,13 @@ func (s *RepositorySuite) TestLogFileForEach(c *C) {
}
expectedIndex := 0
- cIter.ForEach(func(commit *object.Commit) error {
+ err = cIter.ForEach(func(commit *object.Commit) error {
expectedCommitHash := commitOrder[expectedIndex]
c.Assert(commit.Hash.String(), Equals, expectedCommitHash.String())
expectedIndex++
return nil
})
+ c.Assert(err, IsNil)
c.Assert(expectedIndex, Equals, 1)
}
@@ -1551,12 +1553,13 @@ func (s *RepositorySuite) TestLogAllFileForEach(c *C) {
}
expectedIndex := 0
- cIter.ForEach(func(commit *object.Commit) error {
+ err = cIter.ForEach(func(commit *object.Commit) error {
expectedCommitHash := commitOrder[expectedIndex]
c.Assert(commit.Hash.String(), Equals, expectedCommitHash.String())
expectedIndex++
return nil
})
+ c.Assert(err, IsNil)
c.Assert(expectedIndex, Equals, 1)
}
@@ -1598,12 +1601,13 @@ func (s *RepositorySuite) TestLogFileInitialCommit(c *C) {
}
expectedIndex := 0
- cIter.ForEach(func(commit *object.Commit) error {
+ err = cIter.ForEach(func(commit *object.Commit) error {
expectedCommitHash := commitOrder[expectedIndex]
c.Assert(commit.Hash.String(), Equals, expectedCommitHash.String())
expectedIndex++
return nil
})
+ c.Assert(err, IsNil)
c.Assert(expectedIndex, Equals, 1)
}
@@ -1649,6 +1653,28 @@ func (s *RepositorySuite) TestLogFileWithOtherParamsPass(c *C) {
c.Assert(iterErr, Equals, io.EOF)
}
+type mockErrCommitIter struct{}
+
+func (m *mockErrCommitIter) Next() (*object.Commit, error) {
+ return nil, errors.New("mock next error")
+}
+func (m *mockErrCommitIter) ForEach(func(*object.Commit) error) error {
+ return errors.New("mock foreach error")
+}
+
+func (m *mockErrCommitIter) Close() {}
+
+func (s *RepositorySuite) TestLogFileWithError(c *C) {
+ fileName := "README"
+ cIter := object.NewCommitFileIterFromIter(fileName, &mockErrCommitIter{}, false)
+ defer cIter.Close()
+
+ err := cIter.ForEach(func(commit *object.Commit) error {
+ return nil
+ })
+ c.Assert(err, NotNil)
+}
+
func (s *RepositorySuite) TestCommit(c *C) {
r, _ := Init(memory.NewStorage(), nil)
err := r.clone(context.Background(), &CloneOptions{