Skip to content

Make pre-commit hook installation and execution work in git worktrees#1643

Merged
jglogan merged 2 commits into
apple:mainfrom
harshitsinghbhandari:fix/pre-commit-worktree
Jun 4, 2026
Merged

Make pre-commit hook installation and execution work in git worktrees#1643
jglogan merged 2 commits into
apple:mainfrom
harshitsinghbhandari:fix/pre-commit-worktree

Conversation

@harshitsinghbhandari

Copy link
Copy Markdown
Contributor

Summary

make pre-commit and 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-toplevel returns the worktree's checkout path (e.g. /tmp/wt-test).
  • .git inside a worktree is a file containing gitdir: ..., not a directory.
  • So cp scripts/pre-commit.fmt .git/hooks and the generated wrapper's $(show-toplevel)/.git/hooks/pre-commit.fmt both reference a path that does not exist, and any commit attempt from a worktree fails with bash: ...: 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-commit succeeds 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).
  • Existing behavior in the main checkout unchanged.

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.
@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

Code Coverage

Tier Line Coverage
Unit 34.62%
Integration 19.77%
Combined 53.77%

@jglogan jglogan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harshitsinghbhandari applied as a patch to my clone+worktree and works great.

@harshitsinghbhandari

Copy link
Copy Markdown
Contributor Author

Ready to merge @jglogan

@jglogan

jglogan commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

@harshitsinghbhandari Thank you for the contribution!

I'll respond on your other within like 5 min.

@jglogan jglogan merged commit 8bf87e8 into apple:main Jun 4, 2026
3 checks passed
@harshitsinghbhandari harshitsinghbhandari deleted the fix/pre-commit-worktree branch June 5, 2026 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: pre-commit hook generated by 'make pre-commit' fails inside git worktrees

2 participants