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

Fix version comparison with metadata #277

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions pkg/version/semver/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var (
versionRegex = regexp.MustCompile(`^v?([0-9]+)(\.[0-9]+)?(\.[0-9]+)?(.*)$`)
versionRegex = regexp.MustCompile(`^(\D*)?v?([0-9]+)(\.[0-9]+)?(\.[0-9]+)?(.*)$`)
)

// SemVer is a struct to contain a SemVer of an image tag.
Expand Down Expand Up @@ -36,11 +36,11 @@ func Parse(tag string) *SemVer {
}

for i := 0; i < 3; i++ {
if len(match[i+1]) > 0 {
s.version[i], _ = strconv.ParseInt(strings.TrimPrefix(match[i+1], "."), 10, 64)
if len(match[i+2]) > 0 {
s.version[i], _ = strconv.ParseInt(strings.TrimPrefix(match[i+2], "."), 10, 64)
}
}
s.metadata = match[4]
s.metadata = match[5]

return s
}
Expand All @@ -63,23 +63,37 @@ func (s *SemVer) LessThan(other *SemVer) bool {
}

// Compare version numbers
if s.compareVersionNumbers(other) {
less, equal := s.compareVersionNumbers(other)
if less {
return true
}

// Compare pre-release metadata
return s.comparePreReleaseMetadata(other)
if equal {
// Compare pre-release metadata
return s.comparePreReleaseMetadata(other)
}

return false
}

func (s *SemVer) isInvalidComparison(other *SemVer) bool {
return len(other.original) == 0 || len(s.original) == 0
}
func (s *SemVer) compareVersionNumbers(other *SemVer) bool {

func (s *SemVer) compareVersionNumbers(other *SemVer) (less bool, equal bool) {
for i := 0; i < 3; i++ {
if s.version[i] != other.version[i] {
return s.version[i] < other.version[i]
if s.version[i] < other.version[i] {
return true, false
}

if s.version[i] > other.version[i] {
return false, false
}
}
}
return false

return false, true
}

func (s *SemVer) comparePreReleaseMetadata(other *SemVer) bool {
Expand Down
26 changes: 21 additions & 5 deletions pkg/version/semver/semver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ func TestParse(t *testing.T) {
[3]int64{0, 0, 0},
"v",
},
"Not matching semver should no output": {
"hello-1.2.3",
[3]int64{0, 0, 0},
"hello-1.2.3",
},
"1 -> [1 0 0]": {
"1",
[3]int64{1, 0, 0},
Expand Down Expand Up @@ -61,6 +56,11 @@ func TestParse(t *testing.T) {
[3]int64{1, 0, 1},
"-",
},
"develop-version-1.24.1.4740 -> [1 24 1]": {
"develop-version-1.24.1.4740",
[3]int64{1, 24, 1},
".4740",
},
}

for name, test := range tests {
Expand Down Expand Up @@ -104,6 +104,10 @@ func TestMajorMinorPatch(t *testing.T) {
"v1.3.hello1-debian-3.hello-world-12",
[3]int64{1, 3, 0},
},
"develop-version-1.24.1.4740 -> [1 24 1]": {
"develop-version-1.24.1.4740",
[3]int64{1, 24, 1},
},
}

testPart := func(name string, exp int64, f func() int64) {
Expand Down Expand Up @@ -209,6 +213,18 @@ func TestLessThan(t *testing.T) {
"0.21.0-debian-10-r39-hello", "0.21.0-debian-10-r9-hello",
false,
},
"If more with complications for rnumber": {
"1.63.0-debian-12-r0", "1.62.0-debian-12-r3",
false,
},
"If less with complications for rnumber": {
"1.63.0-debian-12-r0", "1.63.0-debian-12-r1",
true,
},
"If less with complications for rnumber with prefix": {
"version-1.23.1.4708", "develop-version-1.24.1.4740",
true,
},
}

for name, test := range tests {
Expand Down