Make pre-commit hook installation and execution work in git worktrees#1643
Merged
Merged
Conversation
In a git worktree, `git rev-parse --show-toplevel` returns the worktree checkout path, and `.git` there is a file containing `gitdir: ...`, not a directory. So both `make pre-commit` (which writes into `.git/hooks`) and the generated hook line (which executes `$(show-toplevel)/.git/hooks/pre-commit.fmt`) fail with "Not a directory" when invoked from inside a worktree. Use `git rev-parse --git-path hooks/...` instead — it resolves to the shared hooks directory in both the main checkout and any worktree. Fixes apple#1641.
3 tasks
Code Coverage
|
jglogan
approved these changes
Jun 4, 2026
jglogan
left a comment
Contributor
There was a problem hiding this comment.
@harshitsinghbhandari applied as a patch to my clone+worktree and works great.
Contributor
Author
|
Ready to merge @jglogan |
Contributor
|
@harshitsinghbhandari Thank you for the contribution! I'll respond on your other within like 5 min. |
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.
Summary
make pre-commitand the hook it installs both assume the current directory is the main repo checkout. In a git worktree both assumptions fail:git rev-parse --show-toplevelreturns the worktree's checkout path (e.g./tmp/wt-test)..gitinside a worktree is a file containinggitdir: ..., not a directory.cp scripts/pre-commit.fmt .git/hooksand the generated wrapper's$(show-toplevel)/.git/hooks/pre-commit.fmtboth reference a path that does not exist, and any commit attempt from a worktree fails withbash: ...: Not a directory.This PR replaces the path computation with
git rev-parse --git-path hooks/..., which resolves to the shared hooks directory in both the main checkout and any worktree.Fixes #1641.
Test plan
make pre-commitsucceeds in the main checkout; generated hook content shows$(git rev-parse --git-path hooks/pre-commit.fmt).git worktree add /tmp/wt && cd /tmp/wt && git commit --allow-empty -m "test"now runs the hook successfully (gets through swift-format and into hawkeye, instead of failing in the wrapper).