mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-26 01:43:56 +01:00
train S1: merge #3892 at 13601c0440 v1.94.16.0
This commit is contained in:
commit
224cf12e6d
18 files changed
+414
-39
No files matched your search
+1
-1
@@ -313,7 +313,7 @@ export function branchAddedLines(cwd: string, since: string, files: string[], to
|
||||
return out;
|
||||
}
|
||||
|
||||
const escapeRegExp = (t: string) => t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
export const escapeRegExp = (t: string) => t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
const VERSION_RE = '(?<v>\\d+(?:\\.\\d+){1,3})(?![0-9.])';
|
||||
/**
|
||||
* The built-in stamp forms as scanners, the version in the `v` group: `(was X)`, `since: X` and `vX`.
|
||||
|
||||
@@ -113,7 +113,8 @@ export function releasePaths(policy: Record<string, any> | null, cwd: string, re
|
||||
* added since the merge base with origin/<base>, and not added by the base since): after a lost slot and a merge of the
|
||||
* base its stale stamp is older than `ref`'s VERSION, so it must be at or newer than the fork point's version instead,
|
||||
* the bound gstack-restamp's stamp_paths stage uses (GST-895; the fork version itself since GST-963). A branch line is
|
||||
* named by its position, and with a kept VERSION only such a line may move (GST-979, GST-980).
|
||||
* named by its position, and with a kept VERSION only such a line may move (GST-979, GST-980); on it a declared
|
||||
* `stamp_placeholders` value may also become the new version inside a `stamp_patterns` template (GST-1026).
|
||||
*/
|
||||
export function stampedFiles(policy: Record<string, any> | null, cwd: string, ref: string, base: string): string[] {
|
||||
const globs = Array.isArray(policy?.stamp_files) ? policy.stamp_files.filter((s: unknown): s is string => typeof s === 'string') : [];
|
||||
@@ -123,11 +124,13 @@ export function stampedFiles(policy: Record<string, any> | null, cwd: string, re
|
||||
let after = '';
|
||||
try { after = extractVersion(fs.readFileSync(path.join(cwd, before.rel), 'utf8'), before.rel); } catch { return []; }
|
||||
if (!after) return [];
|
||||
const templates = Array.isArray(policy?.stamp_patterns) ? policy.stamp_patterns.filter((s: unknown): s is string => typeof s === 'string') : [];
|
||||
const strings = (v: unknown): string[] => (Array.isArray(v) ? v.filter((s): s is string => typeof s === 'string') : []);
|
||||
const templates = strings(policy?.stamp_patterns);
|
||||
const placeholders = strings(policy?.stamp_placeholders);
|
||||
const diff = git(cwd, ['diff', '--unified=0', '--no-color', '--no-ext-diff', '--no-textconv', '--no-renames', ref]);
|
||||
if (diff.status !== 0) return [];
|
||||
const side = branchSides(cwd, `origin/${base}`, 'HEAD', before.version, ref);
|
||||
const stamp = (rel: string, removed: string, added: string, at: number): boolean => stampOnlyLine(removed, added, before.version, after, templates, side(rel), at);
|
||||
const stamp = (rel: string, removed: string, added: string, at: number): boolean => stampOnlyLine(removed, added, before.version, after, templates, side(rel), at, placeholders);
|
||||
return parseUnifiedDiff(diff.stdout).filter(f => f.status === 'M' && !f.binary && !f.modeChanged && matchesAny(globs, f.path) && f.hunks.length > 0 &&
|
||||
f.hunks.every(h => h.removed.length === h.added.length && h.removed.every((line, i) => stamp(f.path, line, h.added[i]!, h.removedAt[i]!)))).map(f => f.path);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Validation of the stamp policy keys (stamp_patterns templates and stamp_placeholders), split out of ship-policy.ts
|
||||
* to keep that module under the module-size ratchet.
|
||||
*/
|
||||
import type { PolicyError } from './ship-policy';
|
||||
import { parseVersion } from './version-source';
|
||||
|
||||
const MAX_TEMPLATES = 16;
|
||||
const MAX_TEMPLATE = 128;
|
||||
const MAX_PLACEHOLDERS = 16;
|
||||
const MAX_PLACEHOLDER = 32;
|
||||
export const VERSION_SLOT = '{version}';
|
||||
|
||||
export function templateErrors(value: unknown, out: PolicyError[]): void {
|
||||
if (!Array.isArray(value)) { out.push({ code: 'POLICY_INVALID', path: 'stamp_patterns', message: 'must be an array of literal templates' }); return; }
|
||||
if (value.length > MAX_TEMPLATES) out.push({ code: 'POLICY_CONTAINMENT', path: 'stamp_patterns', message: `more than ${MAX_TEMPLATES} templates` });
|
||||
value.forEach((t, i) => {
|
||||
const at = `stamp_patterns[${i}]`;
|
||||
if (typeof t !== 'string') { out.push({ code: 'POLICY_INVALID', path: at, message: 'must be a string' }); return; }
|
||||
if (t.split(VERSION_SLOT).length !== 2) out.push({ code: 'POLICY_INVALID', path: at, message: `${JSON.stringify(t)} must contain exactly one ${VERSION_SLOT}` });
|
||||
else if (t.length > MAX_TEMPLATE || /[\0\r\n]/.test(t)) out.push({ code: 'POLICY_CONTAINMENT', path: at, message: `must be one line of at most ${MAX_TEMPLATE} characters` });
|
||||
});
|
||||
}
|
||||
|
||||
export function placeholderErrors(value: unknown, out: PolicyError[]): void {
|
||||
if (!Array.isArray(value)) { out.push({ code: 'POLICY_INVALID', path: 'stamp_placeholders', message: 'must be an array of literal placeholders' }); return; }
|
||||
if (value.length > MAX_PLACEHOLDERS) out.push({ code: 'POLICY_CONTAINMENT', path: 'stamp_placeholders', message: `more than ${MAX_PLACEHOLDERS} placeholders` });
|
||||
value.forEach((p, i) => {
|
||||
const at = `stamp_placeholders[${i}]`;
|
||||
if (typeof p !== 'string') { out.push({ code: 'POLICY_INVALID', path: at, message: 'must be a string' }); return; }
|
||||
if (!p.trim() || p.length > MAX_PLACEHOLDER || /[\0\r\n]/.test(p)) out.push({ code: 'POLICY_CONTAINMENT', path: at, message: `must be one non-empty line of at most ${MAX_PLACEHOLDER} characters` });
|
||||
else if (p.includes(VERSION_SLOT)) out.push({ code: 'POLICY_INVALID', path: at, message: `${JSON.stringify(p)} must not contain ${VERSION_SLOT}` });
|
||||
else if (parseVersion(p)) out.push({ code: 'POLICY_INVALID', path: at, message: `${JSON.stringify(p)} is a version, not a placeholder` });
|
||||
});
|
||||
}
|
||||
+9
-14
@@ -18,6 +18,8 @@ import * as path from 'node:path';
|
||||
import { defaultBaseBranch, noDefaultBaseLine } from './default-base';
|
||||
import { RESULT_CODES, type ResultCodeName } from './result-codes';
|
||||
import { cmpVersion, parseVersion } from './version-source';
|
||||
import { placeholderErrors, templateErrors } from './ship-policy-stamps';
|
||||
export { VERSION_SLOT } from './ship-policy-stamps';
|
||||
|
||||
export const SHIP_POLICY_PATH = '.gstack/ship-policy.json';
|
||||
export const SHIP_POLICY_SCHEMA = 'https://github.com/garrytan/gstack/docs/ship-policy.md#schema-v1';
|
||||
@@ -64,6 +66,8 @@ export interface ShipPolicy {
|
||||
stamp_files: string[];
|
||||
/** Literal stamp templates with one `{version}` slot, beside the built-in `(was X)`, `since: X`, `vX`; writer and classifier share them. */
|
||||
stamp_patterns: string[];
|
||||
/** Placeholders (`TBD`) a restamp may replace with the new version inside a `stamp_patterns` template, on the branch's own lines only. */
|
||||
stamp_placeholders: string[];
|
||||
/** The repo's own restamp, run first in the staging worktree with the target version; gstack's version writer is skipped. */
|
||||
restamp_command: string | null;
|
||||
/** Migration numbering: `{ dir, id }` where `id` is a regex with one capture group over the file name. */
|
||||
@@ -161,6 +165,7 @@ export const DEFAULT_POLICY: ShipPolicy = {
|
||||
maintainers: [],
|
||||
stamp_files: [],
|
||||
stamp_patterns: [],
|
||||
stamp_placeholders: [],
|
||||
restamp_command: null,
|
||||
migrations: null,
|
||||
gate_command: null,
|
||||
@@ -193,6 +198,7 @@ export const POLICY_EXPLANATIONS: Record<PolicyKey, string> = {
|
||||
maintainers: 'GitHub logins treated as maintainers by /review contributor mode; empty uses repository push permission.',
|
||||
stamp_files: 'The repo’s stamp rule: globs of the only files that may differ by a stamp (version file, manifests, plugin trees, the PR’s own since: rows). Empty keeps gstack’s per-file rules; set, it applies before every rule.',
|
||||
stamp_patterns: 'Literal templates with exactly one {version} slot (at most 16, 128 characters each), for example `"version": "{version}"` or `since: \'{version}\'`; the restamp writer and the tree receipt use them beside (was X), since: X and vX.',
|
||||
stamp_placeholders: 'Literal placeholders (at most 16, one line, 32 characters each, never `{version}` or a version), for example `["TBD"]`: a line the branch added that reads a placeholder in a stamp_patterns template (`since: \'TBD\'`) may become that template at the new version, and finish, the receipt carry and the tree receipt read it as a stamp. Empty (default) accepts no placeholder.',
|
||||
restamp_command: 'The repo’s own restamp, run first in gstack-restamp’s staging worktree (a clean checkout of HEAD) with the target version as its argument and in GSTACK_RESTAMP_VERSION; gstack’s version writer is skipped, output confinement still applies. Needs a clean caller tree and --allow-repo-commands.',
|
||||
migrations: '{ dir, id }: the migration directory and a regex with one capture group for the number in a file name; `gstack-pregate regate` reports MIGRATION_COLLISION against the base branch and every open PR.',
|
||||
gate_command: 'The repo’s full pre-merge gate command. gstack-ship-queue gate-ahead and ready print it for the caller to run (in the background or in CI); gstack never executes it. null when the repo has none.',
|
||||
@@ -236,6 +242,7 @@ export const BUMP_NEVER_KEY_VERSION = '1.91.353.0';
|
||||
export const BASE_RED_KEY_VERSION = '1.91.374.0';
|
||||
/** `train.gates` and `train.land` are values of the older train key, so compatErrors checks them by value, as for bump: never. */
|
||||
export const TRAIN_GATES_KEY_VERSION = '1.92.0.0';
|
||||
export const STAMP_PLACEHOLDERS_KEY_VERSION = '1.94.16.0';
|
||||
export const REVIEW_EXCLUDE_KEY_VERSION = '1.93.33.0';
|
||||
export const POLICY_KEY_INTRODUCED: Record<PolicyKey, string> = {
|
||||
bump: '1.91.73.0', queue_mode: '1.91.73.0', release_tool: '1.91.73.0', release_outputs: '1.91.73.0', stamp_paths: '1.91.73.0',
|
||||
@@ -245,6 +252,7 @@ export const POLICY_KEY_INTRODUCED: Record<PolicyKey, string> = {
|
||||
train: TRAIN_KEYS_VERSION, review_scope: REVIEW_SCOPE_KEY_VERSION, docs: DOCS_CAPS_KEY_VERSION,
|
||||
changelog_fragments: CHANGELOG_FRAGMENTS_KEY_VERSION,
|
||||
base_red: BASE_RED_KEY_VERSION, review_exclude_paths: REVIEW_EXCLUDE_KEY_VERSION,
|
||||
stamp_placeholders: STAMP_PLACEHOLDERS_KEY_VERSION,
|
||||
};
|
||||
|
||||
const newer = (a: string, b: string): boolean => cmpVersion(parseVersion(a)!, parseVersion(b)!) > 0;
|
||||
@@ -274,9 +282,6 @@ const MAX_PATTERN = 256;
|
||||
const MAX_REGEX = 512;
|
||||
const MAX_COMMAND = 512;
|
||||
const SHA_RE = /^[0-9a-f]{7,64}$/;
|
||||
const MAX_TEMPLATES = 16;
|
||||
const MAX_TEMPLATE = 128;
|
||||
export const VERSION_SLOT = '{version}';
|
||||
|
||||
function hasDotDot(p: string): boolean {
|
||||
return p.split(/[\\/]+/).some(seg => seg === '..');
|
||||
@@ -343,17 +348,6 @@ function excludeErrors(value: unknown, out: PolicyError[]): void {
|
||||
});
|
||||
}
|
||||
|
||||
function templateErrors(value: unknown, out: PolicyError[]): void {
|
||||
if (!Array.isArray(value)) { out.push({ code: 'POLICY_INVALID', path: 'stamp_patterns', message: 'must be an array of literal templates' }); return; }
|
||||
if (value.length > MAX_TEMPLATES) out.push({ code: 'POLICY_CONTAINMENT', path: 'stamp_patterns', message: `more than ${MAX_TEMPLATES} templates` });
|
||||
value.forEach((t, i) => {
|
||||
const at = `stamp_patterns[${i}]`;
|
||||
if (typeof t !== 'string') { out.push({ code: 'POLICY_INVALID', path: at, message: 'must be a string' }); return; }
|
||||
if (t.split(VERSION_SLOT).length !== 2) out.push({ code: 'POLICY_INVALID', path: at, message: `${JSON.stringify(t)} must contain exactly one ${VERSION_SLOT}` });
|
||||
else if (t.length > MAX_TEMPLATE || /[\0\r\n]/.test(t)) out.push({ code: 'POLICY_CONTAINMENT', path: at, message: `must be one line of at most ${MAX_TEMPLATE} characters` });
|
||||
});
|
||||
}
|
||||
|
||||
function migrationErrors(value: unknown, out: PolicyError[]): void {
|
||||
const m = value as Record<string, unknown> | null;
|
||||
if (!m || typeof m !== 'object' || Array.isArray(m)) { out.push({ code: 'POLICY_INVALID', path: 'migrations', message: 'must be { dir, id } or null' }); return; }
|
||||
@@ -497,6 +491,7 @@ export function validatePolicy(raw: unknown, opts: { gstackVersion?: string } =
|
||||
if ('release_outputs' in obj) listErrors('release_outputs', obj.release_outputs, errors, globError);
|
||||
if ('stamp_files' in obj) listErrors('stamp_files', obj.stamp_files, errors, globError);
|
||||
if ('stamp_patterns' in obj) templateErrors(obj.stamp_patterns, errors);
|
||||
if ('stamp_placeholders' in obj) placeholderErrors(obj.stamp_placeholders, errors);
|
||||
if ('restamp_command' in obj && obj.restamp_command !== null) {
|
||||
const t = obj.restamp_command;
|
||||
if (typeof t !== 'string' || !t.trim()) errors.push({ code: 'POLICY_INVALID', path: 'restamp_command', message: 'must be a non-empty string or null' });
|
||||
|
||||
+3
-3
@@ -192,9 +192,9 @@ const CARRY_RERUNS = ['CI at head (gstack-ship-queue ready requires it); test an
|
||||
|
||||
/**
|
||||
* The file's text at the reviewed head and at HEAD, with the release version each head carries (from `VERSION`, else
|
||||
* `package.json`) and the base policy's `stamp_patterns` templates.
|
||||
* `package.json`) and the base policy's `stamp_patterns` templates and `stamp_placeholders`.
|
||||
*/
|
||||
export interface CarryChange { before: string | null; after: string | null; oldVersion?: string | null; newVersion?: string | null; templates?: readonly string[]; side?: BranchSide }
|
||||
export interface CarryChange { before: string | null; after: string | null; oldVersion?: string | null; newVersion?: string | null; templates?: readonly string[]; side?: BranchSide; placeholders?: readonly string[] }
|
||||
|
||||
/**
|
||||
* A policy-named stamp path's change is a stamp when the new text reads as the
|
||||
@@ -217,7 +217,7 @@ export function stampOnlyText(change: CarryChange): boolean {
|
||||
const now = after.split('\n');
|
||||
if (was.length !== now.length) return false;
|
||||
const changed = was.map((line, i) => i).filter(i => was[i] !== now[i]);
|
||||
return changed.length > 0 && changed.every(i => stampOnlyLine(was[i]!, now[i]!, oldVersion, newVersion, change.templates ?? [], change.side, i + 1));
|
||||
return changed.length > 0 && changed.every(i => stampOnlyLine(was[i]!, now[i]!, oldVersion, newVersion, change.templates ?? [], change.side, i + 1, change.placeholders ?? []));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+28
-4
@@ -4,7 +4,8 @@
|
||||
* lines the branch wrote itself. Split from restamp.ts (module-size ratchet).
|
||||
*/
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { branchAddedLines, forkVersion, rewriteStamps, stampMentions } from './restamp';
|
||||
import { branchAddedLines, escapeRegExp, forkVersion, rewriteStamps, stampMentions } from './restamp';
|
||||
import { VERSION_SLOT } from './ship-policy';
|
||||
import { cmpVersion, parseVersion, versionWidth } from './version-source';
|
||||
|
||||
/**
|
||||
@@ -26,12 +27,20 @@ import { cmpVersion, parseVersion, versionWidth } from './version-source';
|
||||
* stage's bound for its own lines (GST-895, GST-963): finish's stampedFiles,
|
||||
* the tree receipt and the receipt carry all pass the same side. When the
|
||||
* version does not move (a slot that keeps its claimed VERSION), only such a
|
||||
* branch line may change: the stage moves nothing else then.
|
||||
* branch line may change: the stage moves nothing else then. On such a branch
|
||||
* line a declared placeholder (`stamp_placeholders`, GST-1026: gbrain's
|
||||
* `since: 'TBD'`) may also become the new version, but only inside a
|
||||
* `stamp_patterns` template with literal text around its slot, never a built-in
|
||||
* form: with at least one such rewrite, the line must then be the added line,
|
||||
* or one whose remaining stamp-form moves pass the fork-version bound.
|
||||
*/
|
||||
export function stampOnlyLine(removed: string, added: string, oldV: string, newV: string, templates: readonly string[] = [], side?: BranchSide, at?: number): boolean {
|
||||
export function stampOnlyLine(removed: string, added: string, oldV: string, newV: string, templates: readonly string[] = [], side?: BranchSide, at?: number, placeholders: readonly string[] = []): boolean {
|
||||
if (removed === added) return false;
|
||||
if (oldV !== newV && stampOnlyWithin(removed, added, oldV, newV, templates)) return true;
|
||||
return !!side && at !== undefined && side.at.has(at) && side.added.has(removed) && stampOnlyWithin(removed, added, side.fork, newV, templates);
|
||||
if (!side || at === undefined || !side.at.has(at) || !side.added.has(removed)) return false;
|
||||
if (stampOnlyWithin(removed, added, side.fork, newV, templates)) return true;
|
||||
const filled = fillPlaceholders(removed, newV, templates, placeholders);
|
||||
return filled.count > 0 && (filled.text === added || stampOnlyWithin(filled.text, added, side.fork, newV, templates));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,6 +73,21 @@ export function branchSides(repoRoot: string, baseRef: string, head: string, fal
|
||||
};
|
||||
}
|
||||
|
||||
/** Each declared placeholder rewritten to `newV` inside each template with literal text around its slot (a bare edge at a word boundary). */
|
||||
function fillPlaceholders(removed: string, newV: string, templates: readonly string[], placeholders: readonly string[]): { text: string; count: number } {
|
||||
let text = removed;
|
||||
let count = 0;
|
||||
for (const t of templates) {
|
||||
const [pre, post] = t.split(VERSION_SLOT) as [string, string];
|
||||
if (!pre && !post) continue;
|
||||
for (const p of placeholders) {
|
||||
const re = new RegExp(`${escapeRegExp(pre)}${pre ? '' : '(?<![A-Za-z0-9_.])'}${escapeRegExp(p)}${post ? '' : '(?![A-Za-z0-9_.])'}${escapeRegExp(post)}`, 'g');
|
||||
text = text.replace(re, () => { count++; return pre + newV + post; });
|
||||
}
|
||||
}
|
||||
return { text, count };
|
||||
}
|
||||
|
||||
function stampOnlyWithin(removed: string, added: string, oldV: string, newV: string, templates: readonly string[]): boolean {
|
||||
const floor = parseVersion(oldV);
|
||||
const width = versionWidth(oldV);
|
||||
|
||||
+5
-4
@@ -78,15 +78,16 @@ export function classifyFiles(repoRoot: string, fileDiffs: FileDiff[], gated: st
|
||||
/**
|
||||
* Every removed/added pair differs only by stamp rewrites to the new version:
|
||||
* the old version, or a tentative one (newer than it) the stamp_paths stage
|
||||
* moved (#3512), each in a stamp form (`stampOnlyLine`, bounded by the fork version on the branch's own lines).
|
||||
* moved (#3512), each in a stamp form (`stampOnlyLine`, bounded by the fork version on the branch's own lines, where a
|
||||
* declared placeholder may also move inside a stamp_patterns template, GST-1026).
|
||||
*/
|
||||
function stampOnlyHunks(hunks: FileDiff['hunks'], oldV: string, newV: string, templates: readonly string[], side?: BranchSide): { why: string; line: number } | null {
|
||||
function stampOnlyHunks(hunks: FileDiff['hunks'], oldV: string, newV: string, templates: readonly string[], side?: BranchSide, placeholders: readonly string[] = []): { why: string; line: number } | null {
|
||||
for (const h of hunks) {
|
||||
if (h.removed.length !== h.added.length) return { why: `${h.removed.length} line(s) removed, ${h.added.length} added`, line: h.at };
|
||||
for (let i = 0; i < h.removed.length; i++) {
|
||||
const r = h.removed[i]!;
|
||||
const a = h.added[i]!;
|
||||
if (!stampOnlyLine(r, a, oldV, newV, templates, side, h.removedAt[i])) return { why: `${JSON.stringify(r.trim().slice(0, 60))} -> ${JSON.stringify(a.trim().slice(0, 60))}`, line: h.at + i };
|
||||
if (!stampOnlyLine(r, a, oldV, newV, templates, side, h.removedAt[i], placeholders)) return { why: `${JSON.stringify(r.trim().slice(0, 60))} -> ${JSON.stringify(a.trim().slice(0, 60))}`, line: h.at + i };
|
||||
}
|
||||
}
|
||||
return null;
|
||||
@@ -130,7 +131,7 @@ export function classifyFile(repoRoot: string, f: FileDiff, gated: string, curre
|
||||
const rule = matchesAny(policy.stamp_paths, f.path) ? 'stamp_paths' : matchesAny(policy.release_outputs, f.path) ? 'release_outputs' : matchesAny(policy.stamp_files, f.path) ? 'stamp_files' : null;
|
||||
const own = rule ? side?.(f.path) : undefined;
|
||||
if (oldV && newV && (oldV !== newV || own) && rule) {
|
||||
const bad = stampOnlyHunks(f.hunks, oldV, newV, policy.stamp_patterns, own);
|
||||
const bad = stampOnlyHunks(f.hunks, oldV, newV, policy.stamp_patterns, own, policy.stamp_placeholders);
|
||||
return bad ? changed(rule, bad.why, bad.line) : stamp(rule);
|
||||
}
|
||||
return changed('other', `${f.hunks.length} hunk(s) outside every stamp rule`);
|
||||
|
||||
Reference in new issue
Block a user