Skip to content

feat: fall back to main worktree config in linked worktrees#714

Open
KiaraGrouwstra wants to merge 1 commit into
cachix:masterfrom
KiaraGrouwstra:feat-hook-worktree-fallback
Open

feat: fall back to main worktree config in linked worktrees#714
KiaraGrouwstra wants to merge 1 commit into
cachix:masterfrom
KiaraGrouwstra:feat-hook-worktree-fallback

Conversation

@KiaraGrouwstra
Copy link
Copy Markdown

Allows git worktrees to fall back to git hooks from their linked worktree.

Closes #710.

Assisted-by: Claude:claude-sonnet-4-6

When using `git worktree add`, the linked worktree inherits
`core.hooksPath` (pointing at the common git dir's hooks) but not the
untracked `.pre-commit-config.yaml` symlink — that symlink is only
created on shell entry via `installationScript`. Until then every
`git commit` in the linked worktree fails with
"No .pre-commit-config.yaml file was found".

After `pre-commit install` writes the hook scripts, patch each one to
splice a prelude after the shebang and rewrite the `--config=` literal
in the `ARGS=` line to read from a variable resolved by that prelude.

At hook runtime the prelude resolves the config path:

1. The current toplevel's `cfg.configPath` if present.
2. Otherwise, only in a linked worktree (detected by comparing
   `git rev-parse --git-dir` against `--git-common-dir`), the main
   worktree's config.
3. Otherwise `exit 0`.

Behavior in the main worktree is unchanged: a missing config there
still produces pre-commit's normal error. A marker comment makes the
patch idempotent across repeated shell entries.

Fixes cachix#710

Assisted-by: Claude:claude-sonnet-4-6
@KiaraGrouwstra KiaraGrouwstra force-pushed the feat-hook-worktree-fallback branch from 6a701a7 to ba08995 Compare May 18, 2026 09:12
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.

allow pre-commit checks conditional on their presence

1 participant