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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322/** Exclusive creation of one destination path from the private stage.
A regular file is installed as a hard link to its staged copy, which appears atomically with its final bytes and mode and needs no second data copy. Filesystems that refuse the link fall back to an exclusive copy-on-write request with full-copy fallback.
@module */import { constants, } from 'node:fs';import { chmod, copyFile, link, lstat, mkdir, readlink, symlink, unlink,} from 'node:fs/promises';
import { tagged, } from '@monochromatic-dev/module-logger/ts';
import { lstatOrAbsent, } from './entry-compare.ts';import { WorktreeCopyError, } from './errors.ts';import { filesystemPath, } from './ignored-paths.ts';import type { InstalledWorktreePath, StagedWorktreeSnapshot, WorktreeCopyEntry,} from './model.ts';
/** Module logger. */const l = tagged({ tag: 'cli-git', },);
/** Exclusive copy-on-write request with full-copy fallback. */const EXCLUSIVE_COPY_MODE = constants.COPYFILE_EXCL | constants.COPYFILE_FICLONE;
/** Temporary writable mode for newly installed selected directories. */const PRIVATE_DIRECTORY_MODE = 0o700;
/** Error codes meaning the filesystem cannot hard-link the staged file into the destination. */const LINK_UNSUPPORTED_CODES: ReadonlySet<string> = new Set([ 'EMLINK', 'ENOSYS', 'ENOTSUP', 'EOPNOTSUPP', 'EPERM', 'EXDEV',],);
/** Captures exact no-follow identity after successful exclusive creation.
@param destinationPath - newly created native filesystem path
@param relativePath - repository path for durable ownership
@param selected - whether path came from selected source manifest
@returns exact created-path identity
@example ```ts await captureInstalledPath({ destinationPath: '/wt/cache', relativePath: 'cache', selected: true }); ``` */export async function captureInstalledPath({ destinationPath, relativePath, selected,}: Readonly<{ destinationPath: string; relativePath: string; selected: boolean;}>,): Promise<InstalledWorktreePath> { /** Exact no-follow post-creation filesystem identity. */ const stats = await lstat( destinationPath, { bigint: true, }, ); return { device: stats.dev .toString(), inode: stats.ino .toString(), relativePath, selected, };}
/** Creates missing unselected parent directories for one selected entry.
@param destinationRoot - newly registered worktree root
@param entry - selected staged entry
@param created - transaction-owned creation list, in creation order
@mutates created - appends proven scaffold identities
@example ```ts await ensureParents({ destinationRoot: '/wt', entry, created: [] }); ``` */export async function ensureParents({ destinationRoot, entry, created,}: Readonly<{ destinationRoot: string; entry: WorktreeCopyEntry; created: InstalledWorktreePath[];}>,): Promise<void> { /** Selected path components excluding selected entry itself. */ const parentComponents = entry.relativePath .split('/') .slice( 0, -1, ); /** Ordered parent repository paths from shallow to deep. */ const parentPaths = parentComponents.map(function parentPath( _component, index, ): string { return parentComponents .slice( 0, index + 1, ) .join('/'); },); for (const current of parentPaths) { /** Current native destination parent. */ const destinationPath = filesystemPath({ root: destinationRoot, repositoryPath: current, },); /* oxlint-disable no-await-in-loop -- parent chain is ordered and each child depends on prior directory */ /** Current parent no-follow metadata or absence. */ const stats = await lstatOrAbsent(destinationPath,); /* oxlint-enable no-await-in-loop */ if ((typeof stats) !== 'symbol') { if (!stats.isDirectory()) { throw new WorktreeCopyError( `cli-git: ignored-state parent is not a directory: ${JSON.stringify(current,)} in ${JSON.stringify(destinationRoot,)}.`, ); } continue; } // oxlint-disable-next-line no-await-in-loop -- parent chain creation is necessarily sequential await mkdir(destinationPath,); // oxlint-disable-next-line no-await-in-loop -- ownership identity must follow successful scaffold creation created.push(await captureInstalledPath({ destinationPath, relativePath: current, selected: false, },),); }}
/** Reports whether a hard-link failure means the filesystem cannot link the staged file.
@param error - caught link failure
@returns whether a copy should be attempted instead
@example ```ts isLinkUnsupported(Object.assign(new Error('x'), { code: 'EXDEV' })); // => true ``` */function isLinkUnsupported(error: unknown,): boolean { return Error.isError(error,) && ('code' in error) && ((typeof error.code) === 'string') && LINK_UNSUPPORTED_CODES.has(error.code,);}
/** Installs one staged regular file, as a hard link when the filesystem allows it.
@param stagePath - staged file carrying final bytes and mode
@param destinationPath - absent destination path
@param mode - source permission bits
@example ```ts await installFile({ stagePath: '/stage/payload/a', destinationPath: '/wt/a', mode: 0o644 }); ``` */async function installFile({ stagePath, destinationPath, mode,}: Readonly<{ stagePath: string; destinationPath: string; mode: number;}>,): Promise<void> { /** Tagged file-installation logger. */ const fl = tagged({ tag: installFile.name, l, },); try { await link( stagePath, destinationPath, ); return; } catch (error: unknown) { if (!isLinkUnsupported(error,)) throw error; fl.debug(`hard link refused for ${JSON.stringify(destinationPath,)}, copying instead: ${String(error,)}`,); } await copyFile( stagePath, destinationPath, EXCLUSIVE_COPY_MODE, ); try { await chmod( destinationPath, mode, ); } catch (error: unknown) { await unlink(destinationPath,); throw error; }}
/** Creates one absent selected entry from private stage.
@param snapshot - validated staged source state
@param destinationRoot - newly registered worktree root
@param entry - absent selected entry
@example ```ts await createSelectedEntry({ snapshot, destinationRoot: '/wt', entry }); ``` */export async function createSelectedEntry({ snapshot, destinationRoot, entry,}: Readonly<{ snapshot: StagedWorktreeSnapshot; destinationRoot: string; entry: WorktreeCopyEntry;}>,): Promise<void> { /** Staged expected filesystem path. */ const stagePath = filesystemPath({ root: snapshot.stageRoot, repositoryPath: entry.relativePath, },); /** Destination filesystem path. */ const destinationPath = filesystemPath({ root: destinationRoot, repositoryPath: entry.relativePath, },); if (entry.kind === 'directory') { await mkdir( destinationPath, { mode: PRIVATE_DIRECTORY_MODE, }, ); return; } if (entry.kind === 'file') { await installFile({ stagePath, destinationPath, mode: entry.mode, },); return; } /** Exact staged symbolic-link target text. */ const target = await readlink(stagePath,); await symlink( target, destinationPath, );}