utils/git: only mutate describe-cache when .git is user-owned - #23517
Merged
Conversation
Signed-off-by: Patrick Linnane <patrick@linnane.io>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens set-homebrew-version-from-git’s .git/describe-cache handling so brew only deletes/recreates the cache when the repository’s .git directory is owned by the invoking user, avoiding destructive writes inside Git directories owned by other users or reached via symlinked .git.
Changes:
- Add an ownership gate (
-O) around all.git/describe-cachemutations (cleanup + rewrite) inset-homebrew-version-from-git. - Refactor the function to use a shared
git_directoryvariable for.gitpath accesses. - Add bash-level RSpec coverage to assert cache writes occur only when
.gitis user-owned.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| Library/Homebrew/utils/git.sh | Gate .git/describe-cache deletion/creation behind a “.git is user-owned” check to prevent mutating non-owned Git dirs. |
| Library/Homebrew/test/bash_spec.rb | Add tests validating cache writes when .git is owned and no cache mutation when .git is not owned (via symlink to /). |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
carlocab
approved these changes
Aug 14, 2026
Co-authored-by: Carlo Cabrera <github@carlo.cab>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
set-homebrew-version-from-gitdeletes and recreates.git/describe-cachewhenever it has to fall back togit describe. It did so unconditionally, sobrewwouldrm -rfandmkdir -pinside a Git directory the invoking user does not own, e.g. an installation owned by another user or a.gitthat has been replaced by a symlink pointing elsewhere.Only mutate the cache when
.gitis owned by the current user. Reads are unaffected, so a repository owned by someone else still uses an already populated cache and otherwise just falls back togit describeeach run, which is all the cache was ever saving.brewcommands to reproduce the bug?brew lgtm(style, typechecking and tests) locally?Claude Code (Opus 5) drafted the implementation and tests; I reviewed the diff, verified the new ownership test fails without the fix and passes with it, and ran
brew lgtm+ targeted specs.