Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(release): restamp stamps an [Unreleased] CHANGELOG entry; --dry-run runs the same rebuild (GBRA-40)

time-attacka1b75b309c85
diff --git a/scripts/lib/restamp.ts b/scripts/lib/restamp.ts
index f303c04fd2..486d2449be 100644
--- a/scripts/lib/restamp.ts
+++ b/scripts/lib/restamp.ts
@@ -72,8 +72,15 @@ export function versionOfHeader(header: string): string | null {
return /^## \[([^\]]+)\]/.exec(header)?.[1] ?? null;
}
+const VERSION_TOKEN = /^\d+(?:\.\d+){2,3}(?:-[A-Za-z0-9]+)?$/;
+
+/** A release header's version token (`0.60.1.0`); `Unreleased` and other labels are not. */
+export function isVersionToken(token: string): boolean {
+ return VERSION_TOKEN.test(token);
+}
+
function replaceVersionToken(text: string, oldVersion: string, newVersion: string): string {
- if (!/^\d+(?:\.\d+){2,3}(?:-[A-Za-z0-9]+)?$/.test(oldVersion)) throw new Error(`restamp: refusing to rewrite a non-numeric version token '${oldVersion}'`);
+ if (!VERSION_TOKEN.test(oldVersion)) throw new Error(`restamp: refusing to rewrite a non-numeric version token '${oldVersion}'`);
const esc = oldVersion.replace(/\./g, '\\.');
// nosemgrep: javascript.lang.security.audit.detect-non-literal-regexp.detect-non-literal-regexp -- oldVersion is validated above as dotted digits (optional alphanumeric suffix) and its dots escaped, so the pattern is a fixed literal with bounded lookarounds (no ReDoS)
return text.replace(new RegExp(`(?<![\\d.])${esc}(?![\\d.]*\\d)`, 'g'), newVersion);
@@ -84,12 +91,13 @@ function replaceVersionToken(text: string, oldVersion: string, newVersion: strin
* re-stamped: header `## [new] - date`, every other mention of the branch's
* old version inside the entry rewritten. A copy of the entry already in
* `currentText` (a clean merge kept it, or an earlier restamp pass wrote it)
- * is removed first, so the rebuild is idempotent.
+ * is removed first, so the rebuild is idempotent. An entry headed with a label
+ * (`## [Unreleased]`) has no old version to rewrite: only its header is stamped.
*/
export function rebuildChangelog(currentText: string, entry: ChangelogSection, oldVersion: string, newVersion: string, date: string): string {
const { preamble, sections } = splitChangelog(currentText);
const bodyOf = (text: string) => text.split('\n').slice(1).join('\n').replace(/\n+$/, '');
- const body = replaceVersionToken(bodyOf(entry.text), oldVersion, newVersion);
+ const body = isVersionToken(oldVersion) ? replaceVersionToken(bodyOf(entry.text), oldVersion, newVersion) : bodyOf(entry.text);
const ours = new Set([bodyOf(entry.text), body]);
const rest = sections.filter((s) => {
const v = versionOfHeader(s.header);
diff --git a/scripts/release-restamp.ts b/scripts/release-restamp.ts
index 2d766ba699..ef70efa248 100644
--- a/scripts/release-restamp.ts
+++ b/scripts/release-restamp.ts
@@ -37,7 +37,7 @@ import { join, resolve } from 'node:path';
import {
COLLISION_DOCS, MIGRATION_FILE_RE, MIGRATIONS_DIR, RESTAMP_DOCS, RestampError, STAMPS,
branchEntrySections, fail, findLeftoverRefs, isGenerated, migrationPayloadKey, nextPatchVersion, parseAddedLines,
- planRenumber, rebuildChangelog, renumberMigrationText, resolveVersionOnlyHunks, restampTodoLines, versionOfHeader,
+ isVersionToken, planRenumber, rebuildChangelog, renumberMigrationText, resolveVersionOnlyHunks, restampTodoLines, versionOfHeader,
type BranchMigration, type ChangelogSection, type LeftoverRef, type Renumber,
} from './lib/restamp.ts';
import { offlineEnv } from './regen-all.ts';
@@ -344,7 +344,7 @@ function restampTree(ctx: Ctx, state: State, opts: { acceptRefs: boolean; date:
const text = ctx.read(stamp.file);
if (stamp.read(text) !== version) ctx.write(stamp.file, stamp.write(text, version));
}
- const olds = new Set([state.branchVersion, state.entry ? versionOfHeader(state.entry.header) ?? '' : ''].filter((v) => v && v !== version));
+ const olds = new Set([state.branchVersion, state.entry ? versionOfHeader(state.entry.header) ?? '' : ''].filter((v) => v && v !== version && isVersionToken(v)));
if (state.entry) {
const old = versionOfHeader(state.entry.header) ?? state.branchVersion;
ctx.write('CHANGELOG.md', rebuildChangelog(ctx.read('CHANGELOG.md'), state.entry, old, version, opts.date));
@@ -440,6 +440,7 @@ function printDryRun(ctx: Ctx, state: State, date: string): void {
const cur = stamp.read(ctx.git('show', `HEAD:${stamp.file}`));
L(` ${stamp.file}: ${cur} -> ${version}${cur === version ? ' (unchanged)' : ''}`);
}
+ if (state.entry) rebuildChangelog(ctx.git('show', 'HEAD:CHANGELOG.md'), state.entry, versionOfHeader(state.entry.header) ?? state.branchVersion, version, date);
L(state.entry
? ` CHANGELOG.md: "${state.entry.header}" -> "## [${version}] - ${date}", kept above master's entries`
: ' CHANGELOG.md: this branch has no entry yet; the drift check will stop until /ship writes one');
diff --git a/test/scripts/release-restamp.test.ts b/test/scripts/release-restamp.test.ts
index 64980fcd2e..d174886e60 100644
--- a/test/scripts/release-restamp.test.ts
+++ b/test/scripts/release-restamp.test.ts
@@ -391,6 +391,22 @@ describe('release:restamp end to end', () => {
expect(r.out).toContain('PR title: v0.1.2.0 feat(sync): faster widgets');
}, 120_000);
+ test('an [Unreleased] branch entry is stamped to the target version; --dry-run runs the same rebuild', () => {
+ const fx = fixture();
+ const { preamble, sections } = splitChangelog(read(fx.work, 'CHANGELOG.md'));
+ put(fx.work, { 'CHANGELOG.md': [preamble, '## [Unreleased]\n\nWidgets now sync twice as fast.\n', ...sections.map((s) => s.text)].join('\n'), 'src/widgets.ts': 'export const W = 1;\n' });
+ commit(fx.work, 'feat: widgets');
+ landOnMaster(fx, '0.1.1.0', null);
+ const dry = restamp(fx.work, ['--dry-run']);
+ expect({ code: dry.code, planned: dry.out.includes('"## [Unreleased]" -> "## [0.1.2.0] - 2026-10-05"') }).toEqual({ code: 0, planned: true });
+ const r = restamp(fx.work);
+ expect(r.code).toBe(0);
+ const text = read(fx.work, 'CHANGELOG.md');
+ expect(text).not.toContain('[Unreleased]');
+ expect(splitChangelog(text).sections.map((s) => s.header)).toEqual(['## [0.1.2.0] - 2026-10-05', '## [0.1.1.0] - 2026-10-04', '## [0.1.0.0] - 2026-01-01']);
+ expect(text).toContain('## [0.1.2.0] - 2026-10-05\n\nWidgets now sync twice as fast.');
+ }, 120_000);
+
test('--no-commit leaves the restamp edits staged on top of the merge commit', () => {
const fx = fixture();
branchWork(fx, '0.1.1.0', {});
@@ -467,6 +483,14 @@ describe('release:restamp helpers', () => {
expect(once).toBe(changelog(release('0.1.2.0', '2026-10-05', 'Branch v0.1.2.0 notes.'), release('0.1.1.0', '2026-10-04', 'Master.'), release('0.1.0.0', '2026-01-01', 'First.')));
});
+ test('CHANGELOG rebuild stamps an [Unreleased] entry without rewriting version mentions in its body', () => {
+ const entry = { header: '## [Unreleased]', text: '## [Unreleased]\n\nFollows v0.1.1.0.\n' };
+ const merged = changelog('## [Unreleased]\n\nFollows v0.1.1.0.\n', release('0.1.1.0', '2026-10-04', 'Master.'));
+ const once = rebuildChangelog(merged, entry, 'Unreleased', '0.1.2.0', '2026-10-05');
+ expect(once).toBe(changelog(release('0.1.2.0', '2026-10-05', 'Follows v0.1.1.0.'), release('0.1.1.0', '2026-10-04', 'Master.')));
+ expect(rebuildChangelog(once, entry, 'Unreleased', '0.1.2.0', '2026-10-05')).toBe(once);
+ });
+
test('every required row of the CLAUDE.md "Version locations" table is restamped', () => {
const claude = read(REPO, 'CLAUDE.md');
const table = claude.slice(claude.indexOf('**Required (every release must update every row):**'), claude.indexOf('**Auto-derived'));