#!/usr/bin/env bash
# Enforces Conventional Commits (https://www.conventionalcommits.org/) so
# release-plz can correctly infer semver bumps from commit history. Mirrors
# the check in .github/workflows/ci.yml's commitlint job.
commit_msg_file="$1"
subject=$(head -n1 "$commit_msg_file")
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: message does not follow Conventional Commits format:"
    echo "  \"$subject\""
    echo "Expected: <type>(<scope>)!: <description>"
    echo "  type one of: feat fix docs style refactor perf test build ci chore revert"
    echo "  example: feat(vector): add clamp_length"
    exit 1
fi
