diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-15 23:09:33 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-15 23:09:33 +0200 |
commit | 6b9a59be60de5b66aee14e9160ace80734008eca (patch) | |
tree | c302776360b0fd4f774f67f177870fda478b4258 | |
parent | ed2e3b299e03e4bfd4c37bf5232e9fde05c0600d (diff) | |
download | go-git-6b9a59be60de5b66aee14e9160ace80734008eca.tar.gz |
core: *Iter.ForEach method
-rw-r--r-- | commit.go | 14 | ||||
-rw-r--r-- | core/object.go | 39 | ||||
-rw-r--r-- | core/reference.go | 17 | ||||
-rw-r--r-- | core/reference_test.go | 61 | ||||
-rw-r--r-- | core/storage.go | 7 | ||||
-rw-r--r-- | examples/basic/main.go | 19 | ||||
-rw-r--r-- | file.go | 30 | ||||
-rw-r--r-- | repository.go | 37 | ||||
-rw-r--r-- | tag.go | 14 | ||||
-rw-r--r-- | tree.go | 27 | ||||
-rw-r--r-- | tree_walker.go | 2 |
11 files changed, 223 insertions, 44 deletions
@@ -154,6 +154,20 @@ func (iter *CommitIter) Next() (*Commit, error) { return commit, commit.Decode(obj) } +// ForEach call the cb function for each commit contained on this iter until +// an error happends or the end of the iter is reached. If ErrStop is sent +// the iteration is stop but no error is returned +func (iter *CommitIter) ForEach(cb func(*Commit) error) error { + return iter.ObjectIter.ForEach(func(obj core.Object) error { + commit := &Commit{r: iter.r} + if err := commit.Decode(obj); err != nil { + return err + } + + return cb(commit) + }) +} + type commitSorterer struct { l []*Commit } diff --git a/core/object.go b/core/object.go index 36b669e..d777f4a 100644 --- a/core/object.go +++ b/core/object.go @@ -139,6 +139,28 @@ func (iter *ObjectLookupIter) Next() (Object, error) { return obj, err } +// ForEach call the cb function for each object contained on this iter until +// an error happends or the end of the iter is reached. If ErrStop is sent +// the iteration is stop but no error is returned +func (iter *ObjectLookupIter) ForEach(cb func(Object) error) error { + for _, hash := range iter.series { + obj, err := iter.storage.Get(hash) + if err != nil { + return err + } + + if err := cb(obj); err != nil { + if err == ErrStop { + return nil + } + + return nil + } + } + + return nil +} + // Close releases any resources used by the iterator. func (iter *ObjectLookupIter) Close() { iter.pos = len(iter.series) @@ -173,6 +195,23 @@ func (iter *ObjectSliceIter) Next() (Object, error) { return obj, nil } +// ForEach call the cb function for each object contained on this iter until +// an error happends or the end of the iter is reached. If ErrStop is sent +// the iteration is stop but no error is returned +func (iter *ObjectSliceIter) ForEach(cb func(Object) error) error { + for _, o := range iter.series { + if err := cb(o); err != nil { + if err == ErrStop { + return nil + } + + return nil + } + } + + return nil +} + // Close releases any resources used by the iterator. func (iter *ObjectSliceIter) Close() { iter.pos = len(iter.series) diff --git a/core/reference.go b/core/reference.go index 89852da..bde3ff4 100644 --- a/core/reference.go +++ b/core/reference.go @@ -178,6 +178,23 @@ func (iter *ReferenceSliceIter) Next() (*Reference, error) { return obj, nil } +// ForEach call the cb function for each reference contained on this iter until +// an error happends or the end of the iter is reached. If ErrStop is sent +// the iteration is stop but no error is returned +func (iter *ReferenceSliceIter) ForEach(cb func(*Reference) error) error { + for _, r := range iter.series { + if err := cb(r); err != nil { + if err == ErrStop { + return nil + } + + return nil + } + } + + return nil +} + // Close releases any resources used by the iterator. func (iter *ReferenceSliceIter) Close() { iter.pos = len(iter.series) diff --git a/core/reference_test.go b/core/reference_test.go index 5d88f0e..3739a15 100644 --- a/core/reference_test.go +++ b/core/reference_test.go @@ -1,6 +1,10 @@ package core -import . "gopkg.in/check.v1" +import ( + "io" + + . "gopkg.in/check.v1" +) type ReferenceSuite struct{} @@ -62,3 +66,58 @@ func (s *ReferenceSuite) TestIsTag(c *C) { r := NewHashReference(ReferenceName("refs/tags/v3.1."), ZeroHash) c.Assert(r.IsTag(), Equals, true) } + +func (s *ReferenceSuite) TestReferenceSliceIterNext(c *C) { + slice := []*Reference{ + NewReferenceFromStrings("foo", "foo"), + NewReferenceFromStrings("bar", "bar"), + } + + i := NewReferenceSliceIter(slice) + foo, err := i.Next() + c.Assert(err, IsNil) + c.Assert(foo == slice[0], Equals, true) + + bar, err := i.Next() + c.Assert(err, IsNil) + c.Assert(bar == slice[1], Equals, true) + + empty, err := i.Next() + c.Assert(err, Equals, io.EOF) + c.Assert(empty, IsNil) +} + +func (s *ReferenceSuite) TestReferenceSliceIterForEach(c *C) { + slice := []*Reference{ + NewReferenceFromStrings("foo", "foo"), + NewReferenceFromStrings("bar", "bar"), + } + + i := NewReferenceSliceIter(slice) + var count int + i.ForEach(func(r *Reference) error { + c.Assert(r == slice[count], Equals, true) + count++ + return nil + }) + + c.Assert(count, Equals, 2) +} + +func (s *ReferenceSuite) TestReferenceSliceIterForEachStop(c *C) { + slice := []*Reference{ + NewReferenceFromStrings("foo", "foo"), + NewReferenceFromStrings("bar", "bar"), + } + + i := NewReferenceSliceIter(slice) + + var count int + i.ForEach(func(r *Reference) error { + c.Assert(r == slice[count], Equals, true) + count++ + return ErrStop + }) + + c.Assert(count, Equals, 1) +} diff --git a/core/storage.go b/core/storage.go index cb8f45d..6403f5f 100644 --- a/core/storage.go +++ b/core/storage.go @@ -1,5 +1,10 @@ package core +import "errors" + +//ErrStop is used to stop a ForEach function in an Iter +var ErrStop = errors.New("stop iter") + // Storage storage of objects and references type Storage interface { ObjectStorage() ObjectStorage @@ -17,6 +22,7 @@ type ObjectStorage interface { // ObjectIter is a generic closable interface for iterating over objects. type ObjectIter interface { Next() (Object, error) + ForEach(func(Object) error) error Close() } @@ -30,5 +36,6 @@ type ReferenceStorage interface { // ReferenceIter is a generic closable interface for iterating over references type ReferenceIter interface { Next() (*Reference, error) + ForEach(func(*Reference) error) error Close() } diff --git a/examples/basic/main.go b/examples/basic/main.go index e13b5d9..510b39d 100644 --- a/examples/basic/main.go +++ b/examples/basic/main.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io" "os" "gopkg.in/src-d/go-git.v4" @@ -16,7 +15,7 @@ func main() { panic(err) } - if err = r.Clone(&git.RepositoryCloneOptions{URL: url, Depth: 1, SingleBranch: false}); err != nil { + if err = r.Clone(&git.RepositoryCloneOptions{URL: url}); err != nil { panic(err) } @@ -28,20 +27,12 @@ func main() { defer iter.Close() var count = 0 - for { - //the commits are not shorted in any special order - commit, err := iter.Next() - if err != nil { - if err == io.EOF { - break - } - - panic(err) - } - + iter.ForEach(func(commit *git.Commit) error { count++ fmt.Println(commit) - } + + return nil + }) fmt.Println("total commits:", count) } @@ -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() } diff --git a/repository.go b/repository.go index e9aa55a..170a1b4 100644 --- a/repository.go +++ b/repository.go @@ -2,7 +2,6 @@ package git import ( "errors" - "io" "gopkg.in/src-d/go-git.v4/clients/common" "gopkg.in/src-d/go-git.v4/core" @@ -128,24 +127,14 @@ func (r *Repository) getAllRemoteRefences(remote *Remote) ([]*core.Reference, er i := remote.Refs() defer i.Close() - for { - ref, err := i.Next() - if err != nil { - if err == io.EOF { - break - } - - return nil, err - } - + return refs, i.ForEach(func(ref *core.Reference) error { if !ref.IsBranch() { - continue + return nil } refs = append(refs, ref) - } - - return refs, nil + return nil + }) } func (r *Repository) createLocalReferences(ref *core.Reference) error { @@ -234,24 +223,14 @@ func (r *Repository) getLocalReferences() ([]*core.Reference, error) { i := r.Refs() defer i.Close() - for { - ref, err := i.Next() - if err != nil { - if err == io.EOF { - break - } - - return nil, err - } - + return refs, i.ForEach(func(ref *core.Reference) error { if ref.Type() == core.SymbolicReference { - continue + return nil } refs = append(refs, ref) - } - - return refs, nil + return nil + }) } // Commit return the commit with the given hash @@ -181,3 +181,17 @@ func (iter *TagIter) Next() (*Tag, error) { tag := &Tag{r: iter.r} return tag, tag.Decode(obj) } + +// ForEach call the cb function for each tag contained on this iter until +// an error happends or the end of the iter is reached. If ErrStop is sent +// the iteration is stop but no error is returned +func (iter *TagIter) ForEach(cb func(*Tag) error) error { + return iter.ObjectIter.ForEach(func(obj core.Object) error { + tag := &Tag{r: iter.r} + if err := tag.Decode(obj); err != nil { + return err + } + + return cb(tag) + }) +} @@ -241,6 +241,33 @@ func (iter *TreeIter) Next() (*Tree, error) { } } +// ForEach call the cb function for each tree 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 *TreeIter) ForEach(cb func(*Tree) error) error { + i := &TreeIter{w: *NewTreeWalker(iter.w.r, iter.w.t)} + defer i.Close() + + for { + t, err := i.Next() + if err != nil { + if err == io.EOF { + return nil + } + + return err + } + + if err := cb(t); err != nil { + if err == core.ErrStop { + return nil + } + + return err + } + } +} + // Close closes the TreeIter func (iter *TreeIter) Close() { iter.w.Close() diff --git a/tree_walker.go b/tree_walker.go index 9bc1421..5568e1b 100644 --- a/tree_walker.go +++ b/tree_walker.go @@ -15,6 +15,7 @@ type TreeWalker struct { base string r *Repository + t *Tree } // NewTreeWalker returns a new TreeWalker for the given repository and tree. @@ -26,6 +27,7 @@ func NewTreeWalker(r *Repository, t *Tree) *TreeWalker { stack: make([]treeEntryIter, 0, startingStackSize), base: "", r: r, + t: t, } w.stack = append(w.stack, treeEntryIter{t, 0}) return &w |