forked from github/git-sizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoid.go
More file actions
100 lines (86 loc) · 2.05 KB
/
oid.go
File metadata and controls
100 lines (86 loc) · 2.05 KB
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
package git
import (
"bytes"
"crypto/sha1" //nolint:gosec
"crypto/sha256"
"encoding/hex"
"errors"
)
const (
HashSizeSHA256 = sha256.Size
HashSizeSHA1 = sha1.Size
HashSizeMax = HashSizeSHA256
)
type HashAlgo int
const (
HashUnknown HashAlgo = iota
HashSHA1
HashSHA256
)
// OID represents the SHA-1 object ID of a Git object, in binary
// format.
type OID struct {
v [HashSizeMax]byte
hashSize int
}
func (h HashAlgo) NullOID() OID {
switch h {
case HashSHA1:
return OID{hashSize: HashSizeSHA1}
case HashSHA256:
return OID{hashSize: HashSizeSHA256}
}
return OID{}
}
func (h HashAlgo) HashSize() int {
switch h {
case HashSHA1:
return HashSizeSHA1
case HashSHA256:
return HashSizeSHA256
}
return 0
}
// defaultNullOID is the null object ID; i.e., all zeros.
var defaultNullOID OID
func IsNullOID(o OID) bool {
return bytes.Equal(o.v[:], defaultNullOID.v[:])
}
// OIDFromBytes converts a byte slice containing an object ID in
// binary format into an `OID`.
func OIDFromBytes(oidBytes []byte) (OID, error) {
var oid OID
oidSize := len(oidBytes)
if oidSize != HashSizeSHA1 && oidSize != HashSizeSHA256 {
return OID{}, errors.New("bytes oid has the wrong length")
}
oid.hashSize = oidSize
copy(oid.v[0:oidSize], oidBytes)
return oid, nil
}
// NewOID converts an object ID in hex format (i.e., `[0-9a-f]{40,64}`) 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[:oid.hashSize])
}
// Bytes returns a byte slice view of `oid`, in binary format.
func (oid OID) Bytes() []byte {
return oid.v[:oid.hashSize]
}
// MarshalJSON expresses `oid` as a JSON string with its enclosing
// quotation marks.
func (oid OID) MarshalJSON() ([]byte, error) {
src := oid.v[:oid.hashSize]
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
}