#!/bin/sh
# pre-commit — 从 fragment 源重建 changelog 视图并纳入本次提交。
# 保证每个提交里的 full/recent/headlines/by-author 与 entries/ 严格一致。
# 找不到 python 时静默跳过，不阻塞提交。

ROOT=$(git rev-parse --show-toplevel 2>/dev/null) || exit 0
cd "$ROOT" || exit 0

BUILD=".cursor/changelog/tools/changelog_build.py"
[ -f "$BUILD" ] || exit 0

if command -v python >/dev/null 2>&1; then PY=python
elif command -v python3 >/dev/null 2>&1; then PY=python3
else exit 0
fi

PYTHONIOENCODING=utf-8 "$PY" "$BUILD" >/dev/null 2>&1 || exit 0

git add \
  .cursor/changelog/changelog-full.md \
  .cursor/changelog/changelog-recent.md \
  .cursor/changelog/changelog-headlines.md \
  .cursor/changelog/changelog-by-author.md 2>/dev/null || true

exit 0
