Skip to content

Commit c8d8c09

Browse files
authored
Merge pull request #19626 from Homebrew/update_if_needed
Add `brew update-if-needed`
2 parents 9289851 + 5ec5063 commit c8d8c09

File tree

9 files changed

+88
-0
lines changed

9 files changed

+88
-0
lines changed

Library/Homebrew/brew.sh

+16
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,22 @@ auto-update() {
384384
unset HOMEBREW_AUTO_UPDATE_CASK_TAP
385385
}
386386

387+
# Only `brew update-if-needed` should be handled here.
388+
# We want it as fast as possible but it needs auto-update() defined above.
389+
# HOMEBREW_LIBRARY set by bin/brew
390+
# shellcheck disable=SC2154
391+
# doesn't need a default case as other arguments handled elsewhere.
392+
# shellcheck disable=SC2249
393+
# Don't need to pass through any arguments.
394+
# shellcheck disable=SC2119
395+
case "$@" in
396+
update-if-needed)
397+
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/update-if-needed.sh"
398+
homebrew-update-if-needed
399+
exit 0
400+
;;
401+
esac
402+
387403
#####
388404
##### Setup output so e.g. odie looks as nice as possible.
389405
#####
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# typed: strict
2+
# frozen_string_literal: true
3+
4+
require "abstract_command"
5+
require "shell_command"
6+
7+
module Homebrew
8+
module Cmd
9+
class UpdateIfNeeded < AbstractCommand
10+
include ShellCommand
11+
12+
cmd_args do
13+
description <<~EOS
14+
Runs `brew update --auto-update` only if needed.
15+
This is a good replacement for `brew update` in scripts where you want
16+
the no-op case to be both possible and really fast.
17+
EOS
18+
19+
named_args :none
20+
end
21+
end
22+
end
23+
end
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Documentation defined in Library/Homebrew/cmd/update-if-needed.rb
2+
3+
homebrew-update-if-needed() {
4+
export HOMEBREW_AUTO_UPDATE_COMMAND="1"
5+
auto-update "$@"
6+
}

completions/bash/brew

+17
Original file line numberDiff line numberDiff line change
@@ -2798,6 +2798,22 @@ _brew_update() {
27982798
esac
27992799
}
28002800

2801+
_brew_update_if_needed() {
2802+
local cur="${COMP_WORDS[COMP_CWORD]}"
2803+
case "${cur}" in
2804+
-*)
2805+
__brewcomp "
2806+
--debug
2807+
--help
2808+
--quiet
2809+
--verbose
2810+
"
2811+
return
2812+
;;
2813+
*) ;;
2814+
esac
2815+
}
2816+
28012817
_brew_update_license_data() {
28022818
local cur="${COMP_WORDS[COMP_CWORD]}"
28032819
case "${cur}" in
@@ -3214,6 +3230,7 @@ _brew() {
32143230
untap) _brew_untap ;;
32153231
up) _brew_up ;;
32163232
update) _brew_update ;;
3233+
update-if-needed) _brew_update_if_needed ;;
32173234
update-license-data) _brew_update_license_data ;;
32183235
update-maintainers) _brew_update_maintainers ;;
32193236
update-python-resources) _brew_update_python_resources ;;

completions/fish/brew.fish

+7
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,13 @@ __fish_brew_complete_arg 'update' -l quiet -d 'Make some output more quiet'
18001800
__fish_brew_complete_arg 'update' -l verbose -d 'Print the directories checked and `git` operations performed'
18011801

18021802

1803+
__fish_brew_complete_cmd 'update-if-needed' 'Runs `brew update --auto-update` only if needed'
1804+
__fish_brew_complete_arg 'update-if-needed' -l debug -d 'Display any debugging information'
1805+
__fish_brew_complete_arg 'update-if-needed' -l help -d 'Show this message'
1806+
__fish_brew_complete_arg 'update-if-needed' -l quiet -d 'Make some output more quiet'
1807+
__fish_brew_complete_arg 'update-if-needed' -l verbose -d 'Make some output more verbose'
1808+
1809+
18031810
__fish_brew_complete_cmd 'update-license-data' 'Update SPDX license data in the Homebrew repository'
18041811
__fish_brew_complete_arg 'update-license-data' -l debug -d 'Display any debugging information'
18051812
__fish_brew_complete_arg 'update-license-data' -l help -d 'Show this message'

completions/internal_commands_list.txt

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ unpin
114114
untap
115115
up
116116
update
117+
update-if-needed
117118
update-license-data
118119
update-maintainers
119120
update-python-resources

completions/zsh/_brew

+10
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ __brew_internal_commands() {
232232
'unpin:Unpin formula, allowing them to be upgraded by `brew upgrade` formula'
233233
'untap:Remove a tapped formula repository'
234234
'update:Fetch the newest version of Homebrew and all formulae from GitHub using `git`(1) and perform any necessary migrations'
235+
'update-if-needed:Runs `brew update --auto-update` only if needed'
235236
'update-license-data:Update SPDX license data in the Homebrew repository'
236237
'update-maintainers:Update the list of maintainers in the `Homebrew/brew` README'
237238
'update-python-resources:Update versions for PyPI resource blocks in formula'
@@ -2223,6 +2224,15 @@ _brew_update() {
22232224
'--verbose[Print the directories checked and `git` operations performed]'
22242225
}
22252226

2227+
# brew update-if-needed
2228+
_brew_update_if_needed() {
2229+
_arguments \
2230+
'--debug[Display any debugging information]' \
2231+
'--help[Show this message]' \
2232+
'--quiet[Make some output more quiet]' \
2233+
'--verbose[Make some output more verbose]'
2234+
}
2235+
22262236
# brew update-license-data
22272237
_brew_update_license_data() {
22282238
_arguments \

docs/Manpage.md

+6
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,12 @@ and perform any necessary migrations.
16101610

16111611
: Display a trace of all shell commands as they are executed.
16121612

1613+
### `update-if-needed`
1614+
1615+
Runs `brew update --auto-update` only if needed. This is a good replacement for
1616+
`brew update` in scripts where you want the no-op case to be both possible and
1617+
really fast.
1618+
16131619
### `update-reset` \[*`repository`* ...\]
16141620

16151621
Fetch and reset Homebrew and all tap repositories (or any specified

manpages/brew.1

+2
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,8 @@ Print the directories checked and \fBgit\fP operations performed\.
10051005
.TP
10061006
\fB\-d\fP, \fB\-\-debug\fP
10071007
Display a trace of all shell commands as they are executed\.
1008+
.SS "\fBupdate\-if\-needed\fP"
1009+
Runs \fBbrew update \-\-auto\-update\fP only if needed\. This is a good replacement for \fBbrew update\fP in scripts where you want the no\-op case to be both possible and really fast\.
10081010
.SS "\fBupdate\-reset\fP \fR[\fIrepository\fP \.\.\.]"
10091011
Fetch and reset Homebrew and all tap repositories (or any specified \fIrepository\fP) using \fBgit\fP(1) to their latest \fBorigin/HEAD\fP\&\.
10101012
.P

0 commit comments

Comments
 (0)