#!/usr/bin/env bash

# shellcheck disable=SC1091
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/_lib.sh"

if itcase_is_ci; then
  exit 0
fi

branch="$(itcase_current_branch)"

if ! itcase_is_release_branch "$branch"; then
  exit 0
fi

commit_msg=$(git log --format=%B -n 1 2>/dev/null || echo '')

if [ -z "$commit_msg" ]; then
  exit 0
fi

if echo "$commit_msg" | grep -qE "^(major|minor|patch).*:"; then
  exit 0
fi

# Пропускаем мерж-коммиты
if echo "$commit_msg" | grep -qE "^Merge (branch|commit|remote-tracking)|^(Merge pull request|Squash and merge|Rebase and merge)"; then
  exit 0
fi

echo "$commit_msg" | npx commitlint
