aboutsummaryrefslogtreecommitdiffstats
path: root/file.go
diff options
context:
space:
mode:
Diffstat (limited to 'file.go')
-rw-r--r--file.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/file.go b/file.go
index 35bbdc5..58c76d2 100644
--- a/file.go
+++ b/file.go
@@ -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()
}