411 Commits

Author SHA1 Message Date
Mike McQuaid
d661cffc1f
Switch to using main as the default branch
Homebrew/brew has moved to a `main` default branch so we can more move
references from `master` to `main`.
2025-06-24 11:14:57 +01:00
Sam Ford
764d2b2dcc
Add HOMEBREW_MACOS_NEWEST_SUPPORTED env var
We have a `HOMEBREW_MACOS_NEWEST_UNSUPPORTED` environment variable
and this is used in `MacOSVersion` to determine prerelease versions
but we don't have a way of easily determining the newest supported
macOS version.

`bump-cask-pr` contains logic that assumes the first key/value in
`MacOSVersion::SYMBOLS` is the newest macOS version but it recently
became clear that this is a prerelease version between WWDC and the
subsequent macOS release. Similarly, `dev-cmd/generate-cask-api.rb`
tries to compute the newest stable macOS version as
`HOMEBREW_MACOS_NEWEST_UNSUPPORTED.to_i - 1` and this will fail
if/when we update that variable to `"26"`, as the macOS version
before 26 is 15, not 25.

This adds a `HOMEBREW_MACOS_NEWEST_SUPPORTED` environment variable,
so we have a straightforward way of quickly identifying the newest
supported macOS version without having to make potentially unreliable
assumptions or do computations to identify the latest non-prerelease
`MacOSVersion` value. This also updates the two aforementioned areas
to use this environment variable to produce the newest stable macOS
version symbol in a more reliable way.
2025-06-14 18:45:25 -04:00
Mike McQuaid
f9471f9591
Add brew mcp-server: a MCP server for Homebrew.
Add a new `brew mcp-server` command for a Model Context Protocol (MCP)
server for Homebrew. This integrates with AI/LLM tools like Claude,
Claude Code and Cursor.

It currently supports the calls needed/used by the MCP Inspector and
Cursor (where I've tested it).

It provides as `tools` the subcommands output by `brew help` but should
be fairly straightforward to add more in future.

It is implemented in a slightly strange way (a standalone Ruby command
called from a shell command) as MCP servers need a faster startup time
than a normal Homebrew Ruby command allows and fail if they don't get
it.

There are a few Ruby libraries available but, given how relatively
simplistic the implementation is, it didn't feel worthwhile to use and
vendor them.
2025-06-03 15:22:33 +01:00
Mike McQuaid
7c5affa824
brew.sh: move sudo reset timestamp.
Let's move this from `bin/brew` to make things like e.g. `brew --prefix`
or `brew shellenv` not reset the sudo timestamp.

This is still in a place that ensures that e.g. no untrusted formula or
tap code has been run yet so should have no security implications but
provide mild usability improvements.
2025-06-02 15:09:27 +01:00
Mike McQuaid
5bfa27aa79
brew.sh: don't run sudo brew services with Bootsnap.
This is necessary to avoid permission errors.
2025-05-06 11:16:46 +01:00
Bo Anderson
0195d9a6d6
Merge pull request #19853 from Homebrew/ci-fix
Fix CI errors on 20.04
2025-04-29 15:54:31 +00:00
Bo Anderson
9932c387d8
Fix CI errors on 20.04 2025-04-29 15:45:56 +01:00
Mike McQuaid
eb175f01a0
brew.sh: only use /var/tmp on Linux if writable. 2025-04-25 17:56:58 +01:00
Aman Verma
9c79c5b632
Change default temp on Linux to /var/tmp.
Previously, the default temporary directory was /tmp on Linux and
/private/tmp on macOS. On many Linux distros, including at least Fedora,
/tmp is stored in RAM. This diverges from the behavior on macOS and has
led to bugs, most notably the inability to install large bottles on
memory-limited machines.
2025-04-25 17:56:34 +01:00
Mike McQuaid
dbbffbaca5
Improve HOMEBREW_TEMP handling
- if HOMEBREW_TEMP is not writable, use the default temp directory
- when running `brew bundle exec`, strip various temporary directories
  from the environment if they are not writable
2025-04-14 19:10:58 +01:00
Bo Anderson
e0f7085299
brew.sh: fix update-if-needed failing after update 2025-04-02 02:44:12 +01:00
Mike McQuaid
5ec5063dc7
Add brew update-if-needed
This uses the logic in `brew.sh` for deciding whether or not to run
`brew update --auto-update` and makes it a dedicated command that can
be used instead of `brew update` in scripts to be really fast in the
no-op case.

`brew update` will always do at least some updating which is a nicer
default but is much slower.
2025-03-27 13:16:00 +00:00
Mike McQuaid
7a57af242e
Cleanup some missing references to Homebrew Bundle.
Let's just call it `brew bundle` from now on as it's a normal command.

Also, fix a reference to "Homebrew Services" while we're here.
2025-03-19 09:51:39 +00:00
Gibson Fahnestock
1ff3b62d7b
fix: HOMEBREW_NO_SORBET_RUNTIME should work even if developer
Today if you are in developer mode then `HOMEBREW_NO_SORBET_RUNTIME`
doesn't take effect. But when doing development it's often useful to be
able to disable type-checking, so have that env var take effect even in
developer mode.
2025-02-27 11:59:08 +08:00
Mike McQuaid
17bfef29f2
brew.sh: improve HOMEBREW_FORCE_BREW_WRAPPER error message
Rather than just explaining what is happening, let's explain a bit more
why this is happening and how to fix it.
2025-02-07 13:23:52 +00:00
Bo Anderson
fed5321969
Rename aarch64_linux tag to arm64_linux 2025-02-01 13:03:58 +01:00
Mike McQuaid
bb74d26a93
Fix HOMEBREW_*BREW_WRAPPER behaviour
- handle reading from variables that may not be defined yet
- avoid unnecessary handling of defined variables
- fix incorrect `env_config.rbi` file entry
- add `HOMEBREW_NO_FORCE_BREW_WRAPPER` to disable
  `HOMEBREW_FORCE_BREW_WRAPPER` functionality if needed
2025-01-08 14:10:55 +00:00
Mike McQuaid
e9b4979f40
Add support for Homebrew wrappers
Allow the ability for a system administrator to use
`HOMEBREW_BREW_WRAPPER` and `HOMEBREW_FORCE_BREW_WRAPPER` variables to
enforce the usage of a particular `brew` command for non-trivial (e.g.
`brew --prefix` is considered trivial, it doesn't need to write to the
prefix) Homebrew commands.

This also introduces a `HOMEBREW_ORIGINAL_BREW_FILE` variable for some
internal usage; `HOMEBREW_BREW_FILE` was being used internally for
both "how should we shell out to Homebrew" and "what should we use
to check permissions on Homebrew". `HOMEBREW_ORIGINAL_BREW_FILE` is
now used just for the latter case.

Inspired by conversation in
https://github.com/Homebrew/homebrew-bundle/pull/1551 which suggested
this was worth fixing in wider than just `brew bundle`.
2025-01-07 17:40:18 +00:00
Dawid Dziurla
c16d2d291a
brew: allow root in podman containers 2024-10-15 10:39:10 +02:00
Carlo Cabrera
d14accee39
brew.sh: fix some stderr redirections to /dev/null
The order matters here (in a way that I can't explain, unfortunately).

Example:

    bash-3.2$ read -r FOO <nonexistent 2>/dev/null
    bash: nonexistent: No such file or directory

    bash-3.2$ read -r FOO 2>/dev/null <nonexistent
    # no output

Fixes Homebrew/discussions#5653.
2024-10-04 16:36:35 +08:00
Mike McQuaid
1d6683df21
brew.sh: add latest_sdk_version to new macOS version TODO. 2024-09-26 13:03:39 +01:00
Ruoyu Zhong
c17154d2ac
brew.sh: improve brew tap handling 2024-09-25 17:16:18 +08:00
Ruoyu Zhong
ea011ae528
brew.sh: remove outdated comment 2024-09-25 16:50:39 +08:00
Ruoyu Zhong
e9b158cd5c
Further simplify homebrew-tap calling
Co-authored-by: Carlo Cabrera <30379873+carlocab@users.noreply.github.com>
2024-09-25 12:00:30 +08:00
Ruoyu Zhong
369fd3dcaf
Speed up brew tap for no arguments
This provides a >3x speedup for `brew tap` with no arguments (i.e., when
listing taps). It also makes the completion significantly faster.

    $ hyperfine --warmup=3 --setup 'git checkout {branch}' --parameter-list branch master,brew-tap-speedup 'brew tap'
    Benchmark 1: brew tap (branch = master)
      Time (mean ± σ):      1.405 s ±  0.080 s    [User: 0.561 s, System: 0.238 s]
      Range (min … max):    1.332 s …  1.549 s    10 runs

    Benchmark 2: brew tap (branch = brew-tap-speedup)
      Time (mean ± σ):     404.1 ms ± 124.8 ms    [User: 107.9 ms, System: 200.7 ms]
      Range (min … max):   308.8 ms … 693.7 ms    10 runs

    Summary
      brew tap (branch = brew-tap-speedup) ran
        3.48 ± 1.09 times faster than brew tap (branch = master)
2024-09-25 01:50:43 +08:00
Francois-Xavier Coudert
4ab19e09da
Make Sequoia supported 2024-09-11 09:00:37 +01:00
Bo Anderson
d9a339b231
list.sh: improve arg parsing, support brew ls 2024-09-03 15:13:15 +01:00
Carlo Cabrera
43d10940b9
Revert "Revert "cmd/shellenv: improve for newer macOS using /usr/libexec/path_helper"" 2024-09-02 17:32:05 +08:00
Carlo Cabrera
121624e0c2
Revert "cmd/shellenv: improve for newer macOS using /usr/libexec/path_helper" 2024-09-02 17:02:08 +08:00
Mike McQuaid
decc109089
Merge pull request #18188 from Homebrew/better-shellenv 2024-08-30 11:56:07 +01:00
xycabcd
147cfa46e4
Deleted the duplicated "PWD check" 2024-08-29 14:45:27 -04:00
Carlo Cabrera
6dc90cbf83
cmd/shellenv: improve for newer macOS using /usr/libexec/path_helper
On macOS 14 and newer, `/usr/libexec/path_helper` supports setting a
`PATH_HELPER_ROOT` environment variable.

With this set, `path_helper` checks `$PATH_HELPER_ROOT/etc/paths` and
`$PATH_HELPER_ROOT/etc/paths.d` in the same way it checks `/etc/paths`
and `/etc/paths.d`.

We can use this to simplify management of the user's `PATH` variable
when they do `brew shellenv`. In particular, if their system supports
it, we delegate setting the `PATH` environment variable to `path_helper`
instead of our own code. We also write a default `etc/paths` file if one
is not already present.

This is nicer because it simplifies management of the user's `PATH`
variable. For example, if a user wants a keg-only formula to be in their
`PATH`, they can simply add the necessary path to `/etc/paths` or
`/etc/paths.d` without having to do something like `brew link --force`
or adding to `PATH` themselves.

When `/usr/libexec/path_helper` is not available, we just fall back to
the existing code.
2024-08-29 17:00:43 +08:00
Carlo Cabrera
01287a742a
Merge pull request #18153 from Homebrew/version-cache-handling 2024-08-28 19:37:15 +08:00
Carlo Cabrera
b63ecff52c
brew.sh: use git diff instead of git diff-index 2024-08-28 19:21:50 +08:00
Carlo Cabrera
a597a1eecb
Reverse order of if conditional
Checking for readability is a cheaper operation that `git diff-index`.
2024-08-28 18:58:27 +08:00
Carlo Cabrera
58ea96583f
brew.sh: use read instead of cat
This will be slightly more efficient since it can be done in-process.
2024-08-25 04:06:30 +08:00
Carlo Cabrera
13115fee7d
brew.sh: fix handling of dirty git repo
We cache the output of `git describe`, but we don't check whether the
cached value is valid.

This fixes that.
2024-08-25 02:57:35 +08:00
Carlo Cabrera
97403cd309
brew.sh: remove some shellcheck disables
We don't need these, so let's remove them.
2024-08-24 08:21:43 +08:00
Mike McQuaid
4bf0a7f20f
brew.sh: bump minimum version.
We know we're definitely on at least 4.3.0 now, so let's update this.
2024-08-19 16:02:57 +01:00
Mike McQuaid
378f7f8f72
brew.sh: further improve git describe cache.
- 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.
2024-08-19 16:02:57 +01:00
Mike McQuaid
c6ddb9cedb
brew.sh: hide ignorable permission errors.
`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
2024-08-19 09:17:21 +01:00
Maxim Belkin
f69461abd3 Fix auto-update for WSL
`find` on WSL doesn't support time units, so instead of

  find ... -mtime -300s

one has to use

  find ... -mtime -300
2024-08-05 16:32:42 +00:00
Carlo Cabrera
0f71de045e
brew.sh: fix brew -v
`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.
2024-07-30 17:09:11 +08:00
Bo Anderson
bd1eb68c36
brew.sh: fix nested developer command detection 2024-07-15 02:46:22 +01:00
Ruoyu Zhong
319aaefad1
brew.sh: speed up brew --help, brew -h, etc.
We can handle `brew --help` the same way as we do for `brew help`.
2024-07-14 13:41:51 -04:00
Mike McQuaid
4c012a41c6
Port brew help (without arguments) to Bash
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
```
2024-07-14 11:54:05 -04:00
Bo Anderson
fa2731d0c7
cmd/setup-ruby: fix Portable Ruby install failing 2024-06-14 02:26:16 +01:00
Bo Anderson
2f0f5fbb61
bin/brew: fix tracking of original paths with brew-in-brew calls 2024-06-04 03:47:31 +01:00
Mike McQuaid
7073258a6a
Speed up brew list
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>
2024-05-17 12:38:16 +09:00
Mike McQuaid
41bf51e211
brew.sh: avoid shelling out when possible. 2024-05-14 15:46:33 +09:00