#!/usr/bin/env bash
# Conventional Commits. Not cosmetic: python-semantic-release parses these to
# decide the version bump, so a malformed type silently ships nothing.
set -uo pipefail

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

# Let the tooling's own commits and in-progress rebases through untouched.
[[ "$subject" =~ ^(Merge|Revert|fixup!|squash!) ]] && exit 0
[[ "$subject" =~ ^chore\(release\): ]] && exit 0

types='feat|fix|docs|test|chore|refactor|perf|ci|build|style|revert'
if [[ ! "$subject" =~ ^($types)(\([a-z0-9./_-]+\))?!?:\ .+ ]]; then
  cat >&2 <<EOF
commit-msg: not a Conventional Commit.

  got:      $subject
  expected: <type>(<optional scope>)<optional !>: <imperative summary>
  types:    ${types//|/, }

  feat: -> minor bump + release      fix: -> patch bump + release
  feat! or "BREAKING CHANGE:" in the body -> major bump
  everything else -> no release

See CONTRIBUTING.md §3.
EOF
  exit 1
fi

if [[ ${#subject} -gt 72 ]]; then
  echo "commit-msg: subject is ${#subject} chars; keep it under 72." >&2
  exit 1
fi
