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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307import { readFile, realpath,} from 'node:fs/promises';import { basename, isAbsolute, join, resolve,} from 'node:path';
import { runMetadataGit, stripGitLine,} from '../git-metadata.ts';import { readAdminIds, } from './git-observer.ts';import { WorktreeCopyError, } from './errors.ts';import type { CreatedWorktree, WorktreeCopyObservation,} from './model.ts';
/** Git-file prefix introducing linked-worktree administrative path. */const GITDIR_PREFIX = 'gitdir: ';
/** Main worktree has no linked administrative identity. */const MAIN_WORKTREE_ADMIN_ID: unique symbol = Symbol('main worktree has no linked admin identity',);
/** Registered worktree root is currently absent from filesystem. */const REGISTERED_ROOT_MISSING: unique symbol = Symbol('registered worktree root is absent',);
/** Parses NUL-delimited porcelain output into canonical worktree roots. @param output - `git worktree list --porcelain -z` stdout @returns listed worktree path fields @example ```ts parseWorktreeRoots('worktree /repo\0HEAD abc\0\0'); // => ['/repo'] ``` */function parseWorktreeRoots(output: string,): readonly string[] { return output .split('\0',) .flatMap(function worktreeField(field,): readonly string[] { return field.startsWith('worktree ',) ? [field.slice('worktree '.length,),] : []; },);}
/** Resolves linked-worktree admin identity from root `.git` pointer. @param root - canonical linked-worktree root @returns canonical admin directory basename, or main-worktree sentinel @example ```ts await readLinkedAdminId('/worktrees/topic'); // => 'topic' ``` */async function readLinkedAdminId( root: string,): Promise<string | typeof MAIN_WORKTREE_ADMIN_ID> { try { /** Linked-worktree `.git` pointer text. */ const pointer = stripGitLine(await readFile( join( root, '.git', ), 'utf8', ),); if (!pointer.startsWith(GITDIR_PREFIX,)) return MAIN_WORKTREE_ADMIN_ID; /** Raw absolute or root-relative administrative path. */ const rawAdminPath = pointer.slice(GITDIR_PREFIX.length,); /** Canonical linked-worktree administrative path. */ const adminPath = await realpath(isAbsolute(rawAdminPath,) ? rawAdminPath : resolve( root, rawAdminPath, ),); return basename(adminPath,); } catch (error: unknown) { if (Error.isError(error,) && ('code' in error) && (error.code === 'EISDIR')) { return MAIN_WORKTREE_ADMIN_ID; } throw new WorktreeCopyError( `cli-git: could not resolve worktree identity for ${JSON.stringify(root,)}.`, error, ); }}
/** Canonicalizes registered root while tolerating Git-retained missing worktrees. @param root - Git-reported registered worktree path @returns canonical root or missing-root sentinel @example ```ts await canonicalRegisteredRoot('/worktrees/topic'); ``` */async function canonicalRegisteredRoot( root: string,): Promise<string | typeof REGISTERED_ROOT_MISSING> { try { return await realpath(root,); } catch (error: unknown) { if (Error.isError(error,) && ('code' in error) && (error.code === 'ENOENT')) return REGISTERED_ROOT_MISSING; throw error; }}
/** Reads canonical registered worktree roots keyed by linked admin identity. @param observation - effective repository captured before forwarding @param gitPath - absolute real-Git executable @returns linked worktree roots and complete root exclusion set @example ```ts await readRegisteredWorktrees({ observation, gitPath: '/usr/bin/git' }); ``` */export async function readRegisteredWorktrees({ observation, gitPath,}: Readonly<{ observation: WorktreeCopyObservation; gitPath: string;}>,): Promise<Readonly<{ rootsByAdminId: ReadonlyMap<string, string>; roots: readonly string[];}>> { /** NUL-delimited stable worktree inventory. */ const output = await runMetadataGit({ gitPath, args: [ '--git-dir', observation.commonDir, 'worktree', 'list', '--porcelain', '-z', ], cwd: observation.effectiveCwd, },); /** Canonical roots still present on filesystem. */ const rootResults = await Promise.all(parseWorktreeRoots(output,) .map(canonicalRegisteredRoot,),); /** Existing canonical roots, omitting Git-retained missing registrations. */ const roots = rootResults.filter(function existingRoot( root, ): root is string { return (typeof root) === 'string'; },); /** Optional linked admin identities aligned with existing roots. */ const adminIds = await Promise.all(roots.map(function linkedAdminId( root, ): Promise<string | typeof MAIN_WORKTREE_ADMIN_ID> { return (observation.sourceRoot === undefined) && (root === observation.commonDir) ? Promise.resolve(MAIN_WORKTREE_ADMIN_ID,) : readLinkedAdminId(root,); },),); /** Linked-worktree identity map excluding main worktree. */ const rootsByAdminId = new Map(adminIds.flatMap(function indexedRoot( adminId, index, ): readonly (readonly [ string, string ])[] { /** Root aligned with current admin identity. */ const root = roots[index]; return ((typeof adminId) === 'symbol') || (root === undefined) ? [] : [[ adminId, root, ],]; },),); return { rootsByAdminId, roots, };}
/** Finds worktrees newly registered since initial observation. @param observation - effective repository captured before real Git @param gitPath - absolute real-Git executable @returns created worktrees and complete registered root set @example ```ts await findCreatedWorktrees({ observation, gitPath: '/usr/bin/git' }); ``` */export async function findCreatedWorktrees({ observation, gitPath,}: Readonly<{ observation: WorktreeCopyObservation; gitPath: string;}>,): Promise<Readonly<{ created: readonly CreatedWorktree[]; registeredRoots: readonly string[];}>> { /** Linked-worktree identities after real Git returned. */ const afterAdminIds = await readAdminIds(observation.adminRoot,); /** Newly present administrative identities. */ const createdAdminIds = [...afterAdminIds,] .filter(function wasAbsent(adminId,): boolean { return !observation.beforeAdminIds .has(adminId,); },) .toSorted(); if (createdAdminIds.length === 0) { return { created: [], registeredRoots: [], }; } /** Registered worktree paths after creation. */ const registered = await readRegisteredWorktrees({ observation, gitPath, },); /** Created roots resolved from stable admin identities. */ const created = createdAdminIds.map(function createdWorktree(adminId,): CreatedWorktree { /** Created root associated with new admin directory. */ const root = registered.rootsByAdminId .get(adminId,); if (root === undefined) { throw new WorktreeCopyError( `cli-git: linked worktree ${JSON.stringify(adminId,)} was registered but its root could not be resolved.`, ); } return { adminId, root, }; },); return { created, registeredRoots: registered.roots, };}