Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bin/brew: avoid eval and grep #15821

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 21 additions & 26 deletions bin/brew
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,30 @@ unset BREW_FILE_DIRECTORY
HOMEBREW_LIBRARY="${HOMEBREW_REPOSITORY}/Library"

# Load Homebrew's variable configuration files from disk.
export_homebrew_env_file() {
local env_file

env_file="${1}"
[[ -r "${env_file}" ]] || return 0
while read -r line
do
# only load HOMEBREW_* lines
[[ "${line}" = "HOMEBREW_"* ]] || continue
export "${line?}"
done <"${env_file}"
}

# First, load the system-wide configuration.
if [[ -f "/etc/homebrew/brew.env" ]]
unset SYSTEM_ENV_TAKES_PRIORITY
if [[ -n "${HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY-}" ]]
then
unset SYSTEM_ENV_TAKES_PRIORITY
# only load HOMEBREW_*=* lines
SYSTEM_HOMEBREW_ENV="$(grep -E '^HOMEBREW_[A-Z_]+=.*$' "/etc/homebrew/brew.env")"
if [[ -n "${HOMEBREW_SYSTEM_ENV_TAKES_PRIORITY-}" ]]
then
SYSTEM_ENV_TAKES_PRIORITY="1"
else
eval "${SYSTEM_HOMEBREW_ENV}"
fi
SYSTEM_ENV_TAKES_PRIORITY="1"
else
export_homebrew_env_file "/etc/homebrew/brew.env"
fi

# Next, load the prefix configuration
if [[ -f "${HOMEBREW_PREFIX}/etc/homebrew/brew.env" ]]
then
# only load HOMEBREW_*=* lines
PREFIX_HOMEBREW_ENV="$(grep -E '^HOMEBREW_[A-Z_]+=.*$' "${HOMEBREW_PREFIX}/etc/homebrew/brew.env")"
eval "${PREFIX_HOMEBREW_ENV}"
unset PREFIX_HOMEBREW_ENV
fi
export_homebrew_env_file "${HOMEBREW_PREFIX}/etc/homebrew/brew.env"

# Finally, load the user configuration
if [[ -n "${XDG_CONFIG_HOME-}" ]]
Expand Down Expand Up @@ -129,20 +131,13 @@ else
exit 1
fi

if [[ -f "${HOMEBREW_USER_CONFIG_HOME}/brew.env" ]]
then
# only load HOMEBREW_*=* lines
USER_HOMEBREW_ENV="$(grep -E '^HOMEBREW_[A-Z_]+=.*$' "${HOMEBREW_USER_CONFIG_HOME}/brew.env")"
eval "${USER_HOMEBREW_ENV}"
unset USER_HOMEBREW_ENV
fi
export_homebrew_env_file "${HOMEBREW_USER_CONFIG_HOME}/brew.env"

# If the system configuration takes priority, load it last.
if [[ -n "${SYSTEM_ENV_TAKES_PRIORITY-}" ]]
then
eval "${SYSTEM_HOMEBREW_ENV}"
export_homebrew_env_file "/etc/homebrew/brew.env"
fi
unset SYSTEM_HOMEBREW_ENV

# Copy and export all HOMEBREW_* variables previously mentioned in
# manpage or used elsewhere by Homebrew.
Expand Down