fix #865: use git rev-parse --git-path hooks so hook install works in linked worktrees

In a linked worktree .git is a file not a directory, so root/.git/hooks
fails with NotADirectoryError. Fall back to git rev-parse --path-format=absolute
--git-path hooks which resolves the correct hooks dir from any worktree.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Safi
2026-05-14 23:37:05 +01:00
parent 972fdc1b87
commit 4e7648b4e2
+15 -1
View File
@@ -197,8 +197,22 @@ def _hooks_dir(root: Path) -> Path:
f"{root / '.git' / 'config'}: {exc}",
file=sys.stderr,
)
# In a linked worktree .git is a file not a directory, so constructing
# root/.git/hooks directly fails. Ask git for the real hooks path instead.
import subprocess as _sp
try:
res = _sp.run(
["git", "-C", str(root), "rev-parse", "--path-format=absolute", "--git-path", "hooks"],
capture_output=True, text=True,
)
if res.returncode == 0:
d = Path(res.stdout.strip())
d.mkdir(parents=True, exist_ok=True)
return d
except (OSError, FileNotFoundError):
pass
d = root / ".git" / "hooks"
d.mkdir(exist_ok=True)
d.mkdir(parents=True, exist_ok=True)
return d