Lukasz Boryn ddfcdb787f feat(skills): add setup-git-guardrails — tuned port of mattpocock git hook
- feat(setup-git-guardrails): PreToolUse hook blocks reset --hard,
  clean -f combos, branch -D, checkout/restore ., force push
- feat(setup-git-guardrails): tuned vs upstream — plain push and jj git
  push allowed, --force-with-lease exempt, short -f caught, dotfile
  paths (checkout .config) not false-positived
- docs(setup-git-guardrails): three-case verify contract; jj mutations
  left unblocked (op log recoverable)
2026-07-20 01:44:47 +02:00

33 lines
1.0 KiB
Bash
Executable File

#!/bin/bash
# PreToolUse hook: blocks destructive git commands before they execute.
# Tuned from mattpocock/skills git-guardrails-claude-code:
# - plain `git push` allowed (routine flow; `jj git push` must keep working)
# - `--force-with-lease` allowed (the sanctioned force path)
# - short `-f` force and clean flag-combos caught (upstream missed them)
INPUT=$(cat)
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty')
[ -z "$COMMAND" ] && exit 0
DANGEROUS_PATTERNS=(
"reset --hard"
"clean -[A-Za-z]*f"
"branch -D"
"git checkout \\.( |$)"
"git restore \\.( |$)"
"push [^|;&]*--force( |$)"
"push [^|;&]*-f( |$)"
)
for pattern in "${DANGEROUS_PATTERNS[@]}"; do
if echo "$COMMAND" | grep -qE "$pattern"; then
if echo "$COMMAND" | grep -qE "push [^|;&]*--force-with-lease"; then
continue
fi
echo "BLOCKED: '$COMMAND' matches dangerous pattern '$pattern'. The user has prevented you from running this. If genuinely needed, ask the user to run it themselves." >&2
exit 2
fi
done
exit 0