-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
297 lines (256 loc) · 6.59 KB
/
git.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
Mostly taken from/inspired by https://github.com/edupo/semver-cli
*/
package main
import (
"fmt"
"sort"
"strings"
"github.com/Masterminds/semver"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/storer"
)
// Git struct wrapps Repository class from go-git to add a tag map used to perform queries when describing. From https://github.com/edupo/semver-cli/blob/master/gitWrapper/git.go
type Git struct {
TagsMap map[plumbing.Hash][]*plumbing.Reference
*git.Repository
}
// plainOpen opens a git repository from the given path. It detects if the
// repository is bare or a normal one. If the path doesn't contain a valid
// repository ErrRepositoryNotExists is returned
func plainOpen(path string) (*Git, error) {
opts := &git.PlainOpenOptions{
DetectDotGit: true,
}
r, err := git.PlainOpenWithOptions(path, opts)
return &Git{
make(map[plumbing.Hash][]*plumbing.Reference),
r,
}, err
}
func (g *Git) getTagMap() error {
tags, err := g.Tags()
if err != nil {
return err
}
err = tags.ForEach(func(t *plumbing.Reference) error {
references := []*plumbing.Reference{t}
var hash plumbing.Hash
annTag, err := g.TagObject(t.Hash())
switch err {
case nil:
// Annotated Tag object
// This means the tags TARGET is actually what we want to use
hash = annTag.Target
if _, exists := g.TagsMap[hash]; exists {
references = append(g.TagsMap[hash], t)
}
case plumbing.ErrObjectNotFound:
// Not an annotated tag object
// This means the tags hash is the hash of the actual commit
hash = t.Hash()
if _, exists := g.TagsMap[hash]; exists {
references = append(g.TagsMap[hash], t)
}
default:
// Some other error
return err
}
g.TagsMap[hash] = references
return nil
})
return err
}
// Describe returns the latest tag and number of commits from reference to that tag.
// Almost the same as "git describe --tags" other than the "g" prefix on the commit hash
// If the reference itself has a tag the count will be empty
func (g *Git) Describe(reference *plumbing.Reference) (string, int, error) {
// Fetch the reference log
cIter, err := g.Log(&git.LogOptions{
From: reference.Hash(),
Order: git.LogOrderCommitterTime,
})
if err != nil {
return "", 0, err
}
// Build the tag map
err = g.getTagMap()
if err != nil {
return "", 0, err
}
// Search the tag
var tag *plumbing.Reference
var count int
err = cIter.ForEach(func(c *object.Commit) error {
if ts, ok := g.TagsMap[c.Hash]; ok {
if len(ts) != 1 {
tag = getHighestSemverRef(ts)
} else {
tag = ts[0]
}
}
if tag != nil {
return storer.ErrStop
}
count++
return nil
})
if err != nil {
return "", 0, err
}
if tag != nil {
if count == 0 {
return fmt.Sprint(tag.Name().Short()), 0, nil
}
return tag.Name().Short(), count, nil
}
return "", 0, nil
}
func getCurrentCommitFromRepository(repository *Git) (string, error) {
headRef, err := repository.Head()
if err != nil {
return "", err
}
headSha := headRef.Hash().String()[:8]
return headSha, nil
}
func getLatestTagFromRepository(repository *Git) (string, int, error) {
// Check if HEAD has tag before iterating over all tags
headRef, err := repository.Head()
if err != nil {
return "", 0, err
}
tag, count, err := repository.Describe(headRef)
if err != nil {
return "", 0, err
}
return tag, count, nil
}
func getCurrentBranchFromRepository(repository *Git) (string, error) {
branchRefs, err := repository.Branches()
if err != nil {
return "", err
}
headRef, err := repository.Head()
if err != nil {
return "", err
}
var currentBranchName string
err = branchRefs.ForEach(func(branchRef *plumbing.Reference) error {
if branchRef.Hash() == headRef.Hash() {
currentBranchName = branchRef.Name().String()
return nil
}
return nil
})
if err != nil {
return "", err
}
return strings.TrimPrefix(currentBranchName, "refs/heads/"), nil
}
func isDetachedHead(r *Git) bool {
head, err := r.Head()
if err != nil {
return false
}
return head.Name() == plumbing.HEAD
}
func getCurrentBranchFromDetachedHead(r *Git) (string, error) {
hr, err := r.Head()
if err != nil {
return "", err
}
memo := make(map[plumbing.Hash]bool)
rs, err := r.References()
if err != nil {
return "", fmt.Errorf("no branch found in detached head at %q, err %w\n", hr.Hash().String()[:8], err)
}
var branches []plumbing.ReferenceName
rs.ForEach(func(ref *plumbing.Reference) error {
n := ref.Name()
if n.IsBranch() || n.IsRemote() {
b, err := r.Reference(n, true)
if err != nil {
return err
}
v, err := reaches(r.Repository, b.Hash(), hr.Hash(), memo)
if err != nil {
return err
}
if v {
branches = append(branches, n)
}
}
return nil
})
var branch string
for _, b := range branches {
// If there are multiple branches, prefer the local branch over the remote branch.
// If there are multiple local branches, simply return the first one we encounter.
if b.IsBranch() {
return b.Short(), nil
}
// If there are no local branches, return the first remote branch we encounter with the
// remote name stripped.
remotes, err := r.Remotes()
if err != nil {
return "", fmt.Errorf("failed to get remotes: %w", err)
}
for _, remote := range remotes {
b, match := strings.CutPrefix(b.Short(), remote.Config().Name+"/")
if match {
branch = b
break
}
}
}
return branch, nil
}
// reaches returns true if commit, c, can be reached from commit, start. Results are memoized in memo.
func reaches(r *git.Repository, start, c plumbing.Hash, memo map[plumbing.Hash]bool) (bool, error) {
if v, ok := memo[start]; ok {
return v, nil
}
if start == c {
memo[start] = true
return true, nil
}
co, err := r.CommitObject(start)
if err != nil {
return false, err
}
for _, p := range co.ParentHashes {
v, err := reaches(r, p, c, memo)
if err != nil {
return false, err
}
if v {
memo[start] = true
return true, nil
}
}
memo[start] = false
return false, nil
}
func getHighestSemverRef(references []*plumbing.Reference) *plumbing.Reference {
if len(references) == 0 {
return &plumbing.Reference{}
}
refVer := make(map[string]*plumbing.Reference)
for _, ref := range references {
refVer[ref.Name().Short()] = ref
}
vs := make([]*semver.Version, len(references))
for i, r := range references {
v, err := semver.NewVersion(r.Name().Short())
if err != nil {
// One of the strings is not semver formatted, ignore it
continue
}
vs[i] = v
}
sort.Sort(sort.Reverse(semver.Collection(vs)))
return refVer[vs[0].Original()]
}