X Tutup
Skip to content

Commit 27aea42

Browse files
committed
Avoid upgrade notice for recent release if gh is under Homebrew prefix
Before, when gh detected there was a new release in the `cli/cli` repo, it would show this notice: A new release of gh is available: {V1} → {V2} Additionally, when the release was more than 24h old, we would show this to Homebrew users: To upgrade, run: brew update && brew upgrade gh Ref. feb4acc This change makes it so that the original notice "A new release of gh is available" is NOT shown to Homebrew users unless the release is older than 24h. We effectively hide the fact that any release happened until we're sure that the version bump has made it to `homebrew-core`.
1 parent d6798b1 commit 27aea42

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

cmd/gh/main.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,19 @@ func main() {
163163

164164
newRelease := <-updateMessageChan
165165
if newRelease != nil {
166-
ghExe, _ := os.Executable()
166+
isHomebrew := false
167+
if ghExe, err := os.Executable(); err == nil {
168+
isHomebrew = isUnderHomebrew(ghExe)
169+
}
170+
if isHomebrew && isRecentRelease(newRelease.PublishedAt) {
171+
// do not notify Homebrew users before the version bump had a chance to get merged into homebrew-core
172+
return
173+
}
167174
fmt.Fprintf(stderr, "\n\n%s %s → %s\n",
168175
ansi.Color("A new release of gh is available:", "yellow"),
169176
ansi.Color(buildVersion, "cyan"),
170177
ansi.Color(newRelease.Version, "cyan"))
171-
if suggestBrewUpgrade(newRelease, ghExe) {
178+
if isHomebrew {
172179
fmt.Fprintf(stderr, "To upgrade, run: %s\n", "brew update && brew upgrade gh")
173180
}
174181
fmt.Fprintf(stderr, "%s\n\n",
@@ -265,13 +272,12 @@ func apiVerboseLog() api.ClientOption {
265272
return api.VerboseLog(colorable.NewColorable(os.Stderr), logTraffic, colorize)
266273
}
267274

268-
// Suggest to `brew upgrade gh` only if gh was found under homebrew prefix and when the release was
269-
// published over 24h ago, allowing homebrew-core ample time to merge the formula bump.
270-
func suggestBrewUpgrade(rel *update.ReleaseInfo, ghBinary string) bool {
271-
if rel.PublishedAt.IsZero() || time.Since(rel.PublishedAt) < time.Duration(time.Hour*24) {
272-
return false
273-
}
275+
func isRecentRelease(publishedAt time.Time) bool {
276+
return !publishedAt.IsZero() && time.Since(publishedAt) < time.Hour*24
277+
}
274278

279+
// Check whether the gh binary was found under the Homebrew prefix
280+
func isUnderHomebrew(ghBinary string) bool {
275281
brewExe, err := safeexec.LookPath("brew")
276282
if err != nil {
277283
return false

0 commit comments

Comments
 (0)
X Tutup