@@ -540,6 +540,60 @@ function findLostLines(config, { atBase, atHead, upstream, afterRebase = false, |
540 | 540 | return { reasons, ceilings, rises, notes }; |
541 | 541 | } |
542 | 542 |
|
| 543 | +/** |
| 544 | + * Spare listed "added" copies: room for a line nobody announced. From upstream, a copy the head does |
| 545 | + * not add over the base is refused (after a rebase: a note, and `lowered.added` keeps the copies in |
| 546 | + * use). A pull request that drops copies lists exactly main's copies less those in `ownFiles`. |
| 547 | + */ |
| 548 | +function findSpareSeamLines( |
| 549 | + files, |
| 550 | + { atBase, atHead, upstream, afterRebase = false, ownFiles, lowered }, |
| 551 | +) { |
| 552 | + const ceilings = []; |
| 553 | + const notes = []; |
| 554 | + const times = (count) => (count === 1 ? "" : ` ${count}×`); |
| 555 | + for (const [file, seam] of Object.entries(files)) { |
| 556 | + const base = atBase(file); |
| 557 | + const head = atHead(file); |
| 558 | + if (base === undefined || head === undefined) continue; // Refused (or noted) elsewhere. |
| 559 | + const listed = lineCounts(seam.added.join("\n")); |
| 560 | + const baseCounts = lineCounts(base); |
| 561 | + const headCounts = lineCounts(head); |
| 562 | + if (upstream) { |
| 563 | + const used = new Map(); |
| 564 | + const spare = []; |
| 565 | + for (const [line, count] of listed) { |
| 566 | + const over = Math.max(0, (headCounts.get(line) ?? 0) - (baseCounts.get(line) ?? 0)); |
| 567 | + used.set(line, Math.min(count, over)); |
| 568 | + if (over >= count) continue; |
| 569 | + spare.push( |
| 570 | + `${file}: "added" lists \`${line}\`${times(count)}, but the fork adds ${over === 0 ? "no copy" : `${over}×`} over upstream; ${over === 0 ? 'remove it from "added"' : `list it ${over}×`} (a spare copy lets a later change add the line unannounced).`, |
| 571 | + ); |
| 572 | + } |
| 573 | + if (spare.length > 0) { |
| 574 | + lowered?.added.set(file, used); |
| 575 | + (afterRebase ? notes : ceilings).push(...spare); |
| 576 | + } |
| 577 | + continue; |
| 578 | + } |
| 579 | + if (ownFiles === undefined) continue; |
| 580 | + const own = Object.hasOwn(ownFiles, file) |
| 581 | + ? lineCounts(ownFiles[file].added.join("\n")) |
| 582 | + : new Map(); |
| 583 | + for (const [line, count] of listed) { |
| 584 | + const dropped = (baseCounts.get(line) ?? 0) - (headCounts.get(line) ?? 0); |
| 585 | + if (dropped <= 0) continue; |
| 586 | + const want = Math.max(0, count - dropped); |
| 587 | + const has = own.get(line) ?? 0; |
| 588 | + if (has === want) continue; |
| 589 | + ceilings.push( |
| 590 | + `${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).`, |
| 591 | + ); |
| 592 | + } |
| 593 | + } |
| 594 | + return { ceilings, notes }; |
| 595 | +} |
| 596 | + |
543 | 597 | /** |
544 | 598 | * Listed seam and pending paths that neither the base nor the head has: nothing there to guard. |
545 | 599 | * Upstream renamed (or removed) the file, so the list names both paths until the base moves on, and |
@@ -881,10 +935,9 @@ const MOVED_WHY = |
881 | 935 | 'Was "pending": fork-follow moved it here once it had no fork line left, as its "lost" entry still counts upstream lines it loses. No seam line: a fork line goes in a file of the fork\'s own.'; |
882 | 936 |
|
883 | 937 | /** |
884 | | - * `text` with `lowered` ({ pending, lost } Maps of counts, `hidden` Set of upstream files) applied, as |
885 | | - * { text, changes }, or undefined when nothing changes. Only lowers; a count at 0 removes the entry; a |
886 | | - * "pending" file with no fork line left moves to "files"; each hidden file stops being a fork file |
887 | | - * (its own "forkFiles" entry goes, or a `!` entry excludes it). Parsed again before it is returned. |
| 938 | + * `text` with `lowered` (pending/lost/added Maps, `hidden` Set) applied, as { text, changes }, or |
| 939 | + * undefined. Only lowers: a 0 count or spare "added" copy goes, an emptied "pending" file moves to |
| 940 | + * "files", a hidden file stops being a fork file ("forkFiles" entry or `!`). Parsed again after. |
888 | 941 | */ |
889 | 942 | function lowerList(text, lowered) { |
890 | 943 | const raw = JSON.parse(text); |
@@ -946,6 +999,26 @@ function lowerList(text, lowered) { |
946 | 999 | `${file}: "forkFiles" ${what} (upstream has it; a pull request's run took it for a fork file)`, |
947 | 1000 | ); |
948 | 1001 | } |
| 1002 | + // Spare "added" copies go, the first listed copies stay: only narrows what a change may add. |
| 1003 | + for (const [file, used] of lowered.added ?? []) { |
| 1004 | + if (!Object.hasOwn(raw.files ?? {}, file)) continue; |
| 1005 | + const seam = raw.files[file]; |
| 1006 | + const kept = new Map(); |
| 1007 | + const removed = []; |
| 1008 | + const added = seam.added.filter((item) => { |
| 1009 | + const line = item.trim(); |
| 1010 | + const count = (kept.get(line) ?? 0) + 1; |
| 1011 | + kept.set(line, count); |
| 1012 | + if (count <= (used.get(line) ?? Infinity)) return true; |
| 1013 | + removed.push(line); |
| 1014 | + return false; |
| 1015 | + }); |
| 1016 | + if (removed.length === 0) continue; |
| 1017 | + changes.push( |
| 1018 | + `${file}: "added" ${seam.added.length} -> ${added.length} lines (spare: ${removed.map((line) => `\`${line}\``).join(", ")})`, |
| 1019 | + ); |
| 1020 | + seam.added = added; |
| 1021 | + } |
949 | 1022 | // Appended through defineProperty: a file named `__proto__` stays an own key. |
950 | 1023 | for (const file of moved) { |
951 | 1024 | Object.defineProperty(raw.files, file, { |
@@ -1059,7 +1132,7 @@ function main(argv) { |
1059 | 1132 | const lowered = |
1060 | 1133 | parsed.loweredList === undefined |
1061 | 1134 | ? undefined |
1062 | | - : { pending: new Map(), lost: new Map(), hidden: new Set() }; |
| 1135 | + : { pending: new Map(), lost: new Map(), added: new Map(), hidden: new Set() }; |
1063 | 1136 | const unlisted = findUnlistedChanges(config, { |
1064 | 1137 | changed, |
1065 | 1138 | atBase, |
@@ -1082,9 +1155,17 @@ function main(argv) { |
1082 | 1155 | afterRebase: parsed.afterRebase, |
1083 | 1156 | lowered, |
1084 | 1157 | }); |
| 1158 | + const spare = findSpareSeamLines(config.files, { |
| 1159 | + atBase, |
| 1160 | + atHead, |
| 1161 | + upstream, |
| 1162 | + afterRebase: parsed.afterRebase, |
| 1163 | + ownFiles, |
| 1164 | + lowered, |
| 1165 | + }); |
1085 | 1166 | // A ceiling above the count is fixed by lowering it, not by moving lines: its own hint. A count |
1086 | 1167 | // that rose with a rebase names the operator's step in its own text. |
1087 | | - const ceilings = [...unlisted.ceilings, ...lost.ceilings]; |
| 1168 | + const ceilings = [...unlisted.ceilings, ...lost.ceilings, ...spare.ceilings]; |
1088 | 1169 | const rises = [...unlisted.rises, ...lost.rises]; |
1089 | 1170 | // A pull request's run dates an announcement by the commit whose list judges it (main's): its |
1090 | 1171 | // own author sets its own date, and fork-checks' base can be older than the list's commit. |
@@ -1122,7 +1203,12 @@ function main(argv) { |
1122 | 1203 | ) |
1123 | 1204 | : []; |
1124 | 1205 | const from = parsed.configRef ? `--config-ref ${configRef}` : upstream ? "the head" : "the base"; |
1125 | | - const notes = [...findListedGone(config, { atBase, atHead }), ...unlisted.notes, ...lost.notes]; |
| 1206 | + const notes = [ |
| 1207 | + ...findListedGone(config, { atBase, atHead }), |
| 1208 | + ...unlisted.notes, |
| 1209 | + ...lost.notes, |
| 1210 | + ...spare.notes, |
| 1211 | + ]; |
1126 | 1212 | for (const note of notes) console.log(printable(`Note: ${note}`)); |
1127 | 1213 | if (editsList) { |
1128 | 1214 | console.log( |
@@ -1184,7 +1270,7 @@ function main(argv) { |
1184 | 1270 | } |
1185 | 1271 | if (ceilings.length > 0) { |
1186 | 1272 | console.error( |
1187 | | - `Lower each "pending" ceiling and "lost" count named above (or remove the entry, or set the value its line names) in ${CONFIG}: in this change's list where the line says so, otherwise with a pull request to main.`, |
| 1273 | + `Lower each "pending" ceiling and "lost" count, and drop each spare "added" copy, named above (or remove the entry, or set the value its line names) in ${CONFIG}: in this change's list where the line says so, otherwise with a pull request to main.`, |
1188 | 1274 | ); |
1189 | 1275 | } |
1190 | 1276 | if (late.length > 0) { |
@@ -1216,6 +1302,7 @@ module.exports = { |
1216 | 1302 | findListedGone, |
1217 | 1303 | findLostLines, |
1218 | 1304 | findSeamBreaches, |
| 1305 | + findSpareSeamLines, |
1219 | 1306 | findStaleSeamLines, |
1220 | 1307 | findUnlistedChanges, |
1221 | 1308 | forkFileTest, |
|