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
|
package gitpb
// runs git, parses output
// types faster than you can
import (
"errors"
"path/filepath"
"sort"
"strings"
"time"
"unicode"
"go.wit.com/log"
)
func (repo *Repo) GetLastTag() string {
cmd := []string{"git", "rev-list", "--tags", "--max-count=1"}
result := repo.RunQuiet(cmd)
// log.Info("getLastTagVersion()", result.Stdout)
if len(result.Stdout) != 1 {
log.Log(GITPBWARN, "git LastTag() error:", result.Stdout)
return ""
}
hash := result.Stdout[0]
cmd = []string{"git", "describe", "--tags", "--always", hash}
result = repo.RunQuiet(cmd)
if len(result.Stdout) != 1 {
log.Log(GITPBWARN, "git LastTag() error:", result.Stdout)
return ""
}
return result.Stdout[0]
}
func (repo *Repo) GetMasterVersion() string {
bname := repo.GetMasterBranchName()
v, err := repo.gitVersionByName(bname)
/*
count := repo.LenGitTags()
log.Info(repo.GoPath, "tag count", count)
repo.UpdateGitTags()
count = repo.LenGitTags()
log.Info(repo.GoPath, "tag count", count)
*/
if err == nil {
return v
} else {
log.Log(GITPBWARN, "gitpb.GitMasterVersion() error:", err)
return ""
}
}
func (repo *Repo) GetDevelVersion() string {
bname := repo.GetDevelBranchName()
v, err := repo.gitVersionByName(bname)
if err == nil {
return v
} else {
log.Log(GITPBWARN, "gitpb.GitDevelVersion() error:", err)
return ""
}
}
func (repo *Repo) GetUserVersion() string {
bname := repo.GetUserBranchName()
v, err := repo.gitVersionByName(bname)
if err == nil {
return v
} else {
log.Log(GITPBWARN, "gitpb.GitUserVersion() error:", err)
return ""
}
}
func (repo *Repo) GetCurrentBranchName() string {
r := repo.RunQuiet([]string{"git", "branch", "--show-current"})
output := strings.Join(r.Stdout, "\n")
if r.Error != nil {
log.Log(GITPBWARN, "GetCurrentBranchName() not in a git repo?", r.Error, repo.GoPath)
log.Log(GITPBWARN, "GetCurrentBranchName() output might have worked anyway:", output)
}
return strings.TrimSpace(output)
}
func (repo *Repo) GetCurrentBranchVersion() string {
r := repo.RunQuiet([]string{"git", "describe", "--tags", "--always"})
output := strings.Join(r.Stdout, "\n")
if r.Error != nil {
log.Log(GITPBWARN, "GetCurrentBranchVersion() not in a git repo?", r.Error, repo.GoPath)
log.Log(GITPBWARN, "GetCurrentBranchVersion() output might have worked anyway:", output)
}
return strings.TrimSpace(output)
}
func (repo *Repo) gitDescribeByHash(hash string) (string, error) {
if hash == "" {
return "", errors.New("hash was blank")
}
r := repo.RunQuiet([]string{"git", "describe", "--tags", "--always", hash})
out := strings.Join(r.Stdout, "\n")
if r.Error != nil {
log.Warn("not in a git repo or bad hash?", r.Error, repo.GoPath)
return out, r.Error
}
return out, r.Error
}
// this should get the most recent tag
func (repo *Repo) GetLastTagVersion() string {
r := repo.RunQuiet([]string{"git", "rev-list", "--tags", "--max-count=1"})
hash := strings.Join(r.Stdout, "\n")
hash = strings.TrimSpace(hash)
log.Log(GITPB, "getLastTagVersion()", hash)
name, _ := repo.gitDescribeByHash(hash)
return name
}
func (repo *Repo) DebianReleaseVersion() string {
lasttag := repo.GetLastTagVersion()
newv := trimNonNumericFromStart(lasttag)
if newv == "" {
newv = "0.0"
if lasttag != "" {
newv += "-" + lasttag
}
}
return newv
}
func (repo *Repo) DebianCurrentVersion() string {
cbversion := repo.GetCurrentBranchVersion()
newv := trimNonNumericFromStart(cbversion)
if newv == "" {
newv = "0.0"
}
if repo.CheckDirty() {
newv += "-dirty"
}
return newv
}
func (repo *Repo) gitVersionByName(name string) (string, error) {
name = strings.TrimSpace(name)
if name == "" {
// git will return the current tag
r := repo.RunQuiet([]string{"git", "describe", "--tags", "--always"})
output := strings.Join(r.Stdout, "\n")
if r.Error != nil {
log.Log(GITPBWARN, "gitDescribeByName() output might have worked anyway:", output)
log.Log(GITPBWARN, "gitDescribeByName() not in a git repo?", r.Error, repo.GoPath)
return "", r.Error
}
return strings.TrimSpace(output), nil
}
if !repo.IsBranch(name) {
// tag does not exist
log.Log(GITPBWARN, "LocalTagExists()", name, "did not exist")
return "", errors.New("gitDescribeByName() git fatal: Not a valid object name: " + name)
}
cmd := []string{"git", "describe", "--tags", "--always", name}
result := repo.RunQuiet(cmd)
output := strings.Join(result.Stdout, "\n")
if result.Error != nil {
log.Log(GITPBWARN, "cmd =", cmd)
log.Log(GITPBWARN, "err =", result.Error)
log.Log(GITPBWARN, "output (might have worked with error?) =", output)
log.Log(GITPBWARN, "not in a git repo or bad tag?", repo.GoPath)
return "", result.Error
}
return strings.TrimSpace(output), nil
}
// find a branch name
// will find "master" or "devel"
// will also find "v0.1.1"
// or will find "patches-from-foo"
// will return *any* match on any git branch because it doesn't
// matter much here yet
// eventually this will be worked out by forge in some future code that hasn't been made yet
func (repo *Repo) IsBranch(findname string) bool {
loop := repo.Tags.All()
for loop.Scan() {
t := loop.Next()
// log.Info("LocalTagExists() tag:", t.Refname)
tagname := t.Refname
if strings.HasPrefix(tagname, "refs/remotes") {
continue
}
path, filename := filepath.Split(tagname)
log.Log(GITPB, "gitpb.IsBranch() tag:", path, filename, "from", repo.GoPath)
if filename == findname {
log.Log(GITPB, "gitpb.IsBranch() found tag:", path, filename, "from", repo.GoPath)
return true
}
}
log.Log(GITPB, "did not find tag:", findname, "in", repo.GoPath)
return false
}
func trimNonNumericFromStart(s string) string {
for i, r := range s {
if unicode.IsDigit(r) {
return s[i:]
}
}
return ""
}
func (all *GitTags) SortByAge() *GitTagIterator {
packs := all.selectAllGitTag()
sort.Sort(GitTagAge(packs))
iterator := NewGitTagIterator(packs)
return iterator
}
type GitTagAge []*GitTag
func (a GitTagAge) Len() int { return len(a) }
// sorts in ? order
func (a GitTagAge) Less(i, j int) bool {
if time.Since(a[i].Authordate.AsTime()) < time.Since(a[j].Authordate.AsTime()) {
return true
}
return false
}
func (a GitTagAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (repo *Repo) SetTargetVersion(target string) {
repo.TargetVersion = target
}
|