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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375import { realpath, } from 'node:fs/promises';import { resolve, } from 'node:path';
import { SubprocessError, } from 'nano-spawn';
import { runMetadataGit, stripGitLine,} from './git-metadata.ts';import { parseGlobalOptions, } from './parse-global-options.ts';
/** Metadata Git process reports target absence. */const IDENTITY_METADATA_ABSENT: unique symbol = Symbol( 'Git worktree identity metadata is absent',);
/** Repository identity selected by effective Git global options. Canonical administrative paths make main-versus-linked classification stable across symbolic links and relative Git output. */export type GitWorktreeIdentity = | Readonly<{ /** No worktree or bare repository was selected. */ kind: 'outside-worktree'; /** Effective cwd after Git global `-C` chaining. */ effectiveCwd: string; }> | Readonly<{ /** Bare repository with no source worktree root. */ kind: 'bare-repository'; /** Effective cwd after Git global `-C` chaining. */ effectiveCwd: string; /** Canonical common Git directory. */ commonDir: string; /** Canonical invocation-specific Git directory. */ gitDir: string; }> | Readonly<{ /** Main worktree whose Git and common directories are identical. */ kind: 'main-worktree'; /** Effective cwd after Git global `-C` chaining. */ effectiveCwd: string; /** Canonical common Git directory. */ commonDir: string; /** Canonical invocation-specific Git directory. */ gitDir: string; /** Canonical selected worktree root. */ worktreeRoot: string; }> | Readonly<{ /** Linked worktree with distinct per-worktree Git administration. */ kind: 'linked-worktree'; /** Effective cwd after Git global `-C` chaining. */ effectiveCwd: string; /** Canonical common Git directory. */ commonDir: string; /** Canonical invocation-specific Git directory. */ gitDir: string; /** Canonical selected worktree root. */ worktreeRoot: string; }>;
/** Options selecting same repository target as forwarded Git invocation. */type ResolveGitWorktreeIdentityOptions = Readonly<{ /** Complete forwarded Git arguments. */ args: readonly string[]; /** Absolute real-Git executable. */ gitPath: string;}>;
/** Git metadata output needed to classify repository identity. */type ParsedIdentityMetadata = Readonly<{ /** Git-reported bare-repository flag. */ isBare: boolean; /** Raw Git common-directory path. */ commonDir: string; /** Raw invocation-specific Git-directory path. */ gitDir: string; /** Absolute or effective-cwd-relative path back to worktree root. */ worktreePath: string;}>;
/** Runs real Git metadata command against exact effective target selection. @param gitPath - absolute real-Git executable @param preSubcommandArgs - original Git global option region @param invocationCwd - wrapper process working directory @param metadataArgs - arguments after `rev-parse` @returns captured stdout @example ```ts await runIdentityGit({ gitPath: '/usr/bin/git', preSubcommandArgs: [], invocationCwd: '/repo', metadataArgs: ['--git-dir'], }); ``` */function runIdentityGit({ gitPath, preSubcommandArgs, invocationCwd, metadataArgs,}: Readonly<{ gitPath: string; preSubcommandArgs: readonly string[]; invocationCwd: string; metadataArgs: readonly string[];}>,): Promise<string> { return runMetadataGit({ gitPath, args: [ ...preSubcommandArgs, 'rev-parse', '--path-format=absolute', ...metadataArgs, ], cwd: invocationCwd, },);}
/** Reads fixed-order identity metadata or reports Git target absence. @param gitPath - absolute real-Git executable @param preSubcommandArgs - original Git global option region @param invocationCwd - wrapper process working directory @returns raw identity metadata or absence sentinel @example ```ts await readIdentityMetadata({ gitPath: '/usr/bin/git', preSubcommandArgs: [], invocationCwd: '/repo', }); ``` */async function readIdentityMetadata({ gitPath, preSubcommandArgs, invocationCwd,}: Readonly<{ gitPath: string; preSubcommandArgs: readonly string[]; invocationCwd: string;}>,): Promise<string | typeof IDENTITY_METADATA_ABSENT> { try { return await runIdentityGit({ gitPath, preSubcommandArgs, invocationCwd, metadataArgs: [ '--is-bare-repository', '--git-dir', '--git-common-dir', '--show-cdup', ], },); } catch (error: unknown) { if (error instanceof SubprocessError) return IDENTITY_METADATA_ABSENT; throw error; }}
/** Parses fixed-order worktree identity metadata. @param output - three-line real-Git metadata output @returns validated identity metadata @throws when real Git returns incomplete metadata @example ```ts parseIdentityMetadata('false\n/repo/.git\n/repo/.git\n'); ``` */function parseIdentityMetadata(output: string,): ParsedIdentityMetadata { /** Fixed-order metadata fields without terminal line break. */ const [ isBareOutput, gitDir, commonDir, worktreePath = '', ] = stripGitLine(output,) .split('\n',); if ((gitDir === undefined) || (commonDir === undefined)) { throw new Error( 'cli-git: could not classify git worktree because git rev-parse returned incomplete metadata.', ); } return { commonDir, gitDir, isBare: isBareOutput === 'true', worktreePath, };}
/** Resolves canonical repository and worktree identity once for package consumers. This module owns replay of Git global repository-selection options, canonical path resolution, bare and outside handling, and main-versus-linked classification. Policy-specific allowlisting remains outside this shared identity seam. @param args - complete forwarded Git arguments @param gitPath - absolute real-Git executable @returns selected repository identity with canonical repository paths @example ```ts await resolveGitWorktreeIdentity({ args: ['-C', '/repo', 'status'], gitPath: '/usr/bin/git', }); // => { // kind: 'main-worktree', // effectiveCwd: '/repo', // gitDir: '/repo/.git', // commonDir: '/repo/.git', // worktreeRoot: '/repo', // } ``` */export async function resolveGitWorktreeIdentity({ args, gitPath,}: ResolveGitWorktreeIdentityOptions,): Promise<GitWorktreeIdentity> { /** Effective cwd and subcommand location for forwarded invocation. */ const { effectiveCwd, subcommandIndex, } = parseGlobalOptions(args,); /** Exact global option region selecting repository target. */ const preSubcommandArgs = args.slice( 0, subcommandIndex, ); /** Wrapper process cwd from which Git applies original global options. */ const invocationCwd = process.cwd(); /** Raw fixed-order identity metadata or repository absence. */ const metadataOutput = await readIdentityMetadata({ gitPath, preSubcommandArgs, invocationCwd, },); if ((typeof metadataOutput) === 'symbol') { return { kind: 'outside-worktree', effectiveCwd, }; } /** Parsed raw paths and repository-shape flags. */ const metadata = parseIdentityMetadata(metadataOutput,); /** Canonical administrative paths shared by bare and worktree identities. */ const [ gitDir, commonDir, ] = await Promise.all([ realpath(metadata.gitDir,), realpath(metadata.commonDir,), ],); if (metadata.isBare) { return { kind: 'bare-repository', commonDir, effectiveCwd, gitDir, }; } /** Worktree root resolved from Git's absolute cdup result or effective root cwd. */ const worktreeRoot = await realpath(metadata.worktreePath === '' ? effectiveCwd : resolve( effectiveCwd, metadata.worktreePath, ),); return { kind: gitDir === commonDir ? 'main-worktree' : 'linked-worktree', commonDir, effectiveCwd, gitDir, worktreeRoot, };}