diff options
| author | s <[email protected]> | 2025-11-08 08:46:49 -0500 |
|---|---|---|
| committer | s <[email protected]> | 2025-11-08 08:46:49 -0500 |
| commit | f1070f0a2be928d188c418b59fcbc8e82d1a9367 (patch) | |
| tree | 9693bfbc3d8dcddbca59e49af85427b03b28d360 | |
| parent | 25c11b030e2b4fb9bc9742daea834f0a6e049671 (diff) | |
| download | dborg-f1070f0a2be928d188c418b59fcbc8e82d1a9367.tar.gz dborg-f1070f0a2be928d188c418b59fcbc8e82d1a9367.zip | |
fix: improve version comparison logic and handle dev/devel versions properlyv0.5.1
| -rw-r--r-- | internal/utils/version.go | 33 | ||||
| -rw-r--r-- | internal/utils/version_test.go | 30 |
2 files changed, 62 insertions, 1 deletions
diff --git a/internal/utils/version.go b/internal/utils/version.go index 42a6d40..2b52a8b 100644 --- a/internal/utils/version.go +++ b/internal/utils/version.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "os/exec" + "runtime/debug" "strings" "syscall" @@ -15,6 +16,16 @@ var ( RepositoryURL = "[email protected]:repos/dborg.git" ) +func init() { + if Version == "dev" { + if info, ok := debug.ReadBuildInfo(); ok { + if info.Main.Version != "" && info.Main.Version != "(devel)" { + Version = info.Main.Version + } + } + } +} + func CheckForUpdates(cmd *cobra.Command) error { if !isTerminal() { return nil @@ -57,10 +68,30 @@ func getLatestRemoteTag() (string, error) { } func isNewerVersion(remote, local string) bool { + if local == "dev" || local == "(devel)" { + return false + } + remote = strings.TrimPrefix(remote, "v") local = strings.TrimPrefix(local, "v") - return remote != local && remote > local + if remote == local { + return false + } + + remoteParts := strings.Split(remote, ".") + localParts := strings.Split(local, ".") + + for i := 0; i < len(remoteParts) && i < len(localParts); i++ { + if remoteParts[i] > localParts[i] { + return true + } + if remoteParts[i] < localParts[i] { + return false + } + } + + return len(remoteParts) > len(localParts) } func promptAndUpdate(newVersion string) { diff --git a/internal/utils/version_test.go b/internal/utils/version_test.go index aef3645..e3a27b1 100644 --- a/internal/utils/version_test.go +++ b/internal/utils/version_test.go @@ -42,6 +42,36 @@ func TestIsNewerVersion(t *testing.T) { local: "0.1.0", expected: true, }, + { + name: "dev version local", + remote: "v0.5.0", + local: "dev", + expected: false, + }, + { + name: "devel version local", + remote: "v0.5.0", + local: "(devel)", + expected: false, + }, + { + name: "minor version bump", + remote: "v0.5.0", + local: "v0.4.0", + expected: true, + }, + { + name: "patch version bump", + remote: "v0.4.1", + local: "v0.4.0", + expected: true, + }, + { + name: "major version bump", + remote: "v1.0.0", + local: "v0.5.0", + expected: true, + }, } for _, tt := range tests { |
