#!/usr/bin/env bash
# Enforce docs/conventions.md on every commit message.
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

pattern='^(feat|fix|refactor|chore|docs|test|style)(\([a-z0-9-]+\))?: [a-z].*[^.]$'
if ! [[ "$first_line" =~ $pattern ]]; then
  echo "commit-msg: '$first_line'" >&2
  echo "  expected '<type>(<scope>): <imperative summary>', lowercase start, no trailing period" >&2
  echo "  types: feat fix refactor chore docs test style (see docs/conventions.md)" >&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 docs/conventions.md)" >&2
  exit 1
fi
