#!/usr/bin/env node /** * 比较 ledger.json 与 MoviePilot 目标提交,列出需要跟随移植的上游变化。 * * node scripts/mp-sync/check.mjs [--mp ] [--to <目标提交,默认 HEAD>] [--json] * * 输出:已变更 / 已删除 / 未变化 的 ledger 文件,监视目录(spider/、parser/)中 ledger 未收录的新文件, * 以及逐文件 `git diff` 命令。存在变化时退出码为 1(可用于 CI 或定时提醒)。 * 浅克隆需要先取目标提交:git -C fetch --depth=1 origin (或 fetch --unshallow)。 */ import { readFileSync } from "node:fs"; import { DEFAULT_MP_REPO, LEDGER_PATH, listUpstreamDir, parseArgs, resolveCommit, upstreamHash } from "./lib.mjs"; const args = parseArgs(process.argv.slice(2)); const repo = args.mp ?? DEFAULT_MP_REPO; const target = resolveCommit(repo, args.to ?? "HEAD"); const ledger = JSON.parse(readFileSync(args.ledger ?? LEDGER_PATH, "utf8")); const changed = []; const deleted = []; const unchanged = []; for (const e of ledger.entries) { const h = upstreamHash(repo, target, e.upstream); if (h === null) deleted.push(e); else if (h !== e.sha256) changed.push({ ...e, newSha256: h }); else unchanged.push(e); } const known = new Set(ledger.entries.map((e) => e.upstream)); const added = []; for (const dir of ledger.watchDirs ?? []) { for (const f of listUpstreamDir(repo, target, dir)) if (!known.has(f)) added.push(f); } const short = (c) => c.slice(0, 7); if (args.json) { console.log(JSON.stringify({ target, changed, deleted, added, unchanged: unchanged.length }, null, 2)); } else { console.log(`MoviePilot 目标提交 ${short(target)}(ledger ${ledger.entries.length} 个文件)\n`); const section = (title, list, fmt) => { console.log(`## ${title}(${list.length})`); for (const x of list) console.log(fmt(x)); console.log(""); }; section("已变更", changed, (e) => [ `- ${e.upstream} (${short(e.commit)} → ${short(target)})`, ` 本项目:${e.ours.join(", ")}`, ` git -C ${repo} diff ${short(e.commit)} ${short(target)} -- ${e.upstream}`, ].join("\n"), ); section("已删除 / 改名", deleted, (e) => [ `- ${e.upstream}(本项目:${e.ours.join(", ")})`, ` git -C ${repo} log --oneline --follow ${short(e.commit)}..${short(target)} -- ${e.upstream}`, ` git -C ${repo} diff --stat -M ${short(e.commit)} ${short(target)} -- ${e.upstream.replace(/\/[^/]+$/, "/")}`, ].join("\n"), ); section("监视目录新增(ledger 未收录)", added, (f) => `- ${f}\n git -C ${repo} show ${short(target)}:${f}`); console.log(`未变化:${unchanged.length}`); if (changed.length || deleted.length || added.length) { console.log( "\n处理流程:按 diff 增量移植 → 把对应源码文件头 `@mp-source ... @ <提交>` 改为新提交(新文件新增标签)→ node scripts/mp-sync/build-ledger.mjs --commit <新提交> → 测试。", ); } } process.exit(changed.length || deleted.length || added.length ? 1 : 0);