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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282/** Batched private-index and HEAD tree reads for commit-transaction candidates. @module */import { CommitTransactionGitError, runTransactionGit,} from './commit-transaction-git.ts';
/** Strict Git metadata decoder. */const DECODER = new TextDecoder( 'utf-8', { fatal: true, },);/** Paths sent to one Git invocation. Candidate paths expand from pathspecs, so they can outnumber the arguments this process itself received; chunking keeps every invocation far below `ARG_MAX` while spawning once per chunk rather than once per path. */const PATHSPEC_CHUNK_SIZE = 2_048;
/** Stage record parsed from private index. */export type IndexEntry = Readonly<{ /** Raw Git mode text. */ modeText: string; /** Git object ID. */ oid: string; /** Stage number text, where `0` means merged. */ stage: string;}>;
/** Tree record parsed from HEAD. */export type HeadTreeEntry = Readonly<{ /** Raw Git mode text, including `040000` for directory transitions. */ modeText: string; /** Git object ID. */ oid: string;}>;
/** Splits paths into invocation-sized chunks. @param paths - candidate repository paths @returns chunks preserving input order @example ```ts chunkPaths(['a', 'b']); // => [['a', 'b']] ``` */function chunkPaths(paths: readonly string[],): readonly (readonly string[])[] { return Array.from( { length: Math.ceil(paths.length / PATHSPEC_CHUNK_SIZE,), }, function chunkAt( _unused, index, ): readonly string[] { return paths.slice( index * PATHSPEC_CHUNK_SIZE, (index + 1) * PATHSPEC_CHUNK_SIZE, ); }, );}
/** Splits NUL-delimited Git output into metadata and path pairs. @param stdout - exact NUL-delimited output bytes @returns metadata text and repository path per record */function splitRecords(stdout: Uint8Array,): readonly (readonly [ string, string])[] { return DECODER.decode(stdout,) .split('\0',) .flatMap(function toRecord(record,): readonly (readonly [ string, string ])[] { /** Metadata and path boundary. */ const tab = record.indexOf('\t',); return tab === (-1) ? [] : [[ record.slice( 0, tab, ), record.slice(tab + 1,), ],]; },);}
/** Loads stage-zero-first index entries for every requested path. Replaces one `ls-files --stage` spawn per path. Absent paths are simply missing from the result, which is how a deleted candidate is recognised. Unmerged paths keep their lowest stage first, so a caller rejecting a nonzero stage sees exactly what a per-path read reported. @param gitPath - resolved Git executable @param cwd - effective repository directory @param indexPath - private index @param paths - candidate repository paths @returns first stage record per present path @throws CommitTransactionGitError when index metadata is incomplete @example ```ts await loadIndexEntries({ gitPath: '/usr/bin/git', cwd: '/repo', indexPath: '/tmp/index', paths: ['a'] }); ``` */export async function loadIndexEntries({ gitPath, cwd, indexPath, paths,}: Readonly<{ gitPath: string; cwd: string; indexPath: string; paths: readonly string[];}>,): Promise<ReadonlyMap<string, IndexEntry>> { /** Stage records for every chunk, read concurrently over disjoint paths. */ const outputs = await Promise.all(chunkPaths(paths,) .map(function readChunk(chunk,) { return runTransactionGit({ gitPath, cwd, indexPath, args: [ 'ls-files', '--stage', '-z', '--', ...chunk, ], },); },),); /** First stage record per path across every chunk. */ const entries = new Map<string, IndexEntry>(); for (const output of outputs) { for (const [meta, path,] of splitRecords(output.stdout,)) { /** Mode, object ID, and stage fields. */ const [modeText, oid, stage,] = meta.split(' ',); if ((modeText === undefined) || (oid === undefined) || (stage === undefined)) throw new CommitTransactionGitError(`Private index entry is unavailable for ${path}`,); // Git lists ascending stages per path, so the first record retained wins. if (!entries.has(path,)) entries.set( path, { modeText, oid, stage, }, ); } } return entries;}
/** Loads HEAD tree entries for every requested path. Replaces one `ls-tree` spawn per path. An unborn HEAD makes Git exit nonzero for the whole chunk, which yields no entries and classifies every candidate as added, matching a per-path read against a repository without commits. Directory transitions keep their `040000` mode so callers reject them exactly as a per-path read did. @param gitPath - resolved Git executable @param cwd - effective repository directory @param paths - candidate repository paths @param revision - baseline tree-ish: the transaction's preparation base, or live `HEAD` outside a transaction @returns tree record per path present in HEAD @throws CommitTransactionGitError when HEAD tree metadata is incomplete @example ```ts await loadHeadTreeEntries({ gitPath: '/usr/bin/git', cwd: '/repo', paths: ['a'] }); ``` */export async function loadHeadTreeEntries({ gitPath, cwd, paths, revision = 'HEAD',}: Readonly<{ gitPath: string; cwd: string; paths: readonly string[]; revision?: string;}>,): Promise<ReadonlyMap<string, HeadTreeEntry>> { /** Optional tree records for every chunk, read concurrently over disjoint paths. */ const outputs = await Promise.all(chunkPaths(paths,) .map(function readChunk(chunk,) { return runTransactionGit({ gitPath, cwd, args: [ 'ls-tree', '-z', revision, '--', ...chunk, ], allowFailure: true, },); },),); /** Tree record per path across every chunk. */ const entries = new Map<string, HeadTreeEntry>(); for (const output of outputs) { if (output.exitCode !== 0) continue; for (const [meta, path,] of splitRecords(output.stdout,)) { /** Mode, object type, and object ID fields. */ const [modeText, _type, oid,] = meta.split(' ',); if ((modeText === undefined) || (oid === undefined)) throw new CommitTransactionGitError(`HEAD tree metadata is incomplete for ${path}`,); if (!entries.has(path,)) entries.set( path, { modeText, oid, }, ); } } return entries;}