#!/usr/bin/env bash
# Bloque `git commit` quand HEAD est sur main ou master.
# Laisse passer `git push` (utile pour relayer dev → main une fois validé).
# Règle: global-rules.md §3 — main intouchable, travailler en feature/* depuis dev.

set -euo pipefail

input="$(cat)"
command="$(printf '%s' "$input" | jq -r '.tool_input.command // ""')"

case "$command" in
  *"git commit"*) ;;
  *) exit 0 ;;
esac

cwd="$(printf '%s' "$input" | jq -r '.tool_input.cwd // ""')"
if [ -n "$cwd" ] && [ -d "$cwd" ]; then
  branch="$(git -C "$cwd" symbolic-ref --short HEAD 2>/dev/null || echo "")"
else
  branch="$(git symbolic-ref --short HEAD 2>/dev/null || echo "")"
fi

case "$branch" in
  main|master) ;;
  *) exit 0 ;;
esac

cat >&2 <<EOF
[hook] Commit interdit sur la branche '$branch'.

Règle (global-rules.md §3) : main est intouchable.
Travailler dans une branche éphémère depuis dev :
  git checkout dev
  git checkout -b feature/<nom>   # ou fix/<nom>

Puis merger feature/* → dev. main n'est mis à jour qu'après validation totale.
EOF
exit 2
