upstream-admission.mjs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { execFile } from "node:child_process";
  2. import { promisify } from "node:util";
  3. import { readFile } from "node:fs/promises";
  4. import { resolve } from "node:path";
  5. import { gitFileNames } from "./git-file-names.mjs";
  6. import { readUpstreamSnapshot } from "./upstream-snapshot.mjs";
  7. const exec = promisify(execFile);
  8. async function requireMergedCommit(cwd, commit) {
  9. try {
  10. await exec("git", ["merge-base", "--is-ancestor", commit, "HEAD"], { cwd });
  11. } catch (cause) {
  12. // --no-commit 的合并尚未成为 HEAD 祖先;只接受 Git 当前正在合入的父提交,避免先提交才可检查。
  13. let mergeHeads = [];
  14. try {
  15. const { stdout } = await exec("git", ["rev-parse", "--git-path", "MERGE_HEAD"], { cwd });
  16. mergeHeads = (await readFile(resolve(cwd, stdout.trim()), "utf8")).trim().split(/\s+/);
  17. } catch {
  18. // 没有进行中的合并时,继续按 HEAD 祖先约束拒绝。
  19. }
  20. for (const mergeHead of mergeHeads) {
  21. try {
  22. await exec("git", ["merge-base", "--is-ancestor", commit, mergeHead], { cwd });
  23. return;
  24. } catch {
  25. // 多父合并逐个核对,不能把任意本地提交当作已合入。
  26. }
  27. }
  28. throw new Error(`上游准入 commit 必须存在且已合入 HEAD 或当前 MERGE_HEAD:${commit}`, {
  29. cause,
  30. });
  31. }
  32. }
  33. const diffOptions = ["--no-ext-diff", "--no-textconv", "--no-renames", "--name-only", "-z"];
  34. /** lint 的存量行数只按当前 Git 两份快照判断,不把一次准入变成永久路径豁免。 */
  35. export async function pristineUpstreamFiles(cwd, admissions, snapshotPath = null) {
  36. if (snapshotPath) return (await readUpstreamSnapshot(cwd, snapshotPath)).accepted;
  37. const accepted = new Set();
  38. const tracked = new Set(await gitFileNames(cwd, ["ls-files", "-z"]));
  39. for (const { commit, previousHead } of admissions) {
  40. await Promise.all([requireMergedCommit(cwd, commit), requireMergedCommit(cwd, previousHead)]);
  41. const [files, workingDifference, stagedDifference] = await Promise.all([
  42. gitFileNames(cwd, ["ls-tree", "-r", "--name-only", "-z", commit, "--"]),
  43. gitFileNames(cwd, ["diff", ...diffOptions, commit, "--"]),
  44. gitFileNames(cwd, ["diff", "--cached", ...diffOptions, commit, "--"]),
  45. ]);
  46. const different = new Set([...workingDifference, ...stagedDifference]);
  47. for (const file of files) {
  48. if (tracked.has(file) && !different.has(file)) accepted.add(file);
  49. }
  50. }
  51. return accepted;
  52. }
  53. /**
  54. * 固定上游只接纳原样引入的文件,不改变实际 push 的比较范围。
  55. * 同时比暂存区和工作区:只比工作区会把“暂存修改后恢复文件”的提交误判为上游原样。
  56. */
  57. export async function applyUpstreamAdmission(
  58. cwd,
  59. { base, candidates, untracked, admissions, eligibleHistoryFiles, snapshotPath = null },
  60. ) {
  61. if (admissions.length === 0 && !snapshotPath) return candidates;
  62. const required = new Set(candidates);
  63. const introducedFiles = new Set();
  64. const accepted = new Set();
  65. const untrackedSet = new Set(untracked);
  66. let historical = new Set();
  67. const [workingChanges, stagedChanges, tracked] = await Promise.all([
  68. gitFileNames(cwd, ["diff", ...diffOptions, "--diff-filter=ACMRT", base, "--"]),
  69. gitFileNames(cwd, ["diff", "--cached", ...diffOptions, "--diff-filter=ACMRT", base, "--"]),
  70. gitFileNames(cwd, ["ls-files", "-z"]),
  71. ]);
  72. const changed = new Set([...workingChanges, ...stagedChanges]);
  73. const trackedSet = new Set(tracked);
  74. if (snapshotPath) {
  75. const snapshot = await readUpstreamSnapshot(cwd, snapshotPath);
  76. historical = snapshot.historical;
  77. for (const file of snapshot.introduced) {
  78. if (eligibleHistoryFiles && !eligibleHistoryFiles.has(file)) continue;
  79. if (!trackedSet.has(file) || untrackedSet.has(file)) continue;
  80. introducedFiles.add(file);
  81. if (changed.has(file)) required.add(file);
  82. }
  83. for (const file of snapshot.accepted) accepted.add(file);
  84. }
  85. for (const { commit, previousHead } of admissions) {
  86. await Promise.all([requireMergedCommit(cwd, commit), requireMergedCommit(cwd, previousHead)]);
  87. const [introduced, files, workingDifference, stagedDifference] = await Promise.all([
  88. gitFileNames(cwd, ["diff", ...diffOptions, "--diff-filter=A", previousHead, commit, "--"]),
  89. gitFileNames(cwd, ["ls-tree", "-r", "--name-only", "-z", commit, "--"]),
  90. gitFileNames(cwd, ["diff", ...diffOptions, commit, "--"]),
  91. gitFileNames(cwd, ["diff", "--cached", ...diffOptions, commit, "--"]),
  92. ]);
  93. const different = new Set([...workingDifference, ...stagedDifference]);
  94. for (const file of introduced) {
  95. if (eligibleHistoryFiles && !eligibleHistoryFiles.has(file)) continue;
  96. if (!trackedSet.has(file) || untrackedSet.has(file)) continue;
  97. introducedFiles.add(file);
  98. // 首次推送后不再是新增路径,后续编辑仍须退出存量准入并登记模块。
  99. if (changed.has(file)) required.add(file);
  100. }
  101. // 后续上游可原样更新先前引入的路径;仍只对准入历史中的新增路径开放,不扩张到普通旧文件。
  102. for (const file of files) if (!different.has(file)) accepted.add(file);
  103. }
  104. return [...required].filter(
  105. (file) => !historical.has(file) && (!introducedFiles.has(file) || !accepted.has(file)),
  106. );
  107. }