-
Notifications
You must be signed in to change notification settings - Fork 1
/
ref.go
200 lines (165 loc) · 3.95 KB
/
ref.go
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
192
193
194
195
196
197
198
199
200
package retriever
import (
"fmt"
"regexp"
"github.com/go-git/go-git/v5/plumbing"
)
var RefRules = append([]string{"%s"}, plumbing.RefRevParseRules...)
// Reference represents a git reference.
type Reference struct {
name string
hash Hash
typ ReferenceType
}
type ReferenceType int
const (
ReferenceTypeUnknown ReferenceType = iota
ReferenceTypeSymbolic // branch or tag
ReferenceTypeBranch
ReferenceTypeTag
ReferenceTypeHash
)
// NewBranchReference returns a new git reference to a branch.
func NewBranchReference(name string) *Reference {
return &Reference{name: name, typ: ReferenceTypeBranch}
}
// NewTagReference returns a new git reference to a tag.
func NewTagReference(tag string) *Reference {
return &Reference{name: tag, typ: ReferenceTypeTag}
}
// NewSymbolicReference returns a new symbolic git reference including branch and tag. e.g. v0.0.1, main, develop.
// Note: Where possible prefer NewBranchReference or NewTagReference.
func NewSymbolicReference(n string) *Reference {
return &Reference{name: n, typ: ReferenceTypeSymbolic}
}
// NewHashReference returns a new hash git reference.
func NewHashReference(h Hash) (*Reference, error) {
if !h.IsValid() {
return nil, fmt.Errorf("Invalid commit SHA %s", h)
}
return &Reference{hash: h, typ: ReferenceTypeHash}, nil
}
// HEADReference returns a HEAD git reference.
func HEADReference() *Reference {
return &Reference{name: HEAD, typ: ReferenceTypeBranch}
}
// NewReference returns a new Reference.
func NewReference(n string, h Hash) (*Reference, error) {
if n != "" {
ref := NewSymbolicReference(n)
if h.IsValid() {
err := ref.SetHash(h)
if err != nil {
return nil, err
}
}
return ref, nil
} else {
if h.IsZero() {
return HEADReference(), nil
} else {
return NewHashReference(h)
}
}
}
// Name returns the value of the reference name.
func (r *Reference) Name() string {
return r.name
}
// SetName sets the value of the name.
func (r *Reference) SetName(name string) {
r.name = name
}
// Hash returns the value of the hash.
func (r *Reference) Hash() Hash {
return r.hash
}
// SetHash sets the value of the hash.
func (r *Reference) SetHash(hash Hash) error {
if hash.IsZero() {
return fmt.Errorf("Invalid commit SHA")
}
r.hash = hash
return nil
}
func (r *Reference) Type() ReferenceType {
return r.typ
}
const (
HEAD = "HEAD"
)
// IsHEAD reports whether the reference is HEAD.
func (r *Reference) IsHEAD() bool {
return r.name == HEAD
}
// IsEmpty reports whether the reference is empty.
func (r *Reference) IsEmpty() bool {
return r == &Reference{}
}
// IsHash reports whether the reference is a HashReference.
func (r *Reference) IsHash() bool {
return !r.hash.IsZero()
}
// String returns reference representing in string.
func (r *Reference) String() string {
if r.IsHash() {
return r.hash.String()
}
return r.name
}
type Hash [40]byte
var ZeroHash Hash
// NewHash returns a new Hash.
func NewHash(s string) (Hash, error) {
if !isHash(s) {
return ZeroHash, fmt.Errorf("Invalid commit SHA")
}
var h Hash
copy(h[:], []byte(s))
return h, nil
}
// IsZero reports whether a Hash is empty.
func (h Hash) IsZero() bool {
return h == ZeroHash
}
func (h Hash) String() string {
if h.IsZero() {
return ""
}
return string(h[:])
}
// IsValid reports whether a Hash is valid.
func (h Hash) IsValid() bool {
return isHash(h.String())
}
func isHash(str string) bool {
if len(str) == 40 {
if e, err := regexp.MatchString(`[a-fA-F0-9]{40}`, str); err == nil {
return e
}
}
return false
}
type RefIterator struct {
rules []string
ref string
currIdx int
}
func NewRefIterator(rules []string, ref string) *RefIterator {
iter := &RefIterator{
rules: rules,
ref: ref,
currIdx: -1,
}
if ref == HEAD {
iter.rules = []string{"%s"}
}
return iter
}
func (i *RefIterator) Next() bool {
i.currIdx += 1
return i.currIdx < len(i.rules)
}
func (i *RefIterator) Current() string {
return fmt.Sprintf(i.rules[i.currIdx], i.ref)
}