Skip to content

Commit

Permalink
Merge pull request #17317 from Homebrew/list_speedup
Browse files Browse the repository at this point in the history
Speed up `brew list`
  • Loading branch information
MikeMcQuaid authored May 17, 2024
2 parents 23939d6 + 7073258 commit a8bf511
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
10 changes: 9 additions & 1 deletion Library/Homebrew/brew.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ else
HOMEBREW_CELLAR="${HOMEBREW_PREFIX}/Cellar"
fi

HOMEBREW_CASKROOM="${HOMEBREW_PREFIX}/Caskroom"

HOMEBREW_CACHE="${HOMEBREW_CACHE:-${HOMEBREW_DEFAULT_CACHE}}"
HOMEBREW_LOGS="${HOMEBREW_LOGS:-${HOMEBREW_DEFAULT_LOGS}}"
HOMEBREW_TEMP="${HOMEBREW_TEMP:-${HOMEBREW_DEFAULT_TEMP}}"
Expand Down Expand Up @@ -138,7 +140,7 @@ case "$@" in
exit 0
;;
--caskroom)
echo "${HOMEBREW_PREFIX}/Caskroom"
echo "${HOMEBREW_CASKROOM}"
exit 0
;;
--cache)
Expand All @@ -155,6 +157,11 @@ case "$@" in
source "${HOMEBREW_LIBRARY}/Homebrew/command_path.sh"
homebrew-command-path "$@" && exit 0
;;
# falls back to cmd/list.rb on a non-zero return
list*)
source "${HOMEBREW_LIBRARY}/Homebrew/list.sh"
homebrew-list "$@" && exit 0
;;
esac

#####
Expand Down Expand Up @@ -783,6 +790,7 @@ export HOMEBREW_LOGS
export HOMEBREW_DEFAULT_TEMP
export HOMEBREW_TEMP
export HOMEBREW_CELLAR
export HOMEBREW_CASKROOM
export HOMEBREW_SYSTEM
export HOMEBREW_SYSTEM_CA_CERTIFICATES_TOO_OLD
export HOMEBREW_CURL
Expand Down
77 changes: 77 additions & 0 deletions Library/Homebrew/list.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# does the quickest output of brew list possible for no named arguments.
# HOMEBREW_CELLAR, HOMEBREW_PREFIX are set by brew.sh
# shellcheck disable=SC2154
homebrew-list() {
case "$1" in
# check we actually have list and not e.g. listsomething
list) ;;
list*) return 1 ;;
*) ;;
esac

local ls_args=()
local formula=""
local cask=""

for arg in "$@"
do
case "${arg}" in
# check for flags passed to ls
-1 | -l | -r | -t) ls_args+=("${arg}") ;;
--formula | --formulae) formula=1 ;;
--cask | --casks) cask=1 ;;
# reject all other flags
-* | *) return 1 ;;
esac
done

local tty
if [[ -t 1 ]]
then
tty=1
fi

local error_string="LS_ERRORED"
if [[ -z "${cask}" && -d "${HOMEBREW_CELLAR}" ]]
then
if [[ -n "${tty}" && -z "${formula}" ]]
then
ohai "Formulae"
fi

local formula_output
formula_output="$(ls "${ls_args[@]}" "${HOMEBREW_CELLAR}" || echo "${error_string}")"
if [[ "${formula_output}" == "${error_string}" ]]
then
exit 1
elif [[ -n "${formula_output}" ]]
then
echo "${formula_output}"
fi

if [[ -n "${tty}" && -z "${formula}" ]]
then
echo
fi
fi

if [[ -z "${formula}" && -d "${HOMEBREW_CASKROOM}" ]]
then
if [[ -n "${tty}" && -z "${cask}" ]]
then
ohai "Casks"
fi

local cask_output
cask_output="$(ls "${ls_args[@]}" "${HOMEBREW_CASKROOM}" || echo "${error_string}")"
if [[ "${cask_output}" == "${error_string}" ]]
then
exit 1
elif [[ -n "${cask_output}" ]]
then
echo "${cask_output}"
fi

return 0
fi
}
3 changes: 3 additions & 0 deletions Library/Homebrew/startup/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
# Where we store built products
HOMEBREW_CELLAR = Pathname(ENV.fetch("HOMEBREW_CELLAR")).freeze

# Where we store Casks
HOMEBREW_CASKROOM = Pathname(ENV.fetch("HOMEBREW_CASKROOM")).freeze

# Where downloads (bottles, source tarballs, etc.) are cached
HOMEBREW_CACHE = Pathname(ENV.fetch("HOMEBREW_CACHE")).freeze

Expand Down
2 changes: 1 addition & 1 deletion docs/Common-Issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ Help us by [submitting a fix](https://github.com/Homebrew/homebrew-cask/blob/HEA

In this case, it’s likely your user account has no admin rights and therefore lacks permissions for writing to `/Applications`, which is the default install location. You can use [`--appdir`](https://github.com/Homebrew/homebrew-cask/blob/HEAD/USAGE.md#options) to choose where to install your applications.

If `--appdir` doesn’t fix the issue or you do have write permissions to `/Applications`, verify you’re the owner of the `Caskroom` directory by running `ls -dl "$(brew --prefix)/Caskroom"` and checking the third field. If you are not the owner, fix it with `sudo chown -R "$(whoami)" "$(brew --prefix)/Caskroom"`. If you are, the problem may lie in the app bundle itself.
If `--appdir` doesn’t fix the issue or you do have write permissions to `/Applications`, verify you’re the owner of the `Caskroom` directory by running `ls -dl "$(brew --caskroom)"` and checking the third field. If you are not the owner, fix it with `sudo chown -R "$(whoami)" "$(brew --caskroom)"`. If you are, the problem may lie in the app bundle itself.

Some app bundles don’t have certain permissions that are necessary for us to move them to the appropriate location. You may check such permissions with `ls -ls '/path/to/application.app'`. If you see something like `dr-xr-xr-x` at the start of the output, that may be the cause. To fix it, we need to change the app bundle’s permission to allow us to move it, and then set it back to what it was (in case the developer set those permissions deliberately). See [litecoin.rb](https://github.com/Homebrew/homebrew-cask/blob/aa461148bbb5119af26b82cccf5003e2b4e50d95/Casks/l/litecoin.rb#L17-L27) for an example of such a cask.

Expand Down

0 comments on commit a8bf511

Please sign in to comment.