diff --git a/docs/pregate.md b/docs/pregate.md index 0c8a64802..685785542 100644 --- a/docs/pregate.md +++ b/docs/pregate.md @@ -1107,8 +1107,8 @@ HEAD's exact tree is reused instead of rerun: either push's own record or place one. The guards must have checked the tree being pushed, so uncommitted or untracked files that change what they saw stop the push until you commit or stash them. A refused run (an unreadable policy, a base that is on origin but -not fetched, a mistyped `--base`) stops the push too; only a remote with no base -branch at all skips the guards, as `GUARDS: not run`. Outside /ship, run the +not fetched, a mistyped `--base`) stops the push too; only a remote that has no branch except the one +being pushed skips the guards, as `GUARDS: not run`. Outside /ship, run the command above yourself before `git push`. gstack declares its own list in `.gstack/pregate.json`: `typecheck`, diff --git a/lib/ship-release.ts b/lib/ship-release.ts index 2b0ba7501..5ce35b37d 100644 --- a/lib/ship-release.ts +++ b/lib/ship-release.ts @@ -474,7 +474,7 @@ export async function accept(argv: readonly string[], cwd: string): Promise path.resolve(cwd, git(cwd, ['rev-parse', '--git-path', `gstack/${name}`]).stdout.trim() || `.git/gstack/${name}`); const tree = git(cwd, ['rev-parse', 'HEAD^{tree}']).stdout.trim(); const out = gitPath('pregate-guards.json'); @@ -488,16 +488,16 @@ function prepushGuards(cwd: string, base: string | undefined): { line: string; s const pg = bin(cwd, 'gstack-pregate', ['--stage', 'guards', '--allow-repo-commands', '--json', '--out', out, ...(base ? ['--base', base] : [])], 3_600_000); let res: { status?: string; verdict?: string; tree?: string; code?: string; detail?: string; fix?: string; checks?: { id: string; status: string; detail?: string; fix?: string }[] } = {}; try { res = JSON.parse(pg.stdout.trim().split('\n').pop() ?? ''); } catch { /* unreadable: stops below */ } - const missing = res.status === 'refused' && res.code === 'POLICY_SOURCE_UNAVAILABLE' && !base ? /origin\/(\S+) is not a resolvable commit/.exec(res.detail ?? '')?.[1] : undefined; - const onOrigin = missing ? git(cwd, ['ls-remote', '--heads', 'origin', `refs/heads/${missing}`], 120_000) : null; - if (onOrigin && onOrigin.status !== 0) return { line: 'GUARDS: not run (origin unreachable)', stop: `cannot verify the remote branch (${firstLine(onOrigin.stderr)}): restore access before pushing` }; - if (onOrigin?.status === 0 && !onOrigin.stdout.trim()) return { line: `GUARDS: not run (origin has no ${missing} branch to read guards from)` }; + const heads = res.status === 'refused' && res.code === 'POLICY_SOURCE_UNAVAILABLE' ? git(cwd, ['ls-remote', '--heads', 'origin'], 120_000) : null; + if (heads && heads.status !== 0) return { line: 'GUARDS: not run (origin unreachable)', stop: `cannot verify the remote branch (${firstLine(heads.stderr)}): restore access before pushing` }; + if (heads && heads.stdout.split('\n').every(l => !l.trim() || l.trim().endsWith(`\trefs/heads/${branch}`))) return { line: `GUARDS: not run (origin has no branch but ${branch}, so no base to read guards from)` }; if (res.status === 'refused') return { line: `GUARDS: refused ${res.code}`, stop: `the pre-push guards could not run (${res.detail}; fix: ${res.fix}): repair it, then rerun push` }; - if (res.tree && res.tree !== tree) return { line: `GUARDS: not certified (they checked the working tree ${res.tree.slice(0, 12)}, not HEAD's tree ${tree.slice(0, 12)})`, stop: 'the working tree differs from HEAD (uncommitted or untracked files): commit or stash them, then rerun push' }; + const bad = (res.checks ?? []).filter(c => c.status !== 'pass' && c.status !== 'warn').map(c => `${c.id}: ${c.fix ?? c.detail ?? c.status}`).join('; '); + if (res.tree && res.tree !== tree) return { line: `GUARDS: not certified (they checked the working tree ${res.tree.slice(0, 12)}, not HEAD's tree ${tree.slice(0, 12)})`, stop: `the working tree differs from HEAD (uncommitted or untracked files): commit or stash them, then rerun push${bad ? `; the guards also reported ${bad}` : ''}` }; + if (git(cwd, ['rev-parse', 'HEAD^{tree}']).stdout.trim() !== tree) return { line: 'GUARDS: not certified (HEAD moved while the guards ran)', stop: 'HEAD changed while the guards ran: rerun push' }; const verdict = res.verdict ?? `unreadable (exit ${pg.status}: ${firstLine(pg.stderr)})`; const line = `GUARDS: ${verdict}${res.checks ? ` (${res.checks.map(c => `${c.id} ${c.detail ?? c.status}`).join('; ')})` : ''}`; if (pg.status === 0 && /^(pass|warn)/.test(verdict)) return { line }; - const bad = (res.checks ?? []).filter(c => c.status !== 'pass' && c.status !== 'warn').map(c => `${c.id}: ${c.fix ?? c.detail ?? c.status}`).join('; '); return { line, stop: `the pre-push guards did not pass (${bad || firstLine(pg.stderr || pg.stdout)}): fix it, commit, then rerun push` }; } @@ -530,7 +530,7 @@ export function push(argv: readonly string[], cwd: string): Report { r.add(`SECRETS: ${verdict} (early push)`); if (scan.status !== 0 || !/^(pass|warn)/.test(verdict)) return r.stop(`the secrets scan did not pass before the early push (${firstLine(scan.stderr) || verdict}): remove the finding from the commits, then rerun push --early`); } - const guards = prepushGuards(cwd, flag('--base')); + const guards = prepushGuards(cwd, branch, flag('--base')); r.add(guards.line); if (guards.stop) return r.stop(guards.stop); const local = git(cwd, ['rev-parse', 'HEAD']).stdout.trim(); diff --git a/test/ship-release.test.ts b/test/ship-release.test.ts index ffaa7895b..1af6cd264 100644 --- a/test/ship-release.test.ts +++ b/test/ship-release.test.ts @@ -390,7 +390,7 @@ describe('push: the pre-push housekeeping guards (GST-1138)', () => { expect(pushedHead(f)).toBe(''); }); - test('a base that exists on origin but is not fetched, a mistyped --base and an unreadable policy stop the push; only a remote with no base branch skips', () => { + test('a base that exists on origin but is not fetched, a mistyped --base, a dangling origin/HEAD and an unreadable policy stop the push', () => { const f = fx({ '.gstack/pregate.json': policy('exit 4') }); ready(f); f.git('update-ref', '-d', 'refs/remotes/origin/main'); @@ -402,6 +402,10 @@ describe('push: the pre-push housekeeping guards (GST-1138)', () => { const typo = f.run(RELEASE, ['push', '--branch', 'feat', '--base', 'mian']); expect(typo.status).toBe(3); expect(lineOf(typo.stdout, 'GUARDS')).toMatch(/^GUARDS: refused /); + f.git('symbolic-ref', 'refs/remotes/origin/HEAD', 'refs/remotes/origin/retired-default'); + const dangling = f.run(RELEASE, ['push', '--branch', 'feat']); + expect(dangling.status).toBe(3); + expect(lineOf(dangling.stdout, 'GUARDS')).toMatch(/^GUARDS: refused /); expect(pushedHead(f)).toBe(''); const broken = fx({ '.gstack/pregate.json': '{not json\n' }); ready(broken);