generated from axetroy/go-cli-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 10
/
1_parser.go
279 lines (221 loc) · 5.93 KB
/
1_parser.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
package whatchanged
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/pkg/errors"
"github.com/release-lab/whatchanged/internal/client"
)
type Range struct {
*object.Commit
*client.Tag
}
type Scope struct {
Start Range // The range start
End Range // The range end
Tags []*client.Tag // Tags included in the range
}
var (
regShortHash = regexp.MustCompile(`^[a-z\d]{7,8}$`)
regLongHash = regexp.MustCompile(`^[a-z\d]{40}$`)
regTag = regexp.MustCompile(`^@(\d+)$`)
)
func getCommitFromHEAD(git *client.GitClient) (*object.Commit, error) {
ref, err := git.Head()
if err != nil {
return nil, errors.WithStack(err)
}
commit, err := git.Commit(ref.Hash().String())
if err != nil {
return nil, errors.WithStack(err)
}
return commit, err
}
func getCommit(git *client.GitClient, version string, isStart bool) (*object.Commit, error) {
if version == "" {
var (
commit *object.Commit
err error
)
if isStart {
// if version is a empty string, then it should be HEAD
commit, err = getCommitFromHEAD(git)
} else {
// if version is a empty string, then it should be the first commit
commit, err = git.GetFirstCommit()
}
if err != nil {
return nil, errors.WithStack(err)
}
return commit, nil
} else if version == "HEAD" {
commit, err := getCommitFromHEAD(git)
if err != nil {
return nil, errors.WithStack(err)
}
return commit, nil
} else if regTag.MatchString(version) {
matcher := regTag.FindStringSubmatch(version)
tagIndex, err := strconv.ParseInt(matcher[1], 10, 10)
if err != nil {
return nil, errors.WithStack(err)
}
tag, err := git.GetTagN(int(tagIndex))
if err != nil {
return nil, errors.WithStack(err)
}
if tag == nil {
return nil, nil
}
return tag.Commit, nil
} else if regShortHash.MatchString(version) {
return git.CommitByShort(version)
} else if regLongHash.MatchString(version) {
return git.Commit(version)
} else {
// else if it's a tag
tag, err := git.GetTagByName(version)
if err != nil {
return nil, errors.WithStack(err)
}
if tag == nil {
return nil, errors.New(fmt.Sprintf("tag '%s' not found", version))
}
return tag.Commit, nil
}
}
func resolveVersion(git *client.GitClient, versionRanges string) ([]string, error) {
versions := strings.Split(versionRanges, "~")
length := len(versions)
switch true {
case versionRanges == "":
versions[0] = "HEAD"
latestTag, err := git.GetTagN(0)
if err != nil {
return nil, errors.WithStack(err)
}
if latestTag != nil {
commit, err := git.GetPrevCommitOfTag(latestTag)
if err != nil {
return nil, errors.WithStack(err)
}
// if there is no prev tag
// tag is the HEAD
if commit == nil {
nextTag, err := git.NextTag(latestTag)
if err != nil {
return nil, errors.WithStack(err)
}
if nextTag != nil {
if preCommitOfNextTag, err := git.GetPrevCommitOfTag(nextTag); err != nil {
return nil, errors.WithStack(err)
} else {
// if the pre-commit of the tag is nil
if preCommitOfNextTag == nil {
versions = append(versions, nextTag.Commit.Hash.String())
} else {
versions = append(versions, preCommitOfNextTag.Hash.String())
}
}
} else {
versions = append(versions, "")
}
} else {
versions = append(versions, commit.Hash.String())
}
} else {
versions = append(versions, "")
}
case length == 1:
var (
tag *client.Tag
err error
)
if regTag.MatchString(versionRanges) {
matcher := regTag.FindStringSubmatch(versionRanges)
tagIndex, err := strconv.ParseInt(matcher[1], 10, 10)
if err != nil {
return nil, errors.WithStack(err)
}
if tag, err = git.GetTagN(int(tagIndex)); err != nil {
return nil, errors.WithStack(err)
}
} else {
if tag, err = git.GetTagByName(versionRanges); err != nil {
return nil, errors.WithStack(err)
}
}
if tag != nil {
commit, err := git.GetLastCommitOfTag(tag)
if err != nil {
return nil, errors.WithStack(err)
} else if commit != nil {
versions = append(versions, commit.Hash.String())
}
} else {
versions = append(versions, "")
}
}
return versions, nil
}
func Parse(git *client.GitClient, rangesSlice []string) ([]*Scope, error) {
var (
scopeRange = make([]*Scope, 0)
)
if len(rangesSlice) == 0 {
rangesSlice = []string{""}
}
for _, ranges := range rangesSlice {
var (
startCommit *object.Commit
endCommit *object.Commit
scope = &Scope{}
)
ranges = strings.TrimSpace(ranges)
versions, err := resolveVersion(git, ranges)
if err != nil {
return nil, errors.WithStack(err)
}
if startCommit, err = getCommit(git, versions[0], true); err != nil {
return nil, errors.WithStack(err)
} else if startCommit == nil {
return nil, errors.New(fmt.Sprintf("invalid version '%s'", versions[0]))
} else {
scope.Start = Range{Commit: startCommit}
if tag, err := git.GetTagByCommit(startCommit); err != nil {
return nil, errors.WithStack(err)
} else if tag != nil {
scope.Start.Tag = tag
}
}
if endCommit, err = getCommit(git, versions[1], false); err != nil {
return nil, errors.WithStack(err)
} else if endCommit == nil {
return nil, errors.New(fmt.Sprintf("invalid version '%s'", versions[1]))
} else {
scope.End = Range{Commit: endCommit}
if tag, err := git.GetTagByCommit(endCommit); err != nil {
return nil, errors.WithStack(err)
} else if tag != nil {
scope.End.Tag = tag
lastCommitOfTag, err := git.GetLastCommitOfTag(tag)
if err != nil {
return nil, errors.WithStack(err)
} else if lastCommitOfTag != nil {
scope.End.Commit = lastCommitOfTag
}
}
}
tags, err := git.GetTagRangesByCommit(startCommit, endCommit)
if err != nil {
return nil, errors.WithStack(err)
}
if len(tags) != 0 {
scope.Tags = tags
}
scopeRange = append(scopeRange, scope)
}
return scopeRange, nil
}