@@ -216,7 +216,13 @@ func SortVersions(versions BinaryVersionWithDownloadURL) []string {
216
216
return versionTags
217
217
}
218
218
219
- // CompareSemVer compares two semantic version strings and returns true if v1 should be ordered before v2
219
+ // CompareSemVer compares two semantic version strings and returns true if v1 is greater than v2.
220
+ // Examples:
221
+ // - CompareSemVer("v2.0.0", "v1.0.0") returns true
222
+ // - CompareSemVer("v1.2.0", "v1.1.0") returns true
223
+ // - CompareSemVer("v1.0.0", "v1.0.0-beta") returns true
224
+ // - CompareSemVer("v1.0.0-beta.2", "v1.0.0-beta.1") returns true
225
+ // - CompareSemVer("v1.0.0", "v2.0.0") returns false
220
226
func CompareSemVer (v1 , v2 string ) bool {
221
227
// Trim "v" prefix
222
228
v1 = strings .TrimPrefix (v1 , "v" )
@@ -237,15 +243,49 @@ func CompareSemVer(v1, v2 string) bool {
237
243
}
238
244
}
239
245
240
- // Compare pre-release parts if main versions are equal
241
- // A pre-release version is always ordered greater than the normal version
246
+ // If main versions are equal, handle pre-release versions
247
+ // A pre-release version has lower precedence than the normal version
242
248
if v1Pre == "" && v2Pre != "" {
243
- return false
249
+ return true // v1 (normal version) is greater
244
250
}
245
251
if v1Pre != "" && v2Pre == "" {
246
- return true
252
+ return false // v2 (normal version) is greater
247
253
}
248
- return v1Pre > v2Pre
254
+
255
+ // If both have pre-release versions, compare them
256
+ if v1Pre != "" && v2Pre != "" {
257
+ v1PreParts := strings .Split (v1Pre , "." )
258
+ v2PreParts := strings .Split (v2Pre , "." )
259
+
260
+ // Compare each pre-release identifier
261
+ minLen := len (v1PreParts )
262
+ if len (v2PreParts ) < minLen {
263
+ minLen = len (v2PreParts )
264
+ }
265
+
266
+ for i := 0 ; i < minLen ; i ++ {
267
+ // Try to compare as integers first
268
+ v1Int , v1IsInt := strconv .Atoi (v1PreParts [i ])
269
+ v2Int , v2IsInt := strconv .Atoi (v2PreParts [i ])
270
+
271
+ if v1IsInt == nil && v2IsInt == nil {
272
+ // Both are integers
273
+ if v1Int != v2Int {
274
+ return v1Int > v2Int
275
+ }
276
+ } else {
277
+ // Compare as strings if not both integers
278
+ if v1PreParts [i ] != v2PreParts [i ] {
279
+ return v1PreParts [i ] > v2PreParts [i ]
280
+ }
281
+ }
282
+ }
283
+
284
+ // If all parts so far are equal, longer one is greater
285
+ return len (v1PreParts ) > len (v2PreParts )
286
+ }
287
+
288
+ return false // versions are exactly equal
249
289
}
250
290
251
291
// splitVersion separates the main version (e.g., "0.4.11") from the pre-release (e.g., "Binarytion.1")
0 commit comments