-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathversion.go
237 lines (214 loc) · 6.03 KB
/
version.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
// Copyright 2020 The Cockroach Authors.
//
// 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 version
import (
"bytes"
"fmt"
"regexp"
"strconv"
"strings"
)
// Version represents a semantic version; see
// https://semver.org/spec/v2.0.0.html.
type Version struct {
major int32
minor int32
patch int32
preRelease string
metadata string
}
// Major returns the major (first) version number.
func (v *Version) Major() int {
return int(v.major)
}
// Minor returns the minor (second) version number.
func (v *Version) Minor() int {
return int(v.minor)
}
// Patch returns the patch (third) version number.
func (v *Version) Patch() int {
return int(v.patch)
}
// PreRelease returns the pre-release version (if present).
func (v *Version) PreRelease() string {
return v.preRelease
}
// Metadata returns the metadata (if present).
func (v *Version) Metadata() string {
return v.metadata
}
// String returns the string representation, in the format:
// "v1.2.3-beta+md"
func (v Version) String() string {
var b bytes.Buffer
fmt.Fprintf(&b, "v%d.%d.%d", v.major, v.minor, v.patch)
if v.preRelease != "" {
fmt.Fprintf(&b, "-%s", v.preRelease)
}
if v.metadata != "" {
fmt.Fprintf(&b, "+%s", v.metadata)
}
return b.String()
}
// versionRE is the regexp that is used to verify that a version string is
// of the form "vMAJOR.MINOR.PATCH[-PRERELEASE][+METADATA]". This
// conforms to https://semver.org/spec/v2.0.0.html
var versionRE = regexp.MustCompile(
`^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z-.]+)?(\+[0-9A-Za-z-.]+|)?$`,
// ^major ^minor ^patch ^preRelease ^metadata
)
// numericRE is the regexp used to check if an identifier is numeric.
var numericRE = regexp.MustCompile(`^(0|[1-9][0-9]*)$`)
// Parse creates a version from a string. The string must be a valid semantic
// version (as per https://semver.org/spec/v2.0.0.html) in the format:
// "vMINOR.MAJOR.PATCH[-PRERELEASE][+METADATA]".
// MINOR, MAJOR, and PATCH are numeric values (without any leading 0s).
// PRERELEASE and METADATA can contain ASCII characters and digits, hyphens and
// dots.
func Parse(str string) (*Version, error) {
if !versionRE.MatchString(str) {
return nil, fmt.Errorf("invalid version string '%s'", str)
}
var v Version
r := strings.NewReader(str)
// Read the major.minor.patch part.
_, err := fmt.Fscanf(r, "v%d.%d.%d", &v.major, &v.minor, &v.patch)
if err != nil {
panic(fmt.Sprintf("invalid version '%s' passed the regex: %s", str, err))
}
remaining := str[len(str)-r.Len():]
// Read the pre-release, if present.
if len(remaining) > 0 && remaining[0] == '-' {
p := strings.IndexRune(remaining, '+')
if p == -1 {
p = len(remaining)
}
v.preRelease = remaining[1:p]
remaining = remaining[p:]
}
// Read the metadata, if present.
if len(remaining) > 0 {
if remaining[0] != '+' {
panic(fmt.Sprintf("invalid version '%s' passed the regex", str))
}
v.metadata = remaining[1:]
}
return &v, nil
}
// MustParse is like Parse but panics on any error. Recommended as an
// initializer for global values.
func MustParse(str string) *Version {
v, err := Parse(str)
if err != nil {
panic(err)
}
return v
}
func cmpVal(a, b int32) int {
if a > b {
return +1
}
if a < b {
return -1
}
return 0
}
// Compare returns -1, 0, or +1 indicating the relative ordering of versions.
func (v *Version) Compare(w *Version) int {
if v := cmpVal(v.major, w.major); v != 0 {
return v
}
if v := cmpVal(v.minor, w.minor); v != 0 {
return v
}
if v := cmpVal(v.patch, w.patch); v != 0 {
return v
}
if v.preRelease != w.preRelease {
if v.preRelease == "" && w.preRelease != "" {
// 1.0.0 is greater than 1.0.0-alpha.
return 1
}
if v.preRelease != "" && w.preRelease == "" {
// 1.0.0-alpha is less than 1.0.0.
return -1
}
// Quoting from https://semver.org/spec/v2.0.0.html:
// Precedence for two pre-release versions with the same major, minor, and
// patch version MUST be determined by comparing each dot separated
// identifier from left to right until a difference is found as follows:
// (1) Identifiers consisting of only digits are compared numerically.
// (2) identifiers with letters or hyphens are compared lexically in ASCII
// sort order.
// (3) Numeric identifiers always have lower precedence than non-numeric
// identifiers.
// (4) A larger set of pre-release fields has a higher precedence than a
// smaller set, if all of the preceding identifiers are equal.
//
vs := strings.Split(v.preRelease, ".")
ws := strings.Split(w.preRelease, ".")
for ; len(vs) > 0 && len(ws) > 0; vs, ws = vs[1:], ws[1:] {
vStr, wStr := vs[0], ws[0]
if vStr == wStr {
continue
}
vNumeric := numericRE.MatchString(vStr)
wNumeric := numericRE.MatchString(wStr)
switch {
case vNumeric && wNumeric:
// Case 1.
vVal, err := strconv.Atoi(vStr)
if err != nil {
panic(err)
}
wVal, err := strconv.Atoi(wStr)
if err != nil {
panic(err)
}
if vVal == wVal {
panic("different strings yield the same numbers")
}
if vVal < wVal {
return -1
}
return 1
case vNumeric:
// Case 3.
return -1
case wNumeric:
// Case 3.
return 1
default:
// Case 2.
if vStr < wStr {
return -1
}
return 1
}
}
if len(vs) > 0 {
// Case 4.
return +1
}
if len(ws) > 0 {
// Case 4.
return -1
}
}
return 0
}
// AtLeast returns true if v >= w.
func (v *Version) AtLeast(w *Version) bool {
return v.Compare(w) >= 0
}