Skip to content

Commit 4a7cd16

Browse files
committed
Auto-update when running brew install/upgrade.
Also, slightly tweak the behavior of `brew update` in this case so that it doesn't print annoying output and still allows the `brew edit` flow for people with `HOMEBREW_DEVELOPER` set.
1 parent 0382134 commit 4a7cd16

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

Library/Homebrew/cmd/update-report.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
require "descriptions"
55

66
module Homebrew
7+
def update_preinstall_header
8+
@header_already_printed ||= begin
9+
ohai "Auto-updated Homebrew!" if ARGV.include?("--preinstall")
10+
true
11+
end
12+
end
13+
714
def update_report
815
install_core_tap_if_necessary
916

@@ -17,6 +24,7 @@ def update_report
1724
end
1825

1926
if initial_revision != current_revision
27+
update_preinstall_header
2028
puts "Updated Homebrew from #{shorten_revision(initial_revision)} to #{shorten_revision(current_revision)}."
2129
updated = true
2230
end
@@ -37,13 +45,14 @@ def update_report
3745
end
3846

3947
unless updated_taps.empty?
48+
update_preinstall_header
4049
puts "Updated #{updated_taps.size} tap#{plural(updated_taps.size)} " \
4150
"(#{updated_taps.join(", ")})."
4251
updated = true
4352
end
4453

4554
if !updated
46-
puts "Already up-to-date."
55+
puts "Already up-to-date." unless ARGV.include?("--preinstall")
4756
elsif hub.empty?
4857
puts "No changes to formulae."
4958
else

Library/Homebrew/cmd/update.sh

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,14 @@ pull() {
213213

214214
trap '' SIGINT
215215

216-
pop_stash_message
216+
if [[ -n "$HOMEBREW_DEVELOPER" ]] &&
217+
[[ "$INITIAL_BRANCH" != "$UPSTREAM_BRANCH" && -n "$INITIAL_BRANCH" ]]
218+
then
219+
git checkout "${QUIET_ARGS[@]}" "$INITIAL_BRANCH"
220+
pop_stash
221+
else
222+
pop_stash_message
223+
fi
217224

218225
trap - SIGINT
219226
}
@@ -231,6 +238,7 @@ homebrew-update() {
231238
--debug) HOMEBREW_DEBUG=1;;
232239
--rebase) HOMEBREW_REBASE=1 ;;
233240
--simulate-from-current-branch) HOMEBREW_SIMULATE_FROM_CURRENT_BRANCH=1 ;;
241+
--preinstall) HOMEBREW_UPDATE_PREINSTALL=1 ;;
234242
--*) ;;
235243
-*)
236244
[[ "$option" = *v* ]] && HOMEBREW_VERBOSE=1;
@@ -316,11 +324,21 @@ EOS
316324
--header "If-None-Match: \"$UPSTREAM_BRANCH_LOCAL_SHA\"" \
317325
"https://api.github.com/repos/$UPSTREAM_REPOSITORY/commits/$UPSTREAM_BRANCH")"
318326
[[ "$UPSTREAM_SHA_HTTP_CODE" = "304" ]] && exit
327+
elif [[ -n "$HOMEBREW_UPDATE_PREINSTALL" ]]
328+
then
329+
# Don't try to do a `git fetch` that may take longer than expected.
330+
exit
319331
fi
320332

321-
git fetch --force "${QUIET_ARGS[@]}" origin \
322-
"refs/heads/$UPSTREAM_BRANCH:refs/remotes/origin/$UPSTREAM_BRANCH" || \
323-
odie "Fetching $DIR failed!"
333+
if [[ -n "$HOMEBREW_UPDATE_PREINSTALL" ]]
334+
then
335+
git fetch --force "${QUIET_ARGS[@]}" origin \
336+
"refs/heads/$UPSTREAM_BRANCH:refs/remotes/origin/$UPSTREAM_BRANCH" 2>/dev/null
337+
else
338+
git fetch --force "${QUIET_ARGS[@]}" origin \
339+
"refs/heads/$UPSTREAM_BRANCH:refs/remotes/origin/$UPSTREAM_BRANCH" || \
340+
odie "Fetching $DIR failed!"
341+
fi
324342
) &
325343
done
326344

Library/brew.sh

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,19 @@ then
230230
& disown
231231
fi
232232

233+
update-preinstall() {
234+
[[ -n "$HOMEBREW_AUTO_UPDATE" ]] || return
235+
[[ -z "$HOMEBREW_NO_AUTO_UPDATE" ]] || return
236+
237+
if [[ "$HOMEBREW_COMMAND" = "install" || "$HOMEBREW_COMMAND" = "upgrade" ]]
238+
then
239+
# Hide shellcheck complaint:
240+
# shellcheck source=/dev/null
241+
source "$HOMEBREW_LIBRARY/Homebrew/cmd/update.sh"
242+
homebrew-update --preinstall
243+
fi
244+
}
245+
233246
if [[ -n "$HOMEBREW_BASH_COMMAND" ]]
234247
then
235248
# source rather than executing directly to ensure the entire file is read into
@@ -240,9 +253,9 @@ then
240253
# Hide shellcheck complaint:
241254
# shellcheck source=/dev/null
242255
source "$HOMEBREW_BASH_COMMAND"
243-
{ "homebrew-$HOMEBREW_COMMAND" "$@"; exit $?; }
256+
{ update-preinstall; "homebrew-$HOMEBREW_COMMAND" "$@"; exit $?; }
244257
else
245258
# Unshift command back into argument list (unless argument list was empty).
246259
[[ "$HOMEBREW_ARG_COUNT" -gt 0 ]] && set -- "$HOMEBREW_COMMAND" "$@"
247-
exec "$HOMEBREW_RUBY_PATH" -W0 "$HOMEBREW_LIBRARY/brew.rb" "$@"
260+
{ update-preinstall; exec "$HOMEBREW_RUBY_PATH" -W0 "$HOMEBREW_LIBRARY/brew.rb" "$@"; }
248261
fi

0 commit comments

Comments
 (0)