hooks: skip rebuild in linked git worktrees#1806
Open
Claude-Madera wants to merge 1 commit into
Open
Conversation
The post-commit and post-checkout hooks rebuild graphify-out/ on every commit / branch switch. In a linked worktree (`git worktree add`) that is wasteful -- the canonical graph belongs to the primary checkout -- and deploy/CI flows that `git clean` a worktree race the detached rebuild against the cleanup, producing 'failed to remove graphify-out/: Directory not empty'. Guard both hooks: a linked worktree has git-dir != git-common-dir. Compare ABSOLUTE paths (git rev-parse --absolute-git-dir vs a resolved --git-common-dir) so the GIT_DIR env var git exports to hooks -- sometimes absolute -- cannot false-positive and skip the primary checkout. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Problem
The
post-commitandpost-checkouthooks rebuildgraphify-out/on every commit and branch switch. Inside a linked worktree (git worktree add) this misbehaves:The canonical
graphify-out/belongs to the primary checkout; rebuilding it from every worktree is wasteful.Deploy/CI flows that create a worktree, switch/checkout, then
git cleanit, race the detached background rebuild against the cleanup — producing:This surfaced in a deploy "cockpit" that pins release worktrees of three repos: every pin fired a pointless full rebuild and intermittently failed the
git clean.Fix
Guard both hooks: a linked worktree has
git-dir != git-common-dir, so short-circuit before launching the rebuild.Key correctness detail: the compare uses absolute paths —
Git exports
GIT_DIRto hooks and it is sometimes absolute; comparing a raw$GIT_DIRagainst a relative--git-common-dir(.git) would false-positive and wrongly skip the primary checkout. Resolving both to absolute paths avoids that. Factored into a shared_WORKTREE_GUARDconstant, mirroring the existing_PYTHON_DETECTpattern.Tests
test_hooks_skip_linked_worktrees— both generated scripts contain the guard exactly once and use the absolute-path compare.test_worktree_guard_skips_only_linked_worktree— end-to-end against a realgit worktree: the guard runs through on the primary checkout and exits early in a linked worktree.Both pass; no previously-passing tests affected.
🤖 Generated with Claude Code