- Use a safe fallback in case git rev-parse fails, e.g. if this is not
considered a safe git directory. For hopefully obvious reasons: be
super careful and strict with the inputs we'll accept here.
- Better handle more permission errors when reading or writing to/from
the git describe cache. We don't care about these errors because they
are likely a result of a multiuser configuration where Homebrew is run
as several different users and this is just a (small) performance
improvement.
`GIT_DESCRIBE_CACHE` is a performance optimisation that doesn't clean
up consistently when run with different Homebrew users. Instead of
doing user detection: let's just hide these errors that we don't care
about.
Reported in:
https://github.com/orgs/Homebrew/discussions/5479
`brew -v` was previously equivalent to `brew --version`, but it
currently returns the output of `brew help`. (This also occurs with
`brew -x`, where x is any single character.)
This is because the `-?` pattern matches `-` followed by any single
character. We need to quote it to capture the intended meaning.
This provides a decent speedup:
```
$ hyperfine 'git checkout master; brew help' 'git checkout help_bash; brew help'
Benchmark 1: git checkout master; brew help
Time (mean ± σ): 506.4 ms ± 50.9 ms [User: 223.7 ms, System: 99.9 ms]
Range (min … max): 454.6 ms … 634.1 ms 10 runs
Benchmark 2: git checkout help_bash; brew help
Time (mean ± σ): 109.5 ms ± 57.1 ms [User: 1
```
and compares favourably to `pip3 help`:
```
$ hyperfine 'brew help' 'pip3 help'
Benchmark 1: brew help
Time (mean ± σ): 72.9 ms ± 15.9 ms [User: 4.9 ms, System: 6.3 ms]
Range (min … max): 53.6 ms … 126.6 ms 31 runs
Benchmark 2: pip3 help
Time (mean ± σ): 171.5 ms ± 6.1 ms [User: 131.6 ms, System: 24.7 ms]
Range (min … max): 164.2 ms … 189.3 ms 15 runs
Summary
brew help ran
2.35 ± 0.52 times faster than pip3 help
```
Similarly to have we have with other commands, use Bash to speed it up.
Before:
```
$ hyperfine "brew list"
Benchmark 1: brew list
Time (mean ± σ): 559.9 ms ± 122.8 ms [User: 176.2 ms, System: 126.2 ms]
Range (min … max): 503.2 ms … 907.3 ms 10 runs
```
After:
```
$ hyperfine "brew list"
Benchmark 1: brew list
Time (mean ± σ): 223.7 ms ± 31.9 ms [User: 35.0 ms, System: 53.4 ms]
Range (min … max): 198.1 ms … 302.4 ms 10
```
Petty after comparison because someone on Homebrew/discussions compared
them:
```
$ hyperfine "brew list" "pip3 list"
Benchmark 1: brew list
Time (mean ± σ): 213.1 ms ± 22.8 ms [User: 34.2 ms, System: 49.2 ms]
Range (min … max): 191.2 ms … 272.3 ms 13 runs
Benchmark 2: pip3 list
Time (mean ± σ): 271.7 ms ± 4.6 ms [User: 191.9 ms, System: 40.0 ms]
Range (min … max): 264.7 ms … 281.4 ms 10 runs
Summary
brew list ran
1.28 ± 0.14 times faster than pip3 list
```
---
While we're here, also add the `HOMEBREW_CASKROOM` environment variable
to make things a bit cleaner and use `--caskroom` in documentation.
Co-authored-by: Carlo Cabrera <30379873+carlocab@users.noreply.github.com>
This is a (relatively, for this Bash script) expensive operation that
can be trivially cached so: let's do so.
Speedup on my machine:
```
$ hyperfine "brew --version" "HOMEBREW_VERSION_CACHE=1 brew --version"
Benchmark 1: brew --version
Time (mean ± σ): 173.4 ms ± 5.1 ms [User: 28.7 ms, System: 67.0 ms]
Range (min … max): 167.3 ms … 182.9 ms 15 runs
Benchmark 2: HOMEBREW_VERSION_CACHE=1 brew --version
Time (mean ± σ): 145.9 ms ± 4.0 ms [User: 22.4 ms, System: 33.9 ms]
Range (min … max): 140.0 ms … 154.5 ms 17 runs
Summary
HOMEBREW_VERSION_CACHE=1 brew --version ran
1.19 ± 0.05 times faster than brew --version
```
Co-authored-by: Bo Anderson <mail@boanderson.me>
This avoids can avoid UID/EUID related issues with Ruby scripts passed
over `stdin` clashing with Ruby security features.
It's also just a bit nicer to have Ruby scripts in files instead.
While we're here:
- refactor some shared logic into a new `setup-gem-home-bundle-gemfile`
function in `ruby.sh`
- do some general cleanup of `lock.sh`
- prioritise `flock` over `python` in `lock.sh`
- in `brew.sh` split the `case` into those cases that take a single or
no arguments and those that take multiple arguments or handle
multiple commands. This considerably speeds up the
`brew shellenv bash` case that wasn't being handled here before.
- add `setup-ruby` to the list of commands that can be called quickly
by `brew.sh` without additional setup. This speeds up the
`brew setup-ruby` no-op case by ~10x.
- add a parameter to `setup-ruby` to avoid running Bundler if the
command doesn't need it. This makes many more cases for
`brew setup-ruby` to be no-op cases.
- Remove the (now) unused `HOMEBREW_RUBY3` check in `setup-ruby`.
- Improve argument handling in `command_path.sh` to allow it to be
used as a function in `setup-ruby.sh`.
- Add a new RuboCop to check usage of `install_bundler_gems!` is only
inside `dev-cmd` (or a few other acceptable places).
- Use new `processed_source.file_path` API in `formula_cop.rb`
- Add a (large) speedup by moving some logic to Bash for the typical
case of a normal or dev-cmd, Bash or Ruby command.
- Make `brew command` a non-developer command, I don't think it makes
sense to consider it something needed for developing Homebrew.
- Update the manpage/tests/RBI accordingly.
Co-authored-by: Carlo Cabrera <30379873+carlocab@users.noreply.github.com>
When doing an `install`, `outdated` or `upgrade` and referring to a
formula or cask in a specific tap: you probably want to get the latest
version of that formula/cask more often than the default 1 or 24 hours.
Let's check for arguments for these commands that look appropriate and
auto-update every 5 minutes in that case.
- Output a message every time auto-update is run rather than a 3 second
timer. This makes it more obvious that Homebrew isn't just sitting
doing nothing for 2.9 seconds.
- Output a message when running `brew update` so Homebrew doesn't just
sit there silently doing nothing.
- Update all taps when `brew update` is run, not just those hosted on
GitHub. This makes it more obvious that people don't need to explictly
run `brew update` "just in case".
- As a result of this, remove `brew tap --force-auto-update` as it's no
longer necessary.
This variable was getting filtered out before so it wasn't possible
for us to use it as the default for HOMEBREW_DEFAULT_CACHE
and HOMEBREW_DEFAULT_LOGS.
- split jobs into build/test/deploy
- test package on both macOS Intel and Apple Silicon
- cleanup some argument handling
- use `HOMEBREW_MACOS_OLDEST_SUPPORTED` naming to be consistent with
`brew.sh`
- note in `brew.sh` that `Distribution.xml` also needs updated (and do
so)
- various other little bits of style cleanup
We have no commands with Sorbet disabled and have had Sorbet enabled
for developers for a decent amount of time. As a result, we can enable
it for everyone who has run a developer command.
This also allows a bunch of `raise TypeError`s to be removed in favour
of relying on Sorbet here instead.
My understanding is that now https://github.com/Homebrew/brew/pull/15778
has been merged this should now work fine on both older macOS versions
and non-default prefixes so let's try this again.
- warn if running `brew postinstall` explicitly and there's no
`post_install` defined in the formula
- add a `post_install` alias for `brew postinstall` to make life
easier for those jumping between `postinstall` and `post_install` in
e.g. Homebrew development
- refactor `post_install` formula path logic into a new method for
improved readability
- handle the JSON API `post_install` formula path case