Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236/** The per-branch `last-pushed` record single-flight auto-push coordinates through.
The record names the tip the most recent push attempt resolved, its outcome, and the owner-lock token of the attempt, so a waiting commit can tell a push that finished while it waited from one that finished before. A failed attempt also keeps its complete push output, which every commit that joined it surfaces. The record is a cache: an absent or unreadable record only costs an extra push.
@module */import { randomUUID, } from 'node:crypto';import { readFile, rename, rm, writeFile,} from 'node:fs/promises';import { caughtValueText, } from '@monochromatic-dev/module-caught-value/ts';import { tagged, } from '@monochromatic-dev/module-logger/ts';
/** Module logger. */const l = tagged({ tag: 'cli-git', },);
/** Record schema version. */const LAST_PUSHED_SCHEMA_VERSION = 1;
/** Private record file mode; failed-push output stays readable by the owner only. */const PRIVATE_FILE_MODE = 0o600;
/** Record file name suffix after the encoded branch key. */export const LAST_PUSHED_SUFFIX = '.last-pushed.json';
/** Record absent or unreadable; the caller behaves as if no push ever ran. */export const LAST_PUSHED_ABSENT: unique symbol = Symbol('last-pushed record file absent or unreadable',);
/** Outcome of one recorded push attempt. */export type LastPushedOutcome = 'pushed' | 'failed';
/** Most recent push attempt for one branch. */export type LastPushedRecord = Readonly<{ /** Record schema version. */ schemaVersion: 1; /** Branch tip the attempt resolved before pushing. */ tip: string; /** Whether the push succeeded. */ outcome: LastPushedOutcome; /** Owner-lock token of the attempt, unique per attempt. */ ownerToken: string; /** Process that ran the attempt. */ ownerPid: number; /** Push exit code, `0` on success. */ exitCode: number; /** Complete push output of a failed attempt; empty on success. */ output: string;}>;
/** Parses a record.
@param text - record file text
@returns validated record
@throws {@link TypeError} when a field is missing or mistyped
@example ```ts parseLastPushedRecord('{"schemaVersion":1,"tip":"abc","outcome":"pushed","ownerToken":"t","ownerPid":1,"exitCode":0,"output":""}'); ``` */export function parseLastPushedRecord(text: string,): LastPushedRecord { /** Untrusted parsed JSON value. */ const value: unknown = JSON.parse(text,); if (((typeof value) !== 'object') || (value === null) || (!('schemaVersion' in value)) || (value.schemaVersion !== LAST_PUSHED_SCHEMA_VERSION) || (!('tip' in value)) || ((typeof value.tip) !== 'string') || (value.tip === '') || (!('outcome' in value)) || ((value.outcome !== 'pushed') && (value.outcome !== 'failed')) || (!('ownerToken' in value)) || ((typeof value.ownerToken) !== 'string') || (value.ownerToken === '') || (!('ownerPid' in value)) || ((typeof value.ownerPid) !== 'number') || (!Number.isSafeInteger(value.ownerPid,)) || (!('exitCode' in value)) || ((typeof value.exitCode) !== 'number') || (!Number.isSafeInteger(value.exitCode,)) || (!('output' in value)) || ((typeof value.output) !== 'string')) throw new TypeError('last-pushed record is malformed.',); return { schemaVersion: LAST_PUSHED_SCHEMA_VERSION, tip: value.tip, outcome: value.outcome, ownerToken: value.ownerToken, ownerPid: value.ownerPid, exitCode: value.exitCode, output: value.output, };}
/** Reads a branch's record, treating an absent or unreadable one as absent.
@param recordPath - record file
@returns record or absence sentinel
@example ```ts await readLastPushedRecord('/repo/.git/cli-git/push/refs%2Fheads%2Fmain.last-pushed.json'); ``` */export async function readLastPushedRecord(recordPath: string,): Promise<LastPushedRecord | typeof LAST_PUSHED_ABSENT> { /** Tagged reader logger. */ const rl = tagged({ tag: readLastPushedRecord.name, l, },); /** Record text. */ const text = await (async function readRecordText(): Promise<string | typeof LAST_PUSHED_ABSENT> { try { return await readFile( recordPath, 'utf8', ); } catch (error: unknown) { if (Error.isError(error,) && ('code' in error) && (error.code === 'ENOENT')) { rl.debug(`no last-pushed record at ${recordPath}`,); return LAST_PUSHED_ABSENT; } throw error; } })(); if (text === LAST_PUSHED_ABSENT) return LAST_PUSHED_ABSENT; try { return parseLastPushedRecord(text,); } catch (error: unknown) { rl.warn(`ignoring unreadable last-pushed record ${recordPath}; the next push replaces it: ${caughtValueText(error,)}`,); return LAST_PUSHED_ABSENT; }}
/** Publishes a branch's record by rename, so readers never see a partial record.
@param recordPath - record file
@param record - attempt to record
@example ```ts await writeLastPushedRecord({ recordPath, record }); ``` */export async function writeLastPushedRecord({ recordPath, record,}: Readonly<{ recordPath: string; record: LastPushedRecord;}>,): Promise<void> { /** Unpublished candidate beside the record. */ const candidatePath = `${recordPath}.${randomUUID()}.pending`; try { await writeFile( candidatePath, `${JSON.stringify(record,)}\n`, { encoding: 'utf8', mode: PRIVATE_FILE_MODE, flag: 'wx', }, ); await rename( candidatePath, recordPath, ); } catch (error: unknown) { await rm( candidatePath, { force: true, }, ); throw error; }}