#!/usr/bin/env bash
# Git commit-msg hook: rejects commit messages that don't follow CONVENTIONS.md.
#
# Checks:
#   - first line is `<type>(<scope>): <summary>` with a known type, an optional
#     known scope, and no period at the end
#   - no AI attribution lines
#
# Activate once per clone: git config core.hooksPath .githooks
msg_file="$1"
first_line=$(grep -v '^#' "$msg_file" | head -n1)

# Let git's own merge/revert/fixup messages through.
[[ "$first_line" =~ ^(Merge|Revert|fixup!|squash!) ]] && exit 0

types='feature|bug|enhancement|chore|docs'

scopes='ci|claude|inventory|playbooks|roles|scripts|stacks'

pattern="^($types)(\(($scopes)\))?: .*[^.]$"
if ! [[ "$first_line" =~ $pattern ]]; then
  echo "commit-msg: '$first_line'" >&2
  echo "  expected '<type>(<scope>): <summary>' with no trailing period (see CONVENTIONS.md)" >&2
  echo "  types:  ${types//|/ }" >&2
  echo "  scopes: ${scopes//|/ } (optional)" >&2
  exit 1
fi

if grep -qiE '^(Co-Authored-By:.*(claude|anthropic)|.*Generated with \[?Claude)' "$msg_file"; then
  echo "commit-msg: no AI attribution lines (see CLAUDE.md)" >&2
  exit 1
fi
