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
|
package git
import (
"encoding/hex"
"errors"
)
// OID represents the SHA-1 object ID of a Git object, in binary
// format.
type OID struct {
v [20]byte
}
// NullOID is the null object ID; i.e., all zeros.
var NullOID OID
// OIDFromBytes converts a byte slice containing an object ID in
// binary format into an `OID`.
func OIDFromBytes(oidBytes []byte) (OID, error) {
var oid OID
if len(oidBytes) != len(oid.v) {
return OID{}, errors.New("bytes oid has the wrong length")
}
copy(oid.v[0:20], oidBytes)
return oid, nil
}
// NewOID converts an object ID in hex format (i.e., `[0-9a-f]{40}`)
// into an `OID`.
func NewOID(s string) (OID, error) {
oidBytes, err := hex.DecodeString(s)
if err != nil {
return OID{}, err
}
return OIDFromBytes(oidBytes)
}
// String formats `oid` as a string in hex format.
func (oid OID) String() string {
return hex.EncodeToString(oid.v[:])
}
// Bytes returns a byte slice view of `oid`, in binary format.
func (oid OID) Bytes() []byte {
return oid.v[:]
}
// MarshalJSON expresses `oid` as a JSON string with its enclosing
// quotation marks.
func (oid OID) MarshalJSON() ([]byte, error) {
src := oid.v[:]
dst := make([]byte, hex.EncodedLen(len(src))+2)
dst[0] = '"'
dst[len(dst)-1] = '"'
hex.Encode(dst[1:len(dst)-1], src)
return dst, nil
}
|