-
Notifications
You must be signed in to change notification settings - Fork 16
/
maven.go
374 lines (347 loc) · 9.84 KB
/
maven.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package semver
import (
"fmt"
"strings"
)
// Maven-specific support - roughly equivalent to v3.6.0 / v3.8.6
// Note that v3.8.7/v3.9.0 changed some of these rules and it's not
// clear if those will be reverted yet.
// mavenExtension implements the Maven-specific parts of a Version,
// including its peculiar ordering requirements as defined in
// https://maven.apache.org/pom.html#Version_Order_Specification.
type mavenExtension struct {
version *Version
elems []mavenElement
}
// newMavenExtension builds the extension object for an extant Maven Version.
func newMavenExtension(v *Version, str string) (*mavenExtension, error) {
e := &mavenExtension{
version: v,
}
return e, e.init(str)
}
// mavenVersion parses a Maven Version. The str field is set by the caller;
// all else is stored in the extension, which is constructed here and attached
// to the returned Version.
func (p *versionParser) mavenVersion() (*Version, error) {
var err error
p.Version.ext, err = p.Version.newExtension(p.Version.str)
return p.Version, err
}
func (m *mavenExtension) copy(v *Version) extension {
n := new(mavenExtension)
*n = *m
n.version = v
return n
}
func (m *mavenExtension) clearPre() {
// TODO: Implement this when Maven 3 happens.
// For Maven version 2, it's irrelevant.
}
func (m *mavenExtension) empty() bool {
return m == nil || len(m.elems) == 0
}
// A mavenElement describes a component of a Maven version string.
// The separator is significant to the ordering.
type mavenElement struct {
sep byte // Will be zero for first element.
str string // Element value as a string.
int int64 // Value if the element is an integer; must be sizeof(value).
}
// canon returns a canonicalized string representation of the version/extension.
// The showBuild argument is ignored; Maven doesn't have that concept.
func (m *mavenExtension) canon(showBuild bool) string {
var b strings.Builder
for i, e := range m.elems {
if i > 0 {
b.WriteByte(e.sep)
}
b.WriteString(e.str)
}
return b.String()
}
var mavenMinVersion *Version
func init() {
mavenMinVersion = &Version{
sys: Maven,
userNumCount: 1,
isPrerelease: false,
str: "0.alpha",
}
mavenMinVersion.ext = &mavenExtension{
version: mavenMinVersion,
elems: []mavenElement{
{
sep: 0,
str: "0",
int: 0,
},
{
sep: '.',
str: "alpha",
int: 0,
},
},
}
}
// init parses a Maven version string into a slice of elements and stores them in the extension.
func (m *mavenExtension) init(input string) error {
elements := make([]mavenElement, 0, 5) // Pre-allocated to reduce allocations from growth.
// Canonicalize elementwise.
first := true
prevCat := versionUnknown
input = strings.ToLower(input)
for s, i := input, 0; s != ""; s = s[i:] {
var e mavenElement
i = nextVersionElemPos(s)
str := s[:i]
cat := versionCategory(str, 0)
if cat == versionUnknown || cat == versionStar {
return fmt.Errorf("invalid version %#q", input)
}
if cat == versionSeparator {
e.sep = str[0]
str = str[1:]
if str == "" {
str = "0"
}
cat = versionCategory(str, 0)
} else if !first {
e.sep = '-'
if cat == versionNumeric {
if prevCat == versionNumeric {
e.sep = '.'
} else if prevCat == versionQualifier {
// This is a qualifer followed by a number, check for
// the alpha/beta/milestone shortcut
prev := len(elements) - 1
switch elements[prev].str {
case "a":
elements[prev].str = "alpha"
case "b":
elements[prev].str = "beta"
case "m":
elements[prev].str = "milestone"
}
}
}
}
e.str = str
elements = append(elements, e)
prevCat = cat
first = false
}
// Trim step 1: remove trailing zeros at end and before each -.
for i := 1; i < len(elements); i++ {
if i < len(elements)-1 && elements[i+1].sep != '-' {
continue
}
for i > 0 && isEmptyMavenElem(elements[i].str) {
copy(elements[i:], elements[i+1:])
elements = elements[:len(elements)-1]
i--
}
}
// Final step: Integers for numbers.
for i, e := range elements {
if versionCategory(e.str, 0) == versionNumeric {
if e.str == "∞" {
elements[i].int = int64(infinity)
} else {
val, err := parseNum(e.str)
if err != nil {
return err
}
elements[i].int = int64(val)
}
} else {
m.version.isPrerelease = true // TODO is this right?
}
}
m.elems = elements
return nil
}
// isEmptyMavenElem reports whether is defined to be equivalent
// to the empty string for the purpose of ordering.
func isEmptyMavenElem(s string) bool {
if s == "0" {
return true
}
return mavenVersionQualifierOrder[s] == mavenEmptyQualifier
}
// mavenPadElement returns an element for padding out a short
// Version; which one depends on the separator.
func mavenPadElement(sep byte) mavenElement {
if sep == '-' {
return mavenElement{'-', "", 0}
}
return mavenElement{'.', "0", 0}
}
// compare uses Maven's rules to decide the ordering of m and e.
func (m *mavenExtension) compare(e extension) int {
n := e.(*mavenExtension)
as := m.elems
bs := n.elems
max := len(as)
if len(bs) > max {
max = len(bs)
}
for i := 0; i < max; i++ {
var a, b mavenElement
var ac, bc int
if i >= len(as) {
a = mavenPadElement(bs[i].sep)
ac = versionEOF
} else {
a = as[i]
ac = versionCategory(a.str, 0)
}
if i >= len(bs) {
b = mavenPadElement(as[i].sep)
bc = versionEOF
} else {
b = bs[i]
bc = versionCategory(b.str, 0)
}
if a == b {
continue
}
// Special cases for unknown qualifiers (or positive epsilon qualifiers like SP),
// which sort funny: 1.0 < 1.SP < 1.foo < 1.1
if ac == versionQualifier {
if ao := mavenVersionQualifierOrder[a.str]; ao > mavenEmptyQualifier {
return mavenUnknownQualifierCompare(a, b, ao, bc)
}
}
if bc == versionQualifier {
if bo := mavenVersionQualifierOrder[b.str]; bo > mavenEmptyQualifier {
return -mavenUnknownQualifierCompare(b, a, bo, ac)
}
}
if ac == versionEOF { // Empty string / padding
ac = versionQualifier
}
if bc == versionEOF { // Empty string / padding
bc = versionQualifier
}
// Ordinary things now.
if ac > bc {
return 1
}
if ac < bc {
return -1
}
if ac == versionNumeric {
if a.sep != b.sep {
return int(a.sep) - int(b.sep) // Magic: '-'+1 = '.'.
}
return sgn64(a.int, b.int)
}
if a.sep != b.sep {
return int(b.sep) - int(a.sep) // Note: reversed compared to numeric. Nice.
}
c := compareMavenQualifier(a.str, b.str)
if c == 0 {
continue
}
return c
}
if len(bs) > len(as) {
return -1
}
return 0
}
// mavenUnknownQualifierCompare handles the case where a is an unknown
// qualifier, say foo. See the comment at the call site.
func mavenUnknownQualifierCompare(a, b mavenElement, aOrder, bCategory int) int {
switch bCategory {
case versionQualifier:
bOrder := mavenVersionQualifierOrder[b.str]
if aOrder == bOrder {
// b is also undefined or SP; use normal ordering rules.
if a.sep != b.sep {
return int(b.sep) - int(a.sep)
}
return sgnStr(a.str, b.str)
}
return sgn(aOrder, bOrder)
case versionEOF:
// Pad elements compare as 0, but there's no later non-zero elements.
return sgn(aOrder, mavenEmptyQualifier)
case versionNumeric:
// x.0.y means there is an y > 0 later because we trim, so it's less than.
return -1
}
// Should not happen.
panic(bCategory)
}
const mavenEmptyQualifier = -2
// mavenVersionQualifierOrder defines the order in which predefined qualifiers sort.
// 0 is deliberately missing, so unrecognized elements sort as 0, above all these.
var mavenVersionQualifierOrder = map[string]int{
"alpha": mavenEmptyQualifier - 5,
"beta": mavenEmptyQualifier - 4,
"milestone": mavenEmptyQualifier - 3,
"rc": mavenEmptyQualifier - 2,
"cr": mavenEmptyQualifier - 2, // sic.
"snapshot": mavenEmptyQualifier - 1,
"": mavenEmptyQualifier,
"release": mavenEmptyQualifier, // Undocumented but prevalent.
"final": mavenEmptyQualifier,
"ga": mavenEmptyQualifier,
"sp": mavenEmptyQualifier + 1,
}
func compareMavenQualifier(a, b string) int {
aOrder := mavenVersionQualifierOrder[a]
bOrder := mavenVersionQualifierOrder[b]
if aOrder < 0 || bOrder < 0 { // One or the other is predefined.
return sgn(aOrder, bOrder)
}
return sgnStr(a, b)
}
// num returns the number of the i'th element. If the element
// is not a number, it returns -1.
func (m *mavenExtension) num(i int) int64 {
if i >= len(m.elems) {
return -1
}
str := m.elems[i].str
// By the rules of building elements, we know that if the
// first byte is a digit, they all are.
if len(str) == 0 || str[0] < '0' || '9' < str[0] {
return -1
}
return m.elems[i].int
}
// mavenDifference is a weak version of the Difference method. If the first
// difference appears when we have seen up to three numbers and nothing else, it
// returns MajorDiff etc. For anything else it returns OtherDiff. When called,
// we know that there is some difference.
func mavenDifference(u, v *Version) Diff {
// If we have numeric versions up to 3, we go with it.
ue := u.ext.(*mavenExtension)
ve := v.ext.(*mavenExtension)
switch {
case ve.num(nMajor) != ue.num(nMajor):
return DiffMajor
case ve.num(nMinor) != ue.num(nMinor):
return DiffMinor
case ve.num(nPatch) != ue.num(nPatch):
return DiffPatch
}
// Otherwise too hard.
return DiffOther
}