Skip to content

fix(git): resolve linked worktree hooks via commondir - #419

Open
Pybsama wants to merge 1 commit into
AVIDS2:mainfrom
Pybsama:fix/git-hooks-worktree-commondir
Open

Pybsama wants to merge 1 commit into
AVIDS2:mainfrom
Pybsama:fix/git-hooks-worktree-commondir

Conversation

@Pybsama

@Pybsama Pybsama commented Dec 2, 2026

Copy link
Copy Markdown

Summary

  • Fix linked git worktree hook resolution in src/git/hooks-path.ts so resolveHooksDir() and ensureHooksDir() follow <gitdir>/commondir to the shared common .git directory rather than placing hooks inside <main>/.git/worktrees/<name>/hooks/.
  • Export resolveCommonGitDir() (matching git rev-parse --git-common-dir), while keeping resolveGitDir() returning the per-worktree gitdir (matching git rev-parse --git-dir) and preserving fallback behavior for normal repositories and .git pointer files without commondir (e.g., submodules).
  • Guard resolveGitDir() against non-directory gitdir: targets and statSync errors.

Why

In a linked worktree created by git worktree add, <worktree>/.git points to <main>/.git/worktrees/<name>, whose commondir file points (typically via ../..) to <main>/.git. Per gitrepository-layout(5) and git rev-parse --git-path hooks, Git reads shared hooks from <commondir>/hooks/, so hooks written under <main>/.git/worktrees/<name>/hooks/post-commit were silently ignored on commit.

Verification

  • npm run build:workspaces
  • npm run lint (tsc --noEmit) — passed
  • npx vitest run tests/git/ tests/project/ tests/cli/config-command.test.ts — 9 test files / 87 tests passed, including relative & absolute commondir cases, missing commondir fallback, non-directory gitdir handling, and a real git worktree add test cross-checked against git rev-parse --git-dir and git rev-parse --git-common-dir.

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.

1 participant