diff --git a/changelog.d/release-notes-keep-versions-and-headings.md b/changelog.d/release-notes-keep-versions-and-headings.md new file mode 100644 index 000000000..ecfc3e7c2 --- /dev/null +++ b/changelog.d/release-notes-keep-versions-and-headings.md @@ -0,0 +1,3 @@ +### For contributors + +- **A release no longer rewrites version numbers inside changelog entries, counts subsection headings as changes, or prints a subsection heading twice.** `gstack-restamp --release` copies each `changelog.d/` entry as written, even where the CHANGELOG is one of the policy's `stamp_files`, so a note that quotes the version being released from keeps pointing at it. The release PR lists one line per change, and entries that share a heading such as `### For contributors` sit under one copy of it. To correct an entry already on main, land a small PR before the release that deletes it and adds the fixed text under a new file name. diff --git a/docs/ship-policy.md b/docs/ship-policy.md index 68b617cf8..c2225d589 100644 --- a/docs/ship-policy.md +++ b/docs/ship-policy.md @@ -2803,10 +2803,15 @@ writes no version in Step 12 and writes Step 13's entry as **The fold.** `gstack-restamp` (and so `land`) concatenates the fragment bodies in path order above any legacy `[Unreleased]` section, re-heads both as one `## [] - ` section at the top, and deletes the fragment -files, in one stamp. The tree receipt, and so `gstack-pregate clear`'s +files, in one stamp. Same-named `### ` subsections of the fragments merge into +one: their unheaded entries come first, then each subsection once, in the order +it first appears, with its entries in fold order, then the legacy text. A +`### ` line inside a fenced block is text. Every other line is copied +as written, and no version in it is rewritten, even when the CHANGELOG is one +of the policy's `stamp_files`. The tree receipt, and so `gstack-pregate clear`'s stamp-only substitution, calls a deleted fragment a stamp (rule -`changelog_fragments`) only when the new CHANGELOG section is exactly the -deleted fragments' bodies in path order above any legacy `[Unreleased]` text +`changelog_fragments`) only when the new CHANGELOG section is exactly what the +fold makes of the deleted fragments' bodies and any legacy `[Unreleased]` text (rule `changelog-fragments`); an edited fold, or a fragment deleted without its text, is a change. @@ -3349,7 +3354,9 @@ bump any fragment declares (the policy `bump` when none does). It reruns the release tool, and it moves a tentative `stamp_paths` mention (a `*_KEY_VERSION` a PR wrote ahead of its release) on lines added since the last version change. With no fragment on main it refuses with -[`RELEASE_STAMP_EMPTY`](troubleshooting.md#release-stamp-empty). +[`RELEASE_STAMP_EMPTY`](troubleshooting.md#release-stamp-empty). The release +PR's body lists one line per change, never a subsection heading. +To correct a fragment already on main, land a small PR before the stamp that deletes it and adds the corrected text under a new file name (an edit in place is `CHANGELOG_FRAGMENT_INVALID`): `--release` refuses any head but a clean `origin/main`, so the release itself never carries the fix. When the release PR's checks are green, the Captain lands it like any other PR: `gstack-ship-queue land --pr --pin `. On a `release/` diff --git a/lib/changelog-fragments.ts b/lib/changelog-fragments.ts index 903cd4275..cddbb9fe1 100644 --- a/lib/changelog-fragments.ts +++ b/lib/changelog-fragments.ts @@ -85,8 +85,10 @@ export function readFragments(cwd: string, treeish: string | null, dir: string, const shown = exclude && changelog ? readHistoryFile(cwd, exclude, changelog) : null; const released = shown !== null ? parseChangelog(shown.replace(/\r\n/g, '\n')).sections.filter(s => !s.unreleased).map(s => s.lines) : []; const isReleased = (body: string) => { - const want = body.split('\n'); - return released.some(lines => lines.some((_, i) => want.every((w, k) => lines[i + k] === w))); + const at = (lines: string[], want: string[]) => lines.some((_, i) => want.every((w, k) => lines[i + k] === w)); + const parts = subsections(body.split('\n')).map(p => ({ name: p.name, run: trimBlank(p.run, true) })).filter(p => p.run.length); + if (!parts.length) return released.some(lines => at(lines, body.split('\n'))); + return released.some(lines => { const own = subsections(lines); return parts.every(({ name, run }) => (name ? own.filter(p => p.name === name).some(p => at(p.run, run)) : at(lines, run))); }); }; const fragments: Fragment[] = []; const errors: FragmentError[] = []; @@ -117,26 +119,62 @@ export function effectiveBump(level: Bump, fragments: readonly Fragment[]): Bump return fragments.reduce((acc, f) => (f.bump && BUMP_RANK[f.bump] > BUMP_RANK[acc] ? f.bump : acc), level); } +/** Lines with blank lines dropped from the end, and from the start too when `start` is set. */ +function trimBlank(lines: string[], start: boolean): string[] { + const out = [...lines]; + while (out.length && !out[out.length - 1]!.trim()) out.pop(); + while (start && out.length && !out[0]!.trim()) out.shift(); + return out; +} + +/** A fragment body cut at its `### ` subsection headings, never inside a fenced block: each part's heading ('' before the first) and its lines as written. */ +function subsections(lines: readonly string[]): Array<{ name: string; run: string[] }> { + const parts: Array<{ name: string; run: string[] }> = [{ name: '', run: [] }]; + let fence: string | null = null; + for (const line of lines) { + const open = /^\s*(`{3,}|~{3,})/.exec(line)?.[1]; + const close = /^\s*(`{3,}|~{3,})\s*$/.exec(line)?.[1]; + if (fence === null && open) fence = open; + else if (fence !== null && close && close[0] === fence[0] && close.length >= fence.length) fence = null; + else if (fence === null && /^###\s+\S/.test(line)) { parts.push({ name: line.trim(), run: [] }); continue; } + parts[parts.length - 1]!.run.push(line); + } + return parts; +} + /** * Put the fragment bodies, in path order, into the CHANGELOG's `[Unreleased]` section (created at the * top when absent), so the existing re-heading moves fragments and a legacy entry together under one - * version. Returns the text unchanged when there are no fragments. + * version. Same-named `### ` subsections merge into one (#4785: two fragments each brought + * "### For contributors"): the fragments' unheaded entries come first, then each subsection once in the + * order it first appears, its entries in fold order, then any legacy `[Unreleased]` text as it stood + * (crew/scripts/release-notes.ts reads the fragments' changes first). Every other line is copied as + * written. Returns the text unchanged when there are no fragments. */ export function foldIntoUnreleased(text: string, fragments: readonly Fragment[]): string { if (!fragments.length) return text; const parsed = parseChangelog(text); - const bodies = fragments.map(f => f.body).join('\n\n'); const unreleased = parsed.sections.find(s => s.unreleased); const lines = text.replace(/\r\n/g, '\n').split('\n'); - if (unreleased) { - const own = lines.slice(unreleased.start + 1, unreleased.end); - while (own.length && !own[0]!.trim()) own.shift(); - while (own.length && !own[own.length - 1]!.trim()) own.pop(); - const merged = [lines[unreleased.start]!, '', ...bodies.split('\n'), ...(own.length ? ['', ...own] : []), '']; - return [...lines.slice(0, unreleased.start), ...merged, ...lines.slice(unreleased.end)].join('\n'); + const own = trimBlank(unreleased ? lines.slice(unreleased.start + 1, unreleased.end) : [], true); + const groups = new Map([['', []]]); + for (const f of fragments) for (const { name, run } of subsections(f.body.split('\n'))) { + if (!groups.has(name)) groups.set(name, []); + const runs = groups.get(name)!; + const kept = trimBlank(run, !name || runs.length > 0); + if (kept.some(l => l.trim())) runs.push(kept); } + const section: string[] = []; + for (const [name, runs] of groups) { + if (!name && !runs.length) continue; + if (section.length) section.push(''); + if (name) section.push(name); + runs.forEach((r, i) => section.push(...(i ? ['', ...r] : r))); + } + if (own.length) section.push('', ...own); + if (unreleased) return [...lines.slice(0, unreleased.start), lines[unreleased.start]!, '', ...section, '', ...lines.slice(unreleased.end)].join('\n'); const at = parsed.sections[0]?.start ?? lines.length; const head = lines.slice(0, at); while (head.length && !head[head.length - 1]!.trim()) head.pop(); - return [...head, ...(head.length ? [''] : []), '## [Unreleased]', '', ...bodies.split('\n'), '', ...lines.slice(at)].join('\n'); + return [...head, ...(head.length ? [''] : []), '## [Unreleased]', '', ...section, '', ...lines.slice(at)].join('\n'); } diff --git a/lib/release-stamp.ts b/lib/release-stamp.ts index 6dccde13f..8e13bae89 100644 --- a/lib/release-stamp.ts +++ b/lib/release-stamp.ts @@ -26,12 +26,13 @@ export const RELEASE_BRANCH = /^release\/(\d+(?:\.\d+){2,3})$/; /** * The release notes (R76): one plain line per change the release folds, from each changelog fragment's entries (a * fragment may hold several `- ` entries): the bold lead sentence, or else the first sentence, with the pull request - * whose squash commit added the fragment (`(#n)` at the end of its subject) when git finds one. + * whose squash commit added the fragment (`(#n)` at the end of its subject) when git finds one. A heading such as + * `### For contributors` groups entries and is never a change of its own (#4785). */ export function releaseNoteLines(root: string, ref: string, fragments: readonly string[]): string[] { return fragments.flatMap(file => { const pr = /\(#(\d+)\)\s*$/.exec(git(root, ['log', '-1', '--diff-filter=A', '--format=%s', ref, '--', file]).out)?.[1]; - const text = git(root, ['show', `${ref}:${file}`]).out.replace(/^---\n[\s\S]*?\n---\n/, '').trim(); + const text = git(root, ['show', `${ref}:${file}`]).out.replace(/^---\n[\s\S]*?\n---\n/, '').replace(/^#{1,6}(?:[ \t].*)?$/gm, '').trim(); return text.split(/\n(?=- )/).map(entry => { const first = entry.replace(/^- +/, '').replace(/\s+/g, ' ').trim(); const lead = (/^\*\*(.+?)\*\*/.exec(first)?.[1] ?? first.split(/(?<=[.!?])\s/)[0] ?? '').replace(/\*\*/g, '').trim(); diff --git a/lib/restamp.ts b/lib/restamp.ts index 0266cec37..038fc798e 100644 --- a/lib/restamp.ts +++ b/lib/restamp.ts @@ -532,13 +532,14 @@ function runStages(o: RestampOptions, plan: Plan, staging: string, since: string * lost its slot and merged the base). Then a stamp-form mention of the base * version on a line the branch added is its own tentative stamp, stale now, * and moves; the same version on a line the base already carries is the - * base's release and stays. The detail names both counts per file. + * base's release and stays. The detail names both counts per file. Under changelog_fragments the CHANGELOG is never + * rewritten here, even as a stamp file: the changelog stage heads the new section, and folded prose stays as written (GBRA-784). */ function rewriteStampPaths(o: RestampOptions, plan: Plan, staging: string, since: string, sp: StageRecord): void { const stampGlobs = [...plan.policy.stamp_paths, ...plan.policy.stamp_files]; const templates = plan.policy.stamp_patterns; if (stampGlobs.length === 0) { sp.status = 'skipped'; sp.detail = 'no stamp_paths in policy'; return; } - const files = git(staging, ['ls-files', '-z']).out.split('\0').filter(f => f && matchesAny(stampGlobs, f)); + const files = git(staging, ['ls-files', '-z']).out.split('\0').filter(f => f && matchesAny(stampGlobs, f) && !(plan.policy.changelog_fragments && f === plan.policy.changelog)); for (const rel of files) { const why = assertInsideRoot(staging, rel); if (why) throw new Refusal('POLICY_CONTAINMENT', `${why}; nothing written`); diff --git a/test/changelog-fragments.test.ts b/test/changelog-fragments.test.ts index 0e5509448..c4a5042d5 100644 --- a/test/changelog-fragments.test.ts +++ b/test/changelog-fragments.test.ts @@ -68,6 +68,23 @@ describe('effectiveBump and the fold', () => { const r = reheadChangelog(folded, { from: null, to: '1.2.4.0', date: '2026-10-29' }); expect(r.text).toBe('# Changelog\n\n## [1.2.4.0] - 2026-10-29\n\n- a\n\n- b\n\n- legacy\n\n## [1.2.3.0] - 2026-10-01\n\n- base\n'); }); + // Value: protects=one copy of each subsection heading per release section, every entry's lines as written; + // fails_when=fragments each bring "### For contributors" and the section carries it twice (#4785); seam=foldIntoUnreleased + test('same-named subsection headings merge into one, unheaded entries first, legacy text last, and a heading inside a code fence is prose', () => { + const cl = '# Changelog\n\n## [Unreleased]\n\n- legacy\n\n## [1.2.3.0] - 2026-10-01\n\n- base\n'; + const folded = foldIntoUnreleased(cl, [ + { path: 'changelog.d/a.md', body: '### For contributors\n\n- a', bump: null }, + { path: 'changelog.d/b.md', body: '**b lead.**\n\n### Fixed\n- b fix\n\n~~~md\n```\n### not a heading\n~~~', bump: null }, + { path: 'changelog.d/c.md', body: '- c\n\n### For contributors\n\n- c contrib', bump: null }, + ]); + expect(folded).toBe('# Changelog\n\n## [Unreleased]\n\n**b lead.**\n\n- c\n\n### For contributors\n\n- a\n\n- c contrib\n\n### Fixed\n- b fix\n\n~~~md\n```\n### not a heading\n~~~\n\n- legacy\n\n## [1.2.3.0] - 2026-10-01\n\n- base\n'); + }); + test('a fence marker with text after it never closes a fence', () => { + const cl = '# Changelog\n\n## [1.2.3.0] - 2026-10-01\n\n- base\n'; + const body = '### Added\n\n- a\n\n~~~md\n~~~text\n### inside\n~~~'; + const folded = foldIntoUnreleased(cl, [{ path: 'changelog.d/a.md', body, bump: null }, { path: 'changelog.d/b.md', body: '### inside\n\n- b', bump: null }]); + expect(folded).toBe(`# Changelog\n\n## [Unreleased]\n\n${body}\n\n### inside\n\n- b\n\n## [1.2.3.0] - 2026-10-01\n\n- base\n`); + }); test('no fragments leaves the text unchanged', () => { expect(foldIntoUnreleased('# C\n', [])).toBe('# C\n'); }); @@ -206,4 +223,34 @@ describe('readFragments: text already released at the base', () => { expect(set.errors.map(e => e.path)).toEqual(['changelog.d/carried.md']); expect(set.errors[0]!.message).toContain('already released in CHANGELOG.md'); }); + + // Value: protects=a carried-back fragment whose heading the fold shared with another is still found released; + // fails_when=the second "### For contributors" fragment of v1.103.2.0 folds again because its heading no longer sits + // right above its body; seam=readFragments with the base CHANGELOG + test('a released fragment whose subsection heading merged with another still refuses', () => { + const repo = fixture(); + const dir = path.join(import.meta.dir, 'fixtures', 'release-stamp', 'v1.103.2.0'); + const frag = (name: string) => parseFragment(`changelog.d/${name}`, fs.readFileSync(path.join(dir, name), 'utf8')) as { path: string; body: string; bump: null }; + const shared = ['stable-benchmark-record-text-hardening.md', 'stable-release-reads-weekly-benchmark.md']; + const folded = reheadChangelog(foldIntoUnreleased(readFile(repo, 'CHANGELOG.md'), shared.map(frag)), { from: null, to: '1.2.4.0', date: '2026-11-24' }).text; + expect(folded.match(/^### For contributors$/gm)).toHaveLength(1); + fs.writeFileSync(path.join(repo.work, 'CHANGELOG.md'), folded); + repo.git('commit', '-qam', 'release 1.2.4.0'); + repo.git('push', '-q', 'origin', 'HEAD:main'); + repo.git('fetch', '-q', 'origin'); + fragmentPr(repo, 'feat-carry', { [shared[1]!]: fs.readFileSync(path.join(dir, shared[1]!), 'utf8') }); + const set = readFragments(repo.work, 'HEAD', 'changelog.d', 'origin/main', 'CHANGELOG.md'); + expect(set.errors.map(e => e.path)).toEqual([`changelog.d/${shared[1]}`]); + }); + + test('a new fragment whose entry was released only under another subsection is not refused', () => { + const repo = fixture(); + fs.writeFileSync(path.join(repo.work, 'CHANGELOG.md'), readFile(repo, 'CHANGELOG.md').replace('- base release\n', '- base release\n\n### Added\n\n- older addition\n\n### Fixed\n\n- **A shared line.**\n')); + repo.git('commit', '-qam', 'released subsections'); + repo.git('push', '-q', 'origin', 'HEAD:main'); + repo.git('fetch', '-q', 'origin'); + fragmentPr(repo, 'feat-added', { 'added.md': '### Added\n\n- **A shared line.**\n' }); + const set = readFragments(repo.work, 'HEAD', 'changelog.d', 'origin/main', 'CHANGELOG.md'); + expect([set.errors, set.fragments.map(f => f.path)]).toEqual([[], ['changelog.d/added.md']]); + }); }); diff --git a/test/fixtures/release-stamp/gbrain-d7278e4c/capy-ghost-pages-move-evidence.md b/test/fixtures/release-stamp/gbrain-d7278e4c/capy-ghost-pages-move-evidence.md new file mode 100644 index 000000000..98edb8502 --- /dev/null +++ b/test/fixtures/release-stamp/gbrain-d7278e4c/capy-ghost-pages-move-evidence.md @@ -0,0 +1,13 @@ +--- +important: data-loss +--- +**`gbrain repair ghost-pages` no longer soft-deletes a page whose file moved to another page, and `gbrain repair moved-pages` folds a file that was renamed and edited in one commit.** + +### Fixed +- `gbrain repair ghost-pages` approved a page whose file a commit deleted for soft-delete even when Git shows the file moved to another live page, so the old page's links, facts and timeline were purged 72 hours later instead of carried over. The preview now lists such a page apart as `moved_to`, names the fold (`gbrain repair moved-pages --source --slug `, which previews first and applies only with `--apply --expect --yes`) and never approves it. A page whose path Git renamed, or whose last committed content a file in HEAD still holds, but whose fold is not proven to lose nothing, is kept as `move_unproven`. +- A `database_ahead_of_file` ghost now carries move evidence in the preview: `matched` when a moved page holds every line of it, database edit included, with the fold to run, and `unmatched` otherwise, naming any file that looks like where it went. +- `gbrain repair moved-pages` folded only a move that kept the file byte for byte. It now also folds a file the deleting commit renamed (or the one live file with the same name that Git first added after the old file and no later than its deletion) when the moved page holds every line of the old page's body and timeline in order, its type, its tags and its frontmatter, and a strict move whose old page was edited in the database when the moved page holds that edit. A move whose lines changed stays held as before. +- `gbrain extract --stale` crashed with `TypeError: undefined is not an object` on a page stored in segments (over 1 MiB) whose saved timeline derivation named a segment the page no longer holds. The incremental timeline read now falls back to the full pass, and saved timeline derivations are re-derived once. +- Correction to the v0.60.769.0 note on `file_drift` holds: it said a plain `gbrain sync` never re-checks a hold an earlier release wrote. That release adds no automatic re-check of its own, but a hold written before screen version 3 is still re-checked by every sync, as earlier releases already did. + + diff --git a/test/fixtures/release-stamp/v1.103.2.0/make-pdf-nul-cannot-revive-handlers.md b/test/fixtures/release-stamp/v1.103.2.0/make-pdf-nul-cannot-revive-handlers.md new file mode 100644 index 000000000..7b21bf770 --- /dev/null +++ b/test/fixtures/release-stamp/v1.103.2.0/make-pdf-nul-cannot-revive-handlers.md @@ -0,0 +1,4 @@ +--- +important: security +--- +**make-pdf no longer lets a NUL character hide an `onerror` handler, a `javascript:` link or a `