Files
gstack/lib/ship-policy-at.ts
T
Garry Tanandcapy-ai[bot] 36b349d660 Commands that read cut-off output now fail instead of judging part of it (GST-3086) (#4492)
Commands that read large git or gh output, including the credential
pre-push hook, the pre-merge secret and literal scans and the
sealed-output guard, now fail when Bun cuts that output off, instead of
judging the part they got.

Why: Bun can report exit status 0 together with ENOBUFS and partial
stdout, so a push whose diff passed 64 MiB could get a credential past
the pre-push hook.

Nothing to do: the failure names `SPAWN_OUTPUT_INCOMPLETE` and links its
troubleshooting entry.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
2026-11-18 17:00:18 +00:00

35 lines
1.9 KiB
TypeScript

/**
* ship-policy-at — the ship policy committed at a commit the caller already
* resolved (docs/ship-policy.md#review-exclude-paths). Unlike
* loadPolicyFromBase, any git failure is `unavailable`, never a silent
* default. Callers: lib/review-exclude.ts, lib/ship-receipt-depth.ts,
* bin/gstack-review-plan.
*/
import { spawnSync } from './spawn-sync';
import { SHIP_POLICY_PATH, finish, type PolicyLoad, type PolicySource } from './ship-policy';
function git(repoRoot: string, args: string[]): { ok: boolean; out: string; err: string } {
const r = spawnSync('git', args, { cwd: repoRoot, encoding: 'utf8', timeout: 30_000 });
return { ok: r.status === 0, out: (r.stdout ?? '').trim(), err: (r.stderr ?? '').trim() };
}
/**
* The policy committed at `sha` (a commit the caller already resolved, labeled `ref`): `cat-file -e` first, and only an
* `ls-tree` that succeeds with no entry is a missing file (defaults); any git failure, including a `show` failure after
* the file was found, is `unavailable`, never a silent default.
*/
export function loadPolicyAt(repoRoot: string, sha: string, ref: string): PolicyLoad {
const label = `${ref}@${sha.slice(0, 12)}`;
const source: PolicySource = { kind: 'base', ref, sha, label: `${label}:${SHIP_POLICY_PATH}` };
const spec = `${sha}:${SHIP_POLICY_PATH}`;
const base = ref.replace(/^origin\//, '');
if (!git(repoRoot, ['cat-file', '-e', spec]).ok) {
const tree = git(repoRoot, ['ls-tree', sha, '--', SHIP_POLICY_PATH]);
if (!tree.ok) return { unavailable: true, base, detail: `git ls-tree ${sha.slice(0, 12)} failed: ${tree.err.split('\n')[0] || 'no detail'}` };
if (!tree.out) return finish(null, { ...source, kind: 'none', label: `${label} (no ${SHIP_POLICY_PATH})` });
}
const show = git(repoRoot, ['show', spec]);
if (!show.ok) return { unavailable: true, base, detail: `git show ${label}:${SHIP_POLICY_PATH} failed: ${show.err.split('\n')[0] || 'no detail'}` };
return finish(show.out, source);
}