diff options
author | Santiago M. Mola <santi@mola.io> | 2016-12-14 23:12:44 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-12-14 23:12:44 +0100 |
commit | 0af572dd21c0aa79d13745b633ee24ba6c4d6cf1 (patch) | |
tree | 49e81e74e82d84fd88b2fc1e4b0dc7c7bfe9c40f /plumbing/storer | |
parent | df0f38af83f972f026d7e14150f3d37b95f13484 (diff) | |
download | go-git-0af572dd21c0aa79d13745b633ee24ba6c4d6cf1.tar.gz |
move plumbing from top level package to plumbing (#183)
* plumbing: rename Object -> EncodedObject.
* plumbing/storer: rename ObjectStorer -> EncodedObjectStorer.
* move difftree to plumbing/difftree.
* move diff -> utils/diff
* make Object/Tag/Blob/Tree/Commit/File depend on storer.
* Object and its implementations now depend only on
storer.EncodedObjectStorer, not git.Repository.
* Tests are decoupled accordingly.
* move Object/Commit/File/Tag/Tree to plumbing/object.
* move Object/Commit/File/Tag/Tree to plumbing/object.
* move checkClose to utils/ioutil.
* move RevListObjects to plumbing/revlist.Objects.
* move DiffTree to plumbing/difftree package.
* rename files with plural nouns to singular
* plumbing/object: add GetBlob/GetCommit/GetTag/GetTree.
Diffstat (limited to 'plumbing/storer')
-rw-r--r-- | plumbing/storer/object.go | 131 | ||||
-rw-r--r-- | plumbing/storer/object_test.go | 44 |
2 files changed, 91 insertions, 84 deletions
diff --git a/plumbing/storer/object.go b/plumbing/storer/object.go index 60b9171..a733ee6 100644 --- a/plumbing/storer/object.go +++ b/plumbing/storer/object.go @@ -12,27 +12,30 @@ var ( ErrStop = errors.New("stop iter") ) -// ObjectStorer generic storage of objects -type ObjectStorer interface { - // NewObject returns a new plumbing.Object, the real type of the object can - // be a custom implementation or the defaul one, plumbing.MemoryObject - NewObject() plumbing.Object - // SetObject save an object into the storage, the object shuld be create - // with the NewObject, method, and file if the type is not supported. - SetObject(plumbing.Object) (plumbing.Hash, error) - // Object get an object by hash with the given plumbing.ObjectType. - // Implementors should return (nil, plumbing.ErrObjectNotFound) if an object - // doesn't exist with both the given hash and object type. +// EncodedObjectStorer generic storage of objects +type EncodedObjectStorer interface { + // NewEncodedObject returns a new plumbing.EncodedObject, the real type + // of the object can be a custom implementation or the default one, + // plumbing.MemoryObject. + NewEncodedObject() plumbing.EncodedObject + // SetEncodedObject saves an object into the storage, the object should + // be create with the NewEncodedObject, method, and file if the type is + // not supported. + SetEncodedObject(plumbing.EncodedObject) (plumbing.Hash, error) + // EncodedObject gets an object by hash with the given + // plumbing.ObjectType. Implementors should return + // (nil, plumbing.ErrObjectNotFound) if an object doesn't exist with + // both the given hash and object type. // // Valid plumbing.ObjectType values are CommitObject, BlobObject, TagObject, // TreeObject and AnyObject. If plumbing.AnyObject is given, the object must // be looked up regardless of its type. - Object(plumbing.ObjectType, plumbing.Hash) (plumbing.Object, error) - // IterObjects returns a custom ObjectIter over all the object on the - // storage. + EncodedObject(plumbing.ObjectType, plumbing.Hash) (plumbing.EncodedObject, error) + // IterObjects returns a custom EncodedObjectStorer over all the object + // on the storage. // // Valid plumbing.ObjectType values are CommitObject, BlobObject, TagObject, - IterObjects(plumbing.ObjectType) (ObjectIter, error) + IterEncodedObjects(plumbing.ObjectType) (EncodedObjectIter, error) } // Transactioner is a optional method for ObjectStorer, it enable transaction @@ -52,41 +55,41 @@ type PackfileWriter interface { PackfileWriter() (io.WriteCloser, error) } -// ObjectIter is a generic closable interface for iterating over objects. -type ObjectIter interface { - Next() (plumbing.Object, error) - ForEach(func(plumbing.Object) error) error +// EncodedObjectIter is a generic closable interface for iterating over objects. +type EncodedObjectIter interface { + Next() (plumbing.EncodedObject, error) + ForEach(func(plumbing.EncodedObject) error) error Close() } // Transaction is an in-progress storage transaction. A transaction must end // with a call to Commit or Rollback. type Transaction interface { - SetObject(plumbing.Object) (plumbing.Hash, error) - Object(plumbing.ObjectType, plumbing.Hash) (plumbing.Object, error) + SetEncodedObject(plumbing.EncodedObject) (plumbing.Hash, error) + EncodedObject(plumbing.ObjectType, plumbing.Hash) (plumbing.EncodedObject, error) Commit() error Rollback() error } -// ObjectLookupIter implements ObjectIter. It iterates over a series of object -// hashes and yields their associated objects by retrieving each one from -// object storage. The retrievals are lazy and only occur when the iterator -// moves forward with a call to Next(). +// EncodedObjectLookupIter implements EncodedObjectIter. It iterates over a +// series of object hashes and yields their associated objects by retrieving +// each one from object storage. The retrievals are lazy and only occur when the +// iterator moves forward with a call to Next(). // -// The ObjectLookupIter must be closed with a call to Close() when it is no -// longer needed. -type ObjectLookupIter struct { - storage ObjectStorer +// The EncodedObjectLookupIter must be closed with a call to Close() when it is +// no longer needed. +type EncodedObjectLookupIter struct { + storage EncodedObjectStorer series []plumbing.Hash t plumbing.ObjectType pos int } -// NewObjectLookupIter returns an object iterator given an object storage and -// a slice of object hashes. -func NewObjectLookupIter( - storage ObjectStorer, t plumbing.ObjectType, series []plumbing.Hash) *ObjectLookupIter { - return &ObjectLookupIter{ +// NewEncodedObjectLookupIter returns an object iterator given an object storage +// and a slice of object hashes. +func NewEncodedObjectLookupIter( + storage EncodedObjectStorer, t plumbing.ObjectType, series []plumbing.Hash) *EncodedObjectLookupIter { + return &EncodedObjectLookupIter{ storage: storage, series: series, t: t, @@ -97,13 +100,13 @@ func NewObjectLookupIter( // the end it will return io.EOF as an error. If the object can't be found in // the object storage, it will return plumbing.ErrObjectNotFound as an error. // If the object is retreieved successfully error will be nil. -func (iter *ObjectLookupIter) Next() (plumbing.Object, error) { +func (iter *EncodedObjectLookupIter) Next() (plumbing.EncodedObject, error) { if iter.pos >= len(iter.series) { return nil, io.EOF } hash := iter.series[iter.pos] - obj, err := iter.storage.Object(iter.t, hash) + obj, err := iter.storage.EncodedObject(iter.t, hash) if err == nil { iter.pos++ } @@ -114,28 +117,30 @@ func (iter *ObjectLookupIter) Next() (plumbing.Object, error) { // 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. The iterator is closed. -func (iter *ObjectLookupIter) ForEach(cb func(plumbing.Object) error) error { +func (iter *EncodedObjectLookupIter) ForEach(cb func(plumbing.EncodedObject) error) error { return ForEachIterator(iter, cb) } // Close releases any resources used by the iterator. -func (iter *ObjectLookupIter) Close() { +func (iter *EncodedObjectLookupIter) Close() { iter.pos = len(iter.series) } -// ObjectSliceIter implements ObjectIter. It iterates over a series of objects -// stored in a slice and yields each one in turn when Next() is called. +// EncodedObjectSliceIter implements EncodedObjectIter. It iterates over a +// series of objects stored in a slice and yields each one in turn when Next() +// is called. // -// The ObjectSliceIter must be closed with a call to Close() when it is no -// longer needed. -type ObjectSliceIter struct { - series []plumbing.Object +// The EncodedObjectSliceIter must be closed with a call to Close() when it is +// no longer needed. +type EncodedObjectSliceIter struct { + series []plumbing.EncodedObject pos int } -// NewObjectSliceIter returns an object iterator for the given slice of objects. -func NewObjectSliceIter(series []plumbing.Object) *ObjectSliceIter { - return &ObjectSliceIter{ +// NewEncodedObjectSliceIter returns an object iterator for the given slice of +// objects. +func NewEncodedObjectSliceIter(series []plumbing.EncodedObject) *EncodedObjectSliceIter { + return &EncodedObjectSliceIter{ series: series, } } @@ -143,7 +148,7 @@ func NewObjectSliceIter(series []plumbing.Object) *ObjectSliceIter { // Next returns the next object from the iterator. If the iterator has reached // the end it will return io.EOF as an error. If the object is retreieved // successfully error will be nil. -func (iter *ObjectSliceIter) Next() (plumbing.Object, error) { +func (iter *EncodedObjectSliceIter) Next() (plumbing.EncodedObject, error) { if len(iter.series) == 0 { return nil, io.EOF } @@ -157,32 +162,34 @@ func (iter *ObjectSliceIter) Next() (plumbing.Object, error) { // 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. The iterator is closed. -func (iter *ObjectSliceIter) ForEach(cb func(plumbing.Object) error) error { +func (iter *EncodedObjectSliceIter) ForEach(cb func(plumbing.EncodedObject) error) error { return ForEachIterator(iter, cb) } // Close releases any resources used by the iterator. -func (iter *ObjectSliceIter) Close() { - iter.series = []plumbing.Object{} +func (iter *EncodedObjectSliceIter) Close() { + iter.series = []plumbing.EncodedObject{} } -// MultiObjectIter implements ObjectIter. It iterates over several ObjectIter, +// MultiEncodedObjectIter implements EncodedObjectIter. It iterates over several +// EncodedObjectIter, // // The MultiObjectIter must be closed with a call to Close() when it is no // longer needed. -type MultiObjectIter struct { - iters []ObjectIter +type MultiEncodedObjectIter struct { + iters []EncodedObjectIter pos int } -// NewMultiObjectIter returns an object iterator for the given slice of objects. -func NewMultiObjectIter(iters []ObjectIter) ObjectIter { - return &MultiObjectIter{iters: iters} +// NewMultiEncodedObjectIter returns an object iterator for the given slice of +// objects. +func NewMultiEncodedObjectIter(iters []EncodedObjectIter) EncodedObjectIter { + return &MultiEncodedObjectIter{iters: iters} } // Next returns the next object from the iterator, if one iterator reach io.EOF // is removed and the next one is used. -func (iter *MultiObjectIter) Next() (plumbing.Object, error) { +func (iter *MultiEncodedObjectIter) Next() (plumbing.EncodedObject, error) { if len(iter.iters) == 0 { return nil, io.EOF } @@ -200,25 +207,25 @@ func (iter *MultiObjectIter) Next() (plumbing.Object, error) { // 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. The iterator is closed. -func (iter *MultiObjectIter) ForEach(cb func(plumbing.Object) error) error { +func (iter *MultiEncodedObjectIter) ForEach(cb func(plumbing.EncodedObject) error) error { return ForEachIterator(iter, cb) } // Close releases any resources used by the iterator. -func (iter *MultiObjectIter) Close() { +func (iter *MultiEncodedObjectIter) Close() { for _, i := range iter.iters { i.Close() } } type bareIterator interface { - Next() (plumbing.Object, error) + Next() (plumbing.EncodedObject, error) Close() } // ForEachIterator is a helper function to build iterators without need to // rewrite the same ForEach function each time. -func ForEachIterator(iter bareIterator, cb func(plumbing.Object) error) error { +func ForEachIterator(iter bareIterator, cb func(plumbing.EncodedObject) error) error { defer iter.Close() for { obj, err := iter.Next() diff --git a/plumbing/storer/object_test.go b/plumbing/storer/object_test.go index a0a7755..6bdd25c 100644 --- a/plumbing/storer/object_test.go +++ b/plumbing/storer/object_test.go @@ -11,14 +11,14 @@ import ( func Test(t *testing.T) { TestingT(t) } type ObjectSuite struct { - Objects []plumbing.Object + Objects []plumbing.EncodedObject Hash []plumbing.Hash } var _ = Suite(&ObjectSuite{}) func (s *ObjectSuite) SetUpSuite(c *C) { - s.Objects = []plumbing.Object{ + s.Objects = []plumbing.EncodedObject{ s.buildObject([]byte("foo")), s.buildObject([]byte("bar")), } @@ -29,7 +29,7 @@ func (s *ObjectSuite) SetUpSuite(c *C) { } func (s *ObjectSuite) TestMultiObjectIterNext(c *C) { - expected := []plumbing.Object{ + expected := []plumbing.EncodedObject{ &plumbing.MemoryObject{}, &plumbing.MemoryObject{}, &plumbing.MemoryObject{}, @@ -38,14 +38,14 @@ func (s *ObjectSuite) TestMultiObjectIterNext(c *C) { &plumbing.MemoryObject{}, } - iter := NewMultiObjectIter([]ObjectIter{ - NewObjectSliceIter(expected[0:2]), - NewObjectSliceIter(expected[2:4]), - NewObjectSliceIter(expected[4:5]), + iter := NewMultiEncodedObjectIter([]EncodedObjectIter{ + NewEncodedObjectSliceIter(expected[0:2]), + NewEncodedObjectSliceIter(expected[2:4]), + NewEncodedObjectSliceIter(expected[4:5]), }) var i int - iter.ForEach(func(o plumbing.Object) error { + iter.ForEach(func(o plumbing.EncodedObject) error { c.Assert(o, Equals, expected[i]) i++ return nil @@ -54,7 +54,7 @@ func (s *ObjectSuite) TestMultiObjectIterNext(c *C) { iter.Close() } -func (s *ObjectSuite) buildObject(content []byte) plumbing.Object { +func (s *ObjectSuite) buildObject(content []byte) plumbing.EncodedObject { o := &plumbing.MemoryObject{} o.Write(content) @@ -65,8 +65,8 @@ func (s *ObjectSuite) TestObjectLookupIter(c *C) { var count int storage := &MockObjectStorage{s.Objects} - i := NewObjectLookupIter(storage, plumbing.CommitObject, s.Hash) - err := i.ForEach(func(o plumbing.Object) error { + i := NewEncodedObjectLookupIter(storage, plumbing.CommitObject, s.Hash) + err := i.ForEach(func(o plumbing.EncodedObject) error { c.Assert(o, NotNil) c.Assert(o.Hash().String(), Equals, s.Hash[count].String()) count++ @@ -80,8 +80,8 @@ func (s *ObjectSuite) TestObjectLookupIter(c *C) { func (s *ObjectSuite) TestObjectSliceIter(c *C) { var count int - i := NewObjectSliceIter(s.Objects) - err := i.ForEach(func(o plumbing.Object) error { + i := NewEncodedObjectSliceIter(s.Objects) + err := i.ForEach(func(o plumbing.EncodedObject) error { c.Assert(o, NotNil) c.Assert(o.Hash().String(), Equals, s.Hash[count].String()) count++ @@ -94,10 +94,10 @@ func (s *ObjectSuite) TestObjectSliceIter(c *C) { } func (s *ObjectSuite) TestObjectSliceIterStop(c *C) { - i := NewObjectSliceIter(s.Objects) + i := NewEncodedObjectSliceIter(s.Objects) var count = 0 - err := i.ForEach(func(o plumbing.Object) error { + err := i.ForEach(func(o plumbing.EncodedObject) error { c.Assert(o, NotNil) c.Assert(o.Hash().String(), Equals, s.Hash[count].String()) count++ @@ -109,11 +109,11 @@ func (s *ObjectSuite) TestObjectSliceIterStop(c *C) { } func (s *ObjectSuite) TestObjectSliceIterError(c *C) { - i := NewObjectSliceIter([]plumbing.Object{ + i := NewEncodedObjectSliceIter([]plumbing.EncodedObject{ s.buildObject([]byte("foo")), }) - err := i.ForEach(func(plumbing.Object) error { + err := i.ForEach(func(plumbing.EncodedObject) error { return fmt.Errorf("a random error") }) @@ -121,18 +121,18 @@ func (s *ObjectSuite) TestObjectSliceIterError(c *C) { } type MockObjectStorage struct { - db []plumbing.Object + db []plumbing.EncodedObject } -func (o *MockObjectStorage) NewObject() plumbing.Object { +func (o *MockObjectStorage) NewEncodedObject() plumbing.EncodedObject { return nil } -func (o *MockObjectStorage) SetObject(obj plumbing.Object) (plumbing.Hash, error) { +func (o *MockObjectStorage) SetEncodedObject(obj plumbing.EncodedObject) (plumbing.Hash, error) { return plumbing.ZeroHash, nil } -func (o *MockObjectStorage) Object(t plumbing.ObjectType, h plumbing.Hash) (plumbing.Object, error) { +func (o *MockObjectStorage) EncodedObject(t plumbing.ObjectType, h plumbing.Hash) (plumbing.EncodedObject, error) { for _, o := range o.db { if o.Hash() == h { return o, nil @@ -141,7 +141,7 @@ func (o *MockObjectStorage) Object(t plumbing.ObjectType, h plumbing.Hash) (plum return nil, plumbing.ErrObjectNotFound } -func (o *MockObjectStorage) IterObjects(t plumbing.ObjectType) (ObjectIter, error) { +func (o *MockObjectStorage) IterEncodedObjects(t plumbing.ObjectType) (EncodedObjectIter, error) { return nil, nil } |