check-workspace-freshness.mjs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env node
  2. // 开工前基线新鲜度检查 —— 当前分支落后自己的远端、或(非特性分支时)落后
  3. // origin/main 超阈值,直接失败,防止在旧架构上分析、写测试、修已经消失的问题。
  4. //
  5. // 背景:本地 zcode-cua 曾落后 origin/main 140 个提交(本地 Skill 仍
  6. // 558 行、主线已收敛到 128 行),z-code 集成分支曾落后自己的远端 29 个提交,都曾在
  7. // 旧基线上开工。任何会话开始前先跑本脚本(zcode-cua 仓库用它的 python 等价物)。
  8. //
  9. // 判定规则:
  10. // 1) 落后自己的远端跟踪分支(任何数量)→ 失败:先 git merge --ff-only <upstream>。
  11. // 2) ahead==0 且落后 origin/main 超阈值 → 失败:本地 main 类分支纯过期。
  12. // 3) ahead>0(特性/MR 分支)且落后 origin/main 超阈值 → 警告不失败:分叉是正常的,
  13. // 但数字会打出来,由你决定是否 rebase(有未合并的草稿变更时不要盲目 rebase)。
  14. //
  15. // 用法:node scripts/check-workspace-freshness.mjs [--max-behind-main 50] [--no-fetch]
  16. import { execFile as execFileCallback } from "node:child_process";
  17. import { promisify } from "node:util";
  18. const execFile = promisify(execFileCallback);
  19. const args = process.argv.slice(2);
  20. const maxBehindMainIndex = args.indexOf("--max-behind-main");
  21. const maxBehindMain = maxBehindMainIndex >= 0 ? Number(args[maxBehindMainIndex + 1]) : 50;
  22. if (!Number.isInteger(maxBehindMain) || maxBehindMain < 0) {
  23. console.error("[freshness] --max-behind-main 需要一个非负整数");
  24. process.exit(2);
  25. }
  26. const doFetch = !args.includes("--no-fetch");
  27. async function git(...gitArgs) {
  28. const { stdout } = await execFile("git", gitArgs, { encoding: "utf8" });
  29. return stdout.trim();
  30. }
  31. if (doFetch) {
  32. await git("fetch", "origin", "--prune");
  33. }
  34. const branch = await git("rev-parse", "--abbrev-ref", "HEAD");
  35. let upstream = null;
  36. try {
  37. upstream = await git("rev-parse", "--abbrev-ref", "@{upstream}");
  38. } catch {
  39. console.warn(
  40. `[freshness] ${branch} 没有远端跟踪分支,跳过 behind-remote 检查(是否忘了 push -u?)`,
  41. );
  42. }
  43. const failures = [];
  44. if (upstream) {
  45. const behindRemote = Number(await git("rev-list", "--count", `HEAD..${upstream}`));
  46. if (behindRemote > 0) {
  47. failures.push(
  48. `${branch} 落后 ${upstream} ${behindRemote} 个提交:先 git merge --ff-only ${upstream}`,
  49. );
  50. }
  51. }
  52. let mainReport = "";
  53. try {
  54. await git("rev-parse", "--verify", "origin/main^{commit}");
  55. const aheadMain = Number(await git("rev-list", "--count", `origin/main..HEAD`));
  56. const behindMain = Number(await git("rev-list", "--count", `HEAD..origin/main`));
  57. mainReport = `相对 origin/main:ahead ${aheadMain} / behind ${behindMain}(阈值 ${maxBehindMain})`;
  58. if (behindMain > maxBehindMain) {
  59. const message = `落后 origin/main ${behindMain} 个提交,超过阈值 ${maxBehindMain}`;
  60. if (aheadMain === 0) {
  61. failures.push(`${message}:git merge --ff-only origin/main 或重建分支`);
  62. } else {
  63. console.warn(
  64. `[freshness] 警告:${message}。这是特性/MR 分支(ahead ${aheadMain}),` +
  65. `分叉本身正常;若要跟主线对齐请先确认 MR 状态(有未合并的草稿变更时不要盲目 rebase)。`,
  66. );
  67. }
  68. }
  69. } catch {
  70. console.warn("[freshness] 仓库没有 origin/main,跳过 main 距离检查。");
  71. }
  72. if (failures.length > 0) {
  73. console.error(`[freshness] 基线过期,拒绝在旧架构上开工(当前分支:${branch}):`);
  74. for (const failure of failures) console.error(` - ${failure}`);
  75. process.exit(1);
  76. }
  77. console.log(
  78. `[freshness] 基线新鲜:${branch}${upstream ? `(与 ${upstream} 同步)` : ""}${
  79. mainReport ? `,${mainReport}` : ""
  80. }`,
  81. );