#!/bin/sh
# changelog 发布门禁(推送前强制校验)
# 启用(每台机一次): git config core.hooksPath .githooks
# 拦截目标: 接口类 changelog 未部署测试服(backend_status != deployed)就推送给前端,
#          以及文件名/frontmatter 结构违规。规则实现见 scripts/validate-changelog-*.mjs。
zero=0000000000000000000000000000000000000000
status=0
while read local_ref local_sha remote_ref remote_sha; do
  # 删除远端分支的推送没有本地内容可校验
  [ "$local_sha" = "$zero" ] && continue
  if [ "$remote_sha" = "$zero" ]; then
    base=$(git rev-parse --verify origin/main 2>/dev/null) || continue
  else
    base=$remote_sha
  fi
  [ "$base" = "$local_sha" ] && continue
  node scripts/validate-changelog-filenames.mjs --base "$base" --head "$local_sha" || status=1
  node scripts/validate-changelog-frontmatter.mjs --base "$base" --head "$local_sha" || status=1
done
if [ "$status" -ne 0 ]; then
  echo "" >&2
  echo "推送被 changelog 发布门禁拦截:接口类条目必须测试服已部署+实测(backend_status=deployed)后才能推送给前端。" >&2
  echo "修正文件后重试;规则详见 BACKEND_CHANGELOG_DELIVERY_GUIDE.md §2.1。" >&2
fi
exit $status
