Skip to content

fix(git-hooks): resolve cache path in worktrees and submodules#11501

Open
batpigandme wants to merge 2 commits intostdlib-js:developfrom
batpigandme:fix/hooks-cache-worktree-submodule
Open

fix(git-hooks): resolve cache path in worktrees and submodules#11501
batpigandme wants to merge 2 commits intostdlib-js:developfrom
batpigandme:fix/hooks-cache-worktree-submodule

Conversation

@batpigandme
Copy link
Copy Markdown
Member

Fix pre-commit, pre-push, and commit-msg hooks assumption that ${root}/.git is a directory (not true for worktrees and submodules).

Use git rev-parse to resolve the git directory rather than path-joining onto ${root}/.git, so the hooks work in any repo layout git supports — linked worktrees and submodules both put a gitfile at ${root}/.git and fail under the old assumption.

Fixes: #11500
Assisted-by: Claude Opus 4.7 noreply@anthropic.com

Resolves #11500

Description

What is the purpose of this pull request?

This pull request:

  • Fixes the pre-commit, pre-push, and commit-msg git hooks so they work in repository layouts where ${root}/.git is a file rather than a directory — specifically linked worktrees (git worktree add ...) and submodules. Under the previous implementation, every run of these hooks in such layouts emitted dozens of Not a directory errors, and the pre_commit_report.yml that pre-commit writes (and commit-msg reads) was never produced.
  • Resolves the git directory via git rev-parse --absolute-git-dir in each affected hook's prologue, alongside the existing root=$(git rev-parse --show-toplevel) line, and uses ${git_dir} in place of ${root}/.git at the three cache-path sites.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

Questions

Any questions for reviewers of this pull request?

  1. Each hook now has a local git_dir=$(git rev-parse --absolute-git-dir) in its prologue. An alternative would be to inline $(git rev-parse --absolute-git-dir) at each call site — smaller diff, but runs the subshell on every use and doesn't mirror the existing root= pattern. Easy for me to refactor if you'd prefer that style.

  2. The GIT_HOOKS_OUT ?= $(ROOT_DIR)/.git/hooks line on git_hooks.mk:

# Define the destination directory for Git hooks:
GIT_HOOKS_OUT ?= $(ROOT_DIR)/.git/hooks

has the same ${root}/.git assumption and will fail under make init-git-hooks in a worktree or submodule. I scoped this PR to what was proposed in #11500, but can add it to this or submit as its own PR.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

Validation

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

Claude Opus 4.7 (via Claude Code) assisted with drafting the three-file patch, analyzing side effects (the --git-dir vs --absolute-git-dir vs --git-common-dir trade-off, the per-worktree-vs-shared-cache question, the CWD-safety check), and drafting the commit message and this PR description. All changes were reviewed and edited by me before committing. The Assisted-by: trailer on the commit records the same.


@stdlib-js/reviewers

Fix `pre-commit`, `pre-push`, and `commit-msg` hooks assumption
that `${root}/.git` is a directory (not true for worktrees
and submodules).

Use `git rev-parse` to resolve the git directory rather than
path-joining onto `${root}/.git`, so the hooks work in any repo
layout git supports — linked worktrees and submodules both put a
gitfile at `${root}/.git` and fail under the old assumption.

Fixes: stdlib-js#11500
Assisted-by: Claude Opus 4.7 <noreply@anthropic.com>
@batpigandme batpigandme requested a review from a team April 17, 2026 14:48
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Apr 17, 2026
The `commit-msg` hook never referenced `${root}`. Touching
the file for the worktree fix surfaced the pre-existing SC2034
warning; remove the dead line rather than carrying it forward.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Review A pull request which needs code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

git hooks fail to read/write hooks-cache when .git is a file (git worktrees and submodules)

2 participants