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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
package git
import (
"encoding/hex"
"io"
"github.com/git-lfs/git-lfs/v3/tr"
"github.com/git-lfs/gitobj/v2"
"github.com/git-lfs/gitobj/v2/errors"
)
// object represents a generic Git object of any type.
type object struct {
// Contents reads Git's internal object representation.
Contents io.Reader
// Oid is the ID of the object.
Oid string
// Size is the size in bytes of the object.
Size int64
// Type is the type of the object being held.
Type string
// object is the gitobj object being handled.
object gitobj.Object
}
// ObjectScanner is a scanner type that scans for Git objects reference-able in
// Git's object database by their unique OID.
type ObjectScanner struct {
// object is the object that the ObjectScanner last scanned, or nil.
object *object
// err is the error (if any) that the ObjectScanner encountered during
// its last scan, or nil.
err error
gitobj *gitobj.ObjectDatabase
}
// NewObjectScanner constructs a new instance of the `*ObjectScanner` type and
// returns it. It backs the ObjectScanner with an ObjectDatabase from the
// github.com/git-lfs/gitobj/v2 package.
// If any errors are encountered while creating the ObjectDatabase,
// they will be returned immediately.
// Otherwise, an `*ObjectScanner` is returned with no error.
func NewObjectScanner(gitEnv, osEnv Environment) (*ObjectScanner, error) {
gitdir, err := GitCommonDir()
if err != nil {
return nil, err
}
gitobj, err := ObjectDatabase(osEnv, gitEnv, gitdir, "")
if err != nil {
return nil, err
}
return NewObjectScannerFrom(gitobj), nil
}
// NewObjectScannerFrom returns a new `*ObjectScanner` populated with data from
// the given `io.Reader`, "r". It supplies no close function, and discards any
// input given to the Scan() function.
func NewObjectScannerFrom(db *gitobj.ObjectDatabase) *ObjectScanner {
return &ObjectScanner{gitobj: db}
}
// Scan scans for a particular object given by the "oid" parameter. Once the
// scan is complete, the Contents(), Sha1(), Size() and Type() functions may be
// called and will return data corresponding to the given OID.
//
// Scan() returns whether the scan was successful, or in other words, whether or
// not the scanner can continue to progress.
func (s *ObjectScanner) Scan(oid string) bool {
if err := s.reset(); err != nil {
s.err = err
return false
}
obj, err := s.scan(oid)
s.object = obj
if err != nil {
if err != io.EOF {
s.err = err
}
return false
}
return true
}
// Close closes and frees any resources owned by the *ObjectScanner that it is
// called upon. If there were any errors in freeing that (those) resource(s), it
// it will be returned, otherwise nil.
func (s *ObjectScanner) Close() error {
if s == nil {
return nil
}
s.reset()
s.gitobj.Close()
return nil
}
// Contents returns an io.Reader which reads Git's representation of the object
// that was last scanned for.
func (s *ObjectScanner) Contents() io.Reader {
return s.object.Contents
}
// Sha1 returns the SHA1 object ID of the object that was last scanned for.
func (s *ObjectScanner) Sha1() string {
return s.object.Oid
}
// Size returns the size in bytes of the object that was last scanned for.
func (s *ObjectScanner) Size() int64 {
return s.object.Size
}
// Type returns the type of the object that was last scanned for.
func (s *ObjectScanner) Type() string {
return s.object.Type
}
// Err returns the error (if any) that was encountered during the last Scan()
// operation.
func (s *ObjectScanner) Err() error { return s.err }
func (s *ObjectScanner) reset() error {
if s.object != nil {
if c, ok := s.object.object.(interface {
Close() error
}); ok && c != nil {
if err := c.Close(); err != nil {
return err
}
}
}
s.object, s.err = nil, nil
return nil
}
type missingErr struct {
oid string
}
func (m *missingErr) Error() string {
return tr.Tr.Get("missing object: %s", m.oid)
}
func IsMissingObject(err error) bool {
_, ok := err.(*missingErr)
return ok
}
func mustDecode(oid string) []byte {
x, _ := hex.DecodeString(oid)
return x
}
func (s *ObjectScanner) scan(oid string) (*object, error) {
var (
obj gitobj.Object
size int64
contents io.Reader
)
obj, err := s.gitobj.Object(mustDecode(oid))
if err != nil {
if errors.IsNoSuchObject(err) {
return nil, &missingErr{oid: oid}
}
return nil, err
}
// Currently, we're only interested in the size and contents of blobs,
// and gitobj only exposes the size easily for us for blobs anyway.
if obj.Type() == gitobj.BlobObjectType {
blob := obj.(*gitobj.Blob)
size = blob.Size
contents = blob.Contents
}
return &object{
Contents: contents,
Oid: oid,
Size: size,
Type: obj.Type().String(),
object: obj,
}, nil
}
|