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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301/** Content-bearing raw diff-tree record parsing shared by landed and pushed delta facts. @module */import type { CandidateFileMode, GitObjectId,} from '../api/policy-types.ts';
/** Git tree modes mapped to policy modes. */const RAW_DIFF_MODES: Readonly<Record<string, CandidateFileMode>> = { '100644': 'regular', '100755': 'executable', '120000': 'symlink', '160000': 'submodule',};
/** One retained content-bearing change parsed from raw diff-tree output. */export type RawDiffRecord = Readonly<{ /** Repository-relative changed path. */ path: string; /** New-side Git object ID. */ oid: GitObjectId; /** Policy candidate mode. */ mode: CandidateFileMode; /** Change classification against the diff baseline. */ change: 'added' | 'modified';}>;
/** Returns required raw metadata field. @param parts - split metadata fields @param index - required field index @param createError - domain error factory for malformed output @returns present metadata field @throws caller-domain error when field is absent */function requiredDiffPart({ parts, index, createError,}: { readonly parts: readonly string[]; readonly index: number; readonly createError: (message: string) => Error;},): string { /** Metadata field at required position. */ const value = parts[index]; if (value === undefined) throw createError('Raw diff-tree record metadata is incomplete.',); return value;}
/** Retains first-wins content-bearing records from alternating metadata/path tokens. Deletions publish no content and are dropped; with `-m` a merge lists one diff per parent, so the first record retained per path wins. @param tokens - alternating colon-prefixed metadata and path tokens @param createError - domain error factory for malformed output @returns retained content-bearing records in first-appearance order @throws caller-domain error when tokens are malformed */function retainRecords({ tokens, createError,}: { readonly tokens: readonly string[]; readonly createError: (message: string) => Error;},): readonly RawDiffRecord[] { /** First retained record per path across every listed diff. */ const recordsByPath = new Map<string, RawDiffRecord>(); // Tokens alternate strictly: one colon-prefixed metadata token, then its path. for (let cursor = 0; cursor < tokens.length; cursor += 2) { /** Colon-prefixed raw metadata token. */ const meta = tokens[cursor]; /** Companion repository-relative path token. */ const path = tokens[cursor + 1]; if ((meta === undefined) || (!meta.startsWith(':',)) || (path === undefined)) throw createError('Raw diff-tree output is not metadata/path token pairs.',); /** Old mode, new mode, old OID, new OID, and status fields. */ const parts = meta.slice(1,) .split(' ',); /** New-side tree mode text. */ const modeText = requiredDiffPart({ parts, index: 1, createError, },); /** New-side Git object ID. */ const oid = requiredDiffPart({ parts, index: 3, createError, },); /** Single-letter change status against one diff baseline. */ const status = requiredDiffPart({ parts, index: 4, createError, },); // Deleted paths carry no new-side content and never become candidates. if (status === 'D') continue; if ((status !== 'A') && (status !== 'M') && (status !== 'T')) throw createError(`Unsupported raw diff status ${status} for ${path}`,); /** Policy mode mapped from new-side Git mode. */ const mode = RAW_DIFF_MODES[modeText]; if (mode === undefined) throw createError(`Unsupported raw diff mode ${modeText} for ${path}`,); if (!recordsByPath.has(path,)) recordsByPath.set( path, { path, oid, mode, change: status === 'A' ? 'added' : 'modified', }, ); } return [...recordsByPath.values(),];}
/** Splits raw diff-tree output into non-empty NUL-delimited tokens. @param text - decoded NUL-delimited raw diff-tree output @returns tokens without terminal empty token */function rawTokens(text: string,): readonly string[] { return text.split('\0',) .filter(function isToken(token,) { return token.length > 0; },);}
/** Parses NUL-delimited raw diff-tree output into first-wins per-path records. Deletions publish no content and are dropped; with `-m` a merge lists one diff per parent, so the first record retained per path wins. @param text - decoded NUL-delimited raw diff-tree output @param createError - domain error factory for malformed output @returns retained content-bearing records in first-appearance order @throws caller-domain error when output is malformed @example ```ts parseRawDiffRecords({ text: '', createError: function toError(message,) { return new Error(message,); } }); // => [] ``` */export function parseRawDiffRecords({ text, createError,}: { readonly text: string; readonly createError: (message: string) => Error;},): readonly RawDiffRecord[] { return retainRecords({ tokens: rawTokens(text,), createError, },);}
/** Parses `diff-tree --stdin -z` output into per-commit first-wins records. Each diff section starts with a bare commit-ID token where a metadata token would otherwise stand; `-m` repeats that header once per merge parent, and repeated sections of one commit share first-wins path retention exactly as a single-commit {@link parseRawDiffRecords} call would. Commits whose diff is empty print no header and are absent from the result. @param text - decoded NUL-delimited multi-commit raw diff-tree output @param createError - domain error factory for malformed output @returns retained records per commit, in first-header order @throws caller-domain error when a record precedes any header or lacks its path @example ```ts parseRawDiffCommitStream({ text: '', createError: function toError(message,) { return new Error(message,); } }); // => Map {} ``` */export function parseRawDiffCommitStream({ text, createError,}: { readonly text: string; readonly createError: (message: string) => Error;},): ReadonlyMap<string, readonly RawDiffRecord[]> { /** Complete token stream. */ const tokens = rawTokens(text,); /** Metadata/path tokens collected per commit header. */ const tokensByCommit = new Map<string, string[]>(); /** Commit header owning the current section, absent before the first header. */ const cursorState: { commit?: string } = {}; // Tokens are headers where metadata is expected; paths are consumed pairwise and never inspected as headers. for (let cursor = 0; cursor < tokens.length;) { /** Token at metadata-or-header position. */ const token = tokens[cursor] ?? ''; if (!token.startsWith(':',)) { cursorState.commit = token; if (!tokensByCommit.has(token,)) tokensByCommit.set( token, [], ); cursor += 1; continue; } /** Current section's token sink. */ const sink = cursorState.commit === undefined ? undefined : tokensByCommit.get(cursorState.commit,); /** Companion path token. */ const path = tokens[cursor + 1]; if ((sink === undefined) || (path === undefined)) throw createError('Raw diff-tree stream record lacks its commit header or path.',); sink.push( token, path, ); cursor += 2; } return new Map([...tokensByCommit.entries(),].map(function retainCommit([commit, commitTokens,],): readonly [ string, readonly RawDiffRecord[] ] { return [ commit, retainRecords({ tokens: commitTokens, createError, },), ]; },),);}