detached 13b118a

fix: compare update versions as semver

commit
13b118a8f90cf0f8a825499810e61da29fcc25b2
author
mi-skam · · 2026-08-09 22:29 UTC
parent
1e4a8c4
signature
unsigned
4 files · +51 -30
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 +13 -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,23 @@ 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 fail closed so the updater never offers 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- }
127+ a = normalizedVersion(a)
128+ b = normalizedVersion(b)
129+ if !semver.IsValid(a) || !semver.IsValid(b) {
130+ return false
141131 }
142- return false
132+ return semver.Compare(a, b) < 0
143133 }
144134
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]
150- }
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)
135+func normalizedVersion(v string) string {
136+ v = versionOnly(v)
137+ if !strings.HasPrefix(v, "v") {
138+ v = "v" + v
156139 }
157- return out
140+ return v
158141 }
159142
160143 // 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,23 @@ 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 fail closed so the updater never offers 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+ a = normalizedVersion(a)
129- bs := splitVersion(b)128+ b = normalizedVersion(b)
130- for i := 0; i < 3; i++ {129+ if !semver.IsValid(a) || !semver.IsValid(b) {
131- av, bv := 0, 0130+ return false
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 }131 }
142- return false132+ return semver.Compare(a, b) < 0
143 }133 }
144 134
145-func splitVersion(s string) []int {135+func normalizedVersion(v string) string {
146- s = strings.TrimPrefix(s, "v")136+ v = versionOnly(v)
147- // Strip build-metadata suffix like "(abc1234, 2026-04-18)".137+ if !strings.HasPrefix(v, "v") {
148- if i := strings.IndexAny(s, " ("); i > 0 {138+ v = "v" + v
149- s = s[:i]
150- }
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)
156 }139 }
157- return out140+ return v
158 }141 }
159 142
160 // fetchLatestRelease queries the GitHub API for the latest published143 // 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+}

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