Debian Codegraph @

common/version/version.go

raw
1 // Copyright 2025 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package version
15
16 import (
17 "fmt"
18 "io"
19 "math"
20 "runtime"
21 "strconv"
22 "strings"
23
24 "github.com/gohugoio/hugo/compare"
25 "github.com/spf13/cast"
26 )
27
28 // Version represents the Hugo build version.
29 type Version struct {
30 Major int
31
32 Minor int
33
34 // Increment this for bug releases
35 PatchLevel int
36
37 // HugoVersionSuffix is the suffix used in the Hugo version string.
38 // It will be blank for release versions.
39 Suffix string
40
41 // Set when parsed from a string,
42 source string
43 }
44
45 var (
46 _ compare.Eqer = (*VersionString)(nil)
47 _ compare.Comparer = (*VersionString)(nil)
48 )
49
50 // IsAlphaBetaOrRC returns whether this version is an alpha, beta, or release candidate.
51 func (v Version) IsAlphaBetaOrRC() bool {
52 s := strings.ToLower(v.Suffix)
53 // e.g. "alpha.1", "beta.2", "rc.3"
54 return strings.Contains(s, "alpha.") || strings.Contains(s, "beta.") || strings.Contains(s, "rc.")
55 }
56
57 func (v Version) IsZero() bool {
58 return v.Major == 0 && v.Minor == 0 && v.PatchLevel == 0 && v.Suffix == ""
59 }
60
61 func (v Version) String() string {
62 return version(v.Major, v.Minor, v.PatchLevel, v.Suffix)
63 }
64
65 // Version returns the Hugo version.
66 func (v Version) Version() VersionString {
67 return VersionString(v.String())
68 }
69
70 // Compare implements the compare.Comparer interface.
71 func (h Version) Compare(other any) int {
72 return CompareVersions(h, other)
73 }
74
75 // VersionString represents a Hugo version string.
76 type VersionString string
77
78 func (h VersionString) String() string {
79 return string(h)
80 }
81
82 // Compare implements the compare.Comparer interface.
83 func (h VersionString) Compare(other any) int {
84 return CompareVersions(h.Version(), other)
85 }
86
87 func (h VersionString) Version() Version {
88 return MustParseVersion(h.String())
89 }
90
91 // Eq implements the compare.Eqer interface.
92 func (h VersionString) Eq(other any) bool {
93 s, err := cast.ToStringE(other)
94 if err != nil {
95 return false
96 }
97 return s == h.String()
98 }
99
100 // ParseVersion parses a version string.
101 func ParseVersion(s string) (Version, error) {
102 s = strings.TrimPrefix(strings.TrimSpace(s), "v")
103 var vv Version
104 hyphen := strings.Index(s, "-")
105 if hyphen > 0 {
106 suffix := s[hyphen:]
107 if len(suffix) > 1 {
108 if suffix[0] == '-' {
109 suffix = suffix[1:]
110 }
111 if len(suffix) > 0 {
112 vv.Suffix = suffix
113 s = s[:hyphen]
114 }
115 }
116 vv.Suffix = suffix
117 }
118 vv.Major, vv.Minor, vv.PatchLevel = parseVersion(s)
119 vv.source = s
120
121 return vv, nil
122 }
123
124 // MustParseVersion parses a version string
125 // and panics if any error occurs.
126 func MustParseVersion(s string) Version {
127 vv, err := ParseVersion(s)
128 if err != nil {
129 panic(err)
130 }
131 return vv
132 }
133
134 // ReleaseVersion represents the release version.
135 func (v Version) ReleaseVersion() Version {
136 v.Suffix = ""
137 return v
138 }
139
140 // Next returns the next Hugo release version.
141 func (v Version) Next() Version {
142 return Version{Major: v.Major, Minor: v.Minor + 1}
143 }
144
145 // Prev returns the previous Hugo release version.
146 func (v Version) Prev() Version {
147 return Version{Major: v.Major, Minor: v.Minor - 1}
148 }
149
150 // NextPatchLevel returns the next patch/bugfix Hugo version.
151 // This will be a patch increment on the previous Hugo version.
152 func (v Version) NextPatchLevel(level int) Version {
153 prev := v.Prev()
154 prev.PatchLevel = level
155 return prev
156 }
157
158 func version(major, minor, patch int, suffix string) string {
159 if suffix != "" {
160 if suffix[0] != '-' {
161 suffix = "-" + suffix
162 }
163 }
164 if patch > 0 || minor > 53 {
165 return fmt.Sprintf("%d.%d.%d%s", major, minor, patch, suffix)
166 }
167 return fmt.Sprintf("%d.%d%s", major, minor, suffix)
168 }
169
170 // CompareVersion compares v1 with v2.
171 // It returns -1 if the v2 is less than, 0 if equal and 1 if greater than
172 // v1.
173 func CompareVersions(v1 Version, v2 any) int {
174 var c int
175 switch d := v2.(type) {
176 case float64:
177 c = compareFloatWithVersion(d, v1)
178 case float32:
179 c = compareFloatWithVersion(float64(d), v1)
180 case int:
181 c = compareFloatWithVersion(float64(d), v1)
182 case int32:
183 c = compareFloatWithVersion(float64(d), v1)
184 case int64:
185 c = compareFloatWithVersion(float64(d), v1)
186 case Version:
187 if v1.IsZero() && d.IsZero() {
188 // Fall back to source comparison.
189 return strings.Compare(v1.source, d.source)
190 }
191 if d.Major == v1.Major && d.Minor == v1.Minor && d.PatchLevel == v1.PatchLevel {
192 return strings.Compare(v1.Suffix, d.Suffix)
193 }
194 if d.Major > v1.Major {
195 return 1
196 } else if d.Major < v1.Major {
197 return -1
198 }
199 if d.Minor > v1.Minor {
200 return 1
201 } else if d.Minor < v1.Minor {
202 return -1
203 }
204 if d.PatchLevel > v1.PatchLevel {
205 return 1
206 } else if d.PatchLevel < v1.PatchLevel {
207 return -1
208 }
209 default:
210 s, err := cast.ToStringE(v2)
211 if err != nil {
212 return -1
213 }
214
215 v2v, _ := ParseVersion(s)
216 if v1.IsZero() && v2v.IsZero() {
217 // Fall back to source comparison.
218 return strings.Compare(v1.source, v2v.source)
219 }
220
221 return v1.Compare(v2v)
222
223 }
224
225 return c
226 }
227
228 func parseVersion(s string) (int, int, int) {
229 var major, minor, patch int
230 parts := strings.Split(s, ".")
231 if len(parts) > 0 {
232 major, _ = strconv.Atoi(parts[0])
233 }
234 if len(parts) > 1 {
235 minor, _ = strconv.Atoi(parts[1])
236 }
237 if len(parts) > 2 {
238 patch, _ = strconv.Atoi(parts[2])
239 }
240
241 return major, minor, patch
242 }
243
244 // compareFloatWithVersion compares v1 with v2.
245 // It returns -1 if v1 is less than v2, 0 if v1 is equal to v2 and 1 if v1 is greater than v2.
246 func compareFloatWithVersion(v1 float64, v2 Version) int {
247 mf, minf := math.Modf(v1)
248 v1maj := int(mf)
249 v1min := int(minf * 100)
250
251 if v2.Major == v1maj && v2.Minor == v1min {
252 return 0
253 }
254
255 if v1maj > v2.Major {
256 return 1
257 }
258
259 if v1maj < v2.Major {
260 return -1
261 }
262
263 if v1min > v2.Minor {
264 return 1
265 }
266
267 return -1
268 }
269
270 func GoMinorVersion() int {
271 return goMinorVersion(runtime.Version())
272 }
273
274 func goMinorVersion(version string) int {
275 if strings.HasPrefix(version, "devel") {
276 return 9999 // magic
277 }
278 var major, minor int
279 var trailing string
280 n, err := fmt.Sscanf(version, "go%d.%d%s", &major, &minor, &trailing)
281 if n == 2 && err == io.EOF {
282 // Means there were no trailing characters (i.e., not an alpha/beta)
283 err = nil
284 }
285 if err != nil {
286 return 0
287 }
288 return minor
289 }