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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288/** Commit selection parsing and private pathspec materialization. @module */import { writeFile, } from 'node:fs/promises';import { join, resolve,} from 'node:path';import { arrayBuffer, } from 'node:stream/consumers';import { runTransactionGit, } from './commit-transaction-git.ts';import type { CommitTransactionWorkspace, } from './commit-transaction-workspace.ts';
/** No pathspec file participates in current invocation. */export const PATHSPEC_FILE_ABSENT: unique symbol = Symbol('commit pathspec file was absent',);/** Internal and explicit only-mode tokens removed for complete private tree. */const ONLY_MODE_TOKENS: ReadonlySet<string> = new Set([ '-o', '--only',],);/** Selection controls consumed before final private-index commit. */const PRIVATE_SELECTION_TOKENS: ReadonlySet<string> = new Set([ '-i', '-p', '--include', '--interactive', '--patch',],);/** Private pathspec input mode. */const PRIVATE_FILE_MODE = 0o600;
/** Removes selected pathspec and only controls after private tree exists. @param args - transformed Git arguments @param pathspecs - parsed selected paths @returns private-index commit arguments @example ```ts privateExplicitCommitArgs({ args: ['commit', '-o', 'a'], pathspecs: ['a'] }); ``` */export function privateExplicitCommitArgs({ args, pathspecs,}: Readonly<{ args: readonly string[]; pathspecs: readonly string[];}>,): readonly string[] { /** Arguments after pathspec-file controls consumed by private setup. */ const withoutPathspecFile: string[] = []; for (let index = 0; index < args.length; index += 1) { /** Current transformed commit token. */ const token = args[index]; if (token === undefined) continue; if (token === '--pathspec-from-file') { index += 1; continue; } if (token.startsWith('--pathspec-from-file=',) || (token === '--pathspec-file-nul')) continue; withoutPathspecFile.push(token,); } /** Mutable local copy used only to remove known path positions. */ const retained = [...withoutPathspecFile,]; for (const pathspec of [...pathspecs,].toReversed()) { /** Last matching token where commit pathspec appears after option value. */ const index = retained.lastIndexOf(pathspec,); if (index !== (-1)) retained.splice( index, 1, ); } return retained.filter(function retainToken(token,) { return (token !== '--') && (!ONLY_MODE_TOKENS.has(token,)); },);}
/** Removes selection controls after private index contains exact user choice. @param args - transformed Git arguments @param pathspecs - parsed positional pathspecs @returns complete private-index commit arguments @example ```ts privateSelectionCommitArgs({ args: ['commit', '--patch', 'a'], pathspecs: ['a'] }); ``` */export function privateSelectionCommitArgs({ args, pathspecs,}: Readonly<{ args: readonly string[]; pathspecs: readonly string[];}>,): readonly string[] { return privateExplicitCommitArgs({ args, pathspecs, },) .filter(function retainSelectionToken(token,) { return !PRIVATE_SELECTION_TOKENS.has(token,); },);}
/** Resolves final arguments after private selection creates complete index. @param args - transformed Git arguments @param pathspecs - parsed positional pathspecs @param mode - private index construction mode @param selectedPrivately - whether selection controls were consumed privately @returns real Git commit arguments @example ```ts resolvePrivateCommitArgs({ args: ['commit', '--patch', 'a'], pathspecs: ['a'], mode: 'index', selectedPrivately: true }); ``` */export function resolvePrivateCommitArgs({ args, pathspecs, mode, selectedPrivately,}: Readonly<{ args: readonly string[]; pathspecs: readonly string[]; mode: 'explicit-path' | 'index'; selectedPrivately: boolean;}>,): readonly string[] { if (mode === 'explicit-path') return privateExplicitCommitArgs({ args, pathspecs, },); return selectedPrivately ? privateSelectionCommitArgs({ args, pathspecs, },) : args;}
/** Runs native interactive selection against private copied index. @param workspace - private transaction workspace @param gitPath - resolved real Git executable @param cwd - effective repository cwd @param patch - whether patch rather than menu selection applies @param pathspecs - positional selection pathspecs @param pathspecFile - optional materialized pathspec file @param pathspecFileNul - whether file records use NUL separators @example ```ts await prepareInteractiveSelection({ workspace, gitPath, cwd, patch: true, pathspecs: ['a'] }); ``` */export async function prepareInteractiveSelection({ workspace, gitPath, cwd, patch, pathspecs, pathspecFile, pathspecFileNul,}: Readonly<{ workspace: CommitTransactionWorkspace; gitPath: string; cwd: string; patch: boolean; pathspecs: readonly string[]; pathspecFile?: string; pathspecFileNul: boolean;}>,): Promise<void> { /** Git-native add selection mode matching commit selection UI. */ const mode = patch ? '--patch' : '--interactive'; /** Exact pathspec source arguments consumed only by private add. */ const selectionPathspecs = pathspecFile === undefined ? [ '--', ...pathspecs, ] : [ `--pathspec-from-file=${pathspecFile}`, ...(pathspecFileNul ? ['--pathspec-file-nul',] : []), ]; await runTransactionGit({ gitPath, cwd, indexPath: workspace.commitIndexPath, args: [ 'add', mode, ...selectionPathspecs, ], stdio: 'inherit', objectDirectory: workspace.objectDirectory, },);}
/** Materializes optional pathspec source exactly once. @param workspace - private transaction workspace @param effectiveCwd - repository cwd for relative source @param source - configured source spelling @returns private or resolved file path, or absence sentinel @example ```ts await materializePathspecFile({ workspace, effectiveCwd: '/repo' }); ``` */export async function materializePathspecFile({ workspace, effectiveCwd, source,}: Readonly<{ workspace: CommitTransactionWorkspace; effectiveCwd: string; source?: string;}>,): Promise<string | typeof PATHSPEC_FILE_ABSENT> { if (source === undefined) return PATHSPEC_FILE_ABSENT; if (source !== '-') return resolve( effectiveCwd, source, ); /** Private single-consumption stdin snapshot. */ const path = join( workspace.directory, 'pathspec.input', ); await writeFile( path, new Uint8Array(await arrayBuffer(process.stdin,),), { mode: PRIVATE_FILE_MODE, }, ); return path;}