Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compareVersion does not properly compare numerical semver -- needs to cast parts to ints #10

Open
NeilHanlon opened this issue Nov 26, 2024 · 1 comment

Comments

@NeilHanlon
Copy link
Contributor

// compareVersion takes in two kernel version strings either in the form of "X.Y" or "X.Y.Z" and returns:
// 1 if a > b
// 0 if a == b
// -1 if a < b
func compareVersion(a, b string) int {
// Split version strings into parts
aParts := strings.Split(a, ".")
bParts := strings.Split(b, ".")
// Compare major version
if aParts[0] != bParts[0] {
if aParts[0] > bParts[0] {
return 1
}
return -1
}
// Compare minor version
if aParts[1] != bParts[1] {
if aParts[1] > bParts[1] {
return 1
}
return -1
}
// If there's a third part, compare it
if len(aParts) == 3 && len(bParts) == 3 {
if aParts[2] != bParts[2] {
if aParts[2] > bParts[2] {
return 1
}
return -1
}
}
return 0
}

Expected results

6.1 < 6.12

Actual results

6.1 > 6.12

@gvrose8192
Copy link

gvrose8192 commented Dec 11, 2024

I've implemented semver:
diff --git a/packager/v1/v1.go b/packager/v1/v1.go
index 22e044a..23a7143 100644
--- a/packager/v1/v1.go
+++ b/packager/v1/v1.go
@@ -11,6 +11,7 @@ import (
"text/template"

    "github.com/ctrliq/kernels-upstream/packager"
  •   "golang.org/x/mod/semver"
    

)

  •                   "compareVersion": compareVersion,
    
  •                   "Compare": semver.Compare,
    

It fails this test:
+{{if (eq (Compare .Version "6.2.0") -1)}}

With .Version == 6.1.119

Which it should not.

Conclusion - go package semver is as rotten as all the rest of the version comparisons I've seen while searching for version comparison packages. So we'll have to write our own.

TBC - the comparison works for 6.6.x and stable (6.12.x). That 6.1.119 is considered greater than or equal to 6.2.0 shows how completely useless semver is for our circumstances.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants