bin/brew: fix corner cases in prefix computation

If `bin/brew` happens to be symlinked to `/brew`, `/bin/brew`, or some
similar location or (worse yet) Homebrew is installed to `/`, then
computation of the prefix and/or repository path could break down and
result in an invalid or empty path.

Closes Homebrew/homebrew-core#2430.
This commit is contained in:
Martin Afanasjew 2016-06-29 16:20:47 +02:00
parent da06e813c2
commit 3c5f007bfa

View File

@ -5,10 +5,13 @@ quiet_cd() {
cd "$@" >/dev/null cd "$@" >/dev/null
} }
BREW_FILE_DIRECTORY="$(quiet_cd "${0%/*}" && pwd -P)" BREW_FILE_DIRECTORY="$(quiet_cd "${0%/*}/" && pwd -P)"
HOMEBREW_BREW_FILE="$BREW_FILE_DIRECTORY/${0##*/}" HOMEBREW_BREW_FILE="${BREW_FILE_DIRECTORY%/}/${0##*/}"
HOMEBREW_PREFIX="${HOMEBREW_BREW_FILE%/*/*}" HOMEBREW_PREFIX="${HOMEBREW_BREW_FILE%/*/*}"
[[ -n "$HOMEBREW_PREFIX" && "$HOMEBREW_PREFIX" != "$HOMEBREW_BREW_FILE" ]] \
|| HOMEBREW_PREFIX="/"
HOMEBREW_REPOSITORY="$HOMEBREW_PREFIX" HOMEBREW_REPOSITORY="$HOMEBREW_PREFIX"
if [[ -L "$HOMEBREW_BREW_FILE" ]] if [[ -L "$HOMEBREW_BREW_FILE" ]]