X Tutup
Skip to content

Commit 82d19b7

Browse files
committed
Update notifier: avoid false positives when gh is built from source
When gh was built from source, the version number will look something like this since it's taken from `git describe`: v1.4.0-34-g{SHA} When compared as semver against `v1.4.0`, the latter version is falsely reported as newer. This is because the output of `git describe` wasn't meant to be interpreted as semver. The solution is to translate the `git describe` string to faux-semver so it can be safely compared with the version reported from the server. Fixes this case: A new release of gh is available: v1.4.0-41-g2f9e4cb1 → v1.4.0 https://github.com/cli/cli/releases/tag/v1.4.0
1 parent b5366c6 commit 82d19b7

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

internal/update/update.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package update
33
import (
44
"fmt"
55
"io/ioutil"
6+
"regexp"
7+
"strconv"
8+
"strings"
69
"time"
710

811
"github.com/cli/cli/api"
@@ -11,6 +14,8 @@ import (
1114
"gopkg.in/yaml.v3"
1215
)
1316

17+
var gitDescribeSuffixRE = regexp.MustCompile(`\d+-\d+-g[a-f0-9]{8}$`)
18+
1419
// ReleaseInfo stores information about a release
1520
type ReleaseInfo struct {
1621
Version string `json:"tag_name"`
@@ -83,6 +88,12 @@ func setStateEntry(stateFilePath string, t time.Time, r ReleaseInfo) error {
8388
}
8489

8590
func versionGreaterThan(v, w string) bool {
91+
w = gitDescribeSuffixRE.ReplaceAllStringFunc(w, func(m string) string {
92+
idx := strings.IndexRune(m, '-')
93+
n, _ := strconv.Atoi(m[0:idx])
94+
return fmt.Sprintf("%d-pre.0", n+1)
95+
})
96+
8697
vv, ve := version.NewVersion(v)
8798
vw, we := version.NewVersion(w)
8899

internal/update/update_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ func TestCheckForUpdate(t *testing.T) {
3333
LatestURL: "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
3434
ExpectsResult: true,
3535
},
36+
{
37+
Name: "current is built from source",
38+
CurrentVersion: "v1.2.3-123-gdeadbeef",
39+
LatestVersion: "v1.2.3",
40+
LatestURL: "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
41+
ExpectsResult: false,
42+
},
43+
{
44+
Name: "current is built from source after a prerelease",
45+
CurrentVersion: "v1.2.3-rc.1-123-gdeadbeef",
46+
LatestVersion: "v1.2.3",
47+
LatestURL: "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
48+
ExpectsResult: true,
49+
},
50+
{
51+
Name: "latest is newer than version build from source",
52+
CurrentVersion: "v1.2.3-123-gdeadbeef",
53+
LatestVersion: "v1.2.4",
54+
LatestURL: "https://www.spacejam.com/archive/spacejam/movie/jam.htm",
55+
ExpectsResult: true,
56+
},
3657
{
3758
Name: "latest is current",
3859
CurrentVersion: "v1.0.0",

0 commit comments

Comments
 (0)
X Tutup