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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317import { entryMatches, lstatOrAbsent,} from './entry-compare.ts';import { applyEntryModes, } from './entry-manifest.ts';import { WorktreeCopyError, } from './errors.ts';import { filesystemPath, } from './ignored-paths.ts';import { captureInstalledPath, createSelectedEntry, ensureParents,} from './install-entry.ts';import { rollbackCreated, } from './install-rollback.ts';import type { InstalledWorktreePath, StagedWorktreeSnapshot, WorktreeCopyEntry,} from './model.ts';import { isTransactionPath, type JournalState, type RecordedPaths, recordCreations, recordIntents,} from './transaction-journal.ts';
/** Selected entries claimed by one durable intent append and settled by one creation append. Bounds both the append count and the work an interrupted owner can leave unrecorded. */const INSTALL_BATCH_ENTRIES = 512;
/** Asserts every existing destination entry is identical, or is this transaction's own interrupted directory, before mutation.
A directory this transaction claimed or created still has its private installation mode when its owner was interrupted, because modes are applied after every entry exists; accepting it lets recovery resume.
@param snapshot - validated staged source state
@param destinationRoot - newly registered worktree root
@param recorded - claims and creations this transaction recorded
@returns selected paths absent from the destination
@throws {@link WorktreeCopyError} on first differing collision
@example ```ts await preflightDestination({ snapshot, destinationRoot: '/wt', recorded: journalState }); ``` */async function preflightDestination({ snapshot, destinationRoot, recorded,}: Readonly<{ snapshot: StagedWorktreeSnapshot; destinationRoot: string; recorded: RecordedPaths;}>,): Promise<ReadonlySet<string>> { /** Selected paths not present in the destination. */ const absent = new Set<string>(); for (const entry of snapshot.entries) { /** Destination path aligned with staged entry. */ const destinationPath = filesystemPath({ root: destinationRoot, repositoryPath: entry.relativePath, },); /* oxlint-disable no-await-in-loop -- fail-fast collision order must follow deterministic manifest */ /** Current destination no-follow metadata or absence. */ const stats = await lstatOrAbsent(destinationPath,); /* oxlint-enable no-await-in-loop */ if ((typeof stats) === 'symbol') { absent.add(entry.relativePath,); continue; } if ((entry.kind === 'directory') && stats.isDirectory() && isTransactionPath({ recorded, relativePath: entry.relativePath, },)) { continue; } // oxlint-disable-next-line no-await-in-loop -- exact comparison is required before any destination mutation if (!(await entryMatches({ expectedRoot: snapshot.stageRoot, actualRoot: destinationRoot, entry, },))) { throw new WorktreeCopyError( `cli-git: ignored-state copy would overwrite differing destination entry ${JSON.stringify(entry.relativePath,)} in ${JSON.stringify(destinationRoot,)}.`, ); } } return absent;}
/** Splits the manifest into installation batches in manifest order.
@param entries - parent-first manifest
@returns consecutive batches of at most {@link INSTALL_BATCH_ENTRIES} entries
@example ```ts installBatches(snapshot.entries); ``` */function installBatches(entries: readonly WorktreeCopyEntry[],): readonly (readonly WorktreeCopyEntry[])[] { return Array.from( { length: Math.ceil(entries.length / INSTALL_BATCH_ENTRIES,), }, function batchAt( _unused, index, ): readonly WorktreeCopyEntry[] { return entries.slice( index * INSTALL_BATCH_ENTRIES, (index + 1) * INSTALL_BATCH_ENTRIES, ); }, );}
/** Installs one batch: claims its absent selected paths, creates them, then records their identities.
@param snapshot - validated private source snapshot
@param destinationRoot - newly registered worktree root
@param batch - consecutive manifest entries
@param absent - selected paths absent at preflight
@param created - transaction-owned creation list shared with rollback
@param journalState - mutable durable transaction state
@mutates created - appends every path the batch creates
@mutates journalState - records the batch's intents and creations
@example ```ts await installBatch({ snapshot, destinationRoot: '/wt', batch, absent, created, journalState }); ``` */async function installBatch({ snapshot, destinationRoot, batch, absent, created, journalState,}: Readonly<{ snapshot: StagedWorktreeSnapshot; destinationRoot: string; batch: readonly WorktreeCopyEntry[]; absent: ReadonlySet<string>; created: InstalledWorktreePath[]; journalState: JournalState;}>,): Promise<void> { await recordIntents({ state: journalState, relativePaths: batch .map(function entryPath(entry,): string { return entry.relativePath; },) .filter(function isAbsent(relativePath,): boolean { return absent.has(relativePath,); },), },); /** Creation-list length before this batch. */ const batchStart = created.length; for (const entry of batch) { // oxlint-disable-next-line no-await-in-loop -- manifest parent order and rollback ownership require sequential install await ensureParents({ destinationRoot, entry, created, },); /** Current destination path after parent creation. */ const destinationPath = filesystemPath({ root: destinationRoot, repositoryPath: entry.relativePath, },); // oxlint-disable-next-line no-await-in-loop -- destination can change between preflight and exact exclusive creation if ((typeof await lstatOrAbsent(destinationPath,)) !== 'symbol') continue; // oxlint-disable-next-line no-await-in-loop -- deterministic parent-before-child installation await createSelectedEntry({ snapshot, destinationRoot, entry, },); // oxlint-disable-next-line no-await-in-loop -- ownership identity must follow successful selected creation created.push(await captureInstalledPath({ destinationPath, relativePath: entry.relativePath, selected: true, },),); } await recordCreations({ state: journalState, entries: created.slice(batchStart,), },);}
/** Installs validated ignored snapshot without overwriting destination state.
@param snapshot - validated private source snapshot
@param destinationRoot - newly registered worktree root
@param journalState - mutable durable transaction state
@param priorCreations - paths an interrupted owner already proved created
@mutates journalState - records batch intents and proven identities through journal helpers
@returns count of newly installed selected entries
@throws {@link WorktreeCopyError} after ownership-checked rollback on failure
@example ```ts await installSnapshot({ snapshot, destinationRoot: '/wt', journalState, priorCreations: [] }); ``` */export async function installSnapshot({ snapshot, destinationRoot, journalState, priorCreations,}: Readonly<{ snapshot: StagedWorktreeSnapshot; destinationRoot: string; journalState: JournalState; priorCreations: readonly InstalledWorktreePath[];}>,): Promise<number> { /** Prior and new paths with durable or in-memory post-creation identities. */ const created: InstalledWorktreePath[] = priorCreations.map(function priorCreation(entry,): InstalledWorktreePath { return { ...entry, }; },); try { /** Selected paths absent before installation. */ const absent = await preflightDestination({ snapshot, destinationRoot, recorded: journalState, },); for (const batch of installBatches(snapshot.entries,)) { // oxlint-disable-next-line no-await-in-loop -- batches install in manifest order so parents precede children await installBatch({ snapshot, destinationRoot, batch, absent, created, journalState, },); } await applyEntryModes({ root: destinationRoot, entries: snapshot.entries, },); return created.filter(function selectedEntry(installed,): boolean { return installed.selected; },) .length; } catch (error: unknown) { /** Paths that rollback could not safely remove. */ const retained = await rollbackCreated({ snapshot, destinationRoot, created, },); /** Incomplete rollback suffix retaining exact paths. */ const suffix = retained.length === 0 ? '' : ` Rollback retained: ${retained.map(function quotedPath(path,): string { return JSON.stringify(path,); },) .join(', ',)}.`; throw new WorktreeCopyError( `cli-git: ignored-state installation failed for ${JSON.stringify(destinationRoot,)}.${suffix}`, error, ); }}