completion: Detect Homebrew prefix internally, not idiomatically

For performance reasons, we prefer detecting Homebrew prefix internally
instead of the more idiomatic form `brew --prefix`.

We attempt looking up $HOMEBREW_PREFIX or $HOMEBREW_REPOSITORY first (in
case `brew shellenv` has been sourced-in earlier). Else, we look it up
by resolving absolute path of $HOMEBREW_REPOSITORY. $HOMEBREW_PREFIX is
same as $HOMEBREW_REPOSITORY except when Homebrew is installed in
'/usr/local' ($HOMEBREW_REPOSITORY == '/usr/local/Homebrew'). This is
usually the case for Intel Macs.

This should work for most standard (and officially documented) Homebrew
installations.

For implementation details in Homebrew,
see: 2a850e02d8/bin/brew (L62-L70)

Co-authored-by: mattmc3 <mattmc3@gmail.com>
main
Indrajit Raychaudhuri 3 years ago committed by Indrajit Raychaudhuri
parent 2c66331316
commit ac1c39d2e1

@ -14,12 +14,16 @@ fi
# Add zsh-completions to $fpath.
fpath=(${0:h}/external/src $fpath)
# Add completion for keg-only brewed curl when available.
if (( $+commands[brew] )) \
&& [[ -d "${curl_prefix::="$(brew --prefix 2> /dev/null)"/opt/curl}" ]]; then
fpath=($curl_prefix/share/zsh/site-functions $fpath)
# Add completion for keg-only brewed curl on macOS when available.
if (( $+commands[brew] )); then
brew_prefix=${HOMEBREW_PREFIX:-${HOMEBREW_REPOSITORY:-$commands[brew]:A:h:h}}
# $HOMEBREW_PREFIX defaults to $HOMEBREW_REPOSITORY but is explicitly set to
# /usr/local when $HOMEBREW_REPOSITORY is /usr/local/Homebrew.
# https://github.com/Homebrew/brew/blob/2a850e02d8f2dedcad7164c2f4b95d340a7200bb/bin/brew#L66-L69
[[ $brew_prefix == '/usr/local/Homebrew' ]] && brew_prefix=$brew_prefix:h
fpath=($brew_prefix/opt/curl/share/zsh/site-functions(/N) $fpath)
unset brew_prefix
fi
unset curl_prefix
#
# Options

Loading…
Cancel
Save