scope.mjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { resolve } from "node:path";
  2. import { git, run } from "./process.mjs";
  3. import { checkPushWorktree } from "../check-push-worktree.mjs";
  4. async function commit(root, ref) {
  5. try {
  6. return (await git(root, ["rev-parse", "--verify", "--end-of-options", `${ref}^{commit}`]))
  7. .toString()
  8. .trim();
  9. } catch (error) {
  10. throw new Error(`无法解析基线或提交 ${ref}:${error.message}`, { cause: error });
  11. }
  12. }
  13. async function isAncestor(root, ancestor, head) {
  14. const check = await run("git", ["merge-base", "--is-ancestor", ancestor, head], {
  15. cwd: root,
  16. allowFailure: true,
  17. });
  18. return check.code === 0;
  19. }
  20. // rebase / force-push 后远端 SHA 不再是 HEAD 的祖先;直接拿它当基线会让 Vitest
  21. // 找不到公共祖先而退化成全量选择。回退到双方 merge-base,永远给官方选择一个
  22. // 真实分叉点(最坏也只是比严格推送范围多选 staging 侧的新增改动,不会全量)。
  23. async function resolvePushBase(root, head, remote) {
  24. if (await isAncestor(root, remote, head)) {
  25. return remote;
  26. }
  27. return (await git(root, ["merge-base", remote, head])).toString().trim();
  28. }
  29. async function firstPushBase(root, head) {
  30. // 首次推送没有 remote SHA;只使用已确认的集成分支,缺失时严格报错,不猜测其他分支。
  31. const staging = await commit(root, "origin/staging");
  32. return (await git(root, ["merge-base", staging, head])).toString().trim();
  33. }
  34. /** 只解析比较基线;改动文件与关联测试全部由 Vitest 官方选择。 */
  35. export async function resolveScope(root, options = {}, env = process.env) {
  36. if (options.scope && !["worktree", "push"].includes(options.scope))
  37. throw new Error(`未知范围:${options.scope}`);
  38. const explicit = options.base ?? env.ZCODE_AFFECTED_TEST_BASE ?? env.ZCODE_PRE_PUSH_BASE;
  39. if (options.files && (options.scope === "push" || explicit))
  40. throw new Error("--files 不能与 push 或 --base 混用");
  41. if (options.files)
  42. return { files: [...new Set(options.files.map((file) => resolve(root, file)))], bases: [true] };
  43. if (options.scope !== "push") return { bases: [explicit ? await commit(root, explicit) : true] };
  44. await checkPushWorktree(root, env);
  45. const head = await commit(root, "HEAD");
  46. const entries = (env.ZCODE_PRE_PUSH_STDIN ?? "")
  47. .trim()
  48. .split(/\r?\n/u)
  49. .filter(Boolean)
  50. .map((line) => line.split(/\s+/u));
  51. const bases = [];
  52. if (entries.length) {
  53. for (const entry of entries) {
  54. if (/^0+$/u.test(entry[1])) continue;
  55. // hook 曾直接使用 remote SHA,导致显式基线变量失效;校验目标后优先使用调用方指定基线。
  56. bases.push(
  57. explicit
  58. ? await commit(root, explicit)
  59. : /^0+$/u.test(entry[3])
  60. ? await firstPushBase(root, head)
  61. : await resolvePushBase(root, head, await commit(root, entry[3])),
  62. );
  63. }
  64. return { bases: [...new Set(bases)], deletedOnly: !bases.length };
  65. }
  66. if (explicit) return { bases: [await commit(root, explicit)] };
  67. const upstream = await run("git", ["rev-parse", "--verify", "@{upstream}^{commit}"], {
  68. cwd: root,
  69. allowFailure: true,
  70. });
  71. return {
  72. bases: [
  73. upstream.code === 0
  74. ? await resolvePushBase(root, head, upstream.stdout.toString().trim())
  75. : await firstPushBase(root, head),
  76. ],
  77. };
  78. }