#!/usr/bin/env python3
import json, re, sys

try:
    command = json.load(sys.stdin).get("tool_input", {}).get("command", "")
except Exception:
    sys.exit(0)

G = r"(?:^|[;&|(`\n]\s*|\$\(\s*)(?:\w+=\S*\s+)*git\s+(?:-C\s+\S+\s+)?"
rules = [
    (G + r"checkout\b(?!\s+-b\b)", "git checkout"),
    (G + r"restore\b(?![^;&|\n]*--staged)", "git restore (without --staged)"),
    (G + r"stash\b", "git stash"),
    (G + r"reset\b[^;&|\n]*--hard", "git reset --hard"),
    (G + r"clean\b", "git clean"),
    (G + r"switch\b", "git switch"),
]
for pattern, name in rules:
    if re.search(pattern, command):
        print(
            f"Blocked: {name} in the shared checkout wipes other agents' uncommitted work. "
            "Undo your own hunks by editing the file; read an old version with `git show <rev>:<path>`; "
            "unstage with `git restore --staged <path>`.",
            file=sys.stderr,
        )
        sys.exit(2)
sys.exit(0)
