diff options
author | Joshua Sjoding <joshua.sjoding@scjalliance.com> | 2016-03-03 00:33:00 -0800 |
---|---|---|
committer | Joshua Sjoding <joshua.sjoding@scjalliance.com> | 2016-03-08 13:09:54 -0800 |
commit | 9e6a03b7956464ccd9d2fbacedd8e5cc23572d02 (patch) | |
tree | 0282f20de8279db354233d1d67e3743e08509020 /objects.go | |
parent | 9c9cdff966cc181296f400769d3c8596f17e743a (diff) | |
download | go-git-9e6a03b7956464ccd9d2fbacedd8e5cc23572d02.tar.gz |
Added Object interface for Commit, Tree, Blob and Tag
* New Object interface is distinct from core.Object
* New Object interface is used in places where returned object could be of any type
* Object is implemented by Commit, Tree, Blob, File and Tag
* Added Repository.Object function for retrieving objects of any type
* Tag.Object now returns Object instead of core.Object
* Tag target hash is now publicly accessible
* Renamed Tag.Type field to Tag.TargetType, making it distinct from Tag.Type function
* Fixed infinite recursive loop in TagIter.Close
* TreeWalker.Next now returns Object instead of core.Object
* Removed some duplicate test setup code
Diffstat (limited to 'objects.go')
-rw-r--r-- | objects.go | 47 |
1 files changed, 45 insertions, 2 deletions
@@ -10,6 +10,36 @@ import ( "gopkg.in/src-d/go-git.v3/core" ) +var ErrUnsupportedObject = errors.New("unsupported object type") + +// Object is a generic representation of any git object. It is implemented by +// Commit, Tree, Blob and Tag, and includes the functions that are common to +// them. +// +// Object is returned when an object could of any type. It is frequently used +// with a type cast to acquire the specific type of object: +// +// func process(obj Object) { +// switch o := obj.(type) { +// case *Commit: +// // o is a Commit +// case *Tree: +// // o is a Tree +// case *Blob: +// // o is a Blob +// case *Tag: +// // o is a Tag +// } +// } +// +// This interface is intentionally different from core.Object, which is a lower +// level interface used by storage implementations to read and write objects. +type Object interface { + ID() core.Hash + Type() core.ObjectType + Decode(core.Object) error +} + // Blob is used to store file data - it is generally a file. type Blob struct { Hash core.Hash @@ -18,9 +48,22 @@ type Blob struct { obj core.Object } -var ErrUnsupportedObject = errors.New("unsupported object type") +// ID returns the object ID of the blob. The returned value will always match +// the current value of Blob.Hash. +// +// ID is present to fufill the Object interface. +func (b *Blob) ID() core.Hash { + return b.Hash +} + +// Type returns the type of object. It always returns core.BlobObject. +// +// Type is present to fufill the Object interface. +func (b *Blob) Type() core.ObjectType { + return core.BlobObject +} -// Decode transform an core.Object into a Blob struct +// Decode transforms a core.Object into a Blob struct. func (b *Blob) Decode(o core.Object) error { if o.Type() != core.BlobObject { return ErrUnsupportedObject |