check-workspace-freshness.mjs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 =
  22. maxBehindMainIndex >= 0 ? Number(args[maxBehindMainIndex + 1]) : 50;
  23. if (!Number.isInteger(maxBehindMain) || maxBehindMain < 0) {
  24. console.error("[freshness] --max-behind-main 需要一个非负整数");
  25. process.exit(2);
  26. }
  27. const doFetch = !args.includes("--no-fetch");
  28. async function git(...gitArgs) {
  29. const { stdout } = await execFile("git", gitArgs, { encoding: "utf8" });
  30. return stdout.trim();
  31. }
  32. if (doFetch) {
  33. await git("fetch", "origin", "--prune");
  34. }
  35. const branch = await git("rev-parse", "--abbrev-ref", "HEAD");
  36. let upstream = null;
  37. try {
  38. upstream = await git("rev-parse", "--abbrev-ref", "@{upstream}");
  39. } catch {
  40. console.warn(
  41. `[freshness] ${branch} 没有远端跟踪分支,跳过 behind-remote 检查(是否忘了 push -u?)`,
  42. );
  43. }
  44. const failures = [];
  45. if (upstream) {
  46. const behindRemote = Number(await git("rev-list", "--count", `HEAD..${upstream}`));
  47. if (behindRemote > 0) {
  48. failures.push(
  49. `${branch} 落后 ${upstream} ${behindRemote} 个提交:先 git merge --ff-only ${upstream}`,
  50. );
  51. }
  52. }
  53. let mainReport = "";
  54. try {
  55. await git("rev-parse", "--verify", "origin/main^{commit}");
  56. const aheadMain = Number(
  57. await git("rev-list", "--count", `origin/main..HEAD`),
  58. );
  59. const behindMain = Number(
  60. await git("rev-list", "--count", `HEAD..origin/main`),
  61. );
  62. mainReport = `相对 origin/main:ahead ${aheadMain} / behind ${behindMain}(阈值 ${maxBehindMain})`;
  63. if (behindMain > maxBehindMain) {
  64. const message = `落后 origin/main ${behindMain} 个提交,超过阈值 ${maxBehindMain}`;
  65. if (aheadMain === 0) {
  66. failures.push(`${message}:git merge --ff-only origin/main 或重建分支`);
  67. } else {
  68. console.warn(
  69. `[freshness] 警告:${message}。这是特性/MR 分支(ahead ${aheadMain}),` +
  70. `分叉本身正常;若要跟主线对齐请先确认 MR 状态(冻结中的 Draft MR 不要盲目 rebase)。`,
  71. );
  72. }
  73. }
  74. } catch {
  75. console.warn("[freshness] 仓库没有 origin/main,跳过 main 距离检查。");
  76. }
  77. if (failures.length > 0) {
  78. console.error(`[freshness] 基线过期,拒绝在旧架构上开工(当前分支:${branch}):`);
  79. for (const failure of failures) console.error(` - ${failure}`);
  80. process.exit(1);
  81. }
  82. console.log(
  83. `[freshness] 基线新鲜:${branch}${upstream ? `(与 ${upstream} 同步)` : ""}${
  84. mainReport ? `,${mainReport}` : ""
  85. }`,
  86. );