|
| 1 | +#!/usr/bin/env bash |
| 2 | +# guard-git.sh — PreToolUse hook for Bash tool calls |
| 3 | +# Blocks dangerous git commands that interfere with parallel sessions |
| 4 | +# and validates commits against the session edit log. |
| 5 | + |
| 6 | +set -euo pipefail |
| 7 | + |
| 8 | +INPUT=$(cat) |
| 9 | + |
| 10 | +# Extract the command from tool_input JSON |
| 11 | +COMMAND=$(echo "$INPUT" | node -e " |
| 12 | + let d=''; |
| 13 | + process.stdin.on('data',c=>d+=c); |
| 14 | + process.stdin.on('end',()=>{ |
| 15 | + const p=JSON.parse(d).tool_input?.command||''; |
| 16 | + if(p)process.stdout.write(p); |
| 17 | + }); |
| 18 | +" 2>/dev/null) || true |
| 19 | + |
| 20 | +if [ -z "$COMMAND" ]; then |
| 21 | + exit 0 |
| 22 | +fi |
| 23 | + |
| 24 | +# Only act on git commands |
| 25 | +if ! echo "$COMMAND" | grep -qE '^\s*git\s+'; then |
| 26 | + exit 0 |
| 27 | +fi |
| 28 | + |
| 29 | +deny() { |
| 30 | + local reason="$1" |
| 31 | + node -e " |
| 32 | + console.log(JSON.stringify({ |
| 33 | + hookSpecificOutput: { |
| 34 | + hookEventName: 'PreToolUse', |
| 35 | + permissionDecision: 'deny', |
| 36 | + permissionDecisionReason: process.argv[1] |
| 37 | + } |
| 38 | + })); |
| 39 | + " "$reason" |
| 40 | + exit 0 |
| 41 | +} |
| 42 | + |
| 43 | +# --- Block dangerous commands --- |
| 44 | + |
| 45 | +# git add . / git add -A / git add --all (broad staging) |
| 46 | +if echo "$COMMAND" | grep -qE '^\s*git\s+add\s+(\.\s*$|-A|--all)'; then |
| 47 | + deny "BLOCKED: 'git add .' / 'git add -A' stages ALL changes including other sessions' work. Stage specific files instead: git add <file1> <file2>" |
| 48 | +fi |
| 49 | + |
| 50 | +# git reset (unstaging / hard reset) |
| 51 | +if echo "$COMMAND" | grep -qE '^\s*git\s+reset'; then |
| 52 | + deny "BLOCKED: 'git reset' can unstage or destroy other sessions' work. To unstage your own files, use: git restore --staged <file>" |
| 53 | +fi |
| 54 | + |
| 55 | +# git checkout -- <file> (reverting files) |
| 56 | +if echo "$COMMAND" | grep -qE '^\s*git\s+checkout\s+--'; then |
| 57 | + deny "BLOCKED: 'git checkout -- <file>' reverts working tree changes and may destroy other sessions' edits. If you need to discard your own changes, be explicit about which files." |
| 58 | +fi |
| 59 | + |
| 60 | +# git restore (reverting) — EXCEPT git restore --staged (safe unstaging) |
| 61 | +if echo "$COMMAND" | grep -qE '^\s*git\s+restore'; then |
| 62 | + if ! echo "$COMMAND" | grep -qE '^\s*git\s+restore\s+--staged'; then |
| 63 | + deny "BLOCKED: 'git restore <file>' reverts working tree changes and may destroy other sessions' edits. To unstage files safely, use: git restore --staged <file>" |
| 64 | + fi |
| 65 | +fi |
| 66 | + |
| 67 | +# git clean (delete untracked files) |
| 68 | +if echo "$COMMAND" | grep -qE '^\s*git\s+clean'; then |
| 69 | + deny "BLOCKED: 'git clean' deletes untracked files that may belong to other sessions." |
| 70 | +fi |
| 71 | + |
| 72 | +# git stash (hides all changes) |
| 73 | +if echo "$COMMAND" | grep -qE '^\s*git\s+stash'; then |
| 74 | + deny "BLOCKED: 'git stash' hides all working tree changes including other sessions' work. In worktree mode, commit your changes directly instead." |
| 75 | +fi |
| 76 | + |
| 77 | +# --- Commit validation against edit log --- |
| 78 | + |
| 79 | +if echo "$COMMAND" | grep -qE '^\s*git\s+commit'; then |
| 80 | + PROJECT_DIR="${CLAUDE_PROJECT_DIR:-.}" |
| 81 | + LOG_FILE="$PROJECT_DIR/.claude/session-edits.log" |
| 82 | + |
| 83 | + # If no edit log exists, allow (backward compat for sessions without tracking) |
| 84 | + if [ ! -f "$LOG_FILE" ] || [ ! -s "$LOG_FILE" ]; then |
| 85 | + exit 0 |
| 86 | + fi |
| 87 | + |
| 88 | + # Get unique edited files from log |
| 89 | + EDITED_FILES=$(awk '{print $2}' "$LOG_FILE" | sort -u) |
| 90 | + |
| 91 | + # Get staged files |
| 92 | + STAGED_FILES=$(git diff --cached --name-only 2>/dev/null) || true |
| 93 | + |
| 94 | + if [ -z "$STAGED_FILES" ]; then |
| 95 | + exit 0 |
| 96 | + fi |
| 97 | + |
| 98 | + # Find staged files that weren't edited in this session |
| 99 | + UNEXPECTED="" |
| 100 | + while IFS= read -r staged_file; do |
| 101 | + if ! echo "$EDITED_FILES" | grep -qxF "$staged_file"; then |
| 102 | + UNEXPECTED="${UNEXPECTED:+$UNEXPECTED, }$staged_file" |
| 103 | + fi |
| 104 | + done <<< "$STAGED_FILES" |
| 105 | + |
| 106 | + if [ -n "$UNEXPECTED" ]; then |
| 107 | + deny "BLOCKED: These staged files were NOT edited in this session: $UNEXPECTED. They may belong to another session. Commit only your files: git commit <your-files> -m \"msg\"" |
| 108 | + fi |
| 109 | +fi |
| 110 | + |
| 111 | +exit 0 |
0 commit comments