fix(git-hooks): resolve cache path in worktrees and submodules#11501
Open
batpigandme wants to merge 2 commits intostdlib-js:developfrom
Open
fix(git-hooks): resolve cache path in worktrees and submodules#11501batpigandme wants to merge 2 commits intostdlib-js:developfrom
batpigandme wants to merge 2 commits intostdlib-js:developfrom
Conversation
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>
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix
pre-commit,pre-push, andcommit-msghooks assumption that${root}/.gitis a directory (not true for worktrees and submodules).Use
git rev-parseto 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}/.gitand fail under the old assumption.Fixes: #11500
Assisted-by: Claude Opus 4.7 noreply@anthropic.com
Resolves #11500
Description
This pull request:
pre-commit,pre-push, andcommit-msggit hooks so they work in repository layouts where${root}/.gitis 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 ofNot a directoryerrors, and thepre_commit_report.ymlthatpre-commitwrites (andcommit-msgreads) was never produced.git rev-parse --absolute-git-dirin each affected hook's prologue, alongside the existingroot=$(git rev-parse --show-toplevel)line, and uses${git_dir}in place of${root}/.gitat the three cache-path sites.Related Issues
This pull request has the following related issues:
.gitis a file (git worktrees and submodules) #11500Questions
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 existingroot=pattern. Easy for me to refactor if you'd prefer that style.The
GIT_HOOKS_OUT ?= $(ROOT_DIR)/.git/hooksline ongit_hooks.mk:stdlib/tools/make/lib/init/git_hooks.mk
Lines 27 to 28 in d3427df
has the same
${root}/.gitassumption and will fail undermake init-git-hooksin 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
Validation
make lint-commit-messagepasses cleanly on the commit message (0 problems, 0 warnings).git rev-parse --absolute-git-dirin the worktree where git hooks fail to read/write hooks-cache when.gitis a file (git worktrees and submodules) #11500 was originally reproduced and confirmed the resolved path points to an existing directory, whereas${root}/.gitpoints to a gitfile.No.
Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
Claude Opus 4.7 (via Claude Code) assisted with drafting the three-file patch, analyzing side effects (the
--git-dirvs--absolute-git-dirvs--git-common-dirtrade-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. TheAssisted-by:trailer on the commit records the same.@stdlib-js/reviewers