diff --git a/git/odb/pack/object.go b/git/odb/pack/object.go new file mode 100644 index 00000000..fa6edca8 --- /dev/null +++ b/git/odb/pack/object.go @@ -0,0 +1,27 @@ +package pack + +// Object is an encapsulation of an object found in a packfile, or a packed +// object. +type Object struct { + // data is the front-most element of the delta-base chain, and when + // resolved, yields the uncompressed data of this object. + data Chain + // typ is the underlying object's type. It is not the type of the + // front-most chain element, rather, the type of the actual object. + typ PackedObjectType +} + +// Unpack resolves the delta-base chain and returns an uncompressed, unpacked, +// and full representation of the data encoded by this object. +// +// If there was any error in unpacking this object, it is returned immediately, +// and the object's data can be assumed to be corrupt. +func (o *Object) Unpack() ([]byte, error) { + return o.data.Unpack() +} + +// Type returns the underlying object's type. Rather than the type of the +// front-most delta-base component, it is the type of the object itself. +func (o *Object) Type() PackedObjectType { + return o.typ +} diff --git a/git/odb/pack/object_test.go b/git/odb/pack/object_test.go new file mode 100644 index 00000000..8adbb4c2 --- /dev/null +++ b/git/odb/pack/object_test.go @@ -0,0 +1,47 @@ +package pack + +import ( + "testing" + + "github.com/git-lfs/git-lfs/errors" + + "github.com/stretchr/testify/assert" +) + +func TestObjectTypeReturnsObjectType(t *testing.T) { + o := &Object{ + typ: TypeCommit, + } + + assert.Equal(t, TypeCommit, o.Type()) +} + +func TestObjectUnpackUnpacksData(t *testing.T) { + expected := []byte{0x1, 0x2, 0x3, 0x4} + + o := &Object{ + data: &ChainSimple{ + X: expected, + }, + } + + data, err := o.Unpack() + + assert.Equal(t, expected, data) + assert.NoError(t, err) +} + +func TestObjectUnpackPropogatesErrors(t *testing.T) { + expected := errors.New("git/odb/pack: testing") + + o := &Object{ + data: &ChainSimple{ + Err: expected, + }, + } + + data, err := o.Unpack() + + assert.Nil(t, data) + assert.Equal(t, expected, err) +}