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