#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(git rev-parse --show-toplevel)"
cd "${ROOT_DIR}"
# shellcheck source=_common.sh
source "${ROOT_DIR}/.githooks/_common.sh"

COMMIT_MSG_FILE="${1:-}"

if [[ -z "${COMMIT_MSG_FILE}" ]]; then
  echo "[commit-msg] missing commit message file path."
  exit 1
fi

if [[ -f "${ROOT_DIR}/.git/MERGE_HEAD" ]]; then
  exit 0
fi

if ! run_commitlint "${ROOT_DIR}" "${COMMIT_MSG_FILE}"; then
  echo "[commit-msg] 提交信息不符合规范，详见 docs/convention/COMMIT_CONVENTION.md"
  echo "[commit-msg] 示例: feat(auth): add SMS login support"
  echo "[commit-msg] 跳过校验: git commit --no-verify"
  exit 1
fi

exit 0
