fix(hooks): guard-git.sh commit edit-log check falls back to HOOK_CWD too - #2581
Merged
Conversation
… too Same root cause #2386 fixed for validate_branch_name: a bare git commit <files> -m "msg" relying on the Bash tool's persistent cwd from an earlier, separate tool call leaves WORK_DIR empty, and the commit edit-log check fell back to the hook process's own ambient cwd instead of the payload's reported cwd -- resolving against a completely unrelated repo. Milder than #2386's bug: the wrong repo's git diff --cached comes back empty, hitting the existing "no staged files" early-out and silently allowing, rather than wrongly denying. But it means the edit-log protection (catching another session's files sneaking into your commit) doesn't actually run when it should. Applies the same HOOK_CWD fallback to this section, keeping .claude/hooks/guard-git.sh and docs/examples/claude-code-hooks/guard-git.sh byte-identical per the existing hook-guard-git-clean.test.ts check. Closes #2526
Contributor
Greptile SummaryThe PR makes commit edit-log validation use the Bash payload’s reported cwd when the command has no explicit repository directory, matching the existing branch-validation fallback.
Confidence Score: 5/5The PR appears safe to merge, with the targeted cwd fallback and synchronized hook copies adequately covered by regression tests. The fallback is used only when command-text target detection is unresolved, validates that the payload cwd exists, and leaves explicit repository selection in control. Important Files Changed
Reviews (1): Last reviewed commit: "fix(hooks): guard-git.sh commit edit-log..." | Re-trigger Greptile |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Summary
.claude/hooks/guard-git.sh's commit edit-log check resolves the target repo viadetect_work_dir commit, which only finds an explicit-C <dir>/cd <dir> &&in the command text. A baregit commit <files> -m "msg"that relies on acdfrom an earlier, separate Bash tool call leavesWORK_DIRempty, and the check silently fell back to the hook process's own ambient cwd — which can be a completely unrelated repo — instead of the payload's reportedcwd.validate_branch_name; this PR applies the identical$HOOK_CWDfallback to the commit edit-log check.git diff --cached --name-onlycomes back empty there, hitting the existing "no staged files" early-out and silently allowing — so the symptom is the edit-log check getting silently skipped, not a valid commit getting wrongly blocked.docs/examples/claude-code-hooks/guard-git.shbyte-identical to.claude/hooks/guard-git.sh, as enforced bytests/unit/hook-guard-git-clean.test.ts.Closes #2526
Test plan
tests/unit/hook-guard-git-commit-cwd-fallback.test.ts(4 tests): payload cwd catches an unedited staged file when the ambient cwd is a different, clean repo; still allows when every staged file was actually edited; explicit-Cstill wins over payload cwd; falls back to silently allowing (not denying) when no payload cwd is available either.if false && ...) and confirmed the main regression test fails with the exact silent-allow bug the issue describes (AssertionError: expected undefined to be 'deny'); restored the fix and all 4 tests pass again.hook-guard-git-clean.test.ts,hook-guard-git-ifs-bypass.test.ts,hook-guard-git-branch-validation.test.ts, plus the new file) — confirms the byte-identical-copies check still holds.bash -nsyntax check on the modified hook.npm run lintclean.npx vitest run: 5518/5518 tests pass (344 test files).