#!/bin/sh
set -e

msg=$(head -1 "$1")

# Allow merge commits
if echo "$msg" | grep -qE '^Merge '; then
  exit 0
fi

# Conventional Commits: type(optional-scope): description
if ! echo "$msg" | grep -qE '^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))?!?: .+'; then
  echo "ERROR: commit message does not follow Conventional Commits format."
  echo ""
  echo "  Expected: <type>[optional scope]: <description>"
  echo "  Types:    feat fix docs style refactor perf test build ci chore revert"
  echo ""
  echo "  Examples:"
  echo "    feat: add WebSocket reconnection"
  echo "    fix(proxy): handle empty response body"
  echo "    feat!: redesign policy schema"
  echo ""
  echo "  Your message: $msg"
  exit 1
fi
