#!/bin/sh
# 来伊份团队 commit-msg 钩子
# 对应《来伊份命名规范 §7》Commit 命名格式
# 对应《来伊份命名规范 §7》Commit 自动化校验
# 由 lyf-rules 自动生成

commit_message=$(cat "$1")

# 提取 commit 正文（第一行）
message=$(echo "$commit_message" | head -1)

# 格式校验：<type>(<scope>): <description>
# type ∈ {feat, fix, refactor, docs, test, chore, perf, ci}
# scope 可选（字母/数字/下划线/短横线），description 1-72 字符
commit_regex='^(feat|fix|refactor|docs|test|chore|perf|ci)(\([a-zA-Z0-9_-]+\))?: .{1,72}$'

if echo "$message" | grep -qE "$commit_regex"; then
  exit 0
fi

echo ""
echo "❌ [来伊份] Commit 格式错误"
echo ""
echo "   必须符合：<type>(<scope>): <description>"
echo ""
echo "   type 必须是以下之一："
echo "     feat | fix | refactor | docs | test | chore | perf | ci"
echo ""
echo "   scope 可选（如 order、payment、deps），description 不超过 72 字符"
echo ""
echo "   正确示例："
echo "     feat(order): 新增退款功能"
echo "     fix: 修复空指针异常"
echo "     refactor(payment): 提取支付策略"
echo "     chore(deps): 升级 spring-boot 3.2"
echo ""
echo "   实际输入：$message"
echo ""

exit 1