#!/usr/bin/env sh
set -eu

msg_file="$1"
subject_line="$(head -n1 "$msg_file" | tr -d '\r')"

if printf '%s' "$subject_line" | grep -Eq '^(feat|fix|docs|style|refactor|perf|test|build|ci|chore)(!)?: .+|^Merge .+|^Revert ".+"$|^(fixup|squash|amend)! .+'; then
  exit 0
fi

cat <<'ERR' >&2
Invalid commit message subject.

Expected Conventional Commits format:
  <type>: <description>
or
  <type>!: <description>

Examples:
  feat: add check mode
  fix!: handle nested comments

Allowed types:
  feat, fix, docs, style, refactor, perf, test, build, ci, chore

Also allowed:
  Git automatic merge subjects (e.g. "Merge branch 'main'")
  Git revert subjects (e.g. "Revert \"feat: add check mode\"")
  Git autosquash subjects (e.g. "fixup! feat: add check mode")
ERR

exit 1
