From 05c1c5895ebe80f9737557fa320d2c30de68a001 Mon Sep 17 00:00:00 2001 From: XuehaiPan Date: Mon, 30 Aug 2021 22:33:32 +0800 Subject: [PATCH] Apply suggestions from code review --- Library/Homebrew/brew.sh | 36 ++++++++++++++------------------ Library/Homebrew/cmd/shellenv.sh | 16 +++----------- docs/Manpage.md | 8 +------ manpages/brew.1 | 8 +------ 4 files changed, 21 insertions(+), 47 deletions(-) diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh index b53a7750f2..63dac99fb1 100644 --- a/Library/Homebrew/brew.sh +++ b/Library/Homebrew/brew.sh @@ -51,8 +51,23 @@ HOMEBREW_CACHE="${HOMEBREW_CACHE:-${HOMEBREW_DEFAULT_CACHE}}" HOMEBREW_LOGS="${HOMEBREW_LOGS:-${HOMEBREW_DEFAULT_LOGS}}" HOMEBREW_TEMP="${HOMEBREW_TEMP:-${HOMEBREW_DEFAULT_TEMP}}" +# Don't need to handle a default case. +# HOMEBREW_LIBRARY set by bin/brew +# shellcheck disable=SC2249,SC2154 +case "$*" in + --cellar) echo "${HOMEBREW_CELLAR}"; exit 0 ;; + --repository|--repo) echo "${HOMEBREW_REPOSITORY}"; exit 0 ;; + --caskroom) echo "${HOMEBREW_PREFIX}/Caskroom"; exit 0 ;; + --cache) echo "${HOMEBREW_CACHE}"; exit 0 ;; + shellenv) source "${HOMEBREW_LIBRARY}/Homebrew/cmd/shellenv.sh"; homebrew-shellenv; exit 0 ;; + formulae) source "${HOMEBREW_LIBRARY}/Homebrew/cmd/formulae.sh"; homebrew-formulae; exit 0 ;; + casks) source "${HOMEBREW_LIBRARY}/Homebrew/cmd/casks.sh"; homebrew-casks; exit 0 ;; + # falls back to cmd/prefix.rb on a non-zero return + --prefix*) source "${HOMEBREW_LIBRARY}/Homebrew/prefix.sh"; homebrew-prefix "$@" && exit 0 ;; +esac + ##### -##### Next, define helper functions for prompting. +##### Next, define all helper functions. ##### # These variables are set from the user environment. @@ -110,25 +125,6 @@ odie() { exit 1 } -# Don't need to handle a default case. -# HOMEBREW_LIBRARY set by bin/brew -# shellcheck disable=SC2249,SC2154 -case "$*" in - --cellar) echo "${HOMEBREW_CELLAR}"; exit 0 ;; - --repository|--repo) echo "${HOMEBREW_REPOSITORY}"; exit 0 ;; - --caskroom) echo "${HOMEBREW_PREFIX}/Caskroom"; exit 0 ;; - --cache) echo "${HOMEBREW_CACHE}"; exit 0 ;; - shellenv) source "${HOMEBREW_LIBRARY}/Homebrew/cmd/shellenv.sh"; homebrew-shellenv; exit 0 ;; - formulae) source "${HOMEBREW_LIBRARY}/Homebrew/cmd/formulae.sh"; homebrew-formulae; exit 0 ;; - casks) source "${HOMEBREW_LIBRARY}/Homebrew/cmd/casks.sh"; homebrew-casks; exit 0 ;; - # falls back to cmd/prefix.rb on a non-zero return - --prefix*) source "${HOMEBREW_LIBRARY}/Homebrew/prefix.sh"; homebrew-prefix "$@" && exit 0 ;; -esac - -##### -##### Next, define the reset of helper functions. -##### - safe_cd() { cd "$@" >/dev/null || odie "Failed to cd to $*!" } diff --git a/Library/Homebrew/cmd/shellenv.sh b/Library/Homebrew/cmd/shellenv.sh index 0f5c882eb5..4ed9ecb6e8 100644 --- a/Library/Homebrew/cmd/shellenv.sh +++ b/Library/Homebrew/cmd/shellenv.sh @@ -4,24 +4,14 @@ #: #: The variables `HOMEBREW_PREFIX`, `HOMEBREW_CELLAR` and `HOMEBREW_REPOSITORY` are also exported to avoid querying them multiple times. #: The variable `HOMEBREW_SHELLENV_PREFIX` will be exported to avoid adding duplicate entries to the environment variables. -#: -#: Consider adding evaluation of this command's output to your dotfiles (e.g. `~/.profile`, `~/.bash_profile`, or `~/.zprofile`) -#: with: `eval "$(/path/to/brew shellenv)"`. -#: -#: The variable `HOMEBREW_SHELLENV_PREFIX` may be inherited from the parent shell session. If you explicitly -#: modified the path variables (e.g. `PATH`) in your dotfiles which breaks the subshell varibale inheritecement, -#: please use `eval "$(HOMEBREW_SHELLENV_PREFIX='' /path/to/brew shellenv)"` to always get all the export statements. +#: Consider adding evaluation of this command's output to your dotfiles (e.g. `~/.profile`, `~/.bash_profile`, or `~/.zprofile`) with: `eval $(brew shellenv)` # HOMEBREW_CELLAR and HOMEBREW_PREFIX are set by extend/ENV/super.rb # HOMEBREW_REPOSITORY is set by bin/brew # shellcheck disable=SC2154 homebrew-shellenv() { - if [[ "${HOMEBREW_SHELLENV_PREFIX}" == "${HOMEBREW_PREFIX}" ]]; then - [[ "$(PATH="${HOMEBREW_PATH}" command -v brew)" == "${HOMEBREW_PREFIX}/bin/brew" ]] && return - opoo "You have set HOMEBREW_SHELLENV_PREFIX=\"${HOMEBREW_SHELLENV_PREFIX}\"," \ - "but the brew's executable is not present in your PATH." \ - "Please run \`brew help shellenv\` for more information." - fi + [[ "${HOMEBREW_SHELLENV_PREFIX}" == "${HOMEBREW_PREFIX}" && + "$(PATH="${HOMEBREW_PATH}" command -v brew)" == "${HOMEBREW_PREFIX}/bin/brew" ]] && return case "$(/bin/ps -p "${PPID}" -c -o comm=)" in fish | -fish) diff --git a/docs/Manpage.md b/docs/Manpage.md index 63a494ebb8..12b9f20c17 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -570,13 +570,7 @@ Print export statements. When run in a shell, this installation of Homebrew will The variables `HOMEBREW_PREFIX`, `HOMEBREW_CELLAR` and `HOMEBREW_REPOSITORY` are also exported to avoid querying them multiple times. The variable `HOMEBREW_SHELLENV_PREFIX` will be exported to avoid adding duplicate entries to the environment variables. - -Consider adding evaluation of this command's output to your dotfiles (e.g. `~/.profile`, `~/.bash_profile`, or `~/.zprofile`) -with: `eval "$(/path/to/brew shellenv)"`. - -The variable `HOMEBREW_SHELLENV_PREFIX` may be inherited from the parent shell session. If you explicitly -modified the path variables (e.g. `PATH`) in your dotfiles which breaks the subshell varibale inheritecement, -please use `eval "$(HOMEBREW_SHELLENV_PREFIX='' /path/to/brew shellenv)"` to always get all the export statements. +Consider adding evaluation of this command's output to your dotfiles (e.g. `~/.profile`, `~/.bash_profile`, or `~/.zprofile`) with: `eval $(brew shellenv)` ### `tap` [*`options`*] [*`user`*`/`*`repo`*] [*`URL`*] diff --git a/manpages/brew.1 b/manpages/brew.1 index 9a16049cfc..3586e59ecf 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -792,13 +792,7 @@ Search for \fItext\fR in the given database\. Print export statements\. When run in a shell, this installation of Homebrew will be added to your \fBPATH\fR, \fBMANPATH\fR, and \fBINFOPATH\fR\. . .P -The variables \fBHOMEBREW_PREFIX\fR, \fBHOMEBREW_CELLAR\fR and \fBHOMEBREW_REPOSITORY\fR are also exported to avoid querying them multiple times\. The variable \fBHOMEBREW_SHELLENV_PREFIX\fR will be exported to avoid adding duplicate entries to the environment variables\. -. -.P -Consider adding evaluation of this command\'s output to your dotfiles (e\.g\. \fB~/\.profile\fR, \fB~/\.bash_profile\fR, or \fB~/\.zprofile\fR) with: \fBeval "$(/path/to/brew shellenv)"\fR\. -. -.P -The variable \fBHOMEBREW_SHELLENV_PREFIX\fR may be inherited from the parent shell session\. If you explicitly modified the path variables (e\.g\. \fBPATH\fR) in your dotfiles which breaks the subshell varibale inheritecement, please use \fBeval "$(HOMEBREW_SHELLENV_PREFIX=\'\' /path/to/brew shellenv)"\fR to always get all the export statements\. +The variables \fBHOMEBREW_PREFIX\fR, \fBHOMEBREW_CELLAR\fR and \fBHOMEBREW_REPOSITORY\fR are also exported to avoid querying them multiple times\. The variable \fBHOMEBREW_SHELLENV_PREFIX\fR will be exported to avoid adding duplicate entries to the environment variables\. Consider adding evaluation of this command\'s output to your dotfiles (e\.g\. \fB~/\.profile\fR, \fB~/\.bash_profile\fR, or \fB~/\.zprofile\fR) with: \fBeval $(brew shellenv)\fR . .SS "\fBtap\fR [\fIoptions\fR] [\fIuser\fR\fB/\fR\fIrepo\fR] [\fIURL\fR]" Tap a formula repository\.