83 lines
2.5 KiB
Bash
83 lines
2.5 KiB
Bash
#!/bin/sh
|
||
# merge-import.sh - harvest a not-yet-fragmented branch's changelog into fragments, then rebuild views.
|
||
#
|
||
# 合并旧格式分支(如 master)后用它:把对方旧格式 changelog 条目收割成 fragment
|
||
# (只补 entries/ 里还没有的 ID,绝不覆盖已有 fragment),重建四个视图,并报告丢失的 ID。
|
||
#
|
||
# Usage:
|
||
# sh .cursor/changelog/tools/merge-import.sh # harvest from working-tree (post union-merge)
|
||
# sh .cursor/changelog/tools/merge-import.sh master # harvest directly from a ref
|
||
|
||
set -e
|
||
REF="${1:-}"
|
||
export PYTHONIOENCODING=utf-8
|
||
|
||
ROOT=$(git rev-parse --show-toplevel 2>/dev/null) || { echo "not inside a git repository"; exit 1; }
|
||
cd "$ROOT"
|
||
|
||
CL=".cursor/changelog"
|
||
TOOLS="$CL/tools"
|
||
|
||
if command -v python >/dev/null 2>&1; then PY=python
|
||
elif command -v python3 >/dev/null 2>&1; then PY=python3
|
||
else echo "python not found"; exit 1; fi
|
||
|
||
ids_of() {
|
||
[ -f "$1" ] || return 0
|
||
grep -oE "^### \[(CL-[^]]+)\]" "$1" | sed -E 's/^### \[(CL-[^]]+)\]/\1/' | sort -u
|
||
}
|
||
|
||
VIEWS="changelog-full.md changelog-recent.md changelog-headlines.md"
|
||
TMP=$(mktemp -d)
|
||
trap 'rm -rf "$TMP"' EXIT
|
||
|
||
if [ -n "$REF" ]; then
|
||
echo "harvest source: ref '$REF'"
|
||
for f in $VIEWS; do
|
||
git show "$REF:$CL/$f" > "$TMP/$f" 2>/dev/null || true
|
||
done
|
||
else
|
||
echo "harvest source: working-tree (post union-merge)"
|
||
for f in $VIEWS; do
|
||
[ -f "$CL/$f" ] && cp -f "$CL/$f" "$TMP/$f"
|
||
done
|
||
fi
|
||
|
||
SRC_IDS=$(ids_of "$TMP/changelog-full.md")
|
||
SRC_COUNT=$(printf '%s\n' "$SRC_IDS" | grep -c . || true)
|
||
echo "source entries: $SRC_COUNT"
|
||
|
||
# parse source into staging fragments (temp/entries/legacy)
|
||
"$PY" "$TOOLS/migrate_changelog.py" "$TMP" >/dev/null
|
||
|
||
# existing fragment ids in the real repo (filename stem == id)
|
||
mkdir -p "$CL/entries/legacy"
|
||
added=0
|
||
if [ -d "$TMP/entries/legacy" ]; then
|
||
for frag in "$TMP/entries/legacy"/*.md; do
|
||
[ -e "$frag" ] || continue
|
||
base=$(basename "$frag")
|
||
id="${base%.md}"
|
||
if ! find "$CL/entries" -name "$base" | grep -q .; then
|
||
cp -f "$frag" "$CL/entries/legacy/$base"
|
||
added=$((added+1))
|
||
fi
|
||
done
|
||
fi
|
||
echo "new fragments imported: $added"
|
||
|
||
# rebuild views
|
||
"$PY" "$TOOLS/changelog_build.py"
|
||
|
||
# verify
|
||
NOW_IDS=$(ids_of "$CL/changelog-full.md")
|
||
missing=$(comm -23 <(printf '%s\n' "$SRC_IDS") <(printf '%s\n' "$NOW_IDS") || true)
|
||
if [ -n "$missing" ]; then
|
||
echo "WARNING: source ids missing after rebuild:"
|
||
printf ' %s\n' $missing
|
||
exit 2
|
||
fi
|
||
NOW_COUNT=$(printf '%s\n' "$NOW_IDS" | grep -c . || true)
|
||
echo "OK: all $SRC_COUNT source entries present. total now: $NOW_COUNT."
|
||
echo "next: git add -A; git commit"
|