detached be11074

Merge pull request #139 from mi-skam/fix/semver-update-check

Fix update checks for Go pseudo-versions
commit
be11074cd6e8a137143c360423c532a8e53c42ae
author
Patric Eckhart · · 2026-08-10 05:52 UTC
committer
GitHub · · 2026-08-10 05:52 UTC
merge of
1e4a8c4 + 0adeca6 · diff against the first parent
signature
GPG UnverifiedB5690EEEBB952194 The key is unknown
refs
tag: v0.3.39
6 files · +72 -31
M go.mod +1 -0
@@ -9,6 +9,7 @@ require (
99 github.com/mattn/go-runewidth v0.0.16
1010 github.com/sahilm/fuzzy v0.1.1
1111 golang.org/x/image v0.43.0
12+ golang.org/x/mod v0.35.0
1213 golang.org/x/sys v0.26.0
1314 golang.org/x/term v0.25.0
1415 )
@@ -9,6 +9,7 @@ require (
9 github.com/mattn/go-runewidth v0.0.169 github.com/mattn/go-runewidth v0.0.16
10 github.com/sahilm/fuzzy v0.1.110 github.com/sahilm/fuzzy v0.1.1
11 golang.org/x/image v0.43.011 golang.org/x/image v0.43.0
12+ golang.org/x/mod v0.35.0
12 golang.org/x/sys v0.26.013 golang.org/x/sys v0.26.0
13 golang.org/x/term v0.25.014 golang.org/x/term v0.25.0
14 )15 )
M go.sum +2 -0 generated file: collapsed
M packages/agent/update.go +21 -30
@@ -4,10 +4,10 @@ import (
44 "context"
55 "encoding/json"
66 "fmt"
7+ "golang.org/x/mod/semver"
78 "net/http"
89 "os"
910 "path/filepath"
10- "strconv"
1111 "strings"
1212 "sync"
1313 "time"
@@ -121,40 +121,31 @@ func buildInfo(current, latest, url string) UpdateInfo {
121121 return info
122122 }
123123
124-// versionLess returns a < b for dotted semver-ish tags like "0.0.4".
125-// Non-numeric components compare as zero, which is fine for zot's
126-// x.y.z-only scheme.
124+// versionLess reports whether a is an older semantic version than b.
125+// Invalid versions return false so update checks never offer a downgrade.
127126 func versionLess(a, b string) bool {
128- as := splitVersion(a)
129- bs := splitVersion(b)
130- for i := 0; i < 3; i++ {
131- av, bv := 0, 0
132- if i < len(as) {
133- av = as[i]
134- }
135- if i < len(bs) {
136- bv = bs[i]
137- }
138- if av != bv {
139- return av < bv
140- }
141- }
142- return false
127+ comparison, err := compareVersions(a, b)
128+ return err == nil && comparison < 0
143129 }
144130
145-func splitVersion(s string) []int {
146- s = strings.TrimPrefix(s, "v")
147- // Strip build-metadata suffix like "(abc1234, 2026-04-18)".
148- if i := strings.IndexAny(s, " ("); i > 0 {
149- s = s[:i]
131+func compareVersions(a, b string) (int, error) {
132+ a = normalizedVersion(a)
133+ b = normalizedVersion(b)
134+ if !semver.IsValid(a) {
135+ return 0, fmt.Errorf("invalid semantic version %q", strings.TrimPrefix(a, "v"))
150136 }
151- parts := strings.Split(s, ".")
152- out := make([]int, 0, len(parts))
153- for _, p := range parts {
154- n, _ := strconv.Atoi(p)
155- out = append(out, n)
137+ if !semver.IsValid(b) {
138+ return 0, fmt.Errorf("invalid semantic version %q", strings.TrimPrefix(b, "v"))
139+ }
140+ return semver.Compare(a, b), nil
141+}
142+
143+func normalizedVersion(v string) string {
144+ v = versionOnly(v)
145+ if !strings.HasPrefix(v, "v") {
146+ v = "v" + v
156147 }
157- return out
148+ return v
158149 }
159150
160151 // fetchLatestRelease queries the GitHub API for the latest published
@@ -4,10 +4,10 @@ import (
4 "context"4 "context"
5 "encoding/json"5 "encoding/json"
6 "fmt"6 "fmt"
7+ "golang.org/x/mod/semver"
7 "net/http"8 "net/http"
8 "os"9 "os"
9 "path/filepath"10 "path/filepath"
10- "strconv"
11 "strings"11 "strings"
12 "sync"12 "sync"
13 "time"13 "time"
@@ -121,40 +121,31 @@ func buildInfo(current, latest, url string) UpdateInfo {
121 return info121 return info
122 }122 }
123 123
124-// versionLess returns a < b for dotted semver-ish tags like "0.0.4".124+// versionLess reports whether a is an older semantic version than b.
125-// Non-numeric components compare as zero, which is fine for zot's125+// Invalid versions return false so update checks never offer a downgrade.
126-// x.y.z-only scheme.
127 func versionLess(a, b string) bool {126 func versionLess(a, b string) bool {
128- as := splitVersion(a)127+ comparison, err := compareVersions(a, b)
129- bs := splitVersion(b)128+ return err == nil && comparison < 0
130- for i := 0; i < 3; i++ {
131- av, bv := 0, 0
132- if i < len(as) {
133- av = as[i]
134- }
135- if i < len(bs) {
136- bv = bs[i]
137- }
138- if av != bv {
139- return av < bv
140- }
141- }
142- return false
143 }129 }
144 130
145-func splitVersion(s string) []int {131+func compareVersions(a, b string) (int, error) {
146- s = strings.TrimPrefix(s, "v")132+ a = normalizedVersion(a)
147- // Strip build-metadata suffix like "(abc1234, 2026-04-18)".133+ b = normalizedVersion(b)
148- if i := strings.IndexAny(s, " ("); i > 0 {134+ if !semver.IsValid(a) {
149- s = s[:i]135+ return 0, fmt.Errorf("invalid semantic version %q", strings.TrimPrefix(a, "v"))
150 }136 }
151- parts := strings.Split(s, ".")137+ if !semver.IsValid(b) {
152- out := make([]int, 0, len(parts))138+ return 0, fmt.Errorf("invalid semantic version %q", strings.TrimPrefix(b, "v"))
153- for _, p := range parts {139+ }
154- n, _ := strconv.Atoi(p)140+ return semver.Compare(a, b), nil
155- out = append(out, n)141+}
142+
143+func normalizedVersion(v string) string {
144+ v = versionOnly(v)
145+ if !strings.HasPrefix(v, "v") {
146+ v = "v" + v
156 }147 }
157- return out148+ return v
158 }149 }
159 150
160 // fetchLatestRelease queries the GitHub API for the latest published151 // fetchLatestRelease queries the GitHub API for the latest published
A packages/agent/update_test.go +35 -0
@@ -0,0 +1,35 @@
1+package agent
2+
3+import "testing"
4+
5+func TestVersionLess(t *testing.T) {
6+ pseudo := "0.3.35-0.20260809165004-9e3c28d4b65"
7+ cases := []struct {
8+ name string
9+ a, b string
10+ want bool
11+ }{
12+ {name: "older release", a: "0.3.33", b: "0.3.34", want: true},
13+ {name: "newer release", a: "0.3.35", b: "0.3.34", want: false},
14+ {name: "pseudo after prior release", a: pseudo, b: "0.3.34", want: false},
15+ {name: "pseudo before final release", a: pseudo, b: "0.3.35", want: true},
16+ {name: "prior release before pseudo", a: "0.3.34", b: pseudo, want: true},
17+ {name: "decorated build", a: "0.3.33 (abc1234, 2026-08-09)", b: "0.3.34", want: true},
18+ {name: "invalid current", a: "not-a-version", b: "0.3.34", want: false},
19+ }
20+ for _, tc := range cases {
21+ t.Run(tc.name, func(t *testing.T) {
22+ if got := versionLess(tc.a, tc.b); got != tc.want {
23+ t.Fatalf("versionLess(%q, %q) = %v, want %v", tc.a, tc.b, got, tc.want)
24+ }
25+ })
26+ }
27+}
28+
29+func TestBuildInfoDoesNotOfferPseudoVersionDowngrade(t *testing.T) {
30+ current := "0.3.35-0.20260809165004-9e3c28d4b65"
31+ info := buildInfo(current, "v0.3.34", "https://example.com/release")
32+ if info.Available {
33+ t.Fatalf("buildInfo(%q, %q) offered a downgrade", current, info.Latest)
34+ }
35+}
@@ -0,0 +1,35 @@
1+package agent
2+
3+import "testing"
4+
5+func TestVersionLess(t *testing.T) {
6+ pseudo := "0.3.35-0.20260809165004-9e3c28d4b65"
7+ cases := []struct {
8+ name string
9+ a, b string
10+ want bool
11+ }{
12+ {name: "older release", a: "0.3.33", b: "0.3.34", want: true},
13+ {name: "newer release", a: "0.3.35", b: "0.3.34", want: false},
14+ {name: "pseudo after prior release", a: pseudo, b: "0.3.34", want: false},
15+ {name: "pseudo before final release", a: pseudo, b: "0.3.35", want: true},
16+ {name: "prior release before pseudo", a: "0.3.34", b: pseudo, want: true},
17+ {name: "decorated build", a: "0.3.33 (abc1234, 2026-08-09)", b: "0.3.34", want: true},
18+ {name: "invalid current", a: "not-a-version", b: "0.3.34", want: false},
19+ }
20+ for _, tc := range cases {
21+ t.Run(tc.name, func(t *testing.T) {
22+ if got := versionLess(tc.a, tc.b); got != tc.want {
23+ t.Fatalf("versionLess(%q, %q) = %v, want %v", tc.a, tc.b, got, tc.want)
24+ }
25+ })
26+ }
27+}
28+
29+func TestBuildInfoDoesNotOfferPseudoVersionDowngrade(t *testing.T) {
30+ current := "0.3.35-0.20260809165004-9e3c28d4b65"
31+ info := buildInfo(current, "v0.3.34", "https://example.com/release")
32+ if info.Available {
33+ t.Fatalf("buildInfo(%q, %q) offered a downgrade", current, info.Latest)
34+ }
35+}
M packages/agent/zotfile.go +5 -1
@@ -752,7 +752,11 @@ func checkZotfileRequirements(zf zotfileLoaded, version string) error {
752752 if versionOnly(version) == "0.0.0" {
753753 return fmt.Errorf("agent requires zot %s or newer; unversioned development builds cannot satisfy min_zot", min)
754754 }
755- if versionLess(version, min) {
755+ comparison, err := compareVersions(version, min)
756+ if err != nil {
757+ return fmt.Errorf("validate min_zot requirement: %w", err)
758+ }
759+ if comparison < 0 {
756760 return fmt.Errorf("agent requires zot %s or newer; running %s", min, versionOnly(version))
757761 }
758762 }
@@ -752,7 +752,11 @@ func checkZotfileRequirements(zf zotfileLoaded, version string) error {
752 if versionOnly(version) == "0.0.0" {752 if versionOnly(version) == "0.0.0" {
753 return fmt.Errorf("agent requires zot %s or newer; unversioned development builds cannot satisfy min_zot", min)753 return fmt.Errorf("agent requires zot %s or newer; unversioned development builds cannot satisfy min_zot", min)
754 }754 }
755- if versionLess(version, min) {755+ comparison, err := compareVersions(version, min)
756+ if err != nil {
757+ return fmt.Errorf("validate min_zot requirement: %w", err)
758+ }
759+ if comparison < 0 {
756 return fmt.Errorf("agent requires zot %s or newer; running %s", min, versionOnly(version))760 return fmt.Errorf("agent requires zot %s or newer; running %s", min, versionOnly(version))
757 }761 }
758 }762 }
M packages/agent/zotfile_test.go +8 -0
@@ -223,6 +223,14 @@ func TestCheckZotfileMinVersion(t *testing.T) {
223223 if err := checkZotfileRequirements(zf, "0.3.0"); err != nil {
224224 t.Fatalf("minimum version rejected: %v", err)
225225 }
226+ if err := checkZotfileRequirements(zf, "not-a-version"); err == nil {
227+ t.Fatal("invalid zot version accepted")
228+ }
229+
230+ zf.Manifest.Runtime.MinZot = "not-a-version"
231+ if err := checkZotfileRequirements(zf, "0.3.0"); err == nil {
232+ t.Fatal("invalid min_zot requirement accepted")
233+ }
226234 }
227235
228236 func TestApplyZotfileModelRequirementsRejectsUnsupportedFields(t *testing.T) {
@@ -223,6 +223,14 @@ func TestCheckZotfileMinVersion(t *testing.T) {
223 if err := checkZotfileRequirements(zf, "0.3.0"); err != nil {223 if err := checkZotfileRequirements(zf, "0.3.0"); err != nil {
224 t.Fatalf("minimum version rejected: %v", err)224 t.Fatalf("minimum version rejected: %v", err)
225 }225 }
226+ if err := checkZotfileRequirements(zf, "not-a-version"); err == nil {
227+ t.Fatal("invalid zot version accepted")
228+ }
229+
230+ zf.Manifest.Runtime.MinZot = "not-a-version"
231+ if err := checkZotfileRequirements(zf, "0.3.0"); err == nil {
232+ t.Fatal("invalid min_zot requirement accepted")
233+ }
226 }234 }
227 235
228 func TestApplyZotfileModelRequirementsRejectsUnsupportedFields(t *testing.T) {236 func TestApplyZotfileModelRequirementsRejectsUnsupportedFields(t *testing.T) {

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close