aboutsummaryrefslogtreecommitdiffstats
path: root/file.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2016-08-15 23:09:33 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2016-08-15 23:09:33 +0200
commit6b9a59be60de5b66aee14e9160ace80734008eca (patch)
treec302776360b0fd4f774f67f177870fda478b4258 /file.go
parented2e3b299e03e4bfd4c37bf5232e9fde05c0600d (diff)
downloadgo-git-6b9a59be60de5b66aee14e9160ace80734008eca.tar.gz
core: *Iter.ForEach method
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()
}