Skip to content

fix: pass full command to checkGit for cd target detection#32

Merged
George-iam merged 2 commits intomainfrom
fix/hook-pass-full-command-20260407
Apr 7, 2026
Merged

fix: pass full command to checkGit for cd target detection#32
George-iam merged 2 commits intomainfrom
fix/hook-pass-full-command-20260407

Conversation

@George-iam
Copy link
Copy Markdown
Contributor

Summary

Hook's splitCommandSegments was stripping cd /path && prefix before passing to checkGit. extractGitCwd in detectBranch never saw the cd target, so merged-PR check silently passed.

Fix: pass full command string to checkGit instead of individual segments. extractGitCwd can now find cd /path and run git branch --show-current in the correct directory.

Root cause chain

  1. splitCommandSegments("cd /path && git commit -m ...") -> segments: ["cd /path ", " git commit -m ..."]
  2. Only git commit segment passed to checkGit
  3. extractGitCwd("git commit -m ...") -> no cd found -> undefined
  4. git branch --show-current runs in workspace root (no .git) -> null
  5. checkMergedBranch skips (no branch detected) -> allowed

Test plan

  • Merge, reload, verify: cd /path && git commit on merged branch is blocked

splitCommandSegments was stripping cd /path from the command
before passing to checkGit. detectBranch/extractGitCwd never
saw the cd target, so merged-PR check failed on chained commands.
Now checkGit receives the full command string.
Replace fragile command-text parsing with proper cd/pushd state
tracking through command segments in pre-tool-use hook:

- Track effectiveCwd cumulatively through cd/pushd segments
- Parse git -C flag from git segments
- Pass resolved cwd to checkGit/detectBranch explicitly
- If cwd unresolvable (variables, subshells): BLOCK with
  instruction to use git -C /absolute/path
- Remove extractGitCwd regex parsing from safety.ts
- detectBranch/checkMergedBranch accept cwd parameter
@George-iam George-iam merged commit c3ca66f into main Apr 7, 2026
@George-iam George-iam deleted the fix/hook-pass-full-command-20260407 branch April 7, 2026 15:26
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.

1 participant