#!/usr/bin/env bash
# Enforce conventional commit format.
# https://www.conventionalcommits.org/en/v1.0.0/

commit_msg_file="$1"
commit_msg=$(head -1 "$commit_msg_file")

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

# Conventional commit pattern: type(scope): description  OR  type: description
pattern='^(feat|fix|docs|chore|refactor|test|ci|style|perf|build|revert)(\(.+\))?: .+'

if ! echo "$commit_msg" | grep -qE "$pattern"; then
  echo "commit-msg: invalid commit message format"
  echo ""
  echo "  Expected: type(scope): description"
  echo "  Examples: feat(shell): add fzf integration"
  echo "            fix: resolve null pointer in parser"
  echo "            docs(miles): update deployment runbook"
  echo ""
  echo "  Types: feat fix docs chore refactor test ci style perf build revert"
  echo ""
  echo "  Your message: $commit_msg"
  exit 1
fi
