#!/bin/sh
# Validiert Commit-Messages nach Conventional Commits
# https://www.conventionalcommits.org/

commit_msg_file="$1"

first_line=$(sed '/^#/d;/^$/d' "$commit_msg_file" | head -n 1)

# Merge/Revert von Git erlauben
case "$first_line" in
  Merge\ *|Revert\ *)
    exit 0
    ;;
esac

pattern='^(feat|fix|docs|style|refactor|perf|test|build|ci|chore)(\([a-z0-9._-]+\))?!?: .+'

if ! printf '%s\n' "$first_line" | grep -qE "$pattern"; then
  echo >&2 "❌ Commit-Message entspricht nicht Conventional Commits."
  echo >&2 ""
  echo >&2 "  Format:  type(scope): description"
  echo >&2 "  Beispiel: feat(products): hide vpc in navigation"
  echo >&2 ""
  echo >&2 "  Erlaubte types: feat, fix, docs, style, refactor, perf, test, build, ci, chore"
  echo >&2 "  Überspringen: git commit --no-verify"
  exit 1
fi

exit 0
