8 // Blob represents a Git object of type "blob".
10 // Size is the total uncompressed size of the blob's contents.
12 // Contents is a reader that yields the uncompressed blob contents. It
13 // may only be read once. It may or may not implement io.ReadSeeker.
16 // closeFn is a function that is called to free any resources held by
17 // the Blob. In particular, this will close a file, if the Blob is
18 // being read from a file on disk.
22 // NewBlobFromBytes returns a new *Blob that yields the data given.
23 func NewBlobFromBytes(contents []byte) *Blob {
25 Contents: bytes.NewReader(contents),
26 Size: int64(len(contents)),
30 // Type implements Object.ObjectType by returning the correct object type for
31 // Blobs, BlobObjectType.
32 func (b *Blob) Type() ObjectType { return BlobObjectType }
34 // Decode implements Object.Decode and decodes the uncompressed blob contents
35 // being read. It returns the number of bytes that it consumed off of the
36 // stream, which is always zero.
38 // If any errors are encountered while reading the blob, they will be returned.
39 func (b *Blob) Decode(r io.Reader, size int64) (n int, err error) {
41 b.Contents = io.LimitReader(r, size)
43 b.closeFn = func() error {
44 if closer, ok := r.(io.Closer); ok {
53 // Encode encodes the blob's contents to the given io.Writer, "w". If there was
54 // any error copying the blob's contents, that error will be returned.
56 // Otherwise, the number of bytes written will be returned.
57 func (b *Blob) Encode(to io.Writer) (n int, err error) {
58 nn, err := io.Copy(to, b.Contents)
63 // Closes closes any resources held by the open Blob, or returns nil if there
65 func (b *Blob) Close() error {
72 // Equal returns whether the receiving and given blobs are equal, or in other
73 // words, whether they are represented by the same SHA-1 when saved to the
75 func (b *Blob) Equal(other *Blob) bool {
76 if (b == nil) != (other == nil) {
81 return b.Contents == other.Contents &&