fix: hook resilience for git ops, regex bypass, and worktree isolation#153
fix: hook resilience for git ops, regex bypass, and worktree isolation#153carlos-alm merged 1 commit intomainfrom
Conversation
Three fixes to Claude Code hooks: 1. New post-git-ops.sh hook (PostToolUse on Bash) detects git rebase, revert, cherry-pick, merge, pull and automatically rebuilds the codegraph, logs changed files to session-edits.log, and clears stale entries from codegraph-checked.log. 2. Fix regex bypass in guard-git.sh: blocking patterns used ^\s*git which only matched git at command start. Commands like `cd foo && git add .` bypassed all blocks. Now uses (^|\s|&&\s*)git to match chained commands consistently. 3. Fix worktree isolation: session-local state files (session-edits.log, codegraph-checked.log) now use `git rev-parse --show-toplevel` instead of CLAUDE_PROJECT_DIR. This gives each worktree its own edit log, preventing cross-session leakage where session A could commit files only edited by session B. Also adds docs/examples/claude-code-hooks/ with distributable hook examples, settings.json, and setup README.
|
Claude finished @carlos-alm's task —— View job PR Review CompleteTodo List:
Root Cause AnalysisProblem Being Solved: Three critical issues in Claude Code hooks:
Why This Approach: All fixes are well-targeted and address root causes:
Risk Assessment: LOW - All changes are additive security improvements with proper fallbacks Phase 4: Code Quality Review✅ Security Fix - Regex Bypass (guard-git.sh:46-74)CRITICAL FIX: Changed from
✅ New post-git-ops.sh HookWell-designed solution for git operation blind spot:
Minor suggestion: Line 50 ✅ Worktree Isolation (Multiple Files)Consistent pattern applied across all 5 hooks: PROJECT_DIR=$(git rev-parse --show-toplevel 2>/dev/null) || PROJECT_DIR="${CLAUDE_PROJECT_DIR:-.}"
✅ Settings IntegrationClean addition of
✅ Documentation & ExamplesExcellent addition of
One improvement: The example Critical ConcernsNone identified - this is a high-quality security and reliability improvement. Final Recommendation
Ready to merge - this addresses real issues with solid engineering. |
Greptile SummaryThis PR fixes three hook resilience issues and adds distributable examples. Key changes:
Minor observations:
Confidence Score: 5/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Bash Command Executed] --> B{Contains git rebase/<br/>revert/cherry-pick/<br/>merge/pull?}
B -->|No| C[Exit hook]
B -->|Yes| D[Get worktree root via<br/>git rev-parse --show-toplevel]
D --> E[Rebuild codegraph<br/>incrementally]
E --> F{Get changed files via<br/>git diff ORIG_HEAD HEAD}
F -->|Success| G[Append changed files<br/>to session-edits.log]
F -->|Fail/Empty| C
G --> H{codegraph-checked.log<br/>exists?}
H -->|Yes| I[Remove changed files<br/>from checked log]
H -->|No| C
I --> C
Last reviewed commit: 27243b7 |
Additional Comments (1)
|
Summary
Three fixes to Claude Code hooks:
post-git-ops.shhook (PostToolUse on Bash): detectsgit rebase/revert/cherry-pick/merge/pulland automatically rebuilds the codegraph, logs changed files tosession-edits.log, and clears stale entries fromcodegraph-checked.logguard-git.sh: blocking patterns used^\s*gitwhich only matched git at command start —cd foo && git add .bypassed all blocks. Now uses(^|\s|&&\s*)gitconsistently (fixes Greptile review from fix: post-git-ops hook for graph resilience after rebase/revert/merge #151)git rev-parse --show-toplevelinstead ofCLAUDE_PROJECT_DIR, giving each worktree its own edit log and preventing cross-session leakagedocs/examples/claude-code-hooks/with all hooks,settings.json, and setup READMESupersedes #151 (which was closed due to scope mixing with unrelated commits).
Test plan
cd foo && git add .is now blocked byguard-git.shcd foo && git reset --hardis blockedsession-edits.logis created at the worktree root, not the main project rootgit rebaseand verify graph rebuilds automatically viapost-git-ops.shsession-edits.logand can be committedremind-codegraph.shre-fires for affected filesORIG_HEADdoesn't exist (failed rebase with conflicts)