#!/usr/bin/env bash
# {{PROJECT}} commit-msg hook — enforce Conventional Commits.
# Release tooling parses these; a wrong type breaks versioning. Don't bypass.
set -euo pipefail

msg_file="$1"
subject="$(head -1 "$msg_file")"

# allow merge/revert/fixup commits
case "$subject" in
  "Merge "*|"Revert "*|"fixup! "*|"squash! "*) exit 0 ;;
esac

# type(scope)!: description   — scope optional, ! optional
pattern='^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\([a-z0-9_.-]+\))?!?: .+'
if ! printf '%s' "$subject" | grep -qE "$pattern"; then
  echo "✗ commit-msg: subject does not follow Conventional Commits."
  echo "  got:      $subject"
  echo "  expected: <type>(scope): description   (type ∈ feat|fix|docs|refactor|perf|test|build|ci|chore|revert)"
  exit 1
fi
exit 0
