|
| 1 | +// Refuses fork changes to listed upstream files, so every upstream sync applies to them cleanly. |
| 2 | +// Usage: fork-upstream-files-check.cjs <base> <head> <files>. Judged like fork-schema-check.cjs: the |
| 3 | +// diff from the merge-base of <base> and <head>, so upstream changes that reach <base> through |
| 4 | +// fork-follow's rebase are never the branch's. Files are read as git tree entries; nothing runs. |
| 5 | + |
| 6 | +const { execFileSync } = require("node:child_process"); |
| 7 | +const { parseRawDiff } = require("./fork-schema-check.cjs"); |
| 8 | + |
| 9 | +function git(args) { |
| 10 | + return execFileSync("git", ["-c", "core.quotePath=false", ...args], { |
| 11 | + encoding: "utf8", |
| 12 | + maxBuffer: 512 * 1024 * 1024, |
| 13 | + }); |
| 14 | +} |
| 15 | + |
| 16 | +/** The tree entry of `file` in `rev` ("<mode> <type> <sha>"), or undefined when absent. */ |
| 17 | +function entryAt(rev, file) { |
| 18 | + const record = git(["ls-tree", "-z", "--full-tree", rev, "--", file]).split("\0")[0]; |
| 19 | + const match = record.match(/^(\d+ \w+ [0-9a-f]+)\t(.+)$/s); |
| 20 | + return match && match[2] === file ? match[1] : undefined; |
| 21 | +} |
| 22 | + |
| 23 | +/** The list from the config line(s): paths separated by whitespace, each once. */ |
| 24 | +const parseList = (args) => [...new Set(args.join(" ").split(/\s+/).filter(Boolean))]; |
| 25 | + |
| 26 | +/** |
| 27 | + * One reason per refused file. `listed`: the guarded paths. `changed`: paths the branch changes |
| 28 | + * (merge-base to head). `atBase`/`atHead`: a path's tree entry there (undefined when absent). |
| 29 | + */ |
| 30 | +function findUpstreamFileChanges(listed, changed, { atBase, atHead }) { |
| 31 | + const reasons = []; |
| 32 | + const touched = new Set(changed); |
| 33 | + for (const file of listed) { |
| 34 | + const base = atBase(file); |
| 35 | + // A listed file main does not have guards nothing: the list is out of date (upstream moved or |
| 36 | + // removed it). Fail closed, so the guard is never silently empty. |
| 37 | + if (base === undefined) { |
| 38 | + reasons.push( |
| 39 | + `${file}: listed, but main does not have it (upstream moved or removed it?). Update the ` + |
| 40 | + "list in .github/workflows/fork-checks.yml; only the owner may do that.", |
| 41 | + ); |
| 42 | + continue; |
| 43 | + } |
| 44 | + // A directory (or submodule) never shows up as one changed path, so edits inside it would pass. |
| 45 | + if (base.split(" ")[1] !== "blob") { |
| 46 | + reasons.push( |
| 47 | + `${file}: listed, but on main it is not a file. List files only, in ` + |
| 48 | + ".github/workflows/fork-checks.yml; only the owner may do that.", |
| 49 | + ); |
| 50 | + continue; |
| 51 | + } |
| 52 | + if (!touched.has(file)) continue; |
| 53 | + const head = atHead(file); |
| 54 | + // The branch carries main's copy (e.g. it sits on the same upstream as main, reached another |
| 55 | + // way): it changes nothing main does not already have. |
| 56 | + if (head === base) continue; |
| 57 | + const what = |
| 58 | + head === undefined |
| 59 | + ? "deletes or renames it" |
| 60 | + : head.split(" ")[2] === base.split(" ")[2] |
| 61 | + ? "changes its file mode" |
| 62 | + : "changes it"; |
| 63 | + reasons.push(`${file}: this branch ${what}`); |
| 64 | + } |
| 65 | + return reasons; |
| 66 | +} |
| 67 | + |
| 68 | +function main(argv) { |
| 69 | + const [base, head, ...rest] = argv; |
| 70 | + const listed = parseList(rest); |
| 71 | + if (!base || !head || listed.length === 0) { |
| 72 | + console.error( |
| 73 | + "Usage: fork-upstream-files-check.cjs <base> <head> <files (whitespace-separated)>", |
| 74 | + ); |
| 75 | + return 2; |
| 76 | + } |
| 77 | + const mergeBase = git(["merge-base", base, head]).trim(); |
| 78 | + const changed = parseRawDiff( |
| 79 | + git(["diff", "--raw", "-z", "--no-abbrev", "--no-renames", "--no-ext-diff", mergeBase, head]), |
| 80 | + ).map((entry) => entry.path); |
| 81 | + const reasons = findUpstreamFileChanges(listed, changed, { |
| 82 | + atBase: (file) => entryAt(base, file), |
| 83 | + atHead: (file) => entryAt(head, file), |
| 84 | + }); |
| 85 | + if (reasons.length === 0) { |
| 86 | + console.log( |
| 87 | + `No upstream file changes (${listed.length} listed files, ${changed.length} changed files checked).`, |
| 88 | + ); |
| 89 | + return 0; |
| 90 | + } |
| 91 | + console.error("Fork changes must leave these upstream files exactly as upstream has them:"); |
| 92 | + for (const reason of reasons) console.error(`- ${reason}`); |
| 93 | + console.error( |
| 94 | + "Put the change in a file of the fork's own (its own layer, toolkit or budget file). If this " + |
| 95 | + "branch sits on an older main, rebase it onto main first. Only the owner may change these " + |
| 96 | + "files: the check stays red, and the owner merges past it.", |
| 97 | + ); |
| 98 | + return 1; |
| 99 | +} |
| 100 | + |
| 101 | +module.exports = { findUpstreamFileChanges, parseList }; |
| 102 | + |
| 103 | +if (require.main === module) { |
| 104 | + process.exitCode = main(process.argv.slice(2)); |
| 105 | +} |