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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230/** Landing helpers: target comparison, the expected old value of a payload, file identities for landing records, the nonce-bearing reflog message, and the compare-and-swap.
@module */import { lstat, } from 'node:fs/promises';import type { InvocationCapture, PreparationBase,} from './commit-transaction-capture.ts';import { type GitOutput, runTransactionGit,} from './commit-transaction-git.ts';import type { FileIdentity, } from './commit-transaction-journal-states.ts';import type { LandingPayload, } from './commit-landing.ts';
/** Strict Git output decoder. */const DECODER = new TextDecoder( 'utf-8', { fatal: true, },);
/** Reports whether two target values are equal.
@param left - first value
@param right - second value
@returns equality
@example ```ts sameTarget({ left: capture.base, right: current }); ``` */export function sameTarget({ left, right,}: Readonly<{ left: PreparationBase; right: PreparationBase;}>,): boolean { if ((left.kind === 'unborn') || (right.kind === 'unborn')) return left.kind === right.kind; return left.oid === right.oid;}
/** Expected old target value of a payload.
@param payload - landing payload
@param capture - invocation capture
@returns replay parent or preparation base
@example ```ts expectedOldTarget({ payload, capture }); ``` */export function expectedOldTarget({ payload, capture,}: Readonly<{ payload: LandingPayload; capture: InvocationCapture;}>,): PreparationBase { return payload.operation === 'commit' ? payload.expectedOld : capture.base;}
/** Reads a file's device and inode.
@param path - file path
@returns identity
@example ```ts await fileIdentity('/repo/.git/cli-git-transactions/id/post-1.index'); ``` */export async function fileIdentity(path: string,): Promise<FileIdentity> { /** Non-followed metadata. */ const metadata = await lstat( path, { bigint: true, }, ); return { device: String(metadata.dev,), inode: String(metadata.ino,), };}
/** Builds the nonce-bearing reflog message.
@param gitPath - real Git executable
@param cwd - owning worktree directory
@param nonce - transaction ID
@param oid - landed commit
@returns `commit (cli-git <nonce>): <subject>`
@example ```ts await landingReflogMessage({ gitPath: '/usr/bin/git', cwd: '/repo', nonce, oid }); ``` */export async function landingReflogMessage({ gitPath, cwd, nonce, oid,}: Readonly<{ gitPath: string; cwd: string; nonce: string; oid: string;}>,): Promise<string> { /** Commit subject. */ const subject = DECODER.decode((await runTransactionGit({ gitPath, cwd, args: [ 'show', '--no-patch', '--format=%s', oid, ], },)).stdout,) .trim(); return `${landingReflogPrefix(nonce,)} ${subject}`;}
/** Reflog subject prefix carrying the transaction nonce.
@param nonce - transaction ID
@returns prefix before the subject
@example ```ts landingReflogPrefix(id); // 'commit (cli-git <id>):' ``` */export function landingReflogPrefix(nonce: string,): string { return `commit (cli-git ${nonce}):`;}
/** Advances the target by compare-and-swap in the owning worktree's context, which also writes the real `HEAD` reflog.
@param gitPath - real Git executable
@param cwd - owning worktree directory
@param capture - invocation capture naming the target
@param transactionId - transaction ID, the reflog nonce
@param newOid - commit to land
@param expectedOld - value the target must still hold
@returns `git update-ref` outcome; a nonzero exit is a lost race
@example ```ts await compareAndSwapTarget({ gitPath: '/usr/bin/git', cwd: '/repo', capture, transactionId, newOid, expectedOld }); ``` */export async function compareAndSwapTarget({ gitPath, cwd, capture, transactionId, newOid, expectedOld,}: Readonly<{ gitPath: string; cwd: string; capture: Pick<InvocationCapture, 'symbolicHead' | 'targetRef' | 'emptyTreeOid'>; transactionId: string; newOid: string; expectedOld: PreparationBase;}>,): Promise<GitOutput> { return await runTransactionGit({ gitPath, cwd, args: [ 'update-ref', '-m', await landingReflogMessage({ gitPath, cwd, nonce: transactionId, oid: newOid, },), ...(capture.symbolicHead .kind === 'detached' ? ['--no-deref',] : []), capture.targetRef, newOid, expectedOld.kind === 'commit' ? expectedOld.oid : '0'.repeat(capture.emptyTreeOid .length,), ], allowFailure: true, },);}