| 1 |
// package plumbing implement the core interfaces and structs used by go-git |
| 2 |
package plumbing |
| 3 |
|
| 4 |
import ( |
| 5 |
"errors" |
| 6 |
"io" |
| 7 |
) |
| 8 |
|
| 9 |
var ( |
| 10 |
ErrObjectNotFound = errors.New("object not found") |
| 11 |
// ErrInvalidType is returned when an invalid object type is provided. |
| 12 |
ErrInvalidType = errors.New("invalid object type") |
| 13 |
) |
| 14 |
|
| 15 |
// Object is a generic representation of any git object |
| 16 |
type EncodedObject interface { |
| 17 |
Hash() Hash |
| 18 |
Type() ObjectType |
| 19 |
SetType(ObjectType) |
| 20 |
Size() int64 |
| 21 |
SetSize(int64) |
| 22 |
Reader() (io.ReadCloser, error) |
| 23 |
Writer() (io.WriteCloser, error) |
| 24 |
} |
| 25 |
|
| 26 |
// DeltaObject is an EncodedObject representing a delta. |
| 27 |
type DeltaObject interface { |
| 28 |
EncodedObject |
| 29 |
// BaseHash returns the hash of the object used as base for this delta. |
| 30 |
BaseHash() Hash |
| 31 |
// ActualHash returns the hash of the object after applying the delta. |
| 32 |
ActualHash() Hash |
| 33 |
// Size returns the size of the object after applying the delta. |
| 34 |
ActualSize() int64 |
| 35 |
} |
| 36 |
|
| 37 |
// ObjectType internal object type |
| 38 |
// Integer values from 0 to 7 map to those exposed by git. |
| 39 |
// AnyObject is used to represent any from 0 to 7. |
| 40 |
type ObjectType int8 |
| 41 |
|
| 42 |
const ( |
| 43 |
InvalidObject ObjectType = 0 |
| 44 |
CommitObject ObjectType = 1 |
| 45 |
TreeObject ObjectType = 2 |
| 46 |
BlobObject ObjectType = 3 |
| 47 |
TagObject ObjectType = 4 |
| 48 |
// 5 reserved for future expansion |
| 49 |
OFSDeltaObject ObjectType = 6 |
| 50 |
REFDeltaObject ObjectType = 7 |
| 51 |
|
| 52 |
AnyObject ObjectType = -127 |
| 53 |
) |
| 54 |
|
| 55 |
func (t ObjectType) String() string { |
| 56 |
switch t { |
| 57 |
case CommitObject: |
| 58 |
return "commit" |
| 59 |
case TreeObject: |
| 60 |
return "tree" |
| 61 |
case BlobObject: |
| 62 |
return "blob" |
| 63 |
case TagObject: |
| 64 |
return "tag" |
| 65 |
case OFSDeltaObject: |
| 66 |
return "ofs-delta" |
| 67 |
case REFDeltaObject: |
| 68 |
return "ref-delta" |
| 69 |
case AnyObject: |
| 70 |
return "any" |
| 71 |
default: |
| 72 |
return "unknown" |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func (t ObjectType) Bytes() []byte { |
| 77 |
return []byte(t.String()) |
| 78 |
} |
| 79 |
|
| 80 |
// Valid returns true if t is a valid ObjectType. |
| 81 |
func (t ObjectType) Valid() bool { |
| 82 |
return t >= CommitObject && t <= REFDeltaObject |
| 83 |
} |
| 84 |
|
| 85 |
// IsDelta returns true for any ObjectType that represents a delta (i.e. |
| 86 |
// REFDeltaObject or OFSDeltaObject). |
| 87 |
func (t ObjectType) IsDelta() bool { |
| 88 |
return t == REFDeltaObject || t == OFSDeltaObject |
| 89 |
} |
| 90 |
|
| 91 |
// ParseObjectType parses a string representation of ObjectType. It returns an |
| 92 |
// error on parse failure. |
| 93 |
func ParseObjectType(value string) (typ ObjectType, err error) { |
| 94 |
switch value { |
| 95 |
case "commit": |
| 96 |
typ = CommitObject |
| 97 |
case "tree": |
| 98 |
typ = TreeObject |
| 99 |
case "blob": |
| 100 |
typ = BlobObject |
| 101 |
case "tag": |
| 102 |
typ = TagObject |
| 103 |
case "ofs-delta": |
| 104 |
typ = OFSDeltaObject |
| 105 |
case "ref-delta": |
| 106 |
typ = REFDeltaObject |
| 107 |
default: |
| 108 |
err = ErrInvalidType |
| 109 |
} |
| 110 |
return |
| 111 |
} |