Skip to content

Commit

Permalink
Merge pull request src-d#1204 from knqyf263/fix/handle_eof_err
Browse files Browse the repository at this point in the history
Handle EOF error in commitFileIter.ForEach
  • Loading branch information
mcuadros authored Aug 4, 2019
2 parents 0d1a009 + a2af865 commit 6f35480
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 4 additions & 0 deletions plumbing/object/commit_walker_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -138,6 +141,7 @@ func (c *commitFileIter) ForEach(cb func(*Commit) error) error {
return err
}
}
return nil
}

func (c *commitFileIter) Close() {
Expand Down
32 changes: 29 additions & 3 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package git
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 6f35480

Please sign in to comment.