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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491/** Invocation capture: the transaction facts recorded before any Git mutation.
Every later read of `HEAD` inside a transaction uses the recorded preparation base or the landed commit, never live `HEAD`.
@module */import { realpath, } from 'node:fs/promises';import { isAbsolute, join, resolve,} from 'node:path';import { tagged, } from '@monochromatic-dev/module-logger/ts';import { CommitTransactionGitError, runTransactionGit,} from './commit-transaction-git.ts';import { resolveConclusionKind, resolveRefCommit, resolveRefFormat, resolveSymbolicHead,} from './commit-transaction-capture-refs.ts';
/** Module logger. */const l = tagged({ tag: 'cli-git', },);
/** Strict Git output decoder. */const DECODER = new TextDecoder( 'utf-8', { fatal: true, },);
/** Lines the layout request prints: toplevel, Git directory, common directory, index, objects, registry, object format. */const LAYOUT_LINES = 7;
/** Preparation base: the target's commit at invocation, or an unborn target. */export type PreparationBase = | Readonly<{ kind: 'unborn'; }> | Readonly<{ kind: 'commit'; oid: string; }>;
/** Symbolic `HEAD` target at invocation. */export type SymbolicHeadTarget = | Readonly<{ kind: 'branch'; ref: string; }> | Readonly<{ kind: 'detached'; }>;
/** Commit kind that decides whether a moved target fails instead of replaying. */export type ConclusionKind = 'none' | 'amend' | 'merge' | 'cherry-pick' | 'revert';
/** Ref storage backend of the real repository. */export type RefStorageFormat = 'files' | 'reftable';
/** Facts recorded at invocation, before any Git mutation. */export type InvocationCapture = Readonly<{ /** Target commit at invocation, or unborn. */ base: PreparationBase; /** Branch `HEAD` named at invocation, or detached. */ symbolicHead: SymbolicHeadTarget; /** Ref the landing advances by compare-and-swap: the branch, or `HEAD` itself when detached. */ targetRef: string; /** Commit kind deciding moved-target behavior. */ conclusion: ConclusionKind; /** Canonical worktree root. */ repositoryRoot: string; /** Absolute Git directory of the owning worktree. */ gitDir: string; /** Absolute common Git directory. */ commonDir: string; /** Absolute real index path, honoring a caller-set `GIT_INDEX_FILE`. */ realIndexPath: string; /** Absolute real object directory named by the shadow repository's alternates. */ objectDirectory: string; /** Absolute per-worktree transaction registry. */ registryRoot: string; /** Ref storage backend. */ refFormat: RefStorageFormat; /** Object hash algorithm name. */ objectFormat: string; /** Empty tree of the repository's object format, standing in for an unborn base. */ emptyTreeOid: string; /** ISO-8601 invocation start time. */ invokedAt: string;}>;
/** Invocation facts other than the preparation base. */export type InvocationLayout = Omit<InvocationCapture, 'base'>;
/** Decodes one trimmed Git output.
@param bytes - Git stdout
@returns trimmed text */function decodeTrimmed(bytes: Uint8Array,): string { return DECODER.decode(bytes,) .trim();}
/** Reads one required line of a fixed-order Git report.
@param lines - report lines
@param index - line position
@returns nonempty line
@throws {@link CommitTransactionGitError} for a missing or empty line */function requiredLine({ lines, index,}: Readonly<{ lines: readonly string[]; index: number;}>,): string { /** Line at the position. */ const line = lines[index] ?? ''; if (line === '') throw new CommitTransactionGitError('Git returned incomplete repository layout.',); return line;}
/** Resolves one Git-reported path against the invocation directory.
@param cwd - invocation directory
@param lines - fixed-order report lines
@param index - line position of the path
@returns absolute path
@throws {@link CommitTransactionGitError} for an empty report */function absoluteReported({ cwd, lines, index,}: Readonly<{ cwd: string; lines: readonly string[]; index: number;}>,): string { /** Reported path. */ const reported = requiredLine({ lines, index, },); return isAbsolute(reported,) ? reported : resolve( cwd, reported, );}
/** Captures every invocation fact except the preparation base, before any Git mutation. A commit transaction publishes its directory between this and {@link captureInvocationBase}, so pruning of landed-capture records sees it before it reads its base.
@param gitPath - real Git executable
@param cwd - effective invocation directory
@param amend - whether the invocation amends
@returns invocation layout
@throws {@link CommitTransactionGitError} when Git reports an incomplete layout
@example ```ts await captureInvocationLayout({ gitPath: '/usr/bin/git', cwd: '/repo', amend: false }); ``` */export async function captureInvocationLayout({ gitPath, cwd, amend,}: Readonly<{ gitPath: string; cwd: string; amend: boolean;}>,): Promise<InvocationLayout> { /** Tagged capture logger. */ const rl = tagged({ tag: captureInvocationLayout.name, l, },); /** Invocation start time recorded before any Git runs. */ const invokedAt = new Date() .toISOString(); /** Absolute layout in one Git request. */ const layout = await runTransactionGit({ gitPath, cwd, args: [ 'rev-parse', '--path-format=absolute', '--show-toplevel', '--absolute-git-dir', '--git-common-dir', '--git-path', 'index', '--git-path', 'objects', '--git-path', 'cli-git-transactions', '--show-object-format', ], },); /** Fixed-order layout lines. */ const lines = decodeTrimmed(layout.stdout,) .split('\n',); /** Symbolic `HEAD` target and every other independent fact. */ const [symbolicHead, conclusion, refFormat, emptyTree,] = await Promise.all([ resolveSymbolicHead({ gitPath, cwd, },), resolveConclusionKind({ gitPath, cwd, amend, },), resolveRefFormat({ gitPath, cwd, },), runTransactionGit({ gitPath, cwd, args: [ 'hash-object', '-t', 'tree', '--stdin', ], },), ],); /** Ref the landing advances. */ const targetRef = symbolicHead.kind === 'branch' ? symbolicHead.ref : 'HEAD'; /** Recorded layout. */ const layoutCapture: InvocationLayout = { symbolicHead, targetRef, conclusion, repositoryRoot: await realpath(absoluteReported({ cwd, lines, index: 0, },),), gitDir: absoluteReported({ cwd, lines, index: 1, },), commonDir: absoluteReported({ cwd, lines, index: 2, },), realIndexPath: absoluteReported({ cwd, lines, index: 3, },), objectDirectory: absoluteReported({ cwd, lines, index: 4, },), registryRoot: absoluteReported({ cwd, lines, index: 5, },), refFormat, objectFormat: requiredLine({ lines, index: LAYOUT_LINES - 1, },), emptyTreeOid: decodeTrimmed(emptyTree.stdout,), invokedAt, }; rl.debug(`captured the layout of ${layoutCapture.targetRef} (${conclusion})`,); return layoutCapture;}
/** Completes the invocation capture with the preparation base: the target's commit now.
@param gitPath - real Git executable
@param cwd - effective invocation directory
@param layout - invocation layout
@returns invocation capture
@example ```ts await captureInvocationBase({ gitPath: '/usr/bin/git', cwd: '/repo', layout }); ``` */export async function captureInvocationBase({ gitPath, cwd, layout,}: Readonly<{ gitPath: string; cwd: string; layout: InvocationLayout;}>,): Promise<InvocationCapture> { /** Target commit at invocation. */ const base = await resolveRefCommit({ gitPath, cwd, ref: layout.targetRef, },); l.debug(`captured ${layout.targetRef} at ${base.kind === 'commit' ? base.oid : 'unborn'}`,); return { ...layout, base, };}
/** Captures every invocation fact before any Git mutation.
@param gitPath - real Git executable
@param cwd - effective invocation directory
@param amend - whether the invocation amends
@returns invocation capture
@throws {@link CommitTransactionGitError} when Git reports an incomplete layout
@example ```ts await captureInvocation({ gitPath: '/usr/bin/git', cwd: '/repo', amend: false }); ``` */export async function captureInvocation({ gitPath, cwd, amend,}: Readonly<{ gitPath: string; cwd: string; amend: boolean;}>,): Promise<InvocationCapture> { return await captureInvocationBase({ gitPath, cwd, layout: await captureInvocationLayout({ gitPath, cwd, amend, },), },);}
/** Revision the transaction reads as its baseline tree: the base commit, or the empty tree when unborn.
@param capture - invocation capture
@returns commit OID or empty tree OID
@example ```ts baseRevision(capture); ``` */export function baseRevision(capture: Pick<InvocationCapture, 'base' | 'emptyTreeOid'>,): string { return capture.base .kind === 'commit' ? capture.base .oid : capture.emptyTreeOid;}
/** Shadow repository path derived from the transaction ID, so recovery finds it before any record names it.
@param commonDir - absolute common Git directory
@param transactionId - transaction ID
@returns absolute shadow repository path
@example ```ts shadowRepositoryPath({ commonDir: '/repo/.git', transactionId }); ``` */export function shadowRepositoryPath({ commonDir, transactionId,}: Readonly<{ commonDir: string; transactionId: string;}>,): string { return join( commonDir, 'cli-git', 'shadow', transactionId, );}