dotfiles/cursor/hooks/strip-cursor-coauthor-shell.sh
ilia 9dc84bf8f5
All checks were successful
CI / skip-ci-check (pull_request) Successful in 23s
CI / secret-scan (pull_request) Successful in 22s
Add Cursor shell hooks for vault and ansible apply gates
Persist destructive/infra footgun hooks in the repo and teach install.sh
to sync them without clobbering a local hooks.json unless overwritten.
2026-07-30 16:50:56 -04:00

58 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Cursor hook: preToolUse (Shell)
# Strip Cursor co-author trailers from git commit/amend commands before execution.
# Note: read stdin before Python (a heredoc would consume hook JSON).
INPUT="$(cat)"
python3 -c '
import json
import re
import sys
raw = sys.argv[1]
try:
data = json.loads(raw) if raw.strip() else {}
except Exception:
print(json.dumps({"permission": "allow"}))
raise SystemExit(0)
tool_input = data.get("tool_input") or {}
if not isinstance(tool_input, dict):
print(json.dumps({"permission": "allow"}))
raise SystemExit(0)
command = (tool_input.get("command") or "").strip()
if not command or "git" not in command:
print(json.dumps({"permission": "allow"}))
raise SystemExit(0)
if not re.search(r"\bgit\b.*\b(commit|commit\s+-a|commit\s+--amend)\b", command):
print(json.dumps({"permission": "allow"}))
raise SystemExit(0)
cleaned = command
patterns = [
r"""\s*--trailer\s+(['"])Co-authored-by:\s*Cursor\s*<cursoragent@cursor\.com>\1""",
r"""\s*--trailer\s+Co-authored-by:\s*Cursor\s*<cursoragent@cursor\.com>""",
r"""\s*--footer\s+(['"])Co-authored-by:\s*Cursor\s*<cursoragent@cursor\.com>\1""",
r"""\s*--footer\s+Co-authored-by:\s*Cursor\s*<cursoragent@cursor\.com>""",
]
for pattern in patterns:
cleaned = re.sub(pattern, "", cleaned, flags=re.I)
cleaned = re.sub(r"\s{2,}", " ", cleaned).strip()
if cleaned == command:
print(json.dumps({"permission": "allow"}))
else:
tool_input["command"] = cleaned
print(json.dumps({
"permission": "allow",
"updated_input": tool_input,
"agent_message": "Removed Cursor co-author trailer from git commit command."
}))
' "$INPUT"