Skip to content

fix(hooks): resolve the real hooks dir so worktree installs don't crash init - #1808

Merged
RaghavChamadiya merged 1 commit into
repowise-dev:mainfrom
sloemo01:fix/init-worktree-hook-crash
Aug 21, 2026
Merged

RaghavChamadiya merged 1 commit into
repowise-dev:mainfrom
sloemo01:fix/init-worktree-hook-crash

Conversation

@sloemo01

Copy link
Copy Markdown
Contributor

What

Fixes #1609. In a git worktree, .git is a file (gitdir: <path>), not a directory. The hook installer built root / ".git" / "hooks" from _git_root's heuristic, so in a worktree mkdir raised NotADirectoryError and the crash propagated up to take down the whole repowise init.

Root cause

_git_root walks up for any .git entry — but in a worktree that entry is a file, not a directory. install()/status()/uninstall() then did root / ".git" / "hooks", which fails on a worktree because .git is not a directory.

Fix

  • Add _hooks_dir(repo_path) — resolves the real hooks dir via git rev-parse --git-path hooks, which returns the correct (shared) hooks dir for both normal repos and worktrees. Falls back to the .git/hooks heuristic when git is unavailable or not a repo.
  • install, status, and uninstall now use _hooks_dir.

Tests

  • New TestInstallInWorktree in test_hooks_legacy_migration.py: creates a real worktree (where .git is asserted to be a file), verifies install no longer crashes and status reports correctly.
  • Existing hook tests: 13 passed, no regression.
  • Ruff clean.

…sh init

In a git worktree, .git is a *file* (gitdir: <path>) pointing at the shared
metadata dir, not a directory. The hook installer/status/uninstall built
`root / ".git" / "hooks"` from _git_root's heuristic, so in a worktree
`mkdir` raised NotADirectoryError and the crash propagated up to take down
the whole `repowise init` (issue repowise-dev#1609).

- Add _hooks_dir(repo_path): resolve via `git rev-parse --git-path hooks`,
  which returns the real (shared) hooks dir for both normal repos and
  worktrees, falling back to the .git/hooks heuristic when git is
  unavailable or not a repo.
- install/status/uninstall now use _hooks_dir.

Fixes repowise-dev#1609
@repowise-bot

repowise-bot Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

⚠️ Health of changed files: 6.1 → 5.5 (-0.6)

📋 At a glance
1 file changed health · 2 hotspots touched · 3 new findings introduced · 2 files with recent fix history.

Files & modules (2)
  • packages (1 file)
    • .../cli/hooks.py
  • tests (1 file)
    • .../cli/test_hooks_legacy_migration.py

✅ Health gate: passed

📌 Before you merge

  • Run .../cli/test_editor_setup.py: they import the changed files
🔎 More signals (2)

🗺️ Change map

flowchart LR
  subgraph PR ["Changed in this PR (1 with dependents)"]
    f_packages_cli_src_repowise_cli_hooks_py[".../cli/hooks.py 🔥"]:::changed
  end
  f_packages_cli_src_repowise_cli_commands_hook_cmd_py[".../commands/hook_cmd.py"]
  f_packages_cli_src_repowise_cli_hooks_py --> f_packages_cli_src_repowise_cli_commands_hook_cmd_py
  f_packages_cli_src_repowise_cli_commands_init_cmd__interactive_py[".../init_cmd/_interactive.py"]
  f_packages_cli_src_repowise_cli_hooks_py --> f_packages_cli_src_repowise_cli_commands_init_cmd__interactive_py
  f_packages_cli_src_repowise_cli_editor_setup_py[".../cli/editor_setup.py"]
  f_packages_cli_src_repowise_cli_hooks_py --> f_packages_cli_src_repowise_cli_editor_setup_py
  t_tests_unit_cli_test_editor_setup_py(["✅ .../cli/test_editor_setup.py"]):::guard
  t_tests_unit_cli_test_editor_setup_py -.-> f_packages_cli_src_repowise_cli_hooks_py
  classDef changed fill:#dbeafe,stroke:#1d4ed8,color:#1e3a5f
  classDef warn fill:#fef3c7,stroke:#b45309,color:#78350f
  classDef guard fill:#dcfce7,stroke:#15803d,color:#14532d
Loading

Solid arrows: code that imports the changed files (3 direct dependents, from the last indexed snapshot). Dashed: history/tests.

🔥 Hotspots touched (2)

  • .../cli/test_hooks_legacy_migration.py: 1 commits/90d, 0 dependents · primary owner: Raghav Chamadiya (100%)
  • .../cli/hooks.py: 2 commits/90d, 5 dependents · primary owner: Raghav Chamadiya (100%)

👀 Suggested reviewers @RaghavChamadiya


📊 See the full report for this PR
Your repo map with this PR's blast radius lit up, every caller of the contracts it changes, and health before and after. No sign-in. · ⭐ Star Repowise · 📥 Install bot · Silence on a single PR with [skip repowise] in the title · Per-repo toggle on repowise.dev/settings?tab=bot · Updated 2026-08-21 11:16 UTC

@RaghavChamadiya RaghavChamadiya left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @sloemo01. The diagnosis holds exactly as written: _git_root tests with .exists(), not .is_dir(), so in a worktree it happily returns the root and the failure lands one level down on mkdir, which is why a hook-install detail was able to take down the whole init.

Asking git for the path instead of rebuilding it is the right instinct, and it buys more than the worktree fix you claimed. git rev-parse --git-path hooks also honors core.hooksPath:

$ git rev-parse --git-path hooks
C:\Users\...\repowise\.git\hooks
$ git -c core.hooksPath=/tmp/myhooks rev-parse --git-path hooks
/tmp/myhooks

So this quietly fixes a second case nobody has filed: anyone using husky, lefthook or pre-commit has core.hooksPath pointed elsewhere, and the old code was writing a post-commit hook into a directory git had stopped reading. The hook installed, reported success, and never fired. Worth a line in the changelog beyond the worktree crash.

The absolute-or-relative handling on the rev-parse output is correct, and keeping the .git/hooks heuristic as a fallback means a machine without git on PATH degrades the way it used to instead of newly returning nothing.

@RaghavChamadiya
RaghavChamadiya merged commit ca6fcd1 into repowise-dev:main Aug 21, 2026
9 checks passed
@RaghavChamadiya

Copy link
Copy Markdown
Member

Correction to my merge comment above. I said the core.hooksPath behaviour fixed "a second case nobody has filed". That is wrong: @haibeeb filed it as #1512 on 13 August and was fixing it deliberately in #1511, which predates this PR by eight days and which I should have found before merging this one. The credit for identifying that failure mode is theirs, and #1512 stays open because the husky .husky/_ remap it needs is not in what landed here.

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] initialization failures in worktree aren't handled

2 participants