Skip to content

Commit

Permalink
Speedup brew --version.
Browse files Browse the repository at this point in the history
Port `brew --version` to Bash to speed it up.

While we're here:
- remove (now) unused `Tap` Git methods
- use `--quiet` instead of `-q` to be more verbose

Benchmarks:
```
$ hyperfine --min-runs=3 --warmup=2 "git checkout origin/master; brew --version" "git checkout speedup_brew_version; brew --version"
Benchmark #1: git checkout origin/master; brew --version
  Time (mean ± σ):      2.083 s ±  0.004 s    [User: 396.8 ms, System: 597.2 ms]
  Range (min … max):    2.080 s …  2.088 s    3 runs

Benchmark #2: git checkout speedup_brew_version; brew --version
  Time (mean ± σ):     847.9 ms ±  35.2 ms    [User: 100.0 ms, System: 247.9 ms]
  Range (min … max):   824.1 ms … 888.3 ms    3 runs

Summary
  'git checkout speedup_brew_version; brew --version' ran
    2.46 ± 0.10 times faster than 'git checkout origin/master; brew --version'
```
  • Loading branch information
MikeMcQuaid committed Mar 4, 2021
1 parent 3e69c2b commit dc1fdc9
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 130 deletions.
13 changes: 11 additions & 2 deletions Library/Homebrew/brew.sh
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ update-preinstall() {
# last $HOMEBREW_AUTO_UPDATE_SECS.
if [[ "$HOMEBREW_COMMAND" = "cask" ]]
then
tap_fetch_head="$HOMEBREW_LIBRARY/Taps/homebrew/homebrew-cask/.git/FETCH_HEAD"
tap_fetch_head="$HOMEBREW_CASK_REPOSITORY/.git/FETCH_HEAD"
else
tap_fetch_head="$HOMEBREW_LIBRARY/Taps/homebrew/homebrew-core/.git/FETCH_HEAD"
tap_fetch_head="$HOMEBREW_CORE_REPOSITORY/.git/FETCH_HEAD"
fi
if [[ -f "$tap_fetch_head" &&
-n "$(find "$tap_fetch_head" -type f -mtime -"${HOMEBREW_AUTO_UPDATE_SECS}"s 2>/dev/null)" ]]
Expand Down Expand Up @@ -314,6 +314,15 @@ then
HOMEBREW_USER_AGENT_VERSION="2.X.Y"
fi

HOMEBREW_CASK_REPOSITORY="$HOMEBREW_LIBRARY/Taps/homebrew/homebrew-cask"
HOMEBREW_CORE_REPOSITORY="$HOMEBREW_LIBRARY/Taps/homebrew/homebrew-core"

# Don't need shellcheck to follow these `source`.
# shellcheck disable=SC1090
case "$*" in
--version|-v) source "$HOMEBREW_LIBRARY/Homebrew/cmd/--version.sh"; homebrew-version; exit 0 ;;
esac

if [[ -n "$HOMEBREW_MACOS" ]]
then
HOMEBREW_PRODUCT="Homebrew"
Expand Down
30 changes: 0 additions & 30 deletions Library/Homebrew/cmd/--version.rb

This file was deleted.

31 changes: 31 additions & 0 deletions Library/Homebrew/cmd/--version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#: * `--version`, `-v`
#:
#: Print the version numbers of Homebrew, Homebrew/homebrew-core and Homebrew/homebrew-cask (if tapped) to standard output.

version_string() {
local repo="$1"
if ! [ -d "$repo" ]; then
echo "N/A"
return
fi

local pretty_revision
pretty_revision="$(git -C "$repo" rev-parse --short --verify --quiet HEAD)"
if [ -z "$pretty_revision" ]; then
echo "(no Git repository)"
return
fi

local git_last_commit_date
git_last_commit_date=$(git -C "$repo" show -s --format='%cd' --date=short HEAD)
echo "(git revision ${pretty_revision}; last commit ${git_last_commit_date})"
}

homebrew-version() {
echo "Homebrew $HOMEBREW_VERSION"
echo "Homebrew/homebrew-core $(version_string "$HOMEBREW_CORE_REPOSITORY")"

if [ -d "$HOMEBREW_CASK_REPOSITORY" ]; then
echo "Homebrew/homebrew-cask $(version_string "$HOMEBREW_CASK_REPOSITORY")"
fi
}
4 changes: 2 additions & 2 deletions Library/Homebrew/extend/git_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ def git_origin=(origin)
# Gets the full commit hash of the HEAD commit.
sig { params(safe: T::Boolean).returns(T.nilable(String)) }
def git_head(safe: false)
popen_git("rev-parse", "--verify", "-q", "HEAD", safe: safe)
popen_git("rev-parse", "--verify", "--quiet", "HEAD", safe: safe)
end

# Gets a short commit hash of the HEAD commit.
sig { params(length: T.nilable(Integer), safe: T::Boolean).returns(T.nilable(String)) }
def git_short_head(length: nil, safe: false)
short_arg = length.present? ? "--short=#{length}" : "--short"
popen_git("rev-parse", short_arg, "--verify", "-q", "HEAD", safe: safe)
popen_git("rev-parse", short_arg, "--verify", "--quiet", "HEAD", safe: safe)
end

# Gets the relative date of the last commit, e.g. "1 hour ago"
Expand Down
24 changes: 0 additions & 24 deletions Library/Homebrew/tap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,27 +170,13 @@ def git_head
path.git_head
end

# git HEAD in short format for this {Tap}.
def git_short_head
raise TapUnavailableError, name unless installed?

path.git_short_head(length: 4)
end

# Time since last git commit for this {Tap}.
def git_last_commit
raise TapUnavailableError, name unless installed?

path.git_last_commit
end

# Last git commit date for this {Tap}.
def git_last_commit_date
raise TapUnavailableError, name unless installed?

path.git_last_commit_date
end

# The issues URL of this {Tap}.
# e.g. `https://github.com/user/homebrew-repo/issues`
sig { returns(T.nilable(String)) }
Expand All @@ -204,16 +190,6 @@ def to_s
name
end

sig { returns(String) }
def version_string
return "N/A" unless installed?

pretty_revision = git_short_head
return "(no Git repository)" unless pretty_revision

"(git revision #{pretty_revision}; last commit #{git_last_commit_date})"
end

# True if this {Tap} is an official Homebrew tap.
def official?
user == "Homebrew"
Expand Down
6 changes: 2 additions & 4 deletions Library/Homebrew/test/cmd/--version_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
require "cmd/shared_examples/args_parse"

describe "brew --version" do
it_behaves_like "parseable arguments"

it "prints the Homebrew version", :integration_test do
expect { brew "--version" }
it "prints the Homebrew's version", :integration_test do
expect { brew_sh "--version" }
.to output(/^Homebrew #{Regexp.escape(HOMEBREW_VERSION)}\n/o).to_stdout
.and not_to_output.to_stderr
.and be_a_success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ def brew(*args)
end
end

def brew_sh(*args)
Bundler.with_clean_env do
stdout, stderr, status = Open3.capture3("#{ENV["HOMEBREW_PREFIX"]}/bin/brew", *args)
$stdout.print stdout
$stderr.print stderr
status
end
end

def setup_test_formula(name, content = nil, bottle_block: nil)
case name
when /^testball/
Expand Down
2 changes: 0 additions & 2 deletions Library/Homebrew/test/tap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ def setup_completion(link:)
setup_git_repo

expect(homebrew_foo_tap.git_head).to eq("0453e16c8e3fac73104da50927a86221ca0740c2")
expect(homebrew_foo_tap.git_short_head).to eq("0453")
expect(homebrew_foo_tap.git_last_commit).to match(/\A\d+ .+ ago\Z/)
expect(homebrew_foo_tap.git_last_commit_date).to eq("2017-01-22")
end

specify "#private?" do
Expand Down
32 changes: 0 additions & 32 deletions completions/bash/brew
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,6 @@ _brew___repository() {
__brew_complete_tapped
}

_brew___version() {
local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in
-*)
__brewcomp "
--debug
--help
--quiet
--verbose
"
return
;;
esac
}

_brew__s() {
local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in
Expand Down Expand Up @@ -287,21 +272,6 @@ _brew__s() {
esac
}

_brew__v() {
local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in
-*)
__brewcomp "
--debug
--help
--quiet
--verbose
"
return
;;
esac
}

_brew_abv() {
local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in
Expand Down Expand Up @@ -2316,9 +2286,7 @@ _brew() {
--prefix) _brew___prefix ;;
--repo) _brew___repo ;;
--repository) _brew___repository ;;
--version) _brew___version ;;
-S) _brew__s ;;
-v) _brew__v ;;
abv) _brew_abv ;;
analytics) _brew_analytics ;;
audit) _brew_audit ;;
Expand Down
14 changes: 0 additions & 14 deletions completions/fish/brew.fish
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,6 @@ __fish_brew_complete_arg '--repository' -l verbose -d 'Make some output more ver
__fish_brew_complete_arg '--repository' -a '(__fish_brew_suggest_taps_installed)'


__fish_brew_complete_cmd '--version' 'Print the version numbers of Homebrew, Homebrew/homebrew-core and Homebrew/homebrew-cask (if tapped) to standard output'
__fish_brew_complete_arg '--version' -l debug -d 'Display any debugging information'
__fish_brew_complete_arg '--version' -l help -d 'Show this message'
__fish_brew_complete_arg '--version' -l quiet -d 'Make some output more quiet'
__fish_brew_complete_arg '--version' -l verbose -d 'Make some output more verbose'


__fish_brew_complete_cmd '-S' 'Perform a substring search of cask tokens and formula names for text'
__fish_brew_complete_arg '-S' -l cask -d 'Without text, list all locally available casks (including tapped ones, no online search is performed). With text, search online and locally for casks'
__fish_brew_complete_arg '-S' -l closed -d 'Search for only closed GitHub pull requests'
Expand All @@ -297,13 +290,6 @@ __fish_brew_complete_arg '-S' -l ubuntu -d 'Search for text in the given package
__fish_brew_complete_arg '-S' -l verbose -d 'Make some output more verbose'


__fish_brew_complete_cmd '-v' 'Print the version numbers of Homebrew, Homebrew/homebrew-core and Homebrew/homebrew-cask (if tapped) to standard output'
__fish_brew_complete_arg '-v' -l debug -d 'Display any debugging information'
__fish_brew_complete_arg '-v' -l help -d 'Show this message'
__fish_brew_complete_arg '-v' -l quiet -d 'Make some output more quiet'
__fish_brew_complete_arg '-v' -l verbose -d 'Make some output more verbose'


__fish_brew_complete_cmd 'abv' 'Display brief statistics for your Homebrew installation'
__fish_brew_complete_arg 'abv' -l all -d 'Print JSON of all available formulae'
__fish_brew_complete_arg 'abv' -l analytics -d 'List global Homebrew analytics data or, if specified, installation and build error data for formula (provided neither `HOMEBREW_NO_ANALYTICS` nor `HOMEBREW_NO_GITHUB_API` are set)'
Expand Down
18 changes: 0 additions & 18 deletions completions/zsh/_brew
Original file line number Diff line number Diff line change
Expand Up @@ -337,15 +337,6 @@ _brew___repository() {
'::tap:__brew_any_tap'
}

# brew --version
_brew___version() {
_arguments \
'--debug[Display any debugging information]' \
'--help[Show this message]' \
'--quiet[Make some output more quiet]' \
'--verbose[Make some output more verbose]'
}

# brew -S
_brew__s() {
_arguments \
Expand All @@ -367,15 +358,6 @@ _brew__s() {
'--verbose[Make some output more verbose]'
}

# brew -v
_brew__v() {
_arguments \
'--debug[Display any debugging information]' \
'--help[Show this message]' \
'--quiet[Make some output more quiet]' \
'--verbose[Make some output more verbose]'
}

# brew abv
_brew_abv() {
_arguments \
Expand Down
3 changes: 1 addition & 2 deletions docs/Manpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -749,8 +749,7 @@ If *`user`*`/`*`repo`* are provided, display where tap *`user`*`/`*`repo`*'s dir

### `--version`, `-v`

Print the version numbers of Homebrew, Homebrew/homebrew-core and Homebrew/homebrew-cask
(if tapped) to standard output.
Print the version numbers of Homebrew, Homebrew/homebrew-core and Homebrew/homebrew-cask (if tapped) to standard output.

## DEVELOPER COMMANDS

Expand Down

0 comments on commit dc1fdc9

Please sign in to comment.