diff options
Diffstat (limited to 'file.go')
-rw-r--r-- | file.go | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -2,8 +2,11 @@ package git import ( "bytes" + "io" "os" "strings" + + "gopkg.in/src-d/go-git.v4/core" ) // File represents git file objects. @@ -70,6 +73,33 @@ func (iter *FileIter) Next() (*File, error) { } } +// ForEach call the cb function for each file contained on this iter until +// an error happends or the end of the iter is reached. If core.ErrStop is sent +// the iteration is stop but no error is returned +func (iter *FileIter) ForEach(cb func(*File) error) error { + i := &FileIter{w: *NewTreeWalker(iter.w.r, iter.w.t)} + defer i.Close() + + for { + f, err := i.Next() + if err != nil { + if err == io.EOF { + return nil + } + + return err + } + + if err := cb(f); err != nil { + if err == core.ErrStop { + return nil + } + + return err + } + } +} + func (iter *FileIter) Close() { iter.w.Close() } |