From 08475125416d1a95c3bb67e11a4ec1c64f5c56ec Mon Sep 17 00:00:00 2001 From: John Doe <123843277+scuttletoby@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:17:36 +0100 Subject: [PATCH 1/2] fork seams: count every seam list change from upstream, fail closed (#626, #698) A pull request's own list is now counted from upstream whenever it lists a seam line more or less often (or moves a pending file to "files"), both ways: a raised count with no copy added, or a line dropped while still in use, is refused in the pull request's own run instead of stopping the next fork-follow. The upstream fetch always takes commits only (a one-command promisor remote with partialclonefilter=tree:0), whatever the clone; two tries within 120 s in all (fork-checks' job has 5 minutes); its ref is deleted after every run. Every way the count cannot run is refused, naming why: no or malformed .github/fork-upstream.json (https:// only), a shallow clone, an unreachable or unrelated upstream, a URL whose branch holds main itself, a branch without main's upstream commit. Fork tests' two-commit run passes --no-upstream and leaves the count to fork-checks. A drop judged by a list main changed since the merge-base says to update the branch; tests pin lowerList's trimmed match and both Math.max(0, ...) wordings; docs/fork/updates.md says how a fork adopting the checker fixes its list first. Co-Authored-By: Claude Opus 5.5 --- .github/fork-upstream.json | 2 +- .github/scripts/fork-seam-check.cjs | 322 ++++++--- .github/scripts/fork-seam-check.test.cjs | 850 ++++++++++++++++++----- .github/workflows/fork-tests.yml | 6 +- docs/fork/updates.md | 43 +- 5 files changed, 934 insertions(+), 289 deletions(-) diff --git a/.github/fork-upstream.json b/.github/fork-upstream.json index 9d93a3b7a6c7c..678d8a8e92beb 100644 --- a/.github/fork-upstream.json +++ b/.github/fork-upstream.json @@ -1,4 +1,4 @@ { - "//": "Upstream's git URL. .github/scripts/fork-seam-check.cjs reads main's copy (never a pull request's) when a pull request moves a \"pending\" file to \"files\", to count the fork's lines from upstream (docs/fork/updates.md). fork-follow finds upstream as this repository's GitHub parent instead.", + "//": "Upstream's git URL. .github/scripts/fork-seam-check.cjs reads main's copy (never a pull request's) when a pull request moves a \"pending\" file to \"files\" or changes a seam's \"added\", to count the fork's lines from upstream (docs/fork/updates.md). Only https://; without this file such a pull request is refused. fork-follow finds upstream as this repository's GitHub parent instead.", "url": "https://github.com/pingdotgg/t3code.git" } diff --git a/.github/scripts/fork-seam-check.cjs b/.github/scripts/fork-seam-check.cjs index 8d10f120f1fd7..dd013121d6f78 100644 --- a/.github/scripts/fork-seam-check.cjs +++ b/.github/scripts/fork-seam-check.cjs @@ -1,7 +1,8 @@ -// Usage: [--config-ref ] [--upstream ] | --from-upstream [--after-rebase [--lowered-list ]] . +// Usage: [--config-ref ] [--upstream | --no-upstream] | --from-upstream [--after-rebase [--lowered-list ]] . // Refuses 's upstream-file changes beyond .github/fork-seams.json: a PR uses 's list (its own only for -// "pending" ceilings it must lower), and holds a "pending" file it moves to "files" to the fork lines counted from -// upstream (--upstream, else the list commit's .github/fork-upstream.json). Writes only --lowered-list's file. +// "pending" ceilings it must lower), and holds each seam entry its own list changes or moves from "pending" to the +// fork lines counted from upstream (--upstream, else the list commit's .github/fork-upstream.json). Writes only +// --lowered-list's file. const { execFileSync } = require("node:child_process"); const fs = require("node:fs"); @@ -545,11 +546,12 @@ function findLostLines(config, { atBase, atHead, upstream, afterRebase = false, /** * Spare listed "added" copies: room for a line nobody announced. From upstream, a copy the head does * not add over the base is refused (after a rebase: a note, and `lowered.added` keeps the copies in - * use). A pull request that drops copies lists exactly main's copies less those in `ownFiles`. + * use). A pull request that drops copies lists exactly main's copies less those in `ownFiles`; + * `baseFiles` (the merge-base's list) tells when main's count changed since the branch left it. */ function findSpareSeamLines( files, - { atBase, atHead, upstream, afterRebase = false, ownFiles, lowered }, + { atBase, atHead, upstream, afterRebase = false, ownFiles, baseFiles, lowered }, ) { const ceilings = []; const notes = []; @@ -582,14 +584,23 @@ function findSpareSeamLines( const own = Object.hasOwn(ownFiles, file) ? lineCounts(ownFiles[file].added.join("\n")) : new Map(); + const before = + baseFiles !== undefined && Object.hasOwn(baseFiles, file) + ? lineCounts(baseFiles[file].added.join("\n")) + : new Map(); for (const [line, count] of listed) { const dropped = (baseCounts.get(line) ?? 0) - (headCounts.get(line) ?? 0); if (dropped <= 0) continue; const want = Math.max(0, count - dropped); const has = own.get(line) ?? 0; if (has === want) continue; + // `count` is main's (the list's commit), `dropped` this branch's: right once they agree. + const stale = + baseFiles !== undefined && (before.get(line) ?? 0) !== count + ? ` (this branch's merge-base lists it ${before.get(line) ?? 0}×: update the branch from main, whose list changed since)` + : ""; ceilings.push( - `${file}: this change drops${times(dropped)} \`${line}\`, which main's list has${times(count)} under "added": ${want === 0 ? "remove it from" : `list it ${want}× under`} "added" in this change's ${CONFIG} (its list has ${has === 0 ? "none" : `${has}`}; a spare copy lets a later change add the line unannounced, too few fails the next run from upstream).`, + `${file}: this change drops${times(dropped)} \`${line}\`, which main's list has${times(count)} under "added": ${want === 0 ? "remove it from" : `list it ${want}× under`} "added" in this change's ${CONFIG} (its list has ${has === 0 ? "none" : `${has}`}; a spare copy lets a later change add the line unannounced, too few fails the next run from upstream)${stale}.`, ); } } @@ -606,32 +617,72 @@ const promotedFiles = (config, ownFiles = {}, ownPending = {}) => ); /** - * Promoted entries whose "added" is not exactly the head's fork lines counted from `upstream` (an - * upstream commit: `name` says which): a line upstream has listed (a later change could then add - * that upstream line unannounced), or a fork line left out (fork-follow's next run refuses main). - * The pull request's own checks hold only how many lines the entry lists and that the file has them. + * The seam files for which this change's own list lists some line more or less often than the + * merge-base's list (`baseFiles`; a missing entry lists nothing): a raised count with no copy added + * is spare room, a line dropped while still in use stops fork-follow, and main's list cannot tell + * either from upstream's lines, so only upstream can. A new or dropped entry that lists nothing, or a + * change to "announced" only, changes no count. `baseFiles` undefined (unreadable): every entry. */ -function findPromotedNotFork(files, ownFiles, { atUpstream, atHead, name }) { +function listEditedFiles(baseFiles, ownFiles) { + const counts = (files, file) => + files !== undefined && Object.hasOwn(files, file) + ? lineCounts(files[file].added.join("\n")) + : new Map(); + const all = new Set([...Object.keys(baseFiles ?? {}), ...Object.keys(ownFiles)]); + return [...all].filter((file) => { + if (baseFiles === undefined) return true; + const [before, after] = [counts(baseFiles, file), counts(ownFiles, file)]; + return [...new Set([...before.keys(), ...after.keys()])].some( + (line) => (before.get(line) ?? 0) !== (after.get(line) ?? 0), + ); + }); +} + +/** + * Entries of this change's own list (`ownFiles`, `ownPending`) for `files` whose "added" is not exactly + * the head's fork lines counted from `upstream` (an upstream commit: `name` says which), both ways: a + * line listed more often than the fork adds it (a later change could then add that line unannounced), + * or a fork line listed less often (fork-follow's next run refuses main). An entry the list drops + * from "files" lists nothing (or, moved back to "pending", its ceiling counts the lines). `promoted`: + * the files moved from "pending", named so in the reasons. + */ +function findListedNotFork(files, ownFiles, { atUpstream, atHead, name, promoted, ownPending }) { const ceilings = []; const times = (count) => (count === 1 ? "" : ` ${count}×`); - const where = `moved to "files" in this change's ${CONFIG}`; for (const file of files) { const head = atHead(file); if (head === undefined) continue; // Refused elsewhere. + const listedHere = Object.hasOwn(ownFiles, file); + const where = promoted?.has(file) + ? `moved to "files" in this change's ${CONFIG}` + : listedHere + ? `listed in this change's ${CONFIG}` + : `dropped from "files" in this change's ${CONFIG}`; const up = atUpstream(file); if (up === undefined) { ceilings.push( - `${file}: ${where}, but upstream (${name}) does not have it, so its fork lines cannot be counted: keep it in "pending" (upstream moved or removed it?).`, + `${file}: ${where}, but upstream (${name}) does not have it, so its fork lines cannot be counted: ${promoted?.has(file) ? 'keep it in "pending"' : "keep main's entry"} (upstream moved or removed it?).`, ); continue; } const fork = new Map(addedLines(up, head)); + if (!listedHere) { + const ceiling = + ownPending !== undefined && Object.hasOwn(ownPending, file) ? ownPending[file] : 0; + const forkLines = total([...fork]); + if (forkLines !== ceiling) { + ceilings.push( + `${file}: ${where}, but the fork adds ${forkLines} line${forkLines === 1 ? "" : "s"} over upstream (${name})${ceiling === 0 ? "" : `, not its "pending" ceiling of ${ceiling}`}: keep its entry, listing each (every fork line the file holds is listed).`, + ); + } + continue; + } const listed = lineCounts(ownFiles[file].added.join("\n")); for (const [line, count] of listed) { const over = fork.get(line) ?? 0; if (count <= over) continue; ceilings.push( - `${file}: ${where}, whose "added" lists \`${line}\`${times(count)}, but the fork adds ${over === 0 ? "no copy" : `${over}×`} over upstream (${name}): ${over === 0 ? 'remove it from "added"' : `list it ${over}×`} (a line upstream has is never a seam line).`, + `${file}: ${where}, whose "added" lists \`${line}\`${times(count)}, but the fork adds ${over === 0 ? "no copy" : `${over}×`} over upstream (${name}): ${over === 0 ? 'remove it from "added"' : `list it ${over}×`} (a line upstream has is never a seam line, and a spare copy lets a later change add the line unannounced).`, ); } for (const [line, over] of fork) { @@ -645,18 +696,36 @@ function findPromotedNotFork(files, ownFiles, { atUpstream, atHead, name }) { return ceilings; } -// Where a pull request's run keeps upstream's default branch; deleted before each fetch, so a -// failed fetch never leaves an earlier run's head to be judged by. +// Where a pull request's run keeps upstream's default branch while it finds the merge-base. A +// fetch overwrites it, a failed one refuses (an earlier run's head is never judged by), and it is +// deleted after each run: a run by hand leaves nothing behind, and no ref holds upstream's commits. const UPSTREAM_REF = "refs/fork-seam-check/upstream"; +// The upstream fetch: each attempt, and both together, well below fork-checks' 5-minute job (its +// checkout and tests take about a minute): a hung upstream is a named refusal, not a timeout. +const FETCH_ATTEMPT_MS = 60_000; +const FETCH_TOTAL_MS = 120_000; +// Only https:// (tests point a URL at a local repository with git's url..insteadOf): a +// file:// path on the runner (the checkout itself) would count the fork's lines from the fork. +const UPSTREAM_URL = /^https:\/\/[^\s\u0000-\u001f\u007f]+$/; + +/** Why `url` cannot name upstream, or undefined: one https:// URL, never an option or transport. */ +const upstreamUrlProblem = (url, where) => + typeof url === "string" && UPSTREAM_URL.test(url) + ? undefined + : `${where}: "url" must be one https:// git URL`; /** - * The upstream git URL UPSTREAM_FILE names at `rev`: { url }, {} when `rev` has no such file, or - * { problem } when it is malformed (a plain https:// or file:// URL; never an option or transport). + * The upstream git URL UPSTREAM_FILE names at `rev`: { url }, or { problem } when `rev` has no + * such file or it is malformed. Missing is a problem too: the count it allows cannot be skipped. */ function upstreamNamedAt(rev) { const text = textAt(rev, UPSTREAM_FILE); - if (text === undefined) return {}; const at = `${UPSTREAM_FILE} at ${rev.slice(0, 12)}`; + if (text === undefined) { + return { + problem: `there is no ${at} (add it, naming upstream's https:// git URL; by hand, pass --upstream )`, + }; + } let raw; try { raw = JSON.parse(text); @@ -670,87 +739,103 @@ function upstreamNamedAt(rev) { if (unknown !== undefined) { return { problem: `${at} has an unknown key ${JSON.stringify(unknown)} (known: url)` }; } - if ( - typeof raw.url !== "string" || - !/^(?:https|file):\/\/[^\s\u0000-\u001f\u007f]+$/.test(raw.url) - ) { - return { problem: `${at}: "url" must be one https:// or file:// git URL` }; - } - return { url: raw.url }; + const problem = upstreamUrlProblem(raw.url, at); + return problem === undefined ? { url: raw.url } : { problem }; } /** - * Fetches upstream's default branch from `url` (read-only, into UPSTREAM_REF) and returns - * { rev: merge-base of `main` and it }, or { problem } when it cannot (unreachable, no common - * history). Only commits and trees in a partial clone (as fork-checks' is), negotiated from `tips` - * (the checkout has no branch), so only upstream's commits since the fork's base come. + * Fetches upstream's default branch from `url` (read-only, commits only: filter tree:0, whatever + * the clone) and returns { rev: merge-base of `main` and it }, or + * { problem } when it cannot (shallow clone, unreachable, slow, no common history, or a URL whose + * branch holds `main` itself). The merge-base is in `main`'s history, so its files are read from + * the clone; negotiated from `tips` (the checkout has no branch), only upstream's commits since + * the fork's base come. `cwd`, `attemptMs` and `totalMs` are for tests. */ -function upstreamBase(url, main, tips) { +function upstreamBase( + url, + main, + tips, + { cwd, attemptMs = FETCH_ATTEMPT_MS, totalMs = FETCH_TOTAL_MS } = {}, +) { const run = (args, timeout) => execFileSync("git", args, { + cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], timeout, env: { ...process.env, GIT_TERMINAL_PROMPT: "0" }, }); - let partial = ""; - try { - partial = run(["config", "--get", "extensions.partialClone"]).trim(); - } catch { - // Not a partial clone: every object is here, so the fetch needs no filter. + // A shallow clone has no merge-base to find: say so, not "shares no commit". + if (run(["rev-parse", "--is-shallow-repository"]).trim() === "true") { + return { + problem: `this clone is shallow, so the commit main shares with upstream is not in it (fetch its whole history: git fetch --unshallow)`, + }; } + // A promisor remote for this one command only (-c), so none is left in the config: git fetches + // from it with its partialclonefilter (commits only). A --filter flag instead would write that + // filter into the clone's config. const remote = "fork-seam-upstream"; - const config = - partial === "" - ? [] - : [ - "-c", - `remote.${remote}.url=${url}`, - "-c", - `remote.${remote}.promisor=true`, - "-c", - `remote.${remote}.partialclonefilter=blob:none`, - ]; - const from = partial === "" ? url : remote; const fetch = [ - ...config, + "-c", + `remote.${remote}.url=${url}`, + "-c", + `remote.${remote}.promisor=true`, + "-c", + `remote.${remote}.partialclonefilter=tree:0`, "fetch", "--quiet", "--no-tags", "--no-write-fetch-head", "--no-recurse-submodules", ...tips.map((tip) => `--negotiation-tip=${tip}`), - from, + remote, `+HEAD:${UPSTREAM_REF}`, ]; - run(["update-ref", "-d", UPSTREAM_REF]); - let error; - // One retry: a dropped connection is not a reason to skip the check. - for (let attempt = 0; attempt < 2; attempt++) { - try { - run(fetch, 120_000); - error = undefined; - break; - } catch (caught) { - error = caught; - } - } - if (error !== undefined) { - const said = String(error.stderr ?? "") - .trim() - .split("\n") - .slice(-3) - .join(" / "); - const why = error.signal ? `no answer in 120 s (${error.signal})` : said || error.message; - return { problem: `could not fetch the default branch of ${url} (twice): ${why}` }; - } try { - return { rev: run(["merge-base", main, UPSTREAM_REF]).trim() }; - } catch { + const started = Date.now(); + let error; + // One retry, within the total: a dropped connection is not a reason to refuse. + for (let attempt = 0; attempt < 2; attempt++) { + const left = totalMs - (Date.now() - started); + if (left <= 0) break; + try { + run(fetch, Math.min(attemptMs, left)); + error = undefined; + break; + } catch (caught) { + error = caught; + } + } + if (error !== undefined) { + const said = String(error.stderr ?? "") + .trim() + .split("\n") + .slice(-3) + .join(" / "); + const why = error.signal + ? `no answer within ${Math.round(totalMs / 1000)} s in all (${error.signal})` + : said || error.message; + return { problem: `could not fetch the default branch of ${url}: ${why}` }; + } const tip = run(["rev-parse", UPSTREAM_REF]).trim(); - return { - problem: `upstream's default branch (${url}, ${tip.slice(0, 12)}) shares no commit with ${main}`, - }; + let rev; + try { + rev = run(["merge-base", main, UPSTREAM_REF]).trim(); + } catch { + return { + problem: `upstream's default branch (${url}, ${tip.slice(0, 12)}) shares no commit with ${main}`, + }; + } + // Upstream holding main whole: the URL names this fork (or a copy of it), and every fork line + // would count as upstream's. + if (rev === run(["rev-parse", `${main}^{commit}`]).trim()) { + return { + problem: `upstream's default branch (${url}, ${tip.slice(0, 12)}) holds ${main.slice(0, 12)} itself, so the URL names this fork, not its upstream`, + }; + } + return { rev }; + } finally { + run(["update-ref", "-d", UPSTREAM_REF]); } } @@ -1223,6 +1308,7 @@ function parseArgs(argv) { }; const fromUpstream = flag("--from-upstream"); const afterRebase = flag("--after-rebase"); + const noUpstream = flag("--no-upstream"); const option = (name) => { const at = args.indexOf(name); if (at === -1) return { value: undefined }; @@ -1238,14 +1324,16 @@ function parseArgs(argv) { if (args.length !== 2 || args.some((arg) => arg.startsWith("--"))) return undefined; // A pull request's run never runs from upstream; only a run from upstream has a rebase behind it. if (fromUpstream ? configRef.value !== undefined : afterRebase) return undefined; - // From upstream the base is upstream already. - if (fromUpstream && upstreamUrl.value !== undefined) return undefined; + // From upstream the base is upstream already; --no-upstream leaves no upstream to name. + if ((fromUpstream || noUpstream) && upstreamUrl.value !== undefined) return undefined; + if (fromUpstream && noUpstream) return undefined; // Only a run after a rebase lowers the list: fork-follow commits it into the rebased series. if (loweredList.value !== undefined && !afterRebase) return undefined; return { configRef: configRef.value, loweredList: loweredList.value, upstreamUrl: upstreamUrl.value, + noUpstream, fromUpstream, afterRebase, base: args[0], @@ -1257,7 +1345,7 @@ function main(argv) { const parsed = parseArgs(argv); if (!parsed) { console.error( - "Usage: fork-seam-check.cjs [--config-ref ] [--upstream ] \n" + + "Usage: fork-seam-check.cjs [--config-ref ] [--upstream | --no-upstream] \n" + " fork-seam-check.cjs --from-upstream [--after-rebase [--lowered-list ]] ", ); return 2; @@ -1297,12 +1385,18 @@ function main(argv) { let ownPending; let ownFiles; let ownLost; + let baseFiles; if (!upstream && listProblem === undefined && ownList !== undefined) { try { ({ pending: ownPending, files: ownFiles, lost: ownLost } = parseConfig(ownList)); } catch { ownPending = undefined; // The list judged it is main's; a malformed own one is said below. } + try { + baseFiles = readConfigAt(mergeBase).files; + } catch { + baseFiles = undefined; // Unreadable: every entry counts as edited (listEditedFiles). + } } const lowered = parsed.loweredList === undefined @@ -1336,48 +1430,64 @@ function main(argv) { upstream, afterRebase: parsed.afterRebase, ownFiles, + baseFiles, lowered, }); - // A move to "files" lists lines main's list cannot tell from upstream's: only upstream can. + // A move to "files", or a seam's "added" changed, lists lines main's list cannot tell from + // upstream's (a raised count with no copy added, a line dropped while still in use): only + // upstream can. Every way the count cannot run refuses (fail closed); fork-tests' run on the + // merge commit passes --no-upstream, as fork-checks' run counts it. const promoted = upstream ? [] : promotedFiles(config, ownFiles, ownPending); - const promotedCeilings = []; - const warnings = []; - if (promoted.length > 0) { - const moves = `this change moves ${promoted.join(", ")} from "pending" to "files"`; - const byHand = `whether its "added" lists exactly the fork lines is not checked here; fork-follow's next run refuses main if not (check it now: docs/fork/updates.md, "pending")`; - const named = - parsed.upstreamUrl === undefined ? upstreamNamedAt(configRef) : { url: parsed.upstreamUrl }; - if (named.problem !== undefined) { - promotedCeilings.push( - `${promoted.join(", ")}: moved to "files" in this change's ${CONFIG}, but ${named.problem}, so its fork lines cannot be counted: fix that file with a pull request to main first.`, - ); - } else if (named.url === undefined) { + const toCount = + upstream || ownFiles === undefined + ? [] + : [...new Set([...promoted, ...listEditedFiles(baseFiles, ownFiles)])].sort(); + const countedCeilings = []; + if (toCount.length > 0) { + const what = `${toCount.join(", ")}: ${promoted.length > 0 ? `this change moves ${promoted.join(", ")} from "pending" to "files"` : "this change edits their seam lines"} in its ${CONFIG}`; + const named = parsed.noUpstream + ? {} + : parsed.upstreamUrl === undefined + ? upstreamNamedAt(configRef) + : { + url: parsed.upstreamUrl, + problem: upstreamUrlProblem(parsed.upstreamUrl, "--upstream"), + }; + if (parsed.noUpstream) { console.log( printable( - `Note: ${moves}; without --upstream (and no ${UPSTREAM_FILE} at ${configRef.slice(0, 12)}), ${byHand}.`, + `Note: ${what}; --no-upstream: whether each entry lists exactly the fork lines is counted from upstream by the pull request's other run (fork-checks), not here.`, ), ); + } else if (named.problem !== undefined) { + countedCeilings.push( + `${what}, but ${named.problem}, so their fork lines cannot be counted: fix that with a pull request to main first.`, + ); } else { const found = upstreamBase(named.url, configRef, [configRef, head]); if (found.problem !== undefined) { - warnings.push(`${moves}, but ${found.problem}, so ${byHand}.`); + countedCeilings.push( + `${what}, but ${found.problem}, so their fork lines cannot be counted: run the check again, and if it still cannot, fix the cause first.`, + ); } else if (!isAncestor(found.rev, head)) { - promotedCeilings.push( - `${promoted.join(", ")}: moved to "files" in this change's ${CONFIG}, but this branch does not have upstream's commit ${found.rev.slice(0, 12)} that ${configRef.slice(0, 12)} sits on, so its fork lines cannot be counted: update the branch from its base.`, + countedCeilings.push( + `${what}, but this branch does not have upstream's commit ${found.rev.slice(0, 12)} that ${configRef.slice(0, 12)} sits on, so their fork lines cannot be counted: update the branch from its base.`, ); } else { const name = found.rev.slice(0, 12); - promotedCeilings.push( - ...findPromotedNotFork(promoted, ownFiles, { + countedCeilings.push( + ...findListedNotFork(toCount, ownFiles, { atUpstream: (file) => textAt(found.rev, file), atHead, name, + promoted: new Set(promoted), + ownPending, }), ); - if (promotedCeilings.length === 0) { + if (countedCeilings.length === 0) { console.log( printable( - `Note: ${moves}; each entry lists exactly the fork lines counted from upstream (${name}, the merge-base of ${configRef.slice(0, 12)} and ${named.url}).`, + `Note: ${what}; each entry lists exactly the fork lines counted from upstream (${name}, the merge-base of ${configRef.slice(0, 12)} and ${named.url}).`, ), ); } @@ -1386,7 +1496,7 @@ function main(argv) { } // A ceiling above the count is fixed by lowering it, not by moving lines: its own hint. A count // that rose with a rebase names the operator's step in its own text. - const ceilings = [...unlisted.ceilings, ...lost.ceilings, ...spare.ceilings, ...promotedCeilings]; + const ceilings = [...unlisted.ceilings, ...lost.ceilings, ...spare.ceilings, ...countedCeilings]; const rises = [...unlisted.rises, ...lost.rises]; // A pull request's run dates an announcement by the commit whose list judges it (main's): its // own author sets its own date, and fork-checks' base can be older than the list's commit. @@ -1431,9 +1541,6 @@ function main(argv) { ...spare.notes, ]; for (const note of notes) console.log(printable(`Note: ${note}`)); - // In a workflow, an annotation on the run (the check still passes, as without upstream). - const warn = process.env.GITHUB_ACTIONS === "true" ? "::warning::" : "Warning: "; - for (const warning of warnings) console.log(`${warn}${printable(warning)}`); if (editsList) { console.log( `Note: this change edits ${CONFIG}; it is judged by the list in ${from}, not its own. ` + @@ -1525,7 +1632,10 @@ module.exports = { findHiddenUpstreamFiles, findListedGone, findLostLines, - findPromotedNotFork, + findListedNotFork, + FETCH_ATTEMPT_MS, + FETCH_TOTAL_MS, + listEditedFiles, findSeamBreaches, findSpareSeamLines, findStaleSeamLines, @@ -1541,6 +1651,8 @@ module.exports = { sameLines, UPSTREAM_FILE, UPSTREAM_REF, + upstreamBase, + upstreamNamedAt, }; if (require.main === module) { diff --git a/.github/scripts/fork-seam-check.test.cjs b/.github/scripts/fork-seam-check.test.cjs index 529645f8b3d62..2d7a5b0620233 100644 --- a/.github/scripts/fork-seam-check.test.cjs +++ b/.github/scripts/fork-seam-check.test.cjs @@ -14,7 +14,10 @@ const { findSpareSeamLines, findListedGone, findLostLines, - findPromotedNotFork, + findListedNotFork, + FETCH_ATTEMPT_MS, + FETCH_TOTAL_MS, + listEditedFiles, findStaleSeamLines, findUnlistedChanges, forkFileTest, @@ -29,6 +32,8 @@ const { sameLines, UPSTREAM_FILE, UPSTREAM_REF, + upstreamBase, + upstreamNamedAt, } = require("./fork-seam-check.cjs"); const SCRIPT = path.join(__dirname, "fork-seam-check.cjs"); @@ -680,8 +685,16 @@ for (const [how, added] of [ } // A throwaway repository: commit({ path: text | null }) on the current branch, run(...args). +// Upstream as tests name it: only https:// is taken (a file:// path could name the checkout itself), +// so git's url..insteadOf maps these to local bare repositories, as a user's config could. +const UPSTREAM_URL = "https://upstream.test/u.git"; +const GONE_URL = "https://gone.test/u.git"; +const upstreamArgs = ["--from-upstream", "--upstream", "--no-upstream"]; + function withRepo(body) { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "fork-seams-git-")); + const upDir = `${dir}-up.git`; + let published = false; const git = (...args) => execFileSync("git", ["-C", dir, "-c", "user.name=t", "-c", "user.email=t@t", ...args], { encoding: "utf8", @@ -720,14 +733,41 @@ function withRepo(body) { ); return git("rev-parse", "HEAD").trim(); }; - const run = (...args) => spawnSync("node", [SCRIPT, ...args], { cwd: dir, encoding: "utf8" }); + // `rev` as upstream's default branch at UPSTREAM_URL. Once a test publishes one, its pull + // request runs name it (--upstream), as fork-checks' run names main's UPSTREAM_FILE. + const publish = (rev) => { + if (!published) { + execFileSync("git", ["init", "-q", "--bare", upDir]); + execFileSync("git", ["-C", upDir, "config", "uploadpack.allowFilter", "true"]); + published = true; + } + git("push", "-q", "-f", upDir, `${rev}:refs/heads/main`); + execFileSync("git", ["-C", upDir, "symbolic-ref", "HEAD", "refs/heads/main"]); + }; + const run = (...args) => + spawnSync( + "node", + [ + SCRIPT, + ...(published && !args.some((arg) => upstreamArgs.includes(arg)) + ? ["--upstream", UPSTREAM_URL] + : []), + ...args, + ], + { cwd: dir, encoding: "utf8" }, + ); + // As fork-checks runs it once main names no upstream file: no flag at all. + const runBare = (...args) => spawnSync("node", [SCRIPT, ...args], { cwd: dir, encoding: "utf8" }); // As fork-follow runs it: from an upstream commit, the list read from the head. const fromUpstream = (...args) => run("--from-upstream", ...args); try { git("init", "-q"); - body({ dir, git, commit, run, fromUpstream }); + git("config", `url.file://${upDir}.insteadOf`, UPSTREAM_URL); + git("config", `url.file://${dir}-no-such-upstream.git.insteadOf`, GONE_URL); + body({ dir, git, commit, run, runBare, fromUpstream, publish, upDir }); } finally { fs.rmSync(dir, { recursive: true, force: true }); + fs.rmSync(upDir, { recursive: true, force: true }); } } const seams = (config) => ({ [CONFIG]: JSON.stringify(config) }); @@ -857,14 +897,24 @@ test("a pull request's own list is parsed for its format (never its rules) and r assert.doesNotMatch(result.stderr, /a file of the fork's own/, what); } } - // A well-formed list passes, whatever its rules (main's list judges the change). + // A well-formed list passes, whatever its "pending" or "forkFiles" (main's list judges the + // change). git("checkout", "-q", "-f", "-B", "pr", main); - const wide = { - files: { "a.ts": { why: "mine", added: ["anything: 1,"] } }, - pending: { "z.ts": 9 }, - }; + const wide = { files: SEAM, pending: { "z.ts": 9 }, forkFiles: ["tools/"] }; const fine = commit(seams(wide), "a wider list"); assert.equal(run(main, fine).status, 0); + // A seam line it lists more often (or less) is counted from upstream (#626); main names no + // upstream here, so that is refused rather than passed unchecked. + const raised = commit( + seams({ files: { "a.ts": { why: "mine", added: ["anything: 1,"] } } }), + "a raised count", + ); + const counted = run(main, raised); + assert.equal(counted.status, 1, counted.stdout); + assert.match( + counted.stderr, + /- a\.ts: this change edits their seam lines in its .*, but there is no \.github\/fork-upstream\.json/, + ); // From upstream the head's list is the one in force: malformed, it is still a crash. const broken = commit(seams({ files: SEAM, lost: { "a.ts": 3 } }), "broken"); assert.equal(fromUpstream(main, broken).status, 3); @@ -1827,8 +1877,9 @@ test("an announcement is refused from upstream once it is older than its days, o // A pull request is judged by main's list, so a new seam line had no green path. Step 1 (a // list-only change) announces it; step 2 adds it and moves it to "added". test("a new seam line lands green in two steps: announce, then add", () => { - withRepo(({ git, commit, run, fromUpstream }) => { + withRepo(({ git, commit, run, fromUpstream, publish }) => { const upstream = commit({ ...BASE, "n.ts": UP }, "upstream"); + publish(upstream); const day = "2026-11-18T12:00:00Z"; const main = commit({ ...seams({ files: SEAM }), ...SEAMED }, "fork main", day); const announce = (since = "2026-11-18") => ({ @@ -1990,12 +2041,9 @@ test("on a pull request, an announced line in main's file is upstream's and may // An expired announcement stayed spendable in a pull request's run. Dated by main's commit, never // the pull request's (its author sets that date). test("on a pull request, a line only an expired announcement allows is refused, by main's date", () => { - withRepo(({ git, commit, run }) => { - const step1 = commit( - { ...BASE, "n.ts": UP, ...seams(ANNOUNCED()) }, - "main", - "2026-11-18T12:00:00Z", - ); + withRepo(({ git, commit, run, publish }) => { + publish(commit({ ...BASE, "n.ts": UP }, "upstream", "2026-11-18T12:00:00Z")); + const step1 = commit(seams(ANNOUNCED()), "main", "2026-11-18T12:00:00Z"); const step2 = { "n.ts": `${UP}forkNew();\n`, ...seams(ADDED) }; const later = commit({ "fork/own.ts": UP }, "main moves on", "2026-12-03T12:00:00Z"); git("checkout", "-q", "-b", "pr", later); @@ -2043,7 +2091,12 @@ test("on a pull request, a line only an expired announcement allows is refused, /- n\.ts: adds `forkNew\(\);` \(its announcement of 2026-11-18 is/, ); assert.match(doubled.stderr, /- n\.ts: adds `forkNew\(\);`\n/); - assert.doesNotMatch(doubled.stderr, /2×/); + assert.doesNotMatch(doubled.stderr, /adds 2×/); + // Its own list lists one copy where the fork adds two: counted from upstream, refused too. + assert.match( + doubled.stderr, + /- n\.ts: listed in .*, whose "added" lists `forkNew\(\);`, but the fork adds it 2× over upstream/, + ); assert.match(doubled.stderr, /A new seam line needs its own entry/); assert.match(doubled.stderr, /Announce each line named above again/); // Inside the days by main's date, a late pull request date does not refuse it. @@ -2065,9 +2118,10 @@ test("on a pull request, a line only an expired announcement allows is refused, // A future-dated announcement on main never expired in a pull request's run (only fork-follow // refused it). Dated by the commit whose list judges the pull request (--config-ref, else base). test("on a pull request, a line only a future-dated announcement allows is refused, dated by the list's commit", () => { - withRepo(({ git, commit, run }) => { + withRepo(({ git, commit, run, publish }) => { const day = "2026-11-18T12:00:00Z"; - const upstream = commit({ ...BASE, "n.ts": UP, ...seams({ files: SEAM }) }, "main", day); + publish(commit({ ...BASE, "n.ts": UP }, "upstream", day)); + const upstream = commit(seams({ files: SEAM }), "main", day); const step2 = { "n.ts": `${UP}forkNew();\n`, ...seams(ADDED) }; const runs = (main, pr) => [run(main, pr), run("--config-ref", main, main, pr)]; // Dated ahead (more than the day left for time zones): refused, naming the fix. @@ -2727,9 +2781,10 @@ test("the command line refuses a pull request that shrinks a pending file but ke // As fork-checks runs it (main's list through --config-ref), then as fork-follow's next run on main // does (from the upstream commit main sits on): a pending file made a seam passes both. test("the command line lets a pull request make a pending file a seam, and fork-follow then passes main", () => { - withRepo(({ dir, git, commit, run, fromUpstream }) => { + withRepo(({ dir, git, commit, run, fromUpstream, publish }) => { const up = "export const x = 1;\nif (a) {\n}\n"; const upstream = commit({ "p.ts": up }, "upstream"); + publish(upstream); const forked = `${up}forkA(); // Fork\nforkB(); // Fork\n`; const main = commit( { ...seams({ files: {}, pending: { "p.ts": 2 } }), "p.ts": forked }, @@ -2770,11 +2825,15 @@ test("the command line lets a pull request make a pending file a seam, and fork- grows.stderr, /- p\.ts: "pending" \(fork lines to move out\), and this change adds 1 more line than it removes\./, ); - // Which lines are the fork lines, the pull request's run cannot tell (it has no upstream): an - // upstream line listed in place of one passes there, and the run from upstream refuses it, so - // no line is hidden. The owner reviews the entry as any seam entry. + // Which lines are the fork lines only upstream can tell: an upstream line listed in place of + // one is refused in the pull request's run (counted from upstream), and from upstream. const swapped = branch("swapped", asSeam("forkA(); // Fork", "}")); - assert.equal(run("--config-ref", main, main, swapped).status, 0); + const counted = run("--config-ref", main, main, swapped); + assert.equal(counted.status, 1, counted.stdout); + assert.match( + counted.stderr, + /- p\.ts: moved to "files" .*, whose "added" lists `}`, but the fork adds no copy over upstream/, + ); const caught = fromUpstream(upstream, swapped); assert.equal(caught.status, 1); assert.match(caught.stderr, /p\.ts: adds `forkB\(\); \/\/ Fork`/); @@ -3017,12 +3076,60 @@ test("from main, a change that drops a listed seam copy drops it from its own li assert.match(judged(0)[0], /list it 1× under "added" .*\(its list has none;/); }); +// #621's surviving mutants: both `Math.max(0, …)` only showed in the wording ("-1×"). +test("a fork that removes upstream copies of a listed line, or a change that drops more copies than listed, names the right fix", () => { + const generic = (count) => ({ "a.ts": { why: "t", added: Array(count).fill("};") } }); + // From upstream: upstream has `};` twice, the fork keeps one: no copy over upstream. + const twice = { "a.ts": `${BASE["a.ts"]}};\n` }; + assert.deepEqual(spare(generic(1), twice, BASE).ceilings, [ + 'a.ts: "added" lists `};`, but the fork adds no copy over upstream; remove it from "added" (a spare copy lets a later change add the line unannounced).', + ]); + const lowered = { added: new Map() }; + spare(generic(1), twice, BASE, { afterRebase: true, lowered }); + assert.deepEqual(lowered.added.get("a.ts"), new Map([["};", 0]])); + // From main: main lists `};` once, the change drops two copies (one upstream's). + const thrice = { "a.ts": `${BASE["a.ts"]}};\n};\n` }; + const pr = (own) => + spare(generic(1), thrice, BASE, { upstream: false, ownFiles: generic(own) }).ceilings; + assert.deepEqual(pr(0), []); + assert.deepEqual(pr(1), [ + `a.ts: this change drops 2× \`};\`, which main's list has under "added": remove it from "added" in this change's ${CONFIG} (its list has 1; a spare copy lets a later change add the line unannounced, too few fails the next run from upstream).`, + ]); +}); + +// #626 minor 2: main's count comes from the list's commit, the drop from the branch; a branch +// whose merge-base lists another count is told to update, not only given a number. +test("from main, a drop judged by a list main changed since the merge-base says to update the branch", () => { + const seam = (count) => ({ "a.ts": { why: "t", added: ["x();", ...Array(count).fill("};")] } }); + const base = { "a.ts": `${BASE["a.ts"]}x();\n};\n};\n` }; + const head = { "a.ts": `${BASE["a.ts"]}x();\n};\n` }; + const pr = (mainCount, baseCount, own = 0) => + spare(seam(mainCount), base, head, { + upstream: false, + ownFiles: seam(own), + baseFiles: baseCount === undefined ? undefined : seam(baseCount), + }).ceilings; + // Main's list and the merge-base's agree: no hint. + assert.equal(pr(2, 2).length, 1); + assert.doesNotMatch(pr(2, 2)[0], /update the branch/); + assert.doesNotMatch(pr(2, undefined)[0], /update the branch/); + // Main dropped the same copy since (a duplicate fix): main asks for none, the branch's list + // (from its merge-base) keeps one, so it is told to update the branch, not only given a number. + assert.match( + pr(1, 2, 1)[0], + /drops `};`, which main's list has under "added": remove it from .* \(its list has 1; .*\) \(this branch's merge-base lists it 2×: update the branch from main, whose list changed since\)\.$/, + ); + assert.deepEqual(pr(1, 2, 0), []); +}); + test("lowerList drops spare seam copies, keeps the copies in use, and only lowers", () => { const text = `${JSON.stringify( { files: { "a.ts": { why: "w", added: ["x();", "};", "y();", "};"] }, "b.ts": { why: "w", added: ["z();"] }, + // Indented items (as a YAML or nested line is listed): matched by their trimmed text. + "c.ts": { why: "w", added: [" ...x,", "\t...x,"] }, }, }, null, @@ -3041,14 +3148,19 @@ test("lowerList drops spare seam copies, keeps the copies in use, and only lower ]), ], ["b.ts", new Map([["z();", 1]])], + ["c.ts", new Map([["...x,", 1]])], ["gone.ts", new Map([["q();", 0]])], ]), }); assert.deepEqual(JSON.parse(result.text).files, { "a.ts": { why: "w", added: ["x();", "};"] }, "b.ts": { why: "w", added: ["z();"] }, + "c.ts": { why: "w", added: [" ...x,"] }, }); - assert.deepEqual(result.changes, ['a.ts: "added" 4 -> 2 lines (spare: `y();`, `};`)']); + assert.deepEqual(result.changes, [ + 'a.ts: "added" 4 -> 2 lines (spare: `y();`, `};`)', + 'c.ts: "added" 2 -> 1 lines (spare: `...x,`)', + ]); // Nothing spare: nothing to write. A count above the list never adds a copy. const none = { pending: new Map(), @@ -3059,8 +3171,9 @@ test("lowerList drops spare seam copies, keeps the copies in use, and only lower }); test("the command line: a spare seam copy fails fork-follow's run, is lowered after a rebase, and a pull request drops its own", () => { - withRepo(({ dir, git, commit, run, fromUpstream }) => { + withRepo(({ dir, git, commit, run, fromUpstream, publish }) => { const upstream = commit(BASE, "upstream"); + publish(upstream); const main = commit({ ...seams({ files: SEAM }), ...SEAMED }, "fork main"); assert.equal(fromUpstream(upstream, main).status, 0); // A pull request moves `...fork,` out but keeps it listed: refused in its own run. @@ -3109,12 +3222,12 @@ test("the command line: a spare seam copy fails fork-follow's run, is lowered af }); }); -// A pull request that moves a "pending" file to "files" has its entry held to the fork lines -// counted from upstream (--upstream or main's UPSTREAM_FILE, fetched read-only); without, as before. -const WARNING = /^(?:Warning: |::warning::)/m; +// A pull request that moves a "pending" file to "files", or changes how often a seam's "added" +// lists a line, has those entries held to the fork lines counted from upstream (--upstream or +// main's UPSTREAM_FILE, fetched read-only, commits only). Every way that count cannot run refuses. +const reEscape = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); function withUpstream(body) { - withRepo(({ dir, git, commit, run }) => { - const upDir = `${dir}-upstream.git`; + withRepo(({ dir, git, commit, run, runBare, publish, upDir }) => { const ref = () => { try { return git("rev-parse", "--verify", "--quiet", UPSTREAM_REF).trim(); @@ -3122,209 +3235,586 @@ function withUpstream(body) { return undefined; } }; - try { - const up = commit({ "p.ts": "a();\nb();\n" }, "upstream"); - git("clone", "-q", "--bare", dir, upDir); - git("checkout", "-q", "-b", "fork"); - const main = commit( - { - ...seams({ files: {}, pending: { "p.ts": 2 } }), - "p.ts": "a();\nforkA();\nb();\nforkB();\n", - }, - "main", - ); - const move = (added, from = main) => { - git("checkout", "-q", "-f", "-B", `pr-${added.join("-")}`, from); - return commit(seams({ files: { "p.ts": { why: "w", added } } }), "move p.ts to files"); - }; - body({ dir, git, commit, run, up, upDir, main, move, ref }); - } finally { - fs.rmSync(upDir, { recursive: true, force: true }); - } + const up = commit({ "p.ts": "a();\nb();\n" }, "upstream"); + git("checkout", "-q", "-b", "fork"); + const main = commit( + { + ...seams({ files: {}, pending: { "p.ts": 2 } }), + "p.ts": "a();\nforkA();\nb();\nforkB();\n", + }, + "main", + ); + // Published only now: `run` names it from here on (with --upstream), unless a test names + // another (or --no-upstream). + publish(up); + const move = (added, from = main) => { + git("checkout", "-q", "-f", "-B", `pr-${added.join("-")}`, from); + return commit(seams({ files: { "p.ts": { why: "w", added } } }), "move p.ts to files"); + }; + body({ dir, git, commit, run, runBare, up, upDir, main, move, ref, publish }); }); } -test("--upstream: a promoted line upstream has is refused; the pull request's own count passes it", () => { - withUpstream(({ run, upDir, main, move }) => { +test("--upstream: a promoted line upstream has is refused, and a fork line left out", () => { + withUpstream(({ run, main, move, ref }) => { // `a();` is upstream's and `forkB();` is left out: the count (2) and "the file has it" pass. const wrong = move(["a();", "forkA();"]); - const before = run("--config-ref", main, main, wrong); - assert.equal(before.status, 0, before.stderr); - assert.match( - before.stdout, - /^Note: this change moves p\.ts from "pending" to "files"; without --upstream/m, - ); - const result = run("--config-ref", main, "--upstream", upDir, main, wrong); + const result = run("--config-ref", main, main, wrong); assert.equal(result.status, 1, result.stdout); assert.match( result.stderr, - /- p\.ts: moved to "files" in this change's \.github\/fork-seams\.json, whose "added" lists `a\(\);`, but the fork adds no copy over upstream \([0-9a-f]{12}\): remove it from "added" \(a line upstream has is never a seam line\)\./, + /- p\.ts: moved to "files" in this change's \.github\/fork-seams\.json, whose "added" lists `a\(\);`, but the fork adds no copy over upstream \([0-9a-f]{12}\): remove it from "added" \(a line upstream has is never a seam line, and a spare copy lets a later change add the line unannounced\)\./, ); assert.match( result.stderr, /- p\.ts: moved to "files" .*, whose "added" lists `forkB\(\);` not at all, but the fork adds it over upstream \([0-9a-f]{12}\): list it/, ); + assert.equal(ref(), undefined); + }); +}); + +test("with no upstream named, a move or a seam count change is refused, not skipped", () => { + withRepo(({ git, commit, run }) => { + const up = commit({ "p.ts": "a();\nb();\n", ...BASE }, "upstream"); + git("checkout", "-q", "-b", "fork"); + const main = commit( + { + ...seams({ files: SEAM, pending: { "p.ts": 1 } }), + ...SEAMED, + "p.ts": "a();\nf();\nb();\n", + }, + "main", + ); + assert.ok(up); + const moved = commit( + seams({ files: { ...SEAM, "p.ts": { why: "w", added: ["f();"] } } }), + "move p.ts", + ); + git("checkout", "-q", "-f", "-B", "raise", main); + const raised = commit( + seams({ files: { "a.ts": { ...SEAM["a.ts"], added: [...SEAM["a.ts"].added, "};"] } } }), + "raise a count", + ); + for (const [pr, what] of [ + [moved, /p\.ts: this change moves p\.ts from "pending" to "files" in its/], + [raised, /a\.ts: this change edits their seam lines in its/], + ]) { + const result = run("--config-ref", main, main, pr); + assert.equal(result.status, 1, result.stdout); + assert.match(result.stderr, what); + assert.match( + result.stderr, + /but there is no \.github\/fork-upstream\.json at [0-9a-f]{12} \(add it, naming upstream's https:\/\/ git URL; by hand, pass --upstream \), so their fork lines cannot be counted/, + ); + // --no-upstream (fork-tests' run on the merge commit; fork-checks counts): a note. + const skipped = run("--no-upstream", "--config-ref", main, main, pr); + assert.equal(skipped.status, 0, skipped.stderr); + assert.match(skipped.stdout, /^Note: .*; --no-upstream: whether each entry lists exactly/m); + } }); }); test("--upstream: an entry listing exactly the fork lines passes, and runs again the same as upstream moves on", () => { - withUpstream(({ dir, git, run, up, upDir, main, move, ref }) => { + withUpstream(({ git, run, up, upDir, main, move, ref, commit }) => { const right = move(["forkB();", "forkA();"]); const wrong = move(["a();", "forkA();"]); - const judge = (pr) => run("--config-ref", main, "--upstream", upDir, main, pr); + const judge = (pr) => run("--config-ref", main, main, pr); const first = judge(right); assert.equal(first.status, 0, first.stderr); assert.match( first.stdout, new RegExp( - `each entry lists exactly the fork lines counted from upstream \\(${up.slice(0, 12)}, `, + `each entry lists exactly the fork lines counted from upstream \\(${up.slice(0, 12)}, the merge-base of ${main.slice(0, 12)} and ${reEscape(UPSTREAM_URL)}\\)`, ), ); - assert.doesNotMatch(first.stdout, WARNING); - assert.equal(ref(), up); - // Runs again: the same verdicts, the ref fetched afresh. + // The ref is gone after the run (a run by hand leaves nothing behind). + assert.equal(ref(), undefined); + // Runs again: the same verdicts. assert.equal(judge(right).status, 0); assert.equal(judge(wrong).status, 1); // Upstream moved on past the fork's base (and took a fork line word for word): still judged // from the commit main sits on, the merge-base, not upstream's newest head. git("checkout", "-q", "-B", "upnext", up); git("commit", "-q", "--allow-empty", "-m", "noop"); - fs.writeFileSync(path.join(dir, "p.ts"), "a();\nforkA();\nb();\nc();\n"); - git("commit", "-q", "-am", "upstream takes forkA"); - const next = git("rev-parse", "HEAD").trim(); - git( - "push", - "-q", - upDir, - `${next}:refs/heads/${git("-C", upDir, "symbolic-ref", "--short", "HEAD").trim()}`, - ); + const next = commit({ "p.ts": "a();\nforkA();\nb();\nc();\n" }, "upstream takes forkA"); + git("push", "-q", upDir, `${next}:refs/heads/main`); const again = judge(right); assert.equal(again.status, 0, again.stderr); assert.match(again.stdout, new RegExp(`\\(${up.slice(0, 12)}, `)); - assert.equal(ref(), next); + assert.equal(ref(), undefined); }); }); -test("--upstream: an unreachable or unrelated upstream is a warning and today's check, never an old fetch", () => { +test("--upstream: an unreachable, empty, unrelated or self-naming upstream is refused, never an old fetch", () => { withUpstream(({ dir, git, run, main, move, ref }) => { const wrong = move(["a();", "forkA();"]); - const gone = `${dir}-no-such-upstream.git`; + const right = move(["forkA();", "forkB();"]); // An earlier run's fetch (or one stopped half-way) left the ref: it is not judged by. git("update-ref", UPSTREAM_REF, main); - const result = run("--config-ref", main, "--upstream", gone, main, wrong); - assert.equal(result.status, 0, result.stderr); - assert.match( - result.stdout, - new RegExp( - `${WARNING.source}this change moves p\\.ts from "pending" to "files", but could not fetch the default branch of ${gone.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")} \\(twice\\): .*, so whether its "added" lists exactly the fork lines is not checked here`, - "m", - ), - ); - assert.equal(ref(), undefined); - // An upstream with no commit in common with main: named, not taken for a base. + for (const pr of [wrong, right]) { + const result = run("--config-ref", main, "--upstream", GONE_URL, main, pr); + assert.equal(result.status, 1, result.stdout); + assert.match( + result.stderr, + new RegExp( + `- p\\.ts: this change moves p\\.ts from "pending" to "files" in its \\.github/fork-seams\\.json, but could not fetch the default branch of ${reEscape(GONE_URL)}: .*, so their fork lines cannot be counted: run the check again`, + ), + ); + assert.equal(ref(), undefined); + } const other = `${dir}-unrelated.git`; + const named = (url, target) => git("config", `url.file://${target}.insteadOf`, url); try { execFileSync("git", ["init", "-q", "--bare", other]); + named("https://other.test/u.git", other); // An empty upstream (no default branch yet): it cannot be fetched. - const empty = run("--config-ref", main, "--upstream", other, main, wrong); - assert.equal(empty.status, 0, empty.stderr); + const empty = run( + "--config-ref", + main, + "--upstream", + "https://other.test/u.git", + main, + right, + ); + assert.equal(empty.status, 1, empty.stdout); assert.match( - empty.stdout, - /could not fetch the default branch of .*-unrelated\.git \(twice\)/, + empty.stderr, + /could not fetch the default branch of https:\/\/other\.test\/u\.git: /, ); + // An upstream with no commit in common with main: named, not taken for a base. git("checkout", "-q", "--orphan", "lonely"); - const lonely = (() => { - fs.writeFileSync(path.join(dir, "z.ts"), "z();\n"); - git("add", "-A"); - git("commit", "-q", "-m", "unrelated"); - return git("rev-parse", "HEAD").trim(); - })(); + fs.writeFileSync(path.join(dir, "z.ts"), "z();\n"); + git("add", "-A"); + git("commit", "-q", "-m", "unrelated"); + const lonely = git("rev-parse", "HEAD").trim(); git("push", "-q", other, `${lonely}:refs/heads/main`); execFileSync("git", ["-C", other, "symbolic-ref", "HEAD", "refs/heads/main"]); - const unrelated = run("--config-ref", main, "--upstream", other, main, wrong); - assert.equal(unrelated.status, 0, unrelated.stderr); + const unrelated = run( + "--config-ref", + main, + "--upstream", + "https://other.test/u.git", + main, + right, + ); + assert.equal(unrelated.status, 1, unrelated.stdout); + assert.match( + unrelated.stderr, + new RegExp( + `, ${lonely.slice(0, 12)}\\) shares no commit with ${main}, so their fork lines`, + ), + ); + // A URL that names the fork itself: its branch holds main, so every fork line would be + // "upstream's" and every entry would have to list nothing. + git("push", "-q", "-f", other, `${main}:refs/heads/main`); + const self = run("--config-ref", main, "--upstream", "https://other.test/u.git", main, right); + assert.equal(self.status, 1, self.stdout); assert.match( - unrelated.stdout, - new RegExp(`, ${lonely.slice(0, 12)}\\) shares no commit with ${main}`), + self.stderr, + new RegExp( + `holds ${main.slice(0, 12)} itself, so the URL names this fork, not its upstream`, + ), ); + assert.equal(ref(), undefined); } finally { fs.rmSync(other, { recursive: true, force: true }); } }); }); -test("--upstream: a change that moves no pending file fetches nothing", () => { - withUpstream(({ dir, git, commit, run, main, ref }) => { +test("--upstream: a change that moves no pending file and changes no seam count fetches nothing", () => { + withUpstream(({ git, commit, run, main, ref }) => { git("checkout", "-q", "-B", "plain", main); const plain = commit({ "q.ts": "fork();\n" }, "a fork file"); - const result = run( - "--config-ref", - main, - "--upstream", - `${dir}-no-such-upstream.git`, - main, - plain, - ); + git("update-ref", UPSTREAM_REF, main); + const result = run("--config-ref", main, "--upstream", GONE_URL, main, plain); assert.equal(result.status, 0, result.stderr); - assert.doesNotMatch(result.stdout, WARNING); - assert.equal(ref(), undefined); - // From upstream the base is upstream already: --upstream is a usage error there. - assert.equal(run("--from-upstream", "--upstream", dir, main, plain).status, 2); + // Not fetched: the ref it would have deleted first is still there. + assert.equal(ref(), main); + git("update-ref", "-d", UPSTREAM_REF); + // From upstream the base is upstream already, and --no-upstream names none: usage errors. + assert.equal(run("--from-upstream", "--upstream", UPSTREAM_URL, main, plain).status, 2); + assert.equal(run("--no-upstream", "--upstream", UPSTREAM_URL, main, plain).status, 2); + assert.equal(run("--from-upstream", "--no-upstream", main, plain).status, 2); assert.equal(run("--upstream", main, plain).status, 2); }); }); -test("--upstream in a partial clone (as fork-checks checks out): the verdict holds, and no remote is left", () => { - withUpstream(({ dir, git, upDir, main, move }) => { +test("--upstream takes only an https:// URL, as main's upstream file does", () => { + withUpstream(({ upDir, run, main, move, ref }) => { + const right = move(["forkA();", "forkB();"]); + for (const url of [`file://${upDir}`, upDir, "-oProxyCommand=x", "ext::sh -c x"]) { + const result = run("--config-ref", main, "--upstream", url, main, right); + assert.equal(result.status, 1, `${url}: ${result.stdout}`); + assert.match(result.stderr, /but --upstream: "url" must be one https:\/\/ git URL/); + assert.equal(ref(), undefined); + } + }); +}); + +// The filter was chosen from extensions.partialClone, which fork-checks' checkout never sets: CI +// fetched upstream whole (12 s, 35 MB). Now every fetch takes commits only. +test("the upstream fetch takes commits only (no trees), in a full clone and in fork-checks' partial one, and leaves no remote or ref", () => { + withUpstream(({ dir, git, commit, upDir, main, move, up }) => { const right = move(["forkA();", "forkB();"]); const wrong = move(["a();", "forkA();"]); - git("config", "uploadpack.allowFilter", "true"); - execFileSync("git", ["-C", upDir, "config", "uploadpack.allowFilter", "true"]); const clone = `${dir}-partial`; + const full = `${dir}-full`; try { + // A full clone (a run by hand), made before upstream's newest commit. + execFileSync("git", ["clone", "-q", "--no-checkout", dir, full]); + const fg = (...args) => execFileSync("git", ["-C", full, ...args], { encoding: "utf8" }); + fg("config", `url.file://${upDir}.insteadOf`, UPSTREAM_URL); + // Upstream's newest commit, with a tree and a file no clone has. + git("checkout", "-q", "-B", "upnext", up); + const next = commit({ "u-only.ts": "upstream only\n" }, "upstream moves on"); + const tree = git("rev-parse", `${next}^{tree}`).trim(); + git("push", "-q", upDir, `${next}:refs/heads/main`); + // fork-checks' checkout: git init, a blob:none fetch of main and the pull request's commits. execFileSync("git", ["init", "-q", clone]); const cg = (...args) => execFileSync("git", ["-C", clone, ...args], { encoding: "utf8" }); + cg("config", `url.file://${upDir}.insteadOf`, UPSTREAM_URL); + execFileSync("git", ["-C", dir, "config", "uploadpack.allowFilter", "true"]); cg("remote", "add", "origin", `file://${dir}`); cg("fetch", "-q", "--no-tags", "--filter=blob:none", "origin", main, right, wrong); cg("checkout", "-q", "--detach", main); - const judge = (pr) => - spawnSync( - "node", - [SCRIPT, "--config-ref", main, "--upstream", `file://${upDir}`, main, pr], - { - cwd: clone, + assert.throws(() => fg("cat-file", "-e", next), undefined, "the full clone predates it"); + for (const [where, run] of [ + [clone, cg], + [full, fg], + ]) { + const judge = (pr) => + spawnSync("node", [SCRIPT, "--config-ref", main, "--upstream", UPSTREAM_URL, main, pr], { + cwd: where, encoding: "utf8", - }, - ); - const ok = judge(right); + }); + const ok = judge(right); + assert.equal(ok.status, 0, ok.stderr); + assert.match(ok.stdout, /each entry lists exactly the fork lines/); + const bad = judge(wrong); + assert.equal(bad.status, 1, bad.stdout); + assert.match(bad.stderr, /lists `a\(\);`, but the fork adds no copy over upstream/); + // Upstream's newest commit came (the merge-base was found from it) without its tree. + const lazy = { env: { ...process.env, GIT_NO_LAZY_FETCH: "1" }, stdio: "pipe" }; + execFileSync("git", ["-C", where, "cat-file", "-e", next], lazy); + assert.throws(() => execFileSync("git", ["-C", where, "cat-file", "-e", tree], lazy)); + // No remote registered and no ref left: the next run starts from the same config. + assert.doesNotMatch(run("config", "--list"), /fork-seam-upstream/); + assert.equal(run("for-each-ref", "refs/fork-seam-check/"), ""); + } + } finally { + fs.rmSync(clone, { recursive: true, force: true }); + fs.rmSync(full, { recursive: true, force: true }); + } + }); +}); + +// An upload-pack that fails or hangs on purpose: git runs remote..uploadpack for a local +// upstream, so the checker needs no test hook. `$1` counts the attempts. +const uploadPack = (dir, script) => { + const file = `${dir}-upload-pack.sh`; + fs.writeFileSync(file, `#!/bin/sh\necho x >> "${dir}-attempts"\n${script}\n`, { mode: 0o755 }); + return file; +}; +const attempts = (dir) => + fs.existsSync(`${dir}-attempts`) + ? fs.readFileSync(`${dir}-attempts`, "utf8").split("\n").length - 1 + : 0; + +test("the upstream fetch is tried again once: a dropped first attempt still counts the lines", () => { + withUpstream(({ dir, git, run, main, move, ref }) => { + const right = move(["forkA();", "forkB();"]); + const wrong = move(["a();", "forkA();"]); + const script = uploadPack( + dir, + `[ "$(wc -l < "${dir}-attempts")" -gt 1 ] || { echo dropped >&2; exit 1; }\nexec git upload-pack "$@"`, + ); + try { + git("config", "remote.fork-seam-upstream.uploadpack", script); + const ok = run("--config-ref", main, main, right); assert.equal(ok.status, 0, ok.stderr); - assert.match(ok.stdout, /each entry lists exactly the fork lines/); - const bad = judge(wrong); + assert.equal(attempts(dir), 2); + fs.rmSync(`${dir}-attempts`); + const bad = run("--config-ref", main, main, wrong); assert.equal(bad.status, 1, bad.stdout); assert.match(bad.stderr, /lists `a\(\);`, but the fork adds no copy over upstream/); - // The fetch registered no remote: the next run starts from the same config. - assert.doesNotMatch(cg("config", "--list"), /fork-seam-upstream/); + assert.equal(attempts(dir), 2); + // Two failures: refused, naming git's own words. + fs.writeFileSync(script, `#!/bin/sh\necho x >> "${dir}-attempts"\necho down >&2\nexit 1\n`); + fs.rmSync(`${dir}-attempts`); + const down = run("--config-ref", main, main, right); + assert.equal(down.status, 1, down.stdout); + assert.match( + down.stderr, + /could not fetch the default branch of https:\/\/upstream\.test\/u\.git: /, + ); + assert.equal(attempts(dir), 2); + assert.equal(ref(), undefined); } finally { - fs.rmSync(clone, { recursive: true, force: true }); + fs.rmSync(script, { force: true }); + fs.rmSync(`${dir}-attempts`, { force: true }); + } + }); +}); + +test("a hung upstream is refused within the total fetch time, which stays well below fork-checks' job limit", () => { + withUpstream(({ dir, git, main, ref }) => { + // Its stderr closed: the killed fetch's pipes must not wait for the orphaned sleep. + const script = uploadPack(dir, "exec 2>/dev/null\nsleep 5"); + try { + git("config", "remote.fork-seam-upstream.uploadpack", script); + const started = Date.now(); + const found = upstreamBase(UPSTREAM_URL, main, [main], { + cwd: dir, + attemptMs: 1_500, + totalMs: 1_700, + }); + const took = Date.now() - started; + assert.match( + found.problem, + /could not fetch the default branch of .*: no answer within 2 s in all \(SIGTERM\)/, + ); + // Two attempts: 1.5 s, then what is left of the 1.7 s (not another 1.5 s: 3 s in all). + assert.equal(attempts(dir), 2); + assert.ok(took < 2_600, `${took} ms`); + assert.equal(ref(), undefined); + } finally { + fs.rmSync(script, { force: true }); + fs.rmSync(`${dir}-attempts`, { force: true }); + } + }); + // fork-checks' job runs checkout and these tests (about a minute) before the fetch. + const workflow = fs.readFileSync( + path.join(REPO_ROOT, ".github/workflows/fork-checks.yml"), + "utf8", + ); + const job = workflow.slice(workflow.indexOf("no_schema_changes:")); + const minutes = Number(job.match(/timeout-minutes: (\d+)/)[1]); + assert.ok(FETCH_TOTAL_MS <= (minutes * 60_000) / 2, `${FETCH_TOTAL_MS} ms of ${minutes} min`); + assert.ok(FETCH_ATTEMPT_MS < FETCH_TOTAL_MS); +}); + +test("a shallow clone is refused as shallow, not as an upstream that shares no commit", () => { + withUpstream(({ dir, main }) => { + const shallow = `${dir}-shallow`; + try { + execFileSync("git", ["clone", "-q", "--depth", "1", `file://${dir}`, shallow]); + const found = upstreamBase(UPSTREAM_URL, main, [main], { cwd: shallow }); + assert.match( + found.problem, + /^this clone is shallow, so the commit main shares with upstream is not in it \(fetch its whole history: git fetch --unshallow\)$/, + ); + } finally { + fs.rmSync(shallow, { recursive: true, force: true }); + } + }); +}); + +// A stacked pull request whose base branch predates main's last rebase: its merge-base has the +// list, but it lacks the upstream commit main sits on, so its lines cannot be counted from there. +test("a branch without the upstream commit main sits on is told to update, not counted", () => { + withUpstream(({ git, commit, run, up, upDir, main, ref }) => { + // Upstream moves on; main is rebased onto it (a new main with the same fork lines). + git("checkout", "-q", "-f", "-B", "upnext", up); + const next = commit({ "n.ts": "n();\n" }, "upstream moves on"); + git("push", "-q", upDir, `${next}:refs/heads/main`); + const newMain = commit( + { + ...seams({ files: {}, pending: { "p.ts": 2 } }), + "p.ts": "a();\nforkA();\nb();\nforkB();\n", + }, + "main, rebased", + ); + // The old base branch, and a pull request on it that moves p.ts. + git("checkout", "-q", "-f", "-B", "stack", main); + const pr = commit( + seams({ files: { "p.ts": { why: "w", added: ["forkA();", "forkB();"] } } }), + "move p.ts", + ); + const result = run("--config-ref", newMain, main, pr); + assert.equal(result.status, 1, result.stdout); + assert.match( + result.stderr, + new RegExp( + `- p\\.ts: this change moves p\\.ts from "pending" to "files" in its \\.github/fork-seams\\.json, but this branch does not have upstream's commit ${next.slice(0, 12)} that ${newMain.slice(0, 12)} sits on, so their fork lines cannot be counted: update the branch from its base\\.`, + ), + ); + assert.equal(ref(), undefined); + // Updated onto the new main, it is counted and passes. + git("checkout", "-q", "-f", "-B", "updated", newMain); + const updated = commit( + seams({ files: { "p.ts": { why: "w", added: ["forkA();", "forkB();"] } } }), + "move p.ts", + ); + const ok = run("--config-ref", newMain, newMain, updated); + assert.equal(ok.status, 0, ok.stderr); + }); +}); + +// #626: the pull request's run judged only a list change that lowered a count. One that raised a +// count with no copy added (spare room), or dropped a line still in use (fork-follow stops), passed +// its own run and stopped the next fork-follow instead. Now both are counted from upstream. +test("a seam count the pull request raises without the line, or lowers while the line is in use, is refused in its own run", () => { + withRepo(({ git, commit, run, fromUpstream, publish }) => { + const upstream = commit({ ...BASE, "u.ts": UP }, "upstream"); + // Main announces `forkMore();` (step 1), so a step 2 below may add it. + const since = new Date().toISOString().slice(0, 10); + const announced = { since, added: ["forkMore();"] }; + const main = commit( + { ...seams({ files: { "a.ts": { ...SEAM["a.ts"], announced } } }), ...SEAMED }, + "fork main", + ); + publish(upstream); + assert.equal(fromUpstream(upstream, main).status, 0); + const pr = (name, list, files = {}) => { + git("checkout", "-q", "-f", "-B", name, main); + return commit({ ...seams(list), ...files }, name); + }; + const withAdded = (file, added, why = "test") => ({ + files: { ...SEAM, [file]: { why, added } }, + }); + const cases = [ + // A list-only change that lists upstream's `};` (the fork adds no copy): spare room. + [ + "raise", + withAdded("a.ts", [...SEAM["a.ts"].added, "};"]), + {}, + /- a\.ts: listed in this change's \.github\/fork-seams\.json, whose "added" lists `};`, but the fork adds no copy over upstream \([0-9a-f]{12}\): remove it from "added"/, + ], + // A step-2 change that lists a line twice but adds one copy. + [ + "twice", + withAdded("a.ts", [...SEAM["a.ts"].added, "...fork,"]), + {}, + /- a\.ts: listed in .*, whose "added" lists `\.\.\.fork,` 2×, but the fork adds 1× over upstream .*: list it 1×/, + ], + // A list-only change that drops a line the file still has. + [ + "drop", + withAdded("a.ts", ["import { fork } from './fork';"]), + {}, + /- a\.ts: listed in .*, whose "added" lists `\.\.\.fork,` not at all, but the fork adds it over upstream .*: list it/, + ], + // A whole entry dropped while its lines are in use. + [ + "entry", + { files: {} }, + {}, + /- a\.ts: dropped from "files" in this change's \.github\/fork-seams\.json, but the fork adds 2 lines over upstream \([0-9a-f]{12}\): keep its entry, listing each/, + ], + // A new entry for an unlisted upstream file that lists one of upstream's lines. + [ + "new", + { files: { ...SEAM, "u.ts": { why: "new", added: ["export const x = 1;"] } } }, + {}, + /- u\.ts: listed in .*, whose "added" lists `export const x = 1;`, but the fork adds no copy over upstream/, + ], + ]; + for (const [name, list, files, why] of cases) { + const head = pr(name, list, files); + for (const args of [ + [main, head], + ["--config-ref", main, main, head], + ]) { + const result = run(...args); + assert.equal(result.status, 1, `${name}: ${result.stdout}`); + assert.match(result.stderr, why, name); + } + // Merged as it was, the next run from upstream would refuse main. + assert.equal(fromUpstream(upstream, head).status, 1, name); } + // The right changes pass: step 2 lists the line it adds; a change that drops a line drops + // it from its list; a list-only fix of main's spare copy passes (it changes no file). + const passing = [ + pr("step2", withAdded("a.ts", [...SEAM["a.ts"].added, "forkMore();"]), { + "a.ts": `${SEAMED["a.ts"]}forkMore();\n`, + }), + pr("dropped", withAdded("a.ts", ["import { fork } from './fork';"]), { + "a.ts": SEAMED["a.ts"].replace(" ...fork,\n", ""), + }), + ]; + git("checkout", "-q", "-f", "-B", "spare", main); + const spareMain = commit( + seams(withAdded("a.ts", [...SEAM["a.ts"].added, "};"])), + "a spare copy", + ); + passing.push(commit(seams({ files: SEAM }), "fix main's list")); + for (const head of passing.slice(0, 2)) { + const result = run(main, head); + assert.equal(result.status, 0, result.stderr); + assert.match( + result.stdout, + /^Note: a\.ts: this change edits their seam lines .*; each entry lists exactly the fork lines/m, + ); + assert.equal(fromUpstream(upstream, head).status, 0); + } + const fix = run(spareMain, passing[2]); + assert.equal(fix.status, 0, fix.stderr); + assert.equal(fromUpstream(upstream, passing[2]).status, 0); + // A change to "announced" only, or a new entry that lists nothing, changes no count: no fetch. + const announcing = pr("announce", { + files: { + ...SEAM, + "u.ts": { why: "n", added: [], announced: { since, added: ["forkNew();"] } }, + }, + }); + const quiet = run("--upstream", GONE_URL, main, announcing); + assert.equal(quiet.status, 0, quiet.stderr); }); }); -test("findPromotedNotFork: exact copies, a file upstream lacks, and which files are promoted", () => { +test("listEditedFiles: only a line listed more or less often, and every entry when the base list is unreadable", () => { + const entry = (...added) => ({ why: "w", added }); + const base = { "a.ts": entry("x();", "y();"), "b.ts": entry() }; + assert.deepEqual(listEditedFiles(base, { "a.ts": entry(" y();", "x();"), "b.ts": entry() }), []); + assert.deepEqual(listEditedFiles(base, { "a.ts": entry("x();", "y();", "x();") }), ["a.ts"]); + assert.deepEqual(listEditedFiles(base, { "a.ts": entry("x();"), "c.ts": entry() }), ["a.ts"]); + assert.deepEqual(listEditedFiles(base, { "b.ts": entry() }), ["a.ts"]); + assert.deepEqual(listEditedFiles(base, { ...base, "c.ts": entry("z();") }), ["c.ts"]); + assert.deepEqual(listEditedFiles(undefined, { "b.ts": entry() }), ["b.ts"]); +}); + +test("findListedNotFork: exact copies both ways, a file upstream lacks, a dropped entry, and which files are promoted", () => { const up = "a();\nb();\n"; const head = "a();\nforkA();\nforkA();\nb();\n"; - const at = (text) => ({ atUpstream: () => text, atHead: () => head, name: "abc" }); - assert.deepEqual(findPromotedNotFork(["p.ts"], promoted("forkA();", "forkA();"), at(up)), []); - assert.deepEqual(findPromotedNotFork(["p.ts"], promoted("forkA();"), at(up)), [ + const at = (text, promoted = new Set(["p.ts"]), ownPending) => ({ + atUpstream: () => text, + atHead: () => head, + name: "abc", + promoted, + ownPending, + }); + assert.deepEqual(findListedNotFork(["p.ts"], promoted("forkA();", "forkA();"), at(up)), []); + assert.deepEqual(findListedNotFork(["p.ts"], promoted("forkA();"), at(up)), [ 'p.ts: moved to "files" in this change\'s .github/fork-seams.json, whose "added" lists `forkA();`, but the fork adds it 2× over upstream (abc): list it 2× (every fork line the file holds is listed).', ]); assert.match( - findPromotedNotFork(["p.ts"], promoted("forkA();", "forkA();", "forkA();"), at(up))[0], + findListedNotFork(["p.ts"], promoted("forkA();", "forkA();", "forkA();"), at(up))[0], /lists `forkA\(\);` 3×, but the fork adds 2× over upstream \(abc\): list it 2×/, ); assert.match( - findPromotedNotFork(["p.ts"], promoted("forkA();", "forkA();"), at(undefined))[0], - /upstream \(abc\) does not have it/, + findListedNotFork(["p.ts"], promoted("forkA();", "forkA();"), at(undefined))[0], + /upstream \(abc\) does not have it, so its fork lines cannot be counted: keep it in "pending"/, + ); + assert.match( + findListedNotFork(["p.ts"], promoted("forkA();", "forkA();"), at(undefined, new Set()))[0], + /^p\.ts: listed in .*upstream \(abc\) does not have it, .*: keep main's entry/, + ); + // Dropped from "files": no fork line may be left, or exactly its "pending" ceiling. + const none = new Set(); + assert.deepEqual(findListedNotFork(["p.ts"], {}, at(head, none)), []); + assert.deepEqual(findListedNotFork(["p.ts"], {}, at(up, none)), [ + 'p.ts: dropped from "files" in this change\'s .github/fork-seams.json, but the fork adds 2 lines over upstream (abc): keep its entry, listing each (every fork line the file holds is listed).', + ]); + assert.deepEqual(findListedNotFork(["p.ts"], {}, at(up, none, { "p.ts": 2 })), []); + assert.match( + findListedNotFork(["p.ts"], {}, at(up, none, { "p.ts": 1 }))[0], + /adds 2 lines over upstream \(abc\), not its "pending" ceiling of 1: keep its entry/, + ); + // A file the head deletes is refused elsewhere. + assert.deepEqual( + findListedNotFork(["p.ts"], promoted("x();"), { ...at(up), atHead: () => undefined }), + [], ); assert.deepEqual( promotedFiles( @@ -3338,9 +3828,11 @@ test("findPromotedNotFork: exact copies, a file upstream lacks, and which files }); test("the list commit's upstream file: a wrong move is refused without --upstream; the pull request's own copy is never read", () => { - withUpstream(({ git, commit, run, upDir, main, move, ref }) => { - const named = JSON.stringify({ "//": "test", url: `file://${upDir}` }); - // The pull request adds the file itself (main has none): main's list judges, and has no upstream. + withUpstream(({ git, commit, run, runBare, main, move, ref }) => { + const named = JSON.stringify({ "//": "test", url: UPSTREAM_URL }); + const plainRun = (...args) => run("--no-upstream", ...args); + // The pull request adds the file itself (main has none): main's list judges, and names no + // upstream, so the move is refused (with --no-upstream: a note). git("checkout", "-q", "-f", "-B", "own-file", main); const ownFile = commit( { @@ -3349,62 +3841,67 @@ test("the list commit's upstream file: a wrong move is refused without --upstrea }, "move p.ts and name upstream itself", ); - const own = run("--config-ref", main, main, ownFile); - assert.equal(own.status, 0, own.stderr); - assert.match( - own.stdout, - /without --upstream \(and no \.github\/fork-upstream\.json at [0-9a-f]{12}\)/, - ); + const own = runBare("--config-ref", main, main, ownFile); + assert.equal(own.status, 1, own.stdout); + assert.match(own.stderr, /there is no \.github\/fork-upstream\.json at [0-9a-f]{12}/); + assert.equal(plainRun("--config-ref", main, main, ownFile).status, 0); assert.equal(ref(), undefined); // Main names upstream: the same wrong entry is refused with no flag, a right one passes, twice. git("checkout", "-q", "-f", "-B", "main-named", main); const mainNamed = commit({ [UPSTREAM_FILE]: named }, "name upstream"); const wrong = move(["a();", "forkA();"], mainNamed); const right = move(["forkA();", "forkB();"], mainNamed); + const byFile = runBare; for (let round = 0; round < 2; round++) { - const refused = run("--config-ref", mainNamed, mainNamed, wrong); + const refused = byFile("--config-ref", mainNamed, mainNamed, wrong); assert.equal(refused.status, 1, refused.stdout); assert.match(refused.stderr, /lists `a\(\);`, but the fork adds no copy over upstream/); - const passed = run("--config-ref", mainNamed, mainNamed, right); + const passed = byFile("--config-ref", mainNamed, mainNamed, right); assert.equal(passed.status, 0, passed.stderr); - assert.match( - passed.stdout, - new RegExp(`and file://${upDir.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\)`), - ); + assert.match(passed.stdout, new RegExp(`and ${reEscape(UPSTREAM_URL)}\\)`)); } - // --upstream, given, wins over the file (an unreachable one: a warning, not the file's verdict). - const flagged = run("--config-ref", mainNamed, "--upstream", `${upDir}-gone`, mainNamed, wrong); - assert.equal(flagged.status, 0, flagged.stderr); - assert.match(flagged.stdout, /could not fetch the default branch of .*-gone \(twice\)/); + // --upstream, given, wins over the file (an unreachable one: refused, not the file's verdict). + const flagged = byFile("--config-ref", mainNamed, "--upstream", GONE_URL, mainNamed, right); + assert.equal(flagged.status, 1, flagged.stdout); + assert.match( + flagged.stderr, + /could not fetch the default branch of https:\/\/gone\.test\/u\.git: /, + ); }); }); test("a malformed upstream file on the list's commit refuses a move to files, and only a move", () => { - withUpstream(({ git, commit, run, upDir, main, move, ref }) => { + withUpstream(({ upDir, git, commit, runBare, main, move, ref }) => { const bad = { "": /is not JSON/, "[]": /is not a JSON object/, '{"url":"https://example.invalid/u.git","repo":"x"}': /unknown key "repo" \(known: url\)/, - '{"url":"--upload-pack=touch x"}': /"url" must be one https:\/\/ or file:\/\/ git URL/, - '{"url":"ext::sh -c x"}': /"url" must be one https:\/\/ or file:\/\/ git URL/, - [`{"url":"file://${upDir} x"}`]: /"url" must be one https:\/\/ or file:\/\/ git URL/, + '{"url":"--upload-pack=touch x"}': /"url" must be one https:\/\/ git URL/, + '{"url":"ext::sh -c x"}': /"url" must be one https:\/\/ git URL/, + [`{"url":"file://${upDir}"}`]: /"url" must be one https:\/\/ git URL/, + [`{"url":"${UPSTREAM_URL} x"}`]: /"url" must be one https:\/\/ git URL/, }; for (const [text, why] of Object.entries(bad)) { git("checkout", "-q", "-f", "-B", "main-bad", main); const mainBad = commit({ [UPSTREAM_FILE]: text }, "a broken upstream file"); - const result = run("--config-ref", mainBad, mainBad, move(["forkA();", "forkB();"], mainBad)); + const result = runBare( + "--config-ref", + mainBad, + mainBad, + move(["forkA();", "forkB();"], mainBad), + ); assert.equal(result.status, 1, `${text}: ${result.stdout}`); assert.match( result.stderr, - /p\.ts: moved to "files" in this change's \.github\/fork-seams\.json, but \.github\/fork-upstream\.json at [0-9a-f]{12}/, + /p\.ts: this change moves p\.ts from "pending" to "files" in its \.github\/fork-seams\.json, but \.github\/fork-upstream\.json at [0-9a-f]{12}/, ); assert.match(result.stderr, why); - assert.match(result.stderr, /fix that file with a pull request to main first/); + assert.match(result.stderr, /fix that with a pull request to main first/); assert.equal(ref(), undefined); // A change that moves no pending file (the fix itself, say) never reads it. git("checkout", "-q", "-f", "-B", "plain-bad", mainBad); const plain = commit({ "q.ts": "fork();\n" }, "a fork file"); - assert.equal(run("--config-ref", mainBad, mainBad, plain).status, 0); + assert.equal(runBare("--config-ref", mainBad, mainBad, plain).status, 0); } }); }); @@ -3416,4 +3913,21 @@ test("this repository's upstream file is well formed and names an https URL", () Object.keys(raw).filter((key) => !key.startsWith("//")), ["url"], ); + // Read from git as the check reads it (main's copy on a pull request). + const named = upstreamNamedAt("HEAD"); + assert.equal(named.problem, undefined); +}); + +// fork-checks (pull_request_target, main's copy, full history) counts from upstream; fork-tests' +// run on the merge commit is shallow (two commits), so it says --no-upstream instead of being +// refused there. The other run must never say it. +test("fork-checks counts from upstream and fork-tests' shallow run passes --no-upstream", () => { + const read = (file) => fs.readFileSync(path.join(REPO_ROOT, ".github/workflows", file), "utf8"); + const runs = (text) => text.split("\n").filter((line) => line.includes("fork-seam-check.cjs ")); + const checks = runs(read("fork-checks.yml")); + assert.equal(checks.length, 1); + assert.doesNotMatch(checks[0], /--no-upstream|--upstream/); + const tests = runs(read("fork-tests.yml")); + assert.equal(tests.length, 1); + assert.match(tests[0], /fork-seam-check\.cjs --no-upstream HEAD\^1 HEAD$/); }); diff --git a/.github/workflows/fork-tests.yml b/.github/workflows/fork-tests.yml index edebaefc1fd79..17529d3efd546 100644 --- a/.github/workflows/fork-tests.yml +++ b/.github/workflows/fork-tests.yml @@ -462,10 +462,12 @@ jobs: # Fork lines in upstream files stay the seam lines pinned in .github/fork-seams.json (a fork # setting goes in forkSettings.ts, not upstream's settings.ts). A pull request's run checks out - # its merge commit, whose first parent is the base: the lines the pull request adds. + # its merge commit, whose first parent is the base: the lines the pull request adds. Its two + # commits have no upstream base, so the seam entries it changes are counted from upstream by + # fork-checks (the pull request's head, full history), not here: --no-upstream. - name: Fork lines in upstream files if: ${{ github.event_name == 'pull_request' }} - run: node .github/scripts/fork-seam-check.cjs HEAD^1 HEAD + run: node .github/scripts/fork-seam-check.cjs --no-upstream HEAD^1 HEAD # The composite actions' tests (fork-ci/) and the fleet walls' (.github/scripts/fork-fleet-walls*), # split from `workflows`: together they were about two thirds of its script tests' time, which diff --git a/docs/fork/updates.md b/docs/fork/updates.md index e0229fe00c322..16cff18d1cdcb 100644 --- a/docs/fork/updates.md +++ b/docs/fork/updates.md @@ -68,19 +68,28 @@ watches; it changes nothing on the machine, and `0` turns it off. declared seam only in a list-only change to that file (its text unchanged), when the new entry's `added` lists exactly as many copies as are left, each one the file has, and its `announced` none the file has; the run from upstream then holds every one of them to that entry. Which - lines are the fork's only upstream can tell, so for such a move the pull request's run - fetches upstream's default branch from the URL in main's `.github/fork-upstream.json` (never - the pull request's copy; `--upstream ` by hand) read-only (commits and trees only, into - `refs/fork-seam-check/upstream`, deleted before each fetch) and holds the entry's `added` to - exactly the head's fork lines counted from the merge-base of main and upstream's head: a line - upstream has, or a fork line left out, is refused. A change that moves no pending file fetches - nothing; a malformed upstream file refuses only such a move. With no upstream file on main the - run notes that and checks as before; when upstream cannot be fetched (twice) or shares no - commit with main, it warns, naming why, and checks as before; check the entry by hand then with + lines are the fork's only upstream can tell, so for such a move, and for every seam entry whose + `added` the pull request's own list changes (a line listed more or less often, or an entry with + lines added or dropped; a change to `announced` only, or an entry that lists nothing, is not + one), fork-checks' run fetches upstream's default branch from the URL in main's + `.github/fork-upstream.json` (never the pull request's copy; `--upstream ` by hand; only + `https://`) read-only (commits only, filter `tree:0`, into + `refs/fork-seam-check/upstream`, which each fetch overwrites and each run deletes) and holds each such entry's + `added` to exactly the head's fork lines counted from the merge-base of main and upstream's + head, both ways: a line listed more often than the fork adds it (a raised count with no copy + added, or a line upstream has) is spare room, and a line listed less often (dropped while still + in use) would stop the next fork-follow; both are refused in the pull request's own run. A list-only + change that fixes main's list is counted the same way, so it passes. A change that changes no + such entry fetches nothing. Every way the count cannot run is refused, naming why, never + skipped: no upstream file on main, a malformed one (or one not `https://`), a shallow clone, an + upstream that cannot be fetched within two minutes (two tries), one that shares no commit with + main or holds main itself (the URL names the fork), or a branch without the upstream commit main + sits on (update the branch). Run it again once the cause is fixed; to check an entry by hand, `node .github/scripts/fork-seam-check.cjs --from-upstream "$(git merge-base origin/main - upstream/main)" ` (upstream's main fetched as `upstream/main`). Left - higher, the next run from upstream refuses main for the spare room; set lower or dropped, for - the lines still there. Neither in `pending` nor moved may a change gain a fork line (an upstream + upstream/main)" ` (upstream's main fetched as `upstream/main`). Fork tests' + run on the merge commit has only two commits, so it passes `--no-upstream` and leaves the count + to fork-checks. Left higher, the next run from upstream refuses main for the spare room; set + lower or dropped, for the lines still there. Neither in `pending` nor moved may a change gain a fork line (an upstream line swapped for one is one more), whatever its list says. It reads its own list only to refuse, never to widen the gate, and does not hold a list-only change (the file's text unchanged), which may correct main. Right after a rebase, below the ceiling is not refused: when upstream takes a fork line word for word, the rebase drops @@ -110,7 +119,15 @@ watches; it changes nothing on the machine, and `0` turns it off. unannounced), and right after a rebase that took the line word for word fork-follow drops it in its lowering commit instead. So a pull request that drops listed seam lines drops them from its own list in the same change, exactly: its run refuses it unless its own `added` lists, for each - such line, main's copies less the ones it drops. + such line, main's copies less the ones it drops (when main's list changed since the branch's + merge-base, the refusal says to update the branch from main). + A fork that adopts this checker (or updates an older copy) runs + `node .github/scripts/fork-seam-check.cjs --from-upstream HEAD` once and fixes + its list (each spare `added` copy it names) with a list-only pull request before the checker + lands: fork-follow's run before the rebase refuses a spare copy and never lowers it, so an old + list with spare copies stops one fork-follow run otherwise. It also adds + `.github/fork-upstream.json` naming upstream's `https://` git URL: without it, every pull request + that moves a pending file or changes a seam's `added` is refused. Dropped lines are counted by text, so where upstream has a listed line too (`}`), a pull request may drop upstream's copy instead of the fork's and its run passes it; fork-follow, which counts from upstream, refuses it unless the lines left read the same as with the fork's copy dropped. In a pending file it cannot tell From 7f32886872c589985ddb8f574ee3de64ede882a6 Mon Sep 17 00:00:00 2001 From: scuttletoby <123843277+scuttletoby@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:29:23 +0000 Subject: [PATCH 2/2] fork seams: count list-only ceilings, lost and forkFiles from upstream; fixed clock in the hang test - A pull request's own "pending" ceilings, "lost" entries and "forkFiles" it changes are counted from upstream like seam lines; an entry for a file the head lacks is refused. - Upstream that cannot be fetched or named: its own refusal, saying how to run the check again, never blamed on the list. - The hung-upstream test uses an injected clock; no timing assertions. - Long and numbered comments and wall-clock reads in tests removed (lint). Co-Authored-By: Claude Opus 5.5 --- .github/scripts/fork-seam-check.cjs | 253 +++++++++++++++----- .github/scripts/fork-seam-check.test.cjs | 280 ++++++++++++++++++++--- docs/fork/updates.md | 35 +-- 3 files changed, 463 insertions(+), 105 deletions(-) diff --git a/.github/scripts/fork-seam-check.cjs b/.github/scripts/fork-seam-check.cjs index dd013121d6f78..82253ad3af038 100644 --- a/.github/scripts/fork-seam-check.cjs +++ b/.github/scripts/fork-seam-check.cjs @@ -1,8 +1,7 @@ // Usage: [--config-ref ] [--upstream | --no-upstream] | --from-upstream [--after-rebase [--lowered-list ]] . -// Refuses 's upstream-file changes beyond .github/fork-seams.json: a PR uses 's list (its own only for -// "pending" ceilings it must lower), and holds each seam entry its own list changes or moves from "pending" to the -// fork lines counted from upstream (--upstream, else the list commit's .github/fork-upstream.json). Writes only -// --lowered-list's file. +// Refuses 's upstream-file changes beyond .github/fork-seams.json: a PR uses 's list, and holds each entry +// its own list changes (seam counts, ceilings, "lost", "forkFiles") to upstream (--upstream, else the list commit's +// .github/fork-upstream.json). Writes only --lowered-list's file. const { execFileSync } = require("node:child_process"); const fs = require("node:fs"); @@ -617,11 +616,9 @@ const promotedFiles = (config, ownFiles = {}, ownPending = {}) => ); /** - * The seam files for which this change's own list lists some line more or less often than the - * merge-base's list (`baseFiles`; a missing entry lists nothing): a raised count with no copy added - * is spare room, a line dropped while still in use stops fork-follow, and main's list cannot tell - * either from upstream's lines, so only upstream can. A new or dropped entry that lists nothing, or a - * change to "announced" only, changes no count. `baseFiles` undefined (unreadable): every entry. + * The seam files whose own "added" lists some line more or less often than the merge-base's list + * (`baseFiles`; a missing entry lists nothing). Only upstream can tell spare room or a dropped line + * in use; an entry that lists nothing, or "announced" alone, changes no count. Unreadable: all. */ function listEditedFiles(baseFiles, ownFiles) { const counts = (files, file) => @@ -639,25 +636,66 @@ function listEditedFiles(baseFiles, ownFiles) { } /** - * Entries of this change's own list (`ownFiles`, `ownPending`) for `files` whose "added" is not exactly - * the head's fork lines counted from `upstream` (an upstream commit: `name` says which), both ways: a - * line listed more often than the fork adds it (a later change could then add that line unannounced), - * or a fork line listed less often (fork-follow's next run refuses main). An entry the list drops - * from "files" lists nothing (or, moved back to "pending", its ceiling counts the lines). `promoted`: - * the files moved from "pending", named so in the reasons. + * The rest of the own list that only upstream can check, against the merge-base's (`baseList`, + * undefined when unreadable: all of it): "pending" ceilings and "lost" counts it sets, raises, + * lowers or drops, and whether "forkFiles" changed. A raised one is room for fork lines later. */ -function findListedNotFork(files, ownFiles, { atUpstream, atHead, name, promoted, ownPending }) { +function listEditedCounts(baseList, own) { + const differ = (key, same) => + [...new Set([...Object.keys(baseList?.[key] ?? {}), ...Object.keys(own[key])])].filter( + (file) => + baseList === undefined || + !Object.hasOwn(baseList[key], file) || + !Object.hasOwn(own[key], file) || + !same(baseList[key][file], own[key][file]), + ); + const sameLost = (a, b) => a.lines === b.lines && a.gone === b.gone; + const patterns = (list) => JSON.stringify([...new Set(list.forkFiles)].sort()); + return { + pending: differ("pending", (a, b) => a === b), + lost: differ("lost", sameLost), + forkFiles: baseList === undefined || patterns(baseList) !== patterns(own), + }; +} + +const plural = (count, word) => `${count} ${word}${count === 1 ? "" : "s"}`; + +/** + * Own-list entries for `files` that differ from the head's fork lines counted over `atUpstream` + * (`name`): each "added" line exactly (more is spare room, fewer stops fork-follow), a "pending" + * ceiling as a total, and a dropped entry none left. `promoted`: files moved from "pending"; + * `wasPending`: files main's or the merge-base's list has in "pending". + */ +function findListedNotFork( + files, + ownFiles, + { atUpstream, atHead, name, promoted, ownPending, wasPending }, +) { const ceilings = []; const times = (count) => (count === 1 ? "" : ` ${count}×`); for (const file of files) { - const head = atHead(file); - if (head === undefined) continue; // Refused elsewhere. const listedHere = Object.hasOwn(ownFiles, file); + const ceiling = + !listedHere && ownPending !== undefined && Object.hasOwn(ownPending, file) + ? ownPending[file] + : undefined; const where = promoted?.has(file) ? `moved to "files" in this change's ${CONFIG}` : listedHere ? `listed in this change's ${CONFIG}` - : `dropped from "files" in this change's ${CONFIG}`; + : ceiling !== undefined + ? `"pending" with a ceiling of ${ceiling} in this change's ${CONFIG}` + : `dropped from ${wasPending?.has(file) ? '"pending"' : '"files"'} in this change's ${CONFIG}`; + const head = atHead(file); + if (head === undefined) { + // A dropped entry for a file the head deletes is refused as a deletion (or is right). + if (listedHere || ceiling !== undefined) { + ceilings.push( + `${file}: ${where}, but the head does not have it: list only a file this change keeps.`, + ); + } + continue; + } const up = atUpstream(file); if (up === undefined) { ceilings.push( @@ -667,12 +705,14 @@ function findListedNotFork(files, ownFiles, { atUpstream, atHead, name, promoted } const fork = new Map(addedLines(up, head)); if (!listedHere) { - const ceiling = - ownPending !== undefined && Object.hasOwn(ownPending, file) ? ownPending[file] : 0; const forkLines = total([...fork]); - if (forkLines !== ceiling) { + if (ceiling !== undefined && forkLines !== ceiling) { + ceilings.push( + `${file}: ${where}, but the fork adds ${plural(forkLines, "line")} over upstream (${name}): ${forkLines === 0 ? 'remove it from "pending"' : `set its ceiling to ${forkLines}`} (a ceiling above the count is room for a fork line later; below it, fork-follow's next run refuses main).`, + ); + } else if (ceiling === undefined && forkLines !== 0) { ceilings.push( - `${file}: ${where}, but the fork adds ${forkLines} line${forkLines === 1 ? "" : "s"} over upstream (${name})${ceiling === 0 ? "" : `, not its "pending" ceiling of ${ceiling}`}: keep its entry, listing each (every fork line the file holds is listed).`, + `${file}: ${where}, but the fork adds ${plural(forkLines, "line")} over upstream (${name}): keep its entry, listing each (every fork line the file holds is listed).`, ); } continue; @@ -696,6 +736,60 @@ function findListedNotFork(files, ownFiles, { atUpstream, atHead, name, promoted return ceilings; } +/** + * Own "lost" entries for `files` that differ from the upstream lines the head loses over + * `atUpstream` (`name`), as fork-follow counts them: a count set too high lets a later change swap + * an upstream line for a fork line unseen; too low, or dropped, stops fork-follow. + */ +function findLostNotUpstream(files, ownLost, { atUpstream, atHead, name }) { + const ceilings = []; + for (const file of files) { + const own = Object.hasOwn(ownLost, file) ? ownLost[file] : undefined; + const where = `${own === undefined ? "dropped from" : "set in"} "lost" in this change's ${CONFIG}`; + const head = atHead(file); + if (head === undefined) { + if (own !== undefined) { + ceilings.push( + `${file}: ${where}, but the head does not have it: list only a file this change keeps.`, + ); + } + continue; + } + const up = atUpstream(file); + if (up === undefined) { + ceilings.push( + `${file}: ${where}, but upstream (${name}) does not have it, so its lost lines cannot be counted: keep main's entry (upstream moved or removed it?).`, + ); + continue; + } + const lines = lostLines(orderedLines(up), orderedLines(head)); + const gone = total(addedLines(head, up)); + const held = own ?? { lines: 0, gone: 0 }; + if (held.lines === lines && held.gone === gone) continue; + const says = own === undefined ? "no entry" : `{"lines": ${own.lines}, "gone": ${own.gone}}`; + ceilings.push( + `${file}: ${where} (${says}), but the fork loses ${plural(lines, "upstream line")} over upstream (${name}) (deleted, moved or re-indented), ${gone} of them gone: ${lines === 0 ? 'remove it from "lost"' : `set it to {"lines": ${lines}, "gone": ${gone}}`} (a count above the lines lost is room to swap one for a fork line later).`, + ); + } + return ceilings; +} + +/** + * Upstream files (in `upstreamFiles`, those the head has) that this change's own "forkFiles" + * takes for the fork's own where the merge-base's (`baseForkFiles`, undefined: none) did not: + * merged, pull requests could then change them unchecked. + */ +function findOwnHiddenFiles(upstreamFiles, ownForkFiles, baseForkFiles, atHead) { + const isOwn = forkFileTest(ownForkFiles); + const wasOwn = baseForkFiles === undefined ? () => false : forkFileTest(baseForkFiles); + return upstreamFiles + .filter((file) => isOwn(file) && !wasOwn(file) && atHead(file) !== undefined) + .map( + (file) => + `${file}: upstream has it, but this change's ${CONFIG} "forkFiles" takes it for a fork file (merged, pull requests could change it unchecked): exclude it with "!${file}", or narrow the pattern.`, + ); +} + // Where a pull request's run keeps upstream's default branch while it finds the merge-base. A // fetch overwrites it, a failed one refuses (an earlier run's head is never judged by), and it is // deleted after each run: a run by hand leaves nothing behind, and no ref holds upstream's commits. @@ -744,18 +838,15 @@ function upstreamNamedAt(rev) { } /** - * Fetches upstream's default branch from `url` (read-only, commits only: filter tree:0, whatever - * the clone) and returns { rev: merge-base of `main` and it }, or - * { problem } when it cannot (shallow clone, unreachable, slow, no common history, or a URL whose - * branch holds `main` itself). The merge-base is in `main`'s history, so its files are read from - * the clone; negotiated from `tips` (the checkout has no branch), only upstream's commits since - * the fork's base come. `cwd`, `attemptMs` and `totalMs` are for tests. + * Fetches upstream's default branch from `url` (commits only, negotiated from `tips`) and returns + * { rev: its merge-base with `main` }, whose files the clone has, or { problem } (shallow clone, + * unreachable, slow, no common history, or a branch holding `main`). Options are for tests. */ function upstreamBase( url, main, tips, - { cwd, attemptMs = FETCH_ATTEMPT_MS, totalMs = FETCH_TOTAL_MS } = {}, + { cwd, attemptMs = FETCH_ATTEMPT_MS, totalMs = FETCH_TOTAL_MS, now = Date.now } = {}, ) { const run = (args, timeout) => execFileSync("git", args, { @@ -771,16 +862,13 @@ function upstreamBase( problem: `this clone is shallow, so the commit main shares with upstream is not in it (fetch its whole history: git fetch --unshallow)`, }; } - // A promisor remote for this one command only (-c), so none is left in the config: git fetches - // from it with its partialclonefilter (commits only). A --filter flag instead would write that - // filter into the clone's config. + // A remote for this one command only (-c), so none is left in the config: its partialclonefilter + // makes it a promisor, fetched commits only. A --filter flag would write it into the config. const remote = "fork-seam-upstream"; const fetch = [ "-c", `remote.${remote}.url=${url}`, "-c", - `remote.${remote}.promisor=true`, - "-c", `remote.${remote}.partialclonefilter=tree:0`, "fetch", "--quiet", @@ -792,11 +880,11 @@ function upstreamBase( `+HEAD:${UPSTREAM_REF}`, ]; try { - const started = Date.now(); + const started = now(); let error; // One retry, within the total: a dropped connection is not a reason to refuse. for (let attempt = 0; attempt < 2; attempt++) { - const left = totalMs - (Date.now() - started); + const left = totalMs - (now() - started); if (left <= 0) break; try { run(fetch, Math.min(attemptMs, left)); @@ -826,8 +914,8 @@ function upstreamBase( problem: `upstream's default branch (${url}, ${tip.slice(0, 12)}) shares no commit with ${main}`, }; } - // Upstream holding main whole: the URL names this fork (or a copy of it), and every fork line - // would count as upstream's. + // Upstream holding main whole: the URL names this fork (or a copy), and every fork line would + // count as upstream's. Not caught: such a URL while fork-follow rewrites main mid-run. if (rev === run(["rev-parse", `${main}^{commit}`]).trim()) { return { problem: `upstream's default branch (${url}, ${tip.slice(0, 12)}) holds ${main.slice(0, 12)} itself, so the URL names this fork, not its upstream`, @@ -1385,19 +1473,22 @@ function main(argv) { let ownPending; let ownFiles; let ownLost; - let baseFiles; + let ownConfig; + let baseList; if (!upstream && listProblem === undefined && ownList !== undefined) { try { - ({ pending: ownPending, files: ownFiles, lost: ownLost } = parseConfig(ownList)); + ownConfig = parseConfig(ownList); + ({ pending: ownPending, files: ownFiles, lost: ownLost } = ownConfig); } catch { ownPending = undefined; // The list judged it is main's; a malformed own one is said below. } try { - baseFiles = readConfigAt(mergeBase).files; + baseList = readConfigAt(mergeBase); } catch { - baseFiles = undefined; // Unreadable: every entry counts as edited (listEditedFiles). + baseList = undefined; // Unreadable: every entry counts as edited (listEditedFiles). } } + const baseFiles = baseList?.files; const lowered = parsed.loweredList === undefined ? undefined @@ -1433,18 +1524,35 @@ function main(argv) { baseFiles, lowered, }); - // A move to "files", or a seam's "added" changed, lists lines main's list cannot tell from - // upstream's (a raised count with no copy added, a line dropped while still in use): only - // upstream can. Every way the count cannot run refuses (fail closed); fork-tests' run on the - // merge commit passes --no-upstream, as fork-checks' run counts it. + // Counts the own list changes (seam "added", "pending", "lost", "forkFiles", a move to "files") + // main's list cannot check: only upstream can. Every way the count cannot run refuses (fail + // closed); fork-tests' run on the merge commit passes --no-upstream, as fork-checks' run counts. const promoted = upstream ? [] : promotedFiles(config, ownFiles, ownPending); - const toCount = - upstream || ownFiles === undefined - ? [] - : [...new Set([...promoted, ...listEditedFiles(baseFiles, ownFiles)])].sort(); + const edits = + upstream || ownConfig === undefined + ? { files: [], pending: [], lost: [], forkFiles: false } + : { files: listEditedFiles(baseFiles, ownFiles), ...listEditedCounts(baseList, ownConfig) }; + const toCount = [...new Set([...promoted, ...edits.files, ...edits.pending])].sort(); + const lostToCount = [...edits.lost].sort(); const countedCeilings = []; - if (toCount.length > 0) { - const what = `${toCount.join(", ")}: ${promoted.length > 0 ? `this change moves ${promoted.join(", ")} from "pending" to "files"` : "this change edits their seam lines"} in its ${CONFIG}`; + const cannotCount = []; + if (toCount.length > 0 || lostToCount.length > 0 || edits.forkFiles) { + const seamEdited = edits.files.filter((file) => !promoted.includes(file)); + const ceilingEdited = edits.pending.filter((file) => !promoted.includes(file)); + const does = [ + ...(promoted.length > 0 + ? [`moves ${promoted.join(", ")} from "pending" to "files"`] + : seamEdited.length > 0 + ? ["edits their seam lines"] + : []), + ...(ceilingEdited.length > 0 + ? [`changes the "pending" ceiling of ${ceilingEdited.join(", ")}`] + : []), + ...(lostToCount.length > 0 ? [`changes the "lost" entry of ${lostToCount.join(", ")}`] : []), + ...(edits.forkFiles ? ['changes "forkFiles"'] : []), + ]; + const subjects = [...new Set([...toCount, ...lostToCount])].sort(); + const what = `${subjects.length > 0 ? subjects.join(", ") : '"forkFiles"'}: this change ${does.join(", and ")} in its ${CONFIG}`; const named = parsed.noUpstream ? {} : parsed.upstreamUrl === undefined @@ -1460,29 +1568,41 @@ function main(argv) { ), ); } else if (named.problem !== undefined) { - countedCeilings.push( + cannotCount.push( `${what}, but ${named.problem}, so their fork lines cannot be counted: fix that with a pull request to main first.`, ); } else { const found = upstreamBase(named.url, configRef, [configRef, head]); if (found.problem !== undefined) { - countedCeilings.push( - `${what}, but ${found.problem}, so their fork lines cannot be counted: run the check again, and if it still cannot, fix the cause first.`, - ); + cannotCount.push(`${what}, but ${found.problem}, so their fork lines cannot be counted.`); } else if (!isAncestor(found.rev, head)) { - countedCeilings.push( + cannotCount.push( `${what}, but this branch does not have upstream's commit ${found.rev.slice(0, 12)} that ${configRef.slice(0, 12)} sits on, so their fork lines cannot be counted: update the branch from its base.`, ); } else { const name = found.rev.slice(0, 12); + const atUpstream = (file) => textAt(found.rev, file); countedCeilings.push( ...findListedNotFork(toCount, ownFiles, { - atUpstream: (file) => textAt(found.rev, file), + atUpstream, atHead, name, promoted: new Set(promoted), ownPending, + wasPending: new Set([ + ...Object.keys(config.pending), + ...Object.keys(baseList?.pending ?? {}), + ]), }), + ...findLostNotUpstream(lostToCount, ownLost, { atUpstream, atHead, name }), + ...(edits.forkFiles + ? findOwnHiddenFiles( + git(["ls-tree", "-r", "-z", "--name-only", found.rev]).split("\0").filter(Boolean), + ownConfig.forkFiles, + baseList?.forkFiles, + atHead, + ) + : []), ); if (countedCeilings.length === 0) { console.log( @@ -1562,8 +1682,18 @@ function main(argv) { ); } const refused = [...reasons, ...late, ...rises, ...ceilings, ...expired, ...landed]; + if (cannotCount.length > 0) { + // Not a seam breach: the count could not run, so nothing in this change is known wrong yet. + console.error( + `Could not count this change's list edits from upstream (an upstream, fetch or branch problem, not a wrong list; list from ${from}):`, + ); + for (const problem of cannotCount) console.error(printable(`- ${problem}`)); + console.error( + "Run the check again once the cause is fixed: \"Re-run failed jobs\" on the pull request's Fork checks run (the same commits and main's script), or push to the branch. By hand: --upstream .", + ); + } if (refused.length === 0) { - if (listProblem !== undefined) return 1; + if (listProblem !== undefined || cannotCount.length > 0) return 1; if (lowered !== undefined) { // After a rebase only: fork-follow commits the lowered list into the rebased series. const listText = textAt(configRef, CONFIG); @@ -1633,8 +1763,11 @@ module.exports = { findListedGone, findLostLines, findListedNotFork, + findLostNotUpstream, + findOwnHiddenFiles, FETCH_ATTEMPT_MS, FETCH_TOTAL_MS, + listEditedCounts, listEditedFiles, findSeamBreaches, findSpareSeamLines, diff --git a/.github/scripts/fork-seam-check.test.cjs b/.github/scripts/fork-seam-check.test.cjs index 2d7a5b0620233..6875bd549c7a8 100644 --- a/.github/scripts/fork-seam-check.test.cjs +++ b/.github/scripts/fork-seam-check.test.cjs @@ -15,8 +15,11 @@ const { findListedGone, findLostLines, findListedNotFork, + findLostNotUpstream, + findOwnHiddenFiles, FETCH_ATTEMPT_MS, FETCH_TOTAL_MS, + listEditedCounts, listEditedFiles, findStaleSeamLines, findUnlistedChanges, @@ -897,13 +900,16 @@ test("a pull request's own list is parsed for its format (never its rules) and r assert.doesNotMatch(result.stderr, /a file of the fork's own/, what); } } - // A well-formed list passes, whatever its "pending" or "forkFiles" (main's list judges the - // change). + // A well-formed list that widens "pending" or "forkFiles" is counted from upstream too; main + // names no upstream here, so it is refused as uncountable, never passed or blamed on the list. git("checkout", "-q", "-f", "-B", "pr", main); const wide = { files: SEAM, pending: { "z.ts": 9 }, forkFiles: ["tools/"] }; - const fine = commit(seams(wide), "a wider list"); - assert.equal(run(main, fine).status, 0); - // A seam line it lists more often (or less) is counted from upstream (#626); main names no + const wider = run(main, commit(seams(wide), "a wider list")); + assert.equal(wider.status, 1, wider.stdout); + assert.match(wider.stderr, /^Could not count this change's list edits from upstream/m); + assert.match(wider.stderr, /changes the "pending" ceiling of z\.ts, and changes "forkFiles"/); + assert.doesNotMatch(wider.stderr, /Fix the list in this change/); + // A seam line it lists more often (or less) is counted from upstream; main names no // upstream here, so that is refused rather than passed unchecked. const raised = commit( seams({ files: { "a.ts": { why: "mine", added: ["anything: 1,"] } } }), @@ -2261,10 +2267,8 @@ test("a pull request's new announcement that expires within 3 days is a warning, result.stdout, /^Warning: n\.ts: "announced" on 2020-01-01, so fork-follow refuses it from 2020-01-16/m, ); - const other = commit( - seams({ ...ANNOUNCED("2020-01-01"), forkFiles: ["tools/"] }), - "a later list change", - ); + const reworded = { ...ANNOUNCED("2020-01-01").files, "a.ts": { ...SEAM["a.ts"], why: "new" } }; + const other = commit(seams({ files: reworded }), "a later list change"); const again = run(step1, other); assert.equal(again.status, 0, again.stderr); assert.doesNotMatch(again.stdout, /Warning:/); @@ -2757,7 +2761,9 @@ test("property: a pending ceiling the pull request accepts passes the run from u }); test("the command line refuses a pull request that shrinks a pending file but keeps its ceiling", () => { - withRepo(({ git, commit, run }) => { + withRepo(({ git, commit, run, publish }) => { + // The lowered ceiling is counted from upstream, so upstream is published. + publish(commit({ "p.ts": UP }, "upstream")); const config = { files: {}, pending: { "p.ts": 2 } }; const main = commit({ ...seams(config), "p.ts": `${UP}forkA();\nforkB();\n` }, "main"); git("checkout", "-q", "-b", "pr", main); @@ -3076,7 +3082,7 @@ test("from main, a change that drops a listed seam copy drops it from its own li assert.match(judged(0)[0], /list it 1× under "added" .*\(its list has none;/); }); -// #621's surviving mutants: both `Math.max(0, …)` only showed in the wording ("-1×"). +// Earlier surviving mutants: both `Math.max(0, …)` only showed in the wording ("-1×"). test("a fork that removes upstream copies of a listed line, or a change that drops more copies than listed, names the right fix", () => { const generic = (count) => ({ "a.ts": { why: "t", added: Array(count).fill("};") } }); // From upstream: upstream has `};` twice, the fork keeps one: no copy over upstream. @@ -3097,7 +3103,7 @@ test("a fork that removes upstream copies of a listed line, or a change that dro ]); }); -// #626 minor 2: main's count comes from the list's commit, the drop from the branch; a branch +// Main's count comes from the list's commit, the drop from the branch; a branch // whose merge-base lists another count is told to update, not only given a number. test("from main, a drop judged by a list main changed since the merge-base says to update the branch", () => { const seam = (count) => ({ "a.ts": { why: "t", added: ["x();", ...Array(count).fill("};")] } }); @@ -3292,7 +3298,10 @@ test("with no upstream named, a move or a seam count change is refused, not skip ); git("checkout", "-q", "-f", "-B", "raise", main); const raised = commit( - seams({ files: { "a.ts": { ...SEAM["a.ts"], added: [...SEAM["a.ts"].added, "};"] } } }), + seams({ + files: { "a.ts": { ...SEAM["a.ts"], added: [...SEAM["a.ts"].added, "};"] } }, + pending: { "p.ts": 1 }, + }), "raise a count", ); for (const [pr, what] of [ @@ -3357,9 +3366,23 @@ test("--upstream: an unreachable, empty, unrelated or self-naming upstream is re assert.match( result.stderr, new RegExp( - `- p\\.ts: this change moves p\\.ts from "pending" to "files" in its \\.github/fork-seams\\.json, but could not fetch the default branch of ${reEscape(GONE_URL)}: .*, so their fork lines cannot be counted: run the check again`, + `- p\\.ts: this change moves p\\.ts from "pending" to "files" in its \\.github/fork-seams\\.json, but could not fetch the default branch of ${reEscape(GONE_URL)}: .*, so their fork lines cannot be counted\\.$`, + "m", ), ); + // Said as an upstream or fetch problem with how to run it again, not as a wrong list. + assert.match( + result.stderr, + /^Could not count this change's list edits from upstream \(an upstream, fetch or branch problem, not a wrong list;/, + ); + assert.match( + result.stderr, + /Run the check again once the cause is fixed: "Re-run failed jobs" on the pull request's Fork checks run/, + ); + assert.doesNotMatch( + result.stderr, + /must stay within the seam lines|Lower each "pending" ceiling/, + ); assert.equal(ref(), undefined); } const other = `${dir}-unrelated.git`; @@ -3563,20 +3586,21 @@ test("a hung upstream is refused within the total fetch time, which stays well b const script = uploadPack(dir, "exec 2>/dev/null\nsleep 5"); try { git("config", "remote.fork-seam-upstream.uploadpack", script); - const started = Date.now(); + // A fake clock, no timing: the first attempt is capped at the 1.5 s total (not its own 10 s), + // and the clock then says the total is spent, so no second attempt starts. Uncapped, or + // tried again with nothing left (a 0 timeout is none), git waits out the 5 s sleep instead. + const clock = [0, 0]; const found = upstreamBase(UPSTREAM_URL, main, [main], { cwd: dir, - attemptMs: 1_500, - totalMs: 1_700, + attemptMs: 10_000, + totalMs: 1_500, + now: () => clock.shift() ?? 1_500, }); - const took = Date.now() - started; assert.match( found.problem, - /could not fetch the default branch of .*: no answer within 2 s in all \(SIGTERM\)/, + /^could not fetch the default branch of .*: no answer within 2 s in all \(SIGTERM\)$/, ); - // Two attempts: 1.5 s, then what is left of the 1.7 s (not another 1.5 s: 3 s in all). - assert.equal(attempts(dir), 2); - assert.ok(took < 2_600, `${took} ms`); + assert.equal(attempts(dir), 1); assert.equal(ref(), undefined); } finally { fs.rmSync(script, { force: true }); @@ -3651,14 +3675,14 @@ test("a branch without the upstream commit main sits on is told to update, not c }); }); -// #626: the pull request's run judged only a list change that lowered a count. One that raised a +// The pull request's run used to judge only a list change that lowered a count. One that raised a // count with no copy added (spare room), or dropped a line still in use (fork-follow stops), passed // its own run and stopped the next fork-follow instead. Now both are counted from upstream. test("a seam count the pull request raises without the line, or lowers while the line is in use, is refused in its own run", () => { withRepo(({ git, commit, run, fromUpstream, publish }) => { const upstream = commit({ ...BASE, "u.ts": UP }, "upstream"); - // Main announces `forkMore();` (step 1), so a step 2 below may add it. - const since = new Date().toISOString().slice(0, 10); + // Main announces `forkMore();` (step 1) on upstream's commit day, so a step 2 may add it. + const since = git("show", "-s", "--format=%cs", upstream).trim(); const announced = { since, added: ["forkMore();"] }; const main = commit( { ...seams({ files: { "a.ts": { ...SEAM["a.ts"], announced } } }), ...SEAMED }, @@ -3763,6 +3787,192 @@ test("a seam count the pull request raises without the line, or lowers while the }); }); +// A list-only change to a "pending" ceiling, a "lost" count or "forkFiles" was not counted in +// the pull request's run: one raised a ceiling and "lost" with no line changed, a second swapped +// upstream lines for fork lines within them, and neither run nor fork-follow refused either. +test("a pending ceiling, lost count, forkFiles pattern or entry the pull request's list changes is counted from upstream", () => { + withRepo(({ git, commit, run, fromUpstream, publish }) => { + const upstream = commit({ "p.ts": "a();\nb();\nc();\n", "q.ts": "q();\n" }, "upstream"); + git("checkout", "-q", "-b", "fork"); + const P = "a();\nforkA();\nb();\nforkB();\nc();\n"; + const main = commit({ ...seams({ files: {}, pending: { "p.ts": 2 } }), "p.ts": P }, "main"); + publish(upstream); + assert.equal(fromUpstream(upstream, main).status, 0); + const pr = (name, list, files = {}, from = main) => { + git("checkout", "-q", "-f", "-B", name, from); + return commit({ ...seams(list), ...files }, name); + }; + const lost = (lines, gone) => ({ "p.ts": { lines, gone } }); + const swap = pr("swap-list", { files: {}, pending: { "p.ts": 4 }, lost: lost(2, 2) }); + const cases = [ + [ + "raise", + { files: {}, pending: { "p.ts": 4 } }, + /- p\.ts: "pending" with a ceiling of 4 in this change's \.github\/fork-seams\.json, but the fork adds 2 lines over upstream \([0-9a-f]{12}\): set its ceiling to 2 \(a ceiling above the count is room for a fork line later/, + ], + [ + "swap", + swap, + /- p\.ts: set in "lost" in this change's .* \({"lines": 2, "gone": 2}\), but the fork loses 0 upstream lines over upstream \([0-9a-f]{12}\) .*: remove it from "lost"/, + ], + [ + "lost", + { files: {}, pending: { "p.ts": 2 }, lost: lost(1, 0) }, + /- p\.ts: set in "lost" .*remove it from "lost"/, + ], + [ + "lower", + { files: {}, pending: { "p.ts": 1 } }, + /ceiling of 1 .*adds 2 lines over upstream .*: set its ceiling to 2/, + ], + [ + "drop", + { files: {} }, + /- p\.ts: dropped from "pending" in this change's .*, but the fork adds 2 lines over upstream \([0-9a-f]{12}\): keep its entry/, + ], + [ + "new", + { files: {}, pending: { "p.ts": 2, "q.ts": 1 }, lost: { "q.ts": { lines: 1, gone: 1 } } }, + /- q\.ts: "pending" with a ceiling of 1 .*adds 0 lines over upstream .*: remove it from "pending"[^]*- q\.ts: set in "lost" .*remove it from "lost"/, + ], + [ + "forkFiles", + { files: {}, pending: { "p.ts": 2 }, forkFiles: ["tools/", "q.*"] }, + /- q\.ts: upstream has it, but this change's \.github\/fork-seams\.json "forkFiles" takes it for a fork file .*: exclude it with "!q\.ts", or narrow the pattern\./, + ], + [ + "ghost", + { files: { "ghost.ts": { why: "w", added: ["evil();"] } }, pending: { "p.ts": 2 } }, + /- ghost\.ts: listed in this change's \.github\/fork-seams\.json, but the head does not have it: list only a file this change keeps\./, + ], + ]; + for (const [name, list, why] of cases) { + const head = typeof list === "string" ? list : pr(name, list); + for (const args of [ + [main, head], + ["--config-ref", main, main, head], + ]) { + const result = run(...args); + assert.equal(result.status, 1, `${name}: ${result.stdout}`); + assert.match(result.stderr, why, name); + } + } + // Why the swap matters: merged, a change that swaps two upstream lines for fork lines fits + // the raised ceiling and "lost", and neither its run nor fork-follow's refuses it. + const swapped = pr( + "swapped", + { files: {}, pending: { "p.ts": 4 }, lost: lost(2, 2) }, + { + "p.ts": "forkC();\nforkA();\nforkD();\nforkB();\nc();\n", + }, + swap, + ); + assert.equal(run(swap, swapped).status, 0); + assert.equal(fromUpstream(upstream, swapped).status, 0); + // The right changes pass, each counted: a ceiling lowered with its line, a list-only fix of + // main's spare ceiling, and a fork-only "forkFiles" pattern. + const lowered = pr( + "lowered", + { files: {}, pending: { "p.ts": 1 } }, + { + "p.ts": "a();\nforkA();\nb();\nc();\n", + }, + ); + const spareMain = pr("spare", { files: {}, pending: { "p.ts": 3 } }); + const fix = pr("fix", { files: {}, pending: { "p.ts": 2 } }, {}, spareMain); + const tools = pr("tools", { files: {}, pending: { "p.ts": 2 }, forkFiles: ["tools/"] }); + for (const [base, head] of [ + [main, lowered], + [spareMain, fix], + [main, tools], + ]) { + const result = run(base, head); + assert.equal(result.status, 0, result.stderr); + assert.match( + result.stdout, + /; each entry lists exactly the fork lines counted from upstream/, + ); + assert.equal(fromUpstream(upstream, head).status, 0); + } + }); +}); + +test("listEditedCounts: a ceiling or lost count set, changed or dropped, and forkFiles changed", () => { + const list = (pending, lost = {}, forkFiles = []) => ({ files: {}, pending, lost, forkFiles }); + const base = list({ "p.ts": 2 }, { "p.ts": { lines: 1, gone: 0 } }, ["tools/"]); + const none = { pending: [], lost: [], forkFiles: false }; + assert.deepEqual(listEditedCounts(base, base), none); + assert.deepEqual( + listEditedCounts(base, list({ "p.ts": 2 }, base.lost, ["tools/", "tools/"])), + none, + ); + assert.deepEqual(listEditedCounts(base, list({ "p.ts": 3, "q.ts": 1 }, {}, ["x/"])), { + pending: ["p.ts", "q.ts"], + lost: ["p.ts"], + forkFiles: true, + }); + assert.deepEqual( + listEditedCounts(base, list({}, { "p.ts": { lines: 1, gone: 1 } }, ["tools/"])), + { + ...none, + pending: ["p.ts"], + lost: ["p.ts"], + }, + ); + assert.deepEqual(listEditedCounts(undefined, list({ "p.ts": 2 })), { + pending: ["p.ts"], + lost: [], + forkFiles: true, + }); +}); + +test("findLostNotUpstream and findOwnHiddenFiles count from upstream", () => { + const up = "a();\nb();\nc();\n"; + const at = (head) => ({ atUpstream: () => up, atHead: () => head, name: "abc" }); + // b(); deleted: one line lost, its text gone; c(); moved above a(): one more lost, not gone. + assert.deepEqual( + findLostNotUpstream(["p.ts"], { "p.ts": { lines: 1, gone: 1 } }, at("a();\nc();\n")), + [], + ); + assert.deepEqual( + findLostNotUpstream(["p.ts"], { "p.ts": { lines: 2, gone: 1 } }, at("c();\na();\n")), + [], + ); + assert.deepEqual(findLostNotUpstream(["p.ts"], {}, at(up)), []); + assert.deepEqual(findLostNotUpstream(["p.ts"], { "p.ts": { lines: 2, gone: 2 } }, at(up)), [ + 'p.ts: set in "lost" in this change\'s .github/fork-seams.json ({"lines": 2, "gone": 2}), but the fork loses 0 upstream lines over upstream (abc) (deleted, moved or re-indented), 0 of them gone: remove it from "lost" (a count above the lines lost is room to swap one for a fork line later).', + ]); + assert.match( + findLostNotUpstream(["p.ts"], {}, at("a();\nc();\n"))[0], + /^p\.ts: dropped from "lost" .*\(no entry\), but the fork loses 1 upstream line over .*, 1 of them gone: set it to {"lines": 1, "gone": 1}/, + ); + assert.match( + findLostNotUpstream( + ["p.ts"], + { "p.ts": { lines: 1, gone: 1 } }, + { ...at(up), atUpstream: () => undefined }, + )[0], + /upstream \(abc\) does not have it, so its lost lines cannot be counted/, + ); + assert.match( + findLostNotUpstream(["p.ts"], { "p.ts": { lines: 1, gone: 1 } }, at(undefined))[0], + /but the head does not have it/, + ); + assert.deepEqual(findLostNotUpstream(["p.ts"], {}, at(undefined)), []); + const has = () => "x"; + const files = ["src/a.ts", "src/b.ts", "tools/t.ts"]; + assert.deepEqual(findOwnHiddenFiles(files, ["tools/"], [], has).length, 1); + assert.deepEqual(findOwnHiddenFiles(files, ["tools/"], ["tools/"], has), []); + assert.deepEqual( + findOwnHiddenFiles(files, ["src/", "!src/b.ts"], undefined, has).map((r) => r.split(":")[0]), + ["src/a.ts"], + ); + assert.deepEqual( + findOwnHiddenFiles(files, ["tools/"], [], () => undefined), + [], + ); +}); + test("listEditedFiles: only a line listed more or less often, and every entry when the base list is unreadable", () => { const entry = (...added) => ({ why: "w", added }); const base = { "a.ts": entry("x();", "y();"), "b.ts": entry() }; @@ -3807,15 +4017,21 @@ test("findListedNotFork: exact copies both ways, a file upstream lacks, a droppe 'p.ts: dropped from "files" in this change\'s .github/fork-seams.json, but the fork adds 2 lines over upstream (abc): keep its entry, listing each (every fork line the file holds is listed).', ]); assert.deepEqual(findListedNotFork(["p.ts"], {}, at(up, none, { "p.ts": 2 })), []); + assert.deepEqual(findListedNotFork(["p.ts"], {}, at(up, none, { "p.ts": 1 })), [ + "p.ts: \"pending\" with a ceiling of 1 in this change's .github/fork-seams.json, but the fork adds 2 lines over upstream (abc): set its ceiling to 2 (a ceiling above the count is room for a fork line later; below it, fork-follow's next run refuses main).", + ]); assert.match( - findListedNotFork(["p.ts"], {}, at(up, none, { "p.ts": 1 }))[0], - /adds 2 lines over upstream \(abc\), not its "pending" ceiling of 1: keep its entry/, - ); - // A file the head deletes is refused elsewhere. - assert.deepEqual( - findListedNotFork(["p.ts"], promoted("x();"), { ...at(up), atHead: () => undefined }), - [], + findListedNotFork(["p.ts"], {}, at(head, none, { "p.ts": 3 }))[0], + /ceiling of 3 .*, but the fork adds 0 lines over upstream \(abc\): remove it from "pending"/, ); + // An entry for a file the head does not have is refused; a dropped one is a deletion, refused + // elsewhere (or right). + const gone = { ...at(up), atHead: () => undefined }; + assert.deepEqual(findListedNotFork(["p.ts"], promoted("x();"), gone), [ + 'p.ts: moved to "files" in this change\'s .github/fork-seams.json, but the head does not have it: list only a file this change keeps.', + ]); + assert.equal(findListedNotFork(["p.ts"], {}, { ...gone, ownPending: { "p.ts": 1 } }).length, 1); + assert.deepEqual(findListedNotFork(["p.ts"], {}, { ...gone, promoted: none }), []); assert.deepEqual( promotedFiles( { pending: { "p.ts": 1, "q.ts": 1, "r.ts": 1 } }, diff --git a/docs/fork/updates.md b/docs/fork/updates.md index 16cff18d1cdcb..bb93b469df943 100644 --- a/docs/fork/updates.md +++ b/docs/fork/updates.md @@ -78,13 +78,20 @@ watches; it changes nothing on the machine, and `0` turns it off. `added` to exactly the head's fork lines counted from the merge-base of main and upstream's head, both ways: a line listed more often than the fork adds it (a raised count with no copy added, or a line upstream has) is spare room, and a line listed less often (dropped while still - in use) would stop the next fork-follow; both are refused in the pull request's own run. A list-only - change that fixes main's list is counted the same way, so it passes. A change that changes no - such entry fetches nothing. Every way the count cannot run is refused, naming why, never + in use) would stop the next fork-follow; both are refused in the pull request's own run. The same + run holds every `pending` ceiling the own list sets, raises, lowers or drops to the fork lines + counted from there, every `lost` entry it changes to the lines lost from there (both as + fork-follow counts them), and a changed `forkFiles` to taking no upstream file the merge-base's + list did not: raised with no line changed, a ceiling or `lost` count is room for a later change to + swap upstream lines for fork lines within it. An entry for a file the head does not have is + refused too. A list-only change that fixes main's list is counted the same way, so it passes. A + change that changes no such entry fetches nothing. Every way the count cannot run is refused, naming why, never skipped: no upstream file on main, a malformed one (or one not `https://`), a shallow clone, an upstream that cannot be fetched within two minutes (two tries), one that shares no commit with main or holds main itself (the URL names the fork), or a branch without the upstream commit main - sits on (update the branch). Run it again once the cause is fixed; to check an entry by hand, + sits on (update the branch). That refusal says it is an upstream, fetch or branch problem, not a + wrong list: run it again once the cause is fixed ("Re-run failed jobs" on the Fork checks run, + which runs main's script on the same commits, or push to the branch); to check an entry by hand, `node .github/scripts/fork-seam-check.cjs --from-upstream "$(git merge-base origin/main upstream/main)" ` (upstream's main fetched as `upstream/main`). Fork tests' run on the merge commit has only two commits, so it passes `--no-upstream` and leaves the count @@ -127,7 +134,8 @@ watches; it changes nothing on the machine, and `0` turns it off. lands: fork-follow's run before the rebase refuses a spare copy and never lowers it, so an old list with spare copies stops one fork-follow run otherwise. It also adds `.github/fork-upstream.json` naming upstream's `https://` git URL: without it, every pull request - that moves a pending file or changes a seam's `added` is refused. + that moves a pending file or changes a seam's `added`, a ceiling, a `lost` entry or `forkFiles` + is refused. Dropped lines are counted by text, so where upstream has a listed line too (`}`), a pull request may drop upstream's copy instead of the fork's and its run passes it; fork-follow, which counts from upstream, refuses it unless the lines left read the same as with the fork's copy dropped. In a pending file it cannot tell @@ -256,14 +264,15 @@ A new `forkFiles` pattern needs no announcement: a list-only pull request adds i request that edits those files follows. A `pending` file is not for new fork lines: its lines predate the rule and only move out. So only the operator adds a `pending` file or raises a ceiling or a `lost` count, after upstream's own change made the count rise: in the rebased series, or with -a list-only pull request to main once it is pushed (fork-follow's refusal says how). This rule -rests on the owner's review, not on code: a pull request's own list is only parsed, so no check -refuses one that adds a `pending` file or raises a ceiling or count. What the code does hold: - -- A pull request's run is judged by main's list and reads no `lost` count, and a ceiling only to - refuse a change that shrinks a pending file without lowering it in its own list: a pending file - may change but not grow, so a raised ceiling or count gives no pull request a line. A new - `pending` entry does loosen it: that file's lines may then change, though not grow in number. +a list-only pull request to main once it is pushed (fork-follow's refusal says how). The code +holds this: a pull request's run counts every ceiling and `lost` entry its own list changes from +upstream (above), so a new or raised one passes only when the tree already has those lines, as +after upstream's own change. What else it holds: + +- A pull request's run is judged by main's list. Main's `lost` counts are fork-follow's to check, + and main's ceilings refuse a change that shrinks a pending file without lowering it in its own + list: a pending file may change but not grow. A new `pending` entry (counted, so only for lines + the tree already has) does loosen it: that file's lines may then change, though not grow. - From upstream, fork-follow refuses a ceiling or count above the real count ("lower it", or "remove it" when none is left), so room nobody uses does not stay. Right after a rebase it lowers it in a commit of its own on the rebased series instead (only lowers: a rise is refused).