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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273import { basename, dirname, isAbsolute, join, resolve,} from 'node:path';
import { WorktreeCopyError, } from './errors.ts';import { assertSafeRepositoryPath, } from './ignored-paths.ts';import type { InstalledWorktreePath, WorktreeCopyJournal,} from './model.ts';import { STAGE_PREFIX, } from './snapshot.ts';
/** Reports whether unknown JSON value is a non-null object record. @param value - untrusted parsed JSON value @returns whether value supports named field inspection @example ```ts isRecord({ version: 1 }); // => true ``` */function isRecord(value: unknown,): value is Readonly<Record<string, unknown>> { return ((typeof value) === 'object') && (value !== null);}
/** Reports whether unknown JSON value is an array containing only strings. @param value - untrusted parsed JSON field @returns whether every array member is a string @example ```ts isStringArray(['cache']); // => true ``` */export function isStringArray(value: unknown,): value is readonly string[] { return Array.isArray(value,) && value.every(function stringMember(item,): item is string { return (typeof item) === 'string'; },);}
/** Reports whether string is canonical nonnegative decimal identity. @param value - untrusted device or inode text @returns whether text has canonical decimal form @example ```ts isDecimalIdentity('42'); // => true ``` */function isDecimalIdentity(value: unknown,): value is string { if (((typeof value) !== 'string') || (value === '') || ((value.length > 1) && value.startsWith('0',))) { return false; } for (let index = 0; index < value.length; index += 1) { /** Current decimal candidate character. */ const character = value.charAt(index,); if ((character < '0') || (character > '9')) return false; } return true;}
/** Reports whether unknown value is durable created-entry identity. @param value - untrusted created-entry record @returns whether every required primitive field is valid @example ```ts isInstalledEntry({ device: '1', inode: '2', relativePath: 'cache', selected: true }); // => true ``` */function isInstalledEntry(value: unknown,): value is InstalledWorktreePath { return isRecord(value,) && isDecimalIdentity(value.device,) && isDecimalIdentity(value.inode,) && ((typeof value.relativePath) === 'string') && ((typeof value.selected) === 'boolean');}
/** Reports whether unknown value is array of durable created-entry identities. @param value - untrusted created-entry array @returns whether every entry has valid primitive fields @example ```ts isInstalledEntryArray([]); // => true ``` */export function isInstalledEntryArray( value: unknown,): value is readonly InstalledWorktreePath[] { return Array.isArray(value,) && value.every(isInstalledEntry,);}
/** Asserts absolute path is lexically canonical. @param value - untrusted journal path @param field - journal field name for diagnostics @throws {@link WorktreeCopyError} when path is relative or noncanonical @example ```ts assertCanonicalAbsolutePath({ value: '/repo/cache', field: 'sourceRoot' }); ``` */function assertCanonicalAbsolutePath({ value, field,}: Readonly<{ value: string; field: string;}>,): void { if ((!isAbsolute(value,)) || (resolve(value,) !== value)) { throw new WorktreeCopyError( `cli-git: worktree-copy journal has unsafe ${field}: ${JSON.stringify(value,)}.`, ); }}
/** Validates durable JSON record before recovery uses filesystem paths. @param value - parsed unknown JSON value @param path - journal path used in diagnostics @param journalRoot - exact private journal directory @returns validated schema-version-one journal @throws {@link WorktreeCopyError} when record or path relation is malformed @example ```ts validateJournalValue({ value: parsed, path: '/repo/.git/cli-git-worktree-copy/v1/id.json', journalRoot }); ``` */export function validateJournalValue({ value, path, journalRoot,}: Readonly<{ value: unknown; path: string; journalRoot: string;}>,): WorktreeCopyJournal { if (!(isRecord(value,) && (value.version === 1) && ((value.phase === 'staged') || (value.phase === 'installing') || (value.phase === 'complete')) && isInstalledEntryArray(value.createdEntries,) && isStringArray(value.intendedEntries,) && isStringArray(value.selectedRoots,) && ((typeof value.destinationRoot) === 'string') && ((typeof value.sourceRoot) === 'string') && ((typeof value.stageContainer) === 'string') && ((typeof value.stageRoot) === 'string'))) { throw new WorktreeCopyError( `cli-git: worktree-copy journal is corrupt: ${JSON.stringify(path,)}.`, ); } /** Plain immutable journal detached from untrusted parsed object. */ const record: WorktreeCopyJournal = { createdEntries: value.createdEntries .map(function createdEntry( entry, ): InstalledWorktreePath { return { ...entry, }; },), destinationRoot: value.destinationRoot, intendedEntries: [...value.intendedEntries,], phase: value.phase, selectedRoots: [...value.selectedRoots,], sourceRoot: value.sourceRoot, stageContainer: value.stageContainer, stageRoot: value.stageRoot, version: 1, }; if (dirname(path,) !== journalRoot) { throw new WorktreeCopyError( `cli-git: worktree-copy journal escaped private root: ${JSON.stringify(path,)}.`, ); } assertCanonicalAbsolutePath({ value: record.destinationRoot, field: 'destinationRoot', },); assertCanonicalAbsolutePath({ value: record.sourceRoot, field: 'sourceRoot', },); assertCanonicalAbsolutePath({ value: record.stageContainer, field: 'stageContainer', },); assertCanonicalAbsolutePath({ value: record.stageRoot, field: 'stageRoot', },); if ((dirname(record.stageContainer,) !== dirname(record.destinationRoot,)) || (!basename(record.stageContainer,) .startsWith(STAGE_PREFIX,)) || (record.stageRoot !== join( record.stageContainer, 'payload', ))) { throw new WorktreeCopyError( `cli-git: worktree-copy journal has unsafe private stage relation: ${JSON.stringify(path,)}.`, ); } record.selectedRoots .forEach(assertSafeRepositoryPath,); record.intendedEntries .forEach(assertSafeRepositoryPath,); record.createdEntries .forEach(function safeCreatedEntry(entry,): void { assertSafeRepositoryPath(entry.relativePath,); },); /** Created paths used to reject duplicate ownership claims. */ const createdPaths = record.createdEntries .map(function createdPath(entry,): string { return entry.relativePath; },); if ((new Set(record.selectedRoots,).size !== record.selectedRoots .length) || (new Set(record.intendedEntries,).size !== record.intendedEntries .length) || (new Set(createdPaths,).size !== createdPaths.length)) { throw new WorktreeCopyError( `cli-git: worktree-copy journal contains duplicate repository paths: ${JSON.stringify(path,)}.`, ); } return record;}