| 1 |
package gitobj |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"hash" |
| 7 |
"io" |
| 8 |
"os" |
| 9 |
) |
| 10 |
|
| 11 |
// Blob represents a Git object of type "blob". |
| 12 |
type Blob struct { |
| 13 |
// Size is the total uncompressed size of the blob's contents. |
| 14 |
Size int64 |
| 15 |
// Contents is a reader that yields the uncompressed blob contents. It |
| 16 |
// may only be read once. It may or may not implement io.ReadSeeker. |
| 17 |
Contents io.Reader |
| 18 |
|
| 19 |
// closeFn is a function that is called to free any resources held by |
| 20 |
// the Blob. In particular, this will close a file, if the Blob is |
| 21 |
// being read from a file on disk. |
| 22 |
closeFn func() error |
| 23 |
} |
| 24 |
|
| 25 |
// NewBlobFromBytes returns a new *Blob that yields the data given. |
| 26 |
func NewBlobFromBytes(contents []byte) *Blob { |
| 27 |
return &Blob{ |
| 28 |
Contents: bytes.NewReader(contents), |
| 29 |
Size: int64(len(contents)), |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
// NewBlobFromFile returns a new *Blob that contains the contents of the file |
| 34 |
// at location "path" on disk. NewBlobFromFile does not read the file ahead of |
| 35 |
// time, and instead defers this task until encoding the blob to the object |
| 36 |
// database. |
| 37 |
// |
| 38 |
// If the file cannot be opened or stat(1)-ed, an error will be returned. |
| 39 |
// |
| 40 |
// When the blob receives a function call Close(), the file will also be closed, |
| 41 |
// and any error encountered in doing so will be returned from Close(). |
| 42 |
func NewBlobFromFile(path string) (*Blob, error) { |
| 43 |
f, err := os.Open(path) |
| 44 |
if err != nil { |
| 45 |
return nil, fmt.Errorf("gitobj: could not open: %s: %s", path, |
| 46 |
err) |
| 47 |
} |
| 48 |
|
| 49 |
stat, err := f.Stat() |
| 50 |
if err != nil { |
| 51 |
return nil, fmt.Errorf("gitobj: could not stat %s: %s", path, |
| 52 |
err) |
| 53 |
} |
| 54 |
|
| 55 |
return &Blob{ |
| 56 |
Contents: f, |
| 57 |
Size: stat.Size(), |
| 58 |
|
| 59 |
closeFn: func() error { |
| 60 |
if err := f.Close(); err != nil { |
| 61 |
return fmt.Errorf( |
| 62 |
"gitobj: could not close %s: %s", |
| 63 |
path, err) |
| 64 |
} |
| 65 |
return nil |
| 66 |
}, |
| 67 |
}, nil |
| 68 |
} |
| 69 |
|
| 70 |
// Type implements Object.ObjectType by returning the correct object type for |
| 71 |
// Blobs, BlobObjectType. |
| 72 |
func (b *Blob) Type() ObjectType { return BlobObjectType } |
| 73 |
|
| 74 |
// Decode implements Object.Decode and decodes the uncompressed blob contents |
| 75 |
// being read. It returns the number of bytes that it consumed off of the |
| 76 |
// stream, which is always zero. |
| 77 |
// |
| 78 |
// If any errors are encountered while reading the blob, they will be returned. |
| 79 |
func (b *Blob) Decode(hash hash.Hash, r io.Reader, size int64) (n int, err error) { |
| 80 |
b.Size = size |
| 81 |
b.Contents = io.LimitReader(r, size) |
| 82 |
|
| 83 |
b.closeFn = func() error { |
| 84 |
if closer, ok := r.(io.Closer); ok { |
| 85 |
return closer.Close() |
| 86 |
} |
| 87 |
return nil |
| 88 |
} |
| 89 |
|
| 90 |
return 0, nil |
| 91 |
} |
| 92 |
|
| 93 |
// Encode encodes the blob's contents to the given io.Writer, "w". If there was |
| 94 |
// any error copying the blob's contents, that error will be returned. |
| 95 |
// |
| 96 |
// Otherwise, the number of bytes written will be returned. |
| 97 |
func (b *Blob) Encode(to io.Writer) (n int, err error) { |
| 98 |
nn, err := io.Copy(to, b.Contents) |
| 99 |
|
| 100 |
return int(nn), err |
| 101 |
} |
| 102 |
|
| 103 |
// Closes closes any resources held by the open Blob, or returns nil if there |
| 104 |
// were no errors. |
| 105 |
func (b *Blob) Close() error { |
| 106 |
if b.closeFn == nil { |
| 107 |
return nil |
| 108 |
} |
| 109 |
return b.closeFn() |
| 110 |
} |
| 111 |
|
| 112 |
// Equal returns whether the receiving and given blobs are equal, or in other |
| 113 |
// words, whether they are represented by the same SHA-1 when saved to the |
| 114 |
// object database. |
| 115 |
func (b *Blob) Equal(other *Blob) bool { |
| 116 |
if (b == nil) != (other == nil) { |
| 117 |
return false |
| 118 |
} |
| 119 |
|
| 120 |
if b != nil { |
| 121 |
return b.Contents == other.Contents && |
| 122 |
b.Size == other.Size |
| 123 |
} |
| 124 |
return true |
| 125 |
} |