Skip to content

Commit 98df059

Browse files
authored
Merge pull request cli#3020 from cli/brew-upgrade-notice
Avoid upgrade notice for recent release if gh is under Homebrew prefix
2 parents c5af4dd + 27aea42 commit 98df059

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)