Repository navigation
fix(#1228): pre-tool.sh reset --hard guard evaluates the directory the reset targets - #1237
Merged
admarble merged 3 commits intoSep 30, 2026
Conversation
WIP checkpoint before mutation testing Entire-Checkpoint: e2b470885a29
…d target fix Entire-Checkpoint: fe845e95beee
The issue's own repro used cd "$WT": a variable target can't be resolved before the shell runs, so the guard still checks the command's cwd and blocks. That's the safe side, but the message gave no hint why; it now prints the checked directory and says only literal targets resolve. Entire-Checkpoint: 87fbf46730f5
4 tasks
admarble
deleted the
feature/1228-fix-hooks-pre-tool-sh-reset-hard-guard-reads-git-s
branch
September 30, 2026 17:15
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.
Summary
pre-tool.sh'sgit reset --hardguard rangit status, the unpushed-commit check and theMERGE_HEADcheck in the hook process's own cwd. PreToolUse runs before the command's shell, so acd <wt> &&prefix had not taken effect yet. Clean worktrees were blocked whenever the main checkout was dirty, and dirty worktrees were allowed whenever it was clean.git -C <dir> reset --hardnever matched the trigger at all.resolve_git_c_targetnext toresolve_cd_target. It reads the raw command, because$SEGMENTSblanksgit -C "$WT". It fails open on$, backticks and backslashes, and resolves a relative-Cpath against thecdtarget.git +(-C +[^ ]* +)?reset…. It still matches sanitized segments only, so quoted and heredoc text never triggers it (fix(hooks):^gh (issue|pr)prefix skip disarms all 5 pre-tool guards for compound commands, and re-breaks on anycdprefix #763).-Cpath, then thecdtarget, then the payloadcwd. A target that is not a repo falls back to the payloadcwd, then the process cwd, so an unresolvable target never allows silently.--absolute-git-dir, since a linked worktree keepsMERGE_HEADin its per-worktree gitdir) and the remediation branch name all usegit -C "$RESET_DIR". The local variableGIT_DIRis renamed toRESET_GIT_DIRso it cannot shadow an exportedGIT_DIR.Pre-PR AC Verification
cd <clean-wt> && git reset --hard origin/xallowed while main is dirty__tests__/pre-tool-hook.integration.test.ts#1228 block, "AC-1: allows …", ×3 hook copiescd <dirty-wt> &&,git -C <dirty-wt>,git -C "<dirty-wt>"andcd <parent> && git -C <relative>all block with a clean hook cwdgit push origin feature/1228-dirtygit reset --hardin a dirty cwd still blocks; no existing corpus verdict changesnpx tsx scripts/hook-corpus.ts --diff origin/maingives 0 changes on existing entries;__tests__/hook-corpus.integration.test.ts1025/1025; "AC-3: still blocks …"MERGE_HEAD, branchfeature/1228-merge) and "AC-4: allows … only the main checkout is mid-merge"git -C <dir> reset --hardbody text stays allowedgit -C "$WT"falls back to the payload cwdHook verdicts changed:
git -C /Users/user/Projects/missing reset --hard origin/main(new corpus entry, statedirty): allow -> block. This is intended:git -C … resetnow triggers the guard, and a missing target falls back to the cwd. Itscleantwin stays allowed. No existing entry changed.Mutation verification
Mutation-verified: AC-1 — reverted Check 2 to a cwd-relative
git status --porcelain;__tests__/pre-tool-hook.integration.test.tsAC-1 test failed (along with the 4 AC-2 dirty-worktree cases, which also depend on Check 2); restored.Two more mutations, for extra coverage:
git reset.*failed the threegit -CAC-2 cases and AC-6.Runner verification (tracker #1236)
QA verdict: NEEDS_VERIFICATION. The run log's QA summary was
null, and the issue comment carries only the verdict line, so the runner verified the change independently.Probe: the real hook (branch
hooks/pre-tool.sh) was run with real payloads against a sandbox main checkout and two linked worktrees:main)git reset --hard, dirty maingit -C <dirty-wt> reset --hardfrom a clean cwdcd <clean-wt> && git reset --hardfrom a dirty main (the report)cd <dirty-wt> && git reset --hardfrom a clean cwdWT=…; cd "$WT" && git reset --hardfrom a dirty maingh … --body "git -C /x reset --hard"Added by the runner (
b6162999): the issue's own repro usedcd "$WT". A variable target can't be resolved before the command's shell runs, so the guard checks the command's cwd and still blocks there. That's the safe side, but the old message didn't say why. The block now printsChecked: <dir>and says that only a literalcd <dir>/git -C <dir>target is resolved. The AC-6 test asserts both lines.Repeated the AC-1 mutation myself (Check 2 reverted to a cwd-relative
git status, in all three copies). The AC-1 and AC-2 cases failed for every hook copy; restored.__tests__/pre-tool-hook.integration.test.ts,__tests__/hook-corpus.integration.test.tsandscripts/hook-corpus.test.ts: 1461 passed. The three hook copies are byte-identical (cmp).Test plan
npx vitest run __tests__/pre-tool-hook.integration.test.ts: 423/423npx vitest run __tests__/hook-corpus.integration.test.ts: 1025/1025npm run build,npm run lint,bash -n, andcmpacross the three hook copiesnpm testnot run, per the spec's standard block (targeted suites only)Follow-ups (not in scope)
resolve_cd_targettakes the lastcd, socd <dirty> && git reset --hard && cd <clean>evaluates<clean>. It also tests a relativecdtarget against the process cwd. Both limits already affect the commit guard, and the reset guard now inherits them. They are noted in a code comment and are candidates for one shared follow-up.Closes #1228
Refs #1236
🤖 Generated with Claude Code