/** 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 { /** 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 ): ` @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 { /** 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 ):' ``` */ 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; transactionId: string; newOid: string; expectedOld: PreparationBase; }>,): Promise { 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, },); }