mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-18 06:04:33 +01:00
feat(restamp): the repo's stamp rule (stamp_files, literal stamp_patterns) and restamp_command in the staging worktree
stamp_files is an allowlist applied before every stamp rule; stamp_patterns
are bounded literal {version} templates shared by the restamp writer and the
tree receipt. restamp_command runs first in the staging worktree with the
target version (argument and GSTACK_RESTAMP_VERSION), needs a clean caller
tree, skips gstack's version writer and keeps output confinement; a failure
or a wrong version writes nothing. migrations { dir, id } is the policy key
gstack-pregate regate reads.
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
05e6f2e828
commit
c47fd042d9
6 files changed
+298
-26
No files matched your search
@@ -134,3 +134,72 @@ describe('release-tool contract', () => {
|
||||
expect(repo.git('worktree', 'list').split('\n')).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('restamp_command (field wave CD3): the repo owns the stamp inside the staging worktree', () => {
|
||||
const RESTAMP_JS = [
|
||||
"const fs = require('fs'); const { execSync } = require('child_process');",
|
||||
"if (execSync('git status --porcelain', { encoding: 'utf8' }).trim()) { console.error('FAIL: the working tree is not clean'); process.exit(1); }",
|
||||
"const v = process.argv[2];",
|
||||
"if (!v || v !== process.env.GSTACK_RESTAMP_VERSION) { console.error('version argument and GSTACK_RESTAMP_VERSION disagree'); process.exit(2); }",
|
||||
"if (process.env.FIXTURE_FAIL) process.exit(7);",
|
||||
"fs.writeFileSync('VERSION', (process.env.FIXTURE_WRONG_VERSION ? '9.9.9.9' : v) + '\\n');",
|
||||
"const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8')); pkg.version = v.split('.').slice(0, 3).join('.'); fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\\n');",
|
||||
"fs.writeFileSync('generated/digest.md', '# digest v' + v + '\\nbody\\n');",
|
||||
"if (process.env.FIXTURE_STRAY) fs.writeFileSync('stray.txt', 'x\\n');",
|
||||
"execSync('git add -A'); execSync('git -c user.name=r -c user.email=r@example.com -c commit.gpgsign=false commit -q -m restamp');",
|
||||
].join('\n') + '\n';
|
||||
const POLICY = { ...BASE_POLICY, release_tool: null, restamp_command: 'node restamp.js' };
|
||||
|
||||
function delegated(): FixtureRepo {
|
||||
const repo = fixture({ policy: POLICY });
|
||||
fs.writeFileSync(path.join(repo.work, 'restamp.js'), RESTAMP_JS);
|
||||
repo.git('add', 'restamp.js'); repo.git('commit', '-q', '-m', 'repo restamp tool'); repo.git('push', '-q', 'origin', 'main');
|
||||
makePr(repo, 'feat-a');
|
||||
return repo;
|
||||
}
|
||||
|
||||
test('it runs first, on a clean checkout of HEAD, with the allocated version as argument and GSTACK_RESTAMP_VERSION; gstack\'s version writer is skipped', () => {
|
||||
const repo = delegated();
|
||||
const r = runBin(repo, 'gstack-restamp', ['--next', '--base', 'main', '--allow-repo-commands']);
|
||||
expect(r.status).toBe(EXIT.ok);
|
||||
expect(r.out).toContain('STAMP_VERSION: 1.2.4.0');
|
||||
expect(r.out).toContain('stage restamp_command: done (node restamp.js 1.2.4.0)');
|
||||
expect(r.out).toContain('stage version: skipped (restamp_command owns the version)');
|
||||
expect(r.out.indexOf('stage restamp_command:')).toBeLessThan(r.out.indexOf('stage version:'));
|
||||
expect(fs.readFileSync(path.join(repo.work, 'VERSION'), 'utf8')).toBe('1.2.4.0\n');
|
||||
expect(fs.readFileSync(path.join(repo.work, 'generated', 'digest.md'), 'utf8')).toBe('# digest v1.2.4.0\nbody\n');
|
||||
expect(repo.git('worktree', 'list').split('\n')).toHaveLength(1);
|
||||
});
|
||||
|
||||
test('a failure, a wrong version, an output outside the allowed set or a dirty caller tree writes nothing to the branch', () => {
|
||||
const cases: Array<[Record<string, string>, RegExp, number]> = [
|
||||
[{ FIXTURE_FAIL: '1' }, /restamp_command exited 7.*nothing written to the branch.*\(RESTAMP_RELEASE_TOOL_FAILED\)/, EXIT.refused],
|
||||
[{ FIXTURE_WRONG_VERSION: '1' }, /restamp_command left VERSION at 9\.9\.9\.9, expected 1\.2\.4\.0.*nothing written.*\(RESTAMP_VERSION_SOURCE\)/, EXIT.refused],
|
||||
[{ FIXTURE_STRAY: '1' }, /restamp_command wrote outside the allowed set: stray\.txt.*\(RESTAMP_OUTPUT_OUTSIDE_ALLOWED\)/, EXIT.refused],
|
||||
];
|
||||
for (const [env, err, status] of cases) {
|
||||
const repo = delegated();
|
||||
const before = treeOf(repo);
|
||||
const r = runBin(repo, 'gstack-restamp', ['--next', '--base', 'main', '--allow-repo-commands'], env);
|
||||
expect(r.status).toBe(status);
|
||||
expect(r.err).toMatch(err);
|
||||
expect(treeOf(repo)).toBe(before);
|
||||
expect(fs.readFileSync(path.join(repo.work, 'VERSION'), 'utf8')).toBe('1.2.3.0\n');
|
||||
}
|
||||
const dirty = delegated();
|
||||
fs.writeFileSync(path.join(dirty.work, 'notes.txt'), 'uncommitted\n');
|
||||
const before = treeOf(dirty);
|
||||
const d = runBin(dirty, 'gstack-restamp', ['--next', '--base', 'main', '--allow-repo-commands']);
|
||||
expect(d.status).toBe(EXIT.fail);
|
||||
expect(d.err).toMatch(/restamp_command needs a clean HEAD, index and worktree.*notes\.txt.*nothing written.*\(RESTAMP_CONFLICT\)/);
|
||||
expect(treeOf(dirty)).toBe(before);
|
||||
expect(fs.existsSync(journal(dirty))).toBe(false);
|
||||
});
|
||||
|
||||
test('without --allow-repo-commands it is listed and refused like release_tool', () => {
|
||||
const repo = delegated();
|
||||
const r = runBin(repo, 'gstack-restamp', ['--next', '--base', 'main']);
|
||||
expect(r.status).toBe(EXIT.refused);
|
||||
expect(r.out).toContain('COMMAND: restamp_command="node restamp.js" cwd=.');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* Field wave CD3: which files count as stamps is the repo's rule. Ship policy
|
||||
* `stamp_files` is an explicit allowlist applied before any stamp rule;
|
||||
* `stamp_patterns` are literal templates with one `{version}` slot (no free
|
||||
* regex), shared by the restamp writer and the tree-receipt classifier.
|
||||
* Field incident: a pure gbrain restamp from 0.60.188.0 to 0.60.189.0 read
|
||||
* `TREE: changed` for 13 of 16 files because `since: '…'`, `"version": "…"`
|
||||
* and `<!-- …-stamp: … -->` were outside gstack's three built-in forms.
|
||||
*/
|
||||
import { afterEach, describe, expect, test } from 'bun:test';
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import { EXIT } from '../lib/headless-artifacts';
|
||||
import { rewriteStamps } from '../lib/restamp';
|
||||
import { validatePolicy } from '../lib/ship-policy';
|
||||
import { BASE_POLICY, cleanup, makeFixtureRepo, makePr, runBin, type FixtureRepo } from './helpers/restamp-fixture';
|
||||
|
||||
const repos: FixtureRepo[] = [];
|
||||
afterEach(() => { while (repos.length) cleanup(repos.pop()!); });
|
||||
|
||||
const TEMPLATES = ['"version": "{version}"', '<!-- x-plugin-tree-stamp: {version} -->', "since: '{version}'"];
|
||||
const STAMP_POLICY = { ...BASE_POLICY, release_tool: null, stamp_paths: ['docs/**'], stamp_files: ['VERSION', 'package.json', 'plugin/**', 'src/notice.ts'], stamp_patterns: TEMPLATES };
|
||||
|
||||
function stampedFiles(v: string, docs = 'Added in v1.2.4.0.\n'): Record<string, string> {
|
||||
return {
|
||||
VERSION: `${v}\n`,
|
||||
'plugin/.claude-plugin/plugin.json': `{\n "name": "x",\n "version": "${v}",\n "description": "d"\n}\n`,
|
||||
'plugin/README.md': `# plugin\n<!-- x-plugin-tree-stamp: ${v} -->\n`,
|
||||
'src/notice.ts': `export const ROWS = [\n { since: '${v}', key: 'k' },\n];\n`,
|
||||
'docs/guide.md': docs,
|
||||
};
|
||||
}
|
||||
function commitFiles(repo: FixtureRepo, files: Record<string, string>, msg: string): string {
|
||||
for (const [rel, body] of Object.entries(files)) { fs.mkdirSync(path.dirname(path.join(repo.work, rel)), { recursive: true }); fs.writeFileSync(path.join(repo.work, rel), body); }
|
||||
const pkg = JSON.parse(fs.readFileSync(path.join(repo.work, 'package.json'), 'utf8'));
|
||||
pkg.version = files.VERSION!.trim().split('.').slice(0, 3).join('.');
|
||||
fs.writeFileSync(path.join(repo.work, 'package.json'), JSON.stringify(pkg, null, 2) + '\n');
|
||||
repo.git('add', '-A'); repo.git('commit', '-q', '-m', msg);
|
||||
return repo.git('rev-parse', 'HEAD');
|
||||
}
|
||||
|
||||
describe('stamp templates (pure)', () => {
|
||||
test('rewriteStamps applies literal {version} templates beside the three built-in forms, at version boundaries only', () => {
|
||||
expect(rewriteStamps("{ since: '1.2.3.0', x }", '1.2.3.0', '1.2.4.0', ["since: '{version}'"])).toEqual({ text: "{ since: '1.2.4.0', x }", count: 1 });
|
||||
expect(rewriteStamps('"version": "1.2.3.0",', '1.2.3.0', '1.2.4.0', ['"version": "{version}"']).count).toBe(1);
|
||||
expect(rewriteStamps('<!-- x-plugin-tree-stamp: 1.2.3.0 -->', '1.2.3.0', '1.2.4.0', ['<!-- x-plugin-tree-stamp: {version} -->']).text).toBe('<!-- x-plugin-tree-stamp: 1.2.4.0 -->');
|
||||
expect(rewriteStamps("since: '1.2.3.01'", '1.2.3.0', '1.2.4.0', ["since: '{version}'"]).count).toBe(0);
|
||||
expect(rewriteStamps("since: '1.2.3.0'", '1.2.3.0', '1.2.4.0').count).toBe(0);
|
||||
});
|
||||
|
||||
test('policy: stamp_patterns are bounded literal templates with exactly one {version}; stamp_files are contained globs', () => {
|
||||
const codes = (p: unknown) => validatePolicy(p).map(e => `${e.code}:${e.path}`);
|
||||
expect(codes(STAMP_POLICY)).toEqual([]);
|
||||
expect(codes({ stamp_patterns: ['no slot'] })).toEqual(['POLICY_INVALID:stamp_patterns[0]']);
|
||||
expect(codes({ stamp_patterns: ['{version} and {version}'] })).toEqual(['POLICY_INVALID:stamp_patterns[0]']);
|
||||
expect(codes({ stamp_patterns: ['x'.repeat(130) + '{version}'] })).toEqual(['POLICY_CONTAINMENT:stamp_patterns[0]']);
|
||||
expect(codes({ stamp_patterns: Array.from({ length: 17 }, (_, i) => `k${i}: {version}`) })).toEqual(['POLICY_CONTAINMENT:stamp_patterns']);
|
||||
expect(codes({ stamp_files: ['../VERSION'] })).toEqual(['POLICY_CONTAINMENT:stamp_files[0]']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('tree receipt under the repo stamp rule', () => {
|
||||
test('a pure restamp across a manifest, a stamp comment and a since row is same-modulo-stamps; a doc vX mention outside stamp_files is a difference', () => {
|
||||
const repo = makeFixtureRepo({ prefix: 'stamp-templates-', policy: STAMP_POLICY }); repos.push(repo);
|
||||
makePr(repo, 'pr-a', { changelog: false });
|
||||
const gated = commitFiles(repo, stampedFiles('1.2.4.0'), 'stamp 1.2.4.0');
|
||||
commitFiles(repo, stampedFiles('1.2.5.0'), 'restamp 1.2.5.0');
|
||||
const r = runBin(repo, 'gstack-tree-receipt', ['--base', 'main', '--gated', gated]);
|
||||
expect(r.out).toMatch(/^ {2}stamp {3}plugin\/\.claude-plugin\/plugin\.json \(stamp_files\)$/m);
|
||||
expect(r.out).toMatch(/^ {2}stamp {3}plugin\/README\.md \(stamp_files\)$/m);
|
||||
expect(r.out).toMatch(/^ {2}stamp {3}src\/notice\.ts \(stamp_files\)$/m);
|
||||
expect(r.out).toMatch(/^ {2}stamp {3}VERSION \(version-file\)$/m);
|
||||
expect(r.out).toMatch(/^TREE: same-modulo-stamps$/m);
|
||||
commitFiles(repo, stampedFiles('1.2.5.0', 'Added in v1.2.5.0.\n'), 'doc mention');
|
||||
const d = runBin(repo, 'gstack-tree-receipt', ['--base', 'main', '--gated', gated]);
|
||||
expect(d.status).toBe(EXIT.fail);
|
||||
expect(d.out).toMatch(/^ {2}changed docs\/guide\.md \(stamp_files: not in the repo's stamp_files allowlist\)$/m);
|
||||
expect(d.out).toMatch(/^TREE: changed \(docs\/guide\.md\)$/m);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user