Skip to content

fix(cli): resolve the hooks dir git actually runs - #1511

Merged
RaghavChamadiya merged 3 commits into
repowise-dev:mainfrom
haibeeb:fix/hooks-path-resolution
Sep 5, 2026
Merged

RaghavChamadiya merged 3 commits into
repowise-dev:mainfrom
haibeeb:fix/hooks-path-resolution

Conversation

@haibeeb

@haibeeb haibeeb commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • install / uninstall / status hardcoded <root>/.git/hooks. That path crashes on git worktrees and is silently ignored by git whenever core.hooksPath is set, so the hook reports installed and never runs.
  • Resolve the directory with git rev-parse --git-path hooks — the same resolution git itself performs — instead of assuming the layout.
  • Remap husky's generated .husky/_ up to the committed .husky/ user-hook dir, since _ is gitignored and regenerated on every npm install.

The three broken layouts

Layout Old behaviour
Linked worktree / submodule .git is a file, so hooks_dir.mkdir() raised NotADirectoryError and aborted the command
core.hooksPath set (husky, lefthook) git ignores .git/hooks; hook written there is never executed, while status reports installed
Worktree Hooks live in the repository's common dir, not the worktree's own gitdir

The core.hooksPath case is the one worth emphasising: it fails silently. On a repo using husky (very common in JS/TS projects) repowise hook install has been a no-op that reports success, so auto-sync never ran and there was no signal that anything was wrong.

Reproduction of the crash:

$ cd my-worktree && repowise hook install
NotADirectoryError: [Errno 20] Not a directory: '.../worktree/.git/hooks'

Why remap husky's _ directory

husky sets core.hooksPath=.husky/_, but that directory is regenerated on every install and gitignored wholesale (.husky/_/.gitignore is *), so a hook written into it disappears at the next npm install. husky's shim dispatches to .husky/<hook-name> one level up, which is the durable location. The remap is guarded so a directory merely named _ is not affected.

Behaviour is unchanged for a plain checkout, and there is a fallback to the previous path if no git binary is available.

Related Issues

Fixes #1512

Test Plan

New tests/unit/cli/test_hooks_path_resolution.py (9 tests) covers: plain repo, relative and absolute core.hooksPath, husky remap with and without generated helpers, a non-husky _ directory, the worktree crash regression, install/status/uninstall agreeing on one location, and the no-git fallback.

  • Tests pass (pytest) — 9 new; tests/unit/cli/ green at 2108 passed, 1 skipped
  • Lint passes (ruff check .)
  • Web build passes (npm run build) (no frontend changes)

Note: ruff format also wants to reformat pre-existing code in hooks.py (e.g. lines[end + 1 :]). I left that alone to keep this diff focused — CI runs ruff check, which passes.

Checklist

  • My code follows the project's code style
  • I have added tests for new functionality
  • All existing tests still pass
  • I have updated documentation if needed (none needed — internal path resolution)

@RaghavChamadiya

Copy link
Copy Markdown
Member

Thanks for this, and for the issue write-up that came with it. The core.hooksPath case being a silent success is exactly the part that makes this worth fixing, and I would not have guessed how many husky repos have been quietly getting nothing from hook install.

I reproduced all three layouts before reading the diff. In a linked worktree .git is a file and the old mkdir dies just as you describe, on Windows as FileNotFoundError rather than NotADirectoryError, and git rev-parse --git-path hooks points at the main repo's common dir. With core.hooksPath set it returns the configured path, relative when the config is relative, so the cwd=root anchor and the join are both doing real work. Routing install, uninstall and status through one resolver is the part I like most, since those three drifting apart is how you get a hook that reports installed and is not.

One question on the husky remap. husky generates a shim per hook name in _ at install time. If post-commit was not one of them, does the file you write to .husky/post-commit actually get dispatched, or does it sit there until the next npm install regenerates the shims? If it is the latter, the remap is still an improvement over writing into a directory that gets wiped, but the end-to-end path is not fixed yet and I would rather know now.

Two small notes. A user with a global core.hooksPath such as ~/.githooks will now have the hook written outside the repo. The script body guards on $ROOT/.repowise so it is harmless, but status will say installed in every repo and uninstall in one repo turns it off everywhere. Worth a line in the _hooks_dir docstring so that reads as a decision rather than a surprise. And detect_editor_setup_outcome in editor_setup.py still says every probe is a cheap local file read, which is no longer quite true now that status shells out to git.

@haibeeb

haibeeb commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

Thanks — the Windows detail is useful, I only had the POSIX NotADirectoryError, and you reproducing all three layouts before reading the diff caught something I had assumed rather than checked.

The husky shim question

Checked husky's source rather than reasoning about it, because your framing made me realise I had only ever observed a repo where the shim happened to exist. In husky 9.1.7 index.js:

let l = [ 'pre-commit', 'pre-merge-commit', 'prepare-commit-msg', 'commit-msg',
          'post-commit', 'applypatch-msg', ... ]   // 14 names
...
l.forEach(h => w(_(h), `#!/usr/bin/env sh\n. "\$(dirname "\$0")/h"`, { mode: 0o755 }))

The list is fixed and forEach writes every shim unconditionally on each install — it does not consult .husky/ to see which user hooks exist. post-commit is in it. And _/h does s=$(dirname "$(dirname "$0")")/$n; [ ! -f "$s" ] && exit 0; sh -e "$s", so a shim with no user hook is a no-op and one that gains a user hook starts dispatching immediately.

So the end-to-end path is fixed: dispatch is immediate, not deferred to the next npm install. I verified it on a repo whose only user hook was pre-commit, which is now a test (test_husky_dispatches_without_a_preexisting_user_hook). Cited in the _husky_user_hook_dir docstring so the next reader does not have to re-derive it.

But you found a real hole

There is one case where nothing dispatches, and I had it wrong: a checkout where husky has never run. .husky/_ does not exist, core.hooksPath points at a missing directory, and git runs no hooks at all — husky's own pre-commit included. I hit this in one of my own worktrees and read it as "fine, the file is in the durable place" without noticing that the previous install returned a bare installed.

That is the same silent success this PR exists to remove, just relocated one directory over, so leaving it would have undercut the point. Added husky_pending_reason, reported from both install and status:

$ repowise hook install
Post-commit hook: installed (husky is not set up in this checkout (no .husky/_),
so git runs no hooks here yet -- run your package manager's install to activate it)

Writing to .husky/ is still right — it survives, and activates as soon as husky runs — but now it says so instead of looking live.

Your two notes

Global core.hooksPath. Documented in _hooks_dir as a decision: it makes the hook shared, so status reports installed from every repo and uninstall in one turns it off everywhere. Also recorded why that is acceptable rather than merely known — the hook body resolves its own $ROOT and returns early unless $ROOT/.repowise exists, so a shared hook is inert in unindexed repos rather than wrong.

detect_editor_setup_outcome. Fixed; you were right that it went stale. It now says all but one probe is a local file read, and that the autosync probe spawns git rev-parse once because core.hooksPath and worktrees make the path impossible to derive from the repo path alone.

Verification

tests/unit/cli/ + tests/providers/: 2167 passed, 1 skipped. ruff check . clean. Three new tests covering dispatch-with-no-sibling-hook, the husky-not-installed report, and silence for non-husky layouts.

Adding the warning immediately caught one of my own earlier tests asserting a bare installed against a half-set-up husky fixture. The new message was correct and the fixture was incomplete, so I completed the fixture rather than loosening the assertion.

One thing to flag rather than bury: tests/integration/test_cli.py::TestStatusDoctorWithEnvDb has two failures here (FileExistsError on (dest / ".repowise").mkdir()), and they reproduce identically on pristine main. Order-dependent fixture pollution, not from this branch — happy to file it separately if it is not already known.

@RaghavChamadiya

Copy link
Copy Markdown
Member

Thanks @haibeeb, this is a good fix and the diagnosis is better than the title suggests.

Using git rev-parse --git-path hooks instead of assuming <root>/.git/hooks is the right call. It is the resolution git itself performs, so worktrees, submodules and core.hooksPath all fall out of one change instead of three special cases.

You are right that the core.hooksPath case is the worse of the two. A crash is loud and gets reported, which is how #1609 reached us. A hook written to a directory git ignores, while status cheerfully reports installed, is the kind of failure people live with for months without knowing auto-sync never ran. Worth having that called out in the PR body.

The husky _ remap is the part I looked at hardest, since it is the piece that is guessing about another tool's layout rather than asking git. Guarding on the h / husky.sh markers plus the .husky parent name, instead of just the directory being called _, is the right level of caution. And the reasoning holds: .husky/_ is regenerated on every npm install and gitignored wholesale, so anything written there is temporary by construction, while husky's shim dispatches one level up to a location that survives.

This also covers #1609, which is the same NotADirectoryError from hooks.py reached through init rather than through hook install. Worth adding to the closing keywords alongside #1512. I would leave #1609 open even so, since it carries a second point that this PR does not address: offer_hook_install lets an exception from install() abort the whole init, after indexing has already completed. That function already catches click.Abort and EOFError with a comment saying an optional hook is not worth failing the run over, so the intent is there and the catch is just too narrow. Happy for that to be a separate PR, it is not yours to fix here.

One thing before I merge: CI has not run on this recently, only the code health check is reporting. Could you rebase onto main and push so the full matrix runs? Content-wise I have no changes to ask for.

@RaghavChamadiya

Copy link
Copy Markdown
Member

@haibeeb this is my mistake and I want to name it rather than let you find it.

I merged #1808 an hour ago, which fixes the worktree crash the same way yours does, via git rev-parse --git-path hooks. Yours was opened on 13 August, eight days earlier, and covers strictly more ground. I should have found it before merging the newer one and I did not. Nothing about your PR caused this.

For the record of what you had that the merged one did not: you identified the core.hooksPath silent no-op as a first-class failure and filed #1512 for it, which is still open. I actually praised that behaviour on #1808 as an unreported bonus, not realising you had reported it deliberately and were fixing it on purpose. I have corrected that comment.

The husky .husky/_ remap is entirely yours and is the part I most want to land. It is the subtlest of the three layouts: husky points core.hooksPath at a directory that is gitignored wholesale and regenerated on every npm install, so even a correctly resolved hooks dir gets the hook wiped at the next install, and the dispatch to .husky/<hook-name> one level up is the only durable target. Nothing in what I merged handles that. On a JS or TS repo using husky, auto-sync still silently does not run.

So this is not superseded. What I would like:

git fetch origin && git rebase origin/main. The _hooks_dir resolution in hooks.py will conflict with what just landed and the two implementations are near enough that taking either side is fine. What needs to survive is the husky remap, the editor_setup.py change, and your tests. The merged version has one small test for the worktree case; your nine cover the relative and absolute core.hooksPath forms, the non-husky _ directory guard, and install/status/uninstall agreeing on one location, which is the property that actually matters here and is worth keeping over mine.

Your branch is also too old to have a CI run against it, so the rebase is needed to get the matrix going regardless. Ping me when it is pushed and I will approve the run and merge it. #1512 stays open until this lands.

Sorry for the wasted round trip.

Hai Ton and others added 3 commits September 5, 2026 11:43
`install`, `uninstall` and `status` hardcoded `<root>/.git/hooks`, which is
wrong in three layouts:

- In a linked worktree (and a submodule) `.git` is a *file* holding a gitdir
  pointer, so `hooks_dir.mkdir()` raised NotADirectoryError and killed the
  command.
- When `core.hooksPath` is set, git ignores `.git/hooks` entirely. husky and
  lefthook both set it, so most JS repos are affected: the hook installs
  "successfully", `status` reports it installed, and git never runs it.
- A worktree's hooks live in the repository's common dir, not its own gitdir.

Resolve via `git rev-parse --git-path hooks` instead, which is the same
resolution git performs, then remap husky's generated `.husky/_` up to the
committed `.husky/` user-hook dir -- `_` is gitignored and regenerated on
every `npm install`, so a hook written there does not survive.

Falls back to the previous path when no git binary is available.
Review follow-ups on the hooks-path resolver.

husky writes a shim for a fixed list of all 14 git hook names on every install,
regardless of which user hooks exist, and each shim execs `.husky/<name>` when
that file is present. So a hook written to the user-hook dir is dispatched
immediately rather than waiting for the next `npm install`; documented with the
citation, and pinned by a test that installs with only `pre-commit` present.

The exception is a checkout where husky has never run: `.husky/_` is absent,
`core.hooksPath` points at a missing directory, and git runs no hooks at all.
Returning a bare "installed" there would reproduce the silent success this
resolver exists to remove, so `husky_pending_reason` reports it from both
`install` and `status`.

Also documents that a global `core.hooksPath` makes the hook shared across
repos -- `status` reports installed everywhere and `uninstall` removes it
everywhere -- and that the hook body stays repo-scoped through its
`$ROOT/.repowise` guard, so a shared hook is inert rather than wrong.

Corrects `detect_editor_setup_outcome`'s docstring, which claimed every probe
was a local file read; the autosync probe now spawns `git rev-parse` once.
status() now returns installed (reason) in a husky checkout that has not
run its package manager yet. The init offer, the completion panel and the
workspace status table compared the exact string, so a hook that was
installed and merely pending read as absent: the offer asked again, the
panel said no auto-sync, and the table showed a cross.
@RaghavChamadiya

Copy link
Copy Markdown
Member

Thanks @haibeeb for keeping the husky remap guarded on the _/h and _/husky.sh markers plus the .husky parent, and for the pending-reason report. That is the piece #1808 does not have, and it is why this PR is not superseded.

I said I would merge it once rebased, and the rebase is on me, so I did it. I pushed a rebase of your branch onto current main. Your two commits are intact with you as author; the conflict was only _hooks_dir, where main's git rev-parse --git-path hooks resolution and yours were the same idea, so I kept main's function body and added your _husky_user_hook_dir call on its success path, plus your core.hooksPath note in its docstring. husky_pending_reason, the installed (<reason>) annotation on install and status, the editor_setup.py docstring correction and all twelve of your tests survived.

One commit of mine on top. Three places compared status() to the exact string installed: the init offer (init_cmd/_interactive.py), the completion panel (editor_setup.py) and the workspace status table (hook_cmd.py). With your suffix, a hook that was installed and merely pending read as absent in all three, so the offer asked again and the panel said no auto-sync. They now test the prefix. That is the only behaviour change beyond your diff.

tests/unit/cli/test_hooks_path_resolution.py, test_hooks_legacy_migration.py and test_editor_setup.py are green on the rebased branch, ruff check is clean. Approving and merging once CI agrees, and closing #1512 against this.

@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.

Rebased onto main with the husky remap intact; merging once CI is green.

@RaghavChamadiya
RaghavChamadiya merged commit 3f8f773 into repowise-dev:main Sep 5, 2026
8 checks passed
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] hook install writes to .git/hooks, which git ignores when core.hooksPath is set (and crashes in worktrees)

2 participants