diff options
-rw-r--r-- | core/memory.go | 8 | ||||
-rw-r--r-- | core/object.go | 14 | ||||
-rw-r--r-- | core/reference.go | 62 | ||||
-rw-r--r-- | formats/packfile/parser.go | 22 | ||||
-rw-r--r-- | formats/packfile/parser_test.go | 17 | ||||
-rw-r--r-- | remote.go | 2 |
6 files changed, 70 insertions, 55 deletions
diff --git a/core/memory.go b/core/memory.go index 6477f74..e3063d6 100644 --- a/core/memory.go +++ b/core/memory.go @@ -13,11 +13,6 @@ type MemoryObject struct { sz int64 } -// NewMemoryObject creates a new MemoryObject -func NewMemoryObject(t ObjectType, len int64, cont []byte) *MemoryObject { - return &MemoryObject{t: t, sz: len, cont: cont} -} - // Hash return the object Hash, the hash is calculated on-the-fly the first // time is called, the subsequent calls the same Hash is returned even if the // type or the content has changed. The Hash is only generated if the size of @@ -39,7 +34,8 @@ func (o *MemoryObject) SetType(t ObjectType) { o.t = t } // Size return the size of the object func (o *MemoryObject) Size() int64 { return o.sz } -// SetSize set the object size, the given size should be written afterwards +// SetSize set the object size, a content of the given size should be written +// afterwards func (o *MemoryObject) SetSize(s int64) { o.sz = s } // Content returns the contents of the object diff --git a/core/object.go b/core/object.go index e5f9a5e..d73ab0f 100644 --- a/core/object.go +++ b/core/object.go @@ -39,14 +39,6 @@ type Object interface { Writer() (ObjectWriter, error) } -// ObjectStorage generic storage of objects -type ObjectStorage interface { - NewObject() Object - Set(Object) (Hash, error) - Get(Hash) (Object, error) - Iter(ObjectType) (ObjectIter, error) -} - // ObjectType internal object type's type ObjectType int8 @@ -110,12 +102,6 @@ func ParseObjectType(value string) (typ ObjectType, err error) { return } -// ObjectIter is a generic closable interface for iterating over objects. -type ObjectIter interface { - Next() (Object, error) - Close() -} - // 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 diff --git a/core/reference.go b/core/reference.go index 9114b58..ec00787 100644 --- a/core/reference.go +++ b/core/reference.go @@ -1,6 +1,10 @@ package core -import "strings" +import ( + "errors" + "io" + "strings" +) const ( refPrefix = "refs/" @@ -11,6 +15,10 @@ const ( symrefPrefix = "ref: " ) +var ( + ErrReferenceNotFound = errors.New("reference not found") +) + // ReferenceType reference type's type ReferenceType int8 @@ -39,17 +47,14 @@ type Reference struct { // the resulting reference can be a SymbolicReference or a HashReference base // on the target provided func NewReferenceFromStrings(name, target string) *Reference { - r := &Reference{n: ReferenceName(name)} + n := ReferenceName(name) if strings.HasPrefix(target, symrefPrefix) { - r.t = SymbolicReference - r.target = ReferenceName(target[len(symrefPrefix):]) - return r + target := ReferenceName(target[len(symrefPrefix):]) + return NewSymbolicReference(n, target) } - r.t = HashReference - r.h = NewHash(target) - return r + return NewHashReference(n, NewHash(target)) } // NewSymbolicReference creates a new SymbolicReference reference @@ -110,15 +115,38 @@ func (r *Reference) IsTag() bool { return strings.HasPrefix(string(r.n), refTagPrefix) } -// ReferenceStorage generic storage of references -type ReferenceStorage interface { - Set(Reference) error - Get(ReferenceName) (Reference, error) - Iter(ObjectType) (ReferenceIter, error) +// ReferenceSliceIter implements ReferenceIter. It iterates over a series of +// references stored in a slice and yields each one in turn when Next() is +// called. +// +// The ReferenceSliceIter must be closed with a call to Close() when it is no +// longer needed. +type ReferenceSliceIter struct { + series []*Reference + pos int +} + +// NewReferenceSliceIter returns a reference iterator for the given slice of +// objects. +func NewReferenceSliceIter(series []*Reference) *ReferenceSliceIter { + return &ReferenceSliceIter{ + series: series, + } +} + +// Next returns the next reference from the iterator. If the iterator has +// reached the end it will return io.EOF as an error. +func (iter *ReferenceSliceIter) Next() (*Reference, error) { + if iter.pos >= len(iter.series) { + return nil, io.EOF + } + + obj := iter.series[iter.pos] + iter.pos++ + return obj, nil } -// ReferenceIter is a generic closable interface for iterating over references -type ReferenceIter interface { - Next() (Reference, error) - Close() +// Close releases any resources used by the iterator. +func (iter *ReferenceSliceIter) Close() { + iter.pos = len(iter.series) } diff --git a/formats/packfile/parser.go b/formats/packfile/parser.go index 8b7a692..2930dcb 100644 --- a/formats/packfile/parser.go +++ b/formats/packfile/parser.go @@ -172,7 +172,7 @@ func moreBytesInLength(c byte) bool { return c&maskContinue > 0 } -// ReadObject reads and returns a git object from an object entry in the packfile. +// FillObject fills the given object from an object entry in the packfile. // Non-deltified and deltified objects are supported. func (p Parser) FillObject(obj core.Object) error { start, err := p.Offset() @@ -190,11 +190,11 @@ func (p Parser) FillObject(obj core.Object) error { switch t { case core.CommitObject, core.TreeObject, core.BlobObject, core.TagObject: obj.SetType(t) - err = p.ReadNonDeltaObjectContent(obj) + err = p.FillFromNonDeltaContent(obj) case core.REFDeltaObject: - err = p.ReadREFDeltaObjectContent(obj) + err = p.FillREFDeltaObjectContent(obj) case core.OFSDeltaObject: - err = p.ReadOFSDeltaObjectContent(obj, start) + err = p.FillOFSDeltaObjectContent(obj, start) default: err = ErrInvalidObject.AddDetails("tag %q", t) } @@ -202,9 +202,9 @@ func (p Parser) FillObject(obj core.Object) error { return err } -// ReadNonDeltaObjectContent reads and returns a non-deltified object +// FillFromNonDeltaContent reads and fill a non-deltified object // from it zlib stream in an object entry in the packfile. -func (p Parser) ReadNonDeltaObjectContent(obj core.Object) error { +func (p Parser) FillFromNonDeltaContent(obj core.Object) error { w, err := obj.Writer() if err != nil { return err @@ -233,9 +233,9 @@ func (p Parser) inflate(w io.Writer) (err error) { return err } -// ReadREFDeltaObjectContent reads and returns an object specified by a +// FillREFDeltaObjectContent reads and returns an object specified by a // REF-Delta entry in the packfile, form the hash onwards. -func (p Parser) ReadREFDeltaObjectContent(obj core.Object) error { +func (p Parser) FillREFDeltaObjectContent(obj core.Object) error { refHash, err := p.ReadHash() if err != nil { return err @@ -264,7 +264,7 @@ func (p Parser) ReadHash() (core.Hash, error) { return h, nil } -// ReadAndSolveDelta reads and returns the base patched with the contents +// ReadAndApplyDelta reads and apply the base patched with the contents // of a zlib compressed diff data in the delta portion of an object // entry in the packfile. func (p Parser) ReadAndApplyDelta(target, base core.Object) error { @@ -276,11 +276,11 @@ func (p Parser) ReadAndApplyDelta(target, base core.Object) error { return ApplyDelta(target, base, buf.Bytes()) } -// ReadOFSDeltaObjectContent reads an returns an object specified by an +// FillOFSDeltaObjectContent reads an fill an object specified by an // OFS-delta entry in the packfile from it negative offset onwards. The // start parameter is the offset of this particular object entry (the // current offset minus the already processed type and length). -func (p Parser) ReadOFSDeltaObjectContent(obj core.Object, start int64) error { +func (p Parser) FillOFSDeltaObjectContent(obj core.Object, start int64) error { jump, err := p.ReadNegativeOffset() if err != nil { diff --git a/formats/packfile/parser_test.go b/formats/packfile/parser_test.go index c2b99f1..5d41d69 100644 --- a/formats/packfile/parser_test.go +++ b/formats/packfile/parser_test.go @@ -238,13 +238,13 @@ func (s *ParserSuite) TestReadNonDeltaObjectContent(c *C) { c.Assert(err, IsNil, com) obj := &core.MemoryObject{} - err = p.ReadNonDeltaObjectContent(obj) + err = p.FillFromNonDeltaContent(obj) c.Assert(err, IsNil, com) c.Assert(obj.Content(), DeepEquals, test.expected, com) } } -func (s *ParserSuite) TestReadOFSDeltaObjectContent(c *C) { +func (s *ParserSuite) TestFillOFSDeltaObjectContent(c *C) { for i, test := range [...]struct { fixID string offset int64 @@ -294,14 +294,14 @@ func (s *ParserSuite) TestReadOFSDeltaObjectContent(c *C) { c.Assert(err, IsNil, com) obj := &core.MemoryObject{} - err = p.ReadOFSDeltaObjectContent(obj, test.offset) + err = p.FillOFSDeltaObjectContent(obj, test.offset) c.Assert(err, IsNil, com) c.Assert(obj.Type(), Equals, test.expType, com) c.Assert(obj.Content(), DeepEquals, test.expContent, com) } } -func (s *ParserSuite) TestReadREFDeltaObjectContent(c *C) { +func (s *ParserSuite) TestFillREFDeltaObjectContent(c *C) { for i, test := range [...]struct { fixID string offset int64 @@ -358,7 +358,7 @@ func (s *ParserSuite) TestReadREFDeltaObjectContent(c *C) { c.Assert(err, IsNil, com) obj := &core.MemoryObject{} - err = p.ReadREFDeltaObjectContent(obj) + err = p.FillREFDeltaObjectContent(obj) c.Assert(err, IsNil, com) c.Assert(obj.Type(), Equals, test.expType, com) c.Assert(obj.Content(), DeepEquals, test.expContent, com) @@ -368,7 +368,12 @@ func (s *ParserSuite) TestReadREFDeltaObjectContent(c *C) { } func newObject(t core.ObjectType, c []byte) core.Object { - return core.NewMemoryObject(t, int64(len(c)), c) + o := &core.MemoryObject{} + o.SetType(t) + o.SetSize(int64(len(c))) + o.Write(c) + + return o } func (s *ParserSuite) TestReadHeaderBadSignatureError(c *C) { @@ -82,7 +82,7 @@ func (r *Remote) Fetch(req *common.GitUploadPackRequest) (io.ReadCloser, error) return r.upSrv.Fetch(req) } -// Head returns the Hash of the HEAD +// Head returns the Reference of the HEAD func (r *Remote) Head() *core.Reference { return r.upInfo.Head() } |