#!/usr/bin/env bash
# PreToolUse(Bash); self-filters on `git commit`. Enforces commit shape deterministically:
# - no Co-Authored-By / agent branding in the message
# - a body is present (second -m, a newline in -m, -F, or heredoc); subject <= 72 chars
# - the commit is not a "one giant commit": staged set spans too many files / top-level areas
# Exit 2 = deny with instructions. Exit 0 = fine.
set -u
input="$(cat)"
cmd="$(printf '%s' "$input" | jq -r '.tool_input.command // empty')"
cwd="$(printf '%s' "$input" | jq -r '.cwd // empty')"
printf '%s' "$cmd" | grep -Eq '\bgit\b.*\bcommit\b' || exit 0
printf '%s' "$cmd" | grep -Eq '(--amend|--fixup|--squash|--no-edit)' && exit 0 # amend/fixup have their own rules
deny() { printf 'BLOCKED by ~/.claude/hooks/commit-shape.sh: %s\n' "$1" >&2; exit 2; }
# message text: everything after -m/-F/heredoc is too varied to parse perfectly; check the command string
printf '%s' "$cmd" | grep -Eiq 'co-authored-by|generated with|\[codex\]|\[claude\]|^codex:|^claude:' \
&& deny "no Co-Authored-By trailers or agent branding in commits"
m_count="$(printf '%s' "$cmd" | grep -oE '(^|[[:space:]])-m([[:space:]]|=)' | wc -l | tr -d ' ')"
has_body=0
[ "$m_count" -ge 2 ] && has_body=1
printf '%s' "$cmd" | grep -Eq '(^|[[:space:]])(-F|--file)[[:space:]=]|<<' && has_body=1
# a single -m whose quoted text spans lines (the command string itself contains a newline)
case "$cmd" in *