You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: prevent main-branch-guard from blocking non-repository files (#36)
Replace Python implementation with shell script that correctly handles
files outside git repositories.
ROOT CAUSE
----------
The Python implementation ran branch checks from PWD without validating
whether the target file was actually in a git repository. This caused
legitimate operations on non-repo files (like ~/.claude/plans/) to be
blocked when the current directory was on the main branch.
SOLUTION
--------
Replaced 110-line Python script with ~60-line shell script that:
1. Extracts file path from JSON input (handles file_path and notebook_path)
2. Checks if file is in a git repository from file's directory context
3. Only applies branch checks if file is actually in a repository
4. Blocks edits to tracked files on main branch
Key fix:
# NEW: Check git repo from file's directory, not PWD
if ! (cd "$file_dir" && git rev-parse --git-dir >/dev/null 2>&1); then
exit 0 # Not in repo, allow operation
fi
TEST RESULTS
------------
All manual tests pass:
- ✅ Allows edits to non-repo files (even when CWD is main)
- ✅ Blocks tracked files in main worktree
- ✅ Allows all edits in feature branches
- ✅ Allows untracked files everywhere
FILES CHANGED
-------------
- git-guards/scripts/main-branch-guard.sh (NEW) - Shell implementation
- git-guards/scripts/main-branch-guard.py (REMOVED) - Python implementation
- git-guards/hooks/hooks.json - Updated script reference (.py → .sh)
- git-guards/README.md (NEW) - Plugin documentation
- content-guards/README.md - Simplified for AI-only repo
- AGENTS.md - Added hook language selection guidance
BENEFITS
--------
1. Fixes plan mode and other non-repo file operations
2. 45% code reduction (110 → 60 lines)
3. Simpler implementation (shell vs Python subprocess handling)
4. Same functionality preserved
5. Better guidance for future hook development
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
0 commit comments