| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "encoding/hex" |
| 5 | "io" |
| 6 | |
| 7 | "github.com/git-lfs/git-lfs/v3/tr" |
| 8 | "github.com/git-lfs/gitobj/v2" |
| 9 | "github.com/git-lfs/gitobj/v2/errors" |
| 10 | ) |
| 11 | |
| 12 | // object represents a generic Git object of any type. |
| 13 | type object struct { |
| 14 | // Contents reads Git's internal object representation. |
| 15 | Contents io.Reader |
| 16 | // Oid is the ID of the object. |
| 17 | Oid string |
| 18 | // Size is the size in bytes of the object. |
| 19 | Size int64 |
| 20 | // Type is the type of the object being held. |
| 21 | Type string |
| 22 | // object is the gitobj object being handled. |
| 23 | object gitobj.Object |
| 24 | } |
| 25 | |
| 26 | // ObjectScanner is a scanner type that scans for Git objects reference-able in |
| 27 | // Git's object database by their unique OID. |
| 28 | type ObjectScanner struct { |
| 29 | // object is the object that the ObjectScanner last scanned, or nil. |
| 30 | object *object |
| 31 | // err is the error (if any) that the ObjectScanner encountered during |
| 32 | // its last scan, or nil. |
| 33 | err error |
| 34 | |
| 35 | gitobj *gitobj.ObjectDatabase |
| 36 | } |
| 37 | |
| 38 | // NewObjectScanner constructs a new instance of the `*ObjectScanner` type and |
| 39 | // returns it. It backs the ObjectScanner with an ObjectDatabase from the |
| 40 | // github.com/git-lfs/gitobj/v2 package. |
| 41 | // If any errors are encountered while creating the ObjectDatabase, |
| 42 | // they will be returned immediately. |
| 43 | // Otherwise, an `*ObjectScanner` is returned with no error. |
| 44 | func NewObjectScanner(gitEnv, osEnv Environment) (*ObjectScanner, error) { |
| 45 | gitdir, err := GitCommonDir() |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | |
| 50 | db, err := ObjectDatabase(osEnv, gitEnv, gitdir, "") |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | |
| 55 | return NewObjectScannerFrom(db), nil |
| 56 | } |
| 57 | |
| 58 | // NewObjectScannerFrom returns a new `*ObjectScanner` populated with data from |
| 59 | // the given `io.Reader`, "r". It supplies no close function, and discards any |
| 60 | // input given to the Scan() function. |
| 61 | func NewObjectScannerFrom(db *gitobj.ObjectDatabase) *ObjectScanner { |
| 62 | return &ObjectScanner{gitobj: db} |
| 63 | } |
| 64 | |
| 65 | // Scan scans for a particular object given by the "oid" parameter. Once the |
| 66 | // scan is complete, the Contents(), Sha1(), Size() and Type() functions may be |
| 67 | // called and will return data corresponding to the given OID. |
| 68 | // |
| 69 | // Scan() returns whether the scan was successful, or in other words, whether or |
| 70 | // not the scanner can continue to progress. |
| 71 | func (s *ObjectScanner) Scan(oid string) bool { |
| 72 | if err := s.reset(); err != nil { |
| 73 | s.err = err |
| 74 | return false |
| 75 | } |
| 76 | |
| 77 | obj, err := s.scan(oid) |
| 78 | s.object = obj |
| 79 | |
| 80 | if err != nil { |
| 81 | if err != io.EOF { |
| 82 | s.err = err |
| 83 | } |
| 84 | return false |
| 85 | } |
| 86 | return true |
| 87 | } |
| 88 | |
| 89 | // Close closes and frees any resources owned by the *ObjectScanner that it is |
| 90 | // called upon. If there were any errors in freeing that (those) resource(s), it |
| 91 | // it will be returned, otherwise nil. |
| 92 | func (s *ObjectScanner) Close() error { |
| 93 | if s == nil { |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | s.reset() |
| 98 | s.gitobj.Close() |
| 99 | |
| 100 | return nil |
| 101 | } |
| 102 | |
| 103 | // Contents returns an io.Reader which reads Git's representation of the object |
| 104 | // that was last scanned for. |
| 105 | func (s *ObjectScanner) Contents() io.Reader { |
| 106 | return s.object.Contents |
| 107 | } |
| 108 | |
| 109 | // Sha1 returns the SHA1 object ID of the object that was last scanned for. |
| 110 | func (s *ObjectScanner) Sha1() string { |
| 111 | return s.object.Oid |
| 112 | } |
| 113 | |
| 114 | // Size returns the size in bytes of the object that was last scanned for. |
| 115 | func (s *ObjectScanner) Size() int64 { |
| 116 | return s.object.Size |
| 117 | } |
| 118 | |
| 119 | // Type returns the type of the object that was last scanned for. |
| 120 | func (s *ObjectScanner) Type() string { |
| 121 | return s.object.Type |
| 122 | } |
| 123 | |
| 124 | // Err returns the error (if any) that was encountered during the last Scan() |
| 125 | // operation. |
| 126 | func (s *ObjectScanner) Err() error { return s.err } |
| 127 | |
| 128 | func (s *ObjectScanner) reset() error { |
| 129 | if s.object != nil { |
| 130 | if c, ok := s.object.object.(interface { |
| 131 | Close() error |
| 132 | }); ok && c != nil { |
| 133 | if err := c.Close(); err != nil { |
| 134 | return err |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | s.object, s.err = nil, nil |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | type missingErr struct { |
| 144 | oid string |
| 145 | } |
| 146 | |
| 147 | func (m *missingErr) Error() string { |
| 148 | return tr.Tr.Get("missing object: %s", m.oid) |
| 149 | } |
| 150 | |
| 151 | func IsMissingObject(err error) bool { |
| 152 | _, ok := err.(*missingErr) |
| 153 | return ok |
| 154 | } |
| 155 | |
| 156 | func mustDecode(oid string) []byte { |
| 157 | x, _ := hex.DecodeString(oid) |
| 158 | return x |
| 159 | } |
| 160 | |
| 161 | func (s *ObjectScanner) scan(oid string) (*object, error) { |
| 162 | var ( |
| 163 | obj gitobj.Object |
| 164 | size int64 |
| 165 | contents io.Reader |
| 166 | ) |
| 167 | |
| 168 | obj, err := s.gitobj.Object(mustDecode(oid)) |
| 169 | if err != nil { |
| 170 | if errors.IsNoSuchObject(err) { |
| 171 | return nil, &missingErr{oid: oid} |
| 172 | } |
| 173 | return nil, err |
| 174 | } |
| 175 | |
| 176 | // Currently, we're only interested in the size and contents of blobs, |
| 177 | // and gitobj only exposes the size easily for us for blobs anyway. |
| 178 | if obj.Type() == gitobj.BlobObjectType { |
| 179 | blob := obj.(*gitobj.Blob) |
| 180 | size = blob.Size |
| 181 | contents = blob.Contents |
| 182 | } |
| 183 | |
| 184 | return &object{ |
| 185 | Contents: contents, |
| 186 | Oid: oid, |
| 187 | Size: size, |
| 188 | Type: obj.Type().String(), |
| 189 | object: obj, |
| 190 | }, nil |
| 191 | } |