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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386/** Dispatch plan the hook shim reads: which hooks run, how, and under which lock.
The plan is computed in the wrapper process from the real repository and the caller's environment, because inside native `git commit` the shim only sees the transaction's overrides.
@module */import { isAbsolute, join, resolve,} from 'node:path';import { tagged, } from '@monochromatic-dev/module-logger/ts';import { runTransactionGit, } from '../policy-engine/commit-transaction-git.ts';
/** Module logger. */const l = tagged({ tag: 'cli-git', },);
/** Strict Git output decoder. */const DECODER = new TextDecoder( 'utf-8', { fatal: true, },);
/** Commit events the shim dispatches during preparation; `post-commit` runs once after landing instead. */export const PREPARATION_HOOK_EVENTS = [ 'pre-commit', 'prepare-commit-msg', 'commit-msg',] as const;
/** Every commit event the transaction silences inside native `git commit`. */export const SILENCED_HOOK_EVENTS = [ 'pre-commit', 'prepare-commit-msg', 'commit-msg', 'post-commit',] as const;
/** One commit hook event. */export type CommitHookEvent = typeof SILENCED_HOOK_EVENTS[number];
/** Environment variable carrying the preparation lease into hook descendants. */export const PREPARATION_LEASE_ENV = 'CLI_GIT_PREPARATION_LEASE';
/** Value the dispatcher restores as `GIT_CONFIG_PARAMETERS`, or its absence. */export type ConfigParameters = | Readonly<{ kind: 'absent'; }> | Readonly<{ kind: 'present'; value: string; }>;
/** Config key is not set. */const CONFIG_UNSET: unique symbol = Symbol('git config key unset',);
/** Plan written to `<tx>/hooks/plan.json`. */export type HookDispatchPlan = Readonly<{ /** Plan schema version. */ schemaVersion: 1; /** Absolute real Git executable the shim runs `git hook run` with. */ gitPath: string; /** Repository's own absolute hooks directory: `core.hooksPath`, or `<git-common-dir>/hooks` when unset. */ hooksPath: string; /** Events the user disabled through `hook.<event>.enabled=false`. */ disabledEvents: readonly CommitHookEvent[]; /** Caller's `GIT_CONFIG_PARAMETERS` including its global `-c` options, or absence when there were none. */ configParameters: ConfigParameters; /** Absolute worktree root exported as `GIT_WORK_TREE`. */ worktreeRoot: string; /** Preparation lease exported to hook descendants. */ lease: string; /** Hook lock directory. */ hookLockPath: string; /** Whether hooks skip the hook lock (`hooks.concurrentCommits`, or a valid inherited lease). */ skipHookLock: boolean;}>;
/** Quotes one value in the single-quoted form Git reads from `GIT_CONFIG_PARAMETERS`.
Git's `sq_quote_buf` closes the quote around `'` and `!`; the parser accepts that form in every supported version.
@param value - `key=value` text
@returns quoted entry
@example ```ts quoteConfigParameter("user.name=O'Brien"); // "'user.name=O'\\''Brien'" ``` */export function quoteConfigParameter(value: string,): string { /** Quoted characters, closing and reopening the quote around the two characters Git escapes. */ const body = Array.from(value,) .map(function quoteCharacter(character,): string { if (character === '\'') return String.raw`'\''`; if (character === '!') return String.raw`'\!'`; return character; },) .join('',); return `'${body}'`;}
/** Collects the values of every global `-c` option before the subcommand.
@param globalArgs - arguments before the subcommand
@returns `key=value` texts in order
@example ```ts globalConfigOverrides(['-c', 'a.b=1', '--no-pager']); // ['a.b=1'] ``` */export function globalConfigOverrides(globalArgs: readonly string[],): readonly string[] { return globalArgs.flatMap(function valueAfterFlag( token, index, ): readonly string[] { /** Following token. */ const next = globalArgs[index + 1]; return (token === '-c') && (next !== undefined) ? [next,] : []; },);}
/** Combines the caller's `GIT_CONFIG_PARAMETERS` with its global `-c` options, the value native Git would hand its hooks.
@param inherited - caller environment value, possibly absent
@param overrides - global `-c` values
@returns combined value, or absence when both are absent
@example ```ts combineConfigParameters({ inherited: '', overrides: ['a.b=1'] }); // { kind: 'present', value: "'a.b=1'" } ``` */export function combineConfigParameters({ inherited, overrides,}: Readonly<{ inherited: string; overrides: readonly string[];}>,): ConfigParameters { /** Entries in Git's order: inherited first, then command-line options. */ const entries = [ ...(inherited === '' ? [] : [inherited,]), ...overrides.map(quoteConfigParameter,), ]; return entries.length === 0 ? { kind: 'absent', } : { kind: 'present', value: entries.join(' ',), };}
/** Reads one optional config value in the owning worktree with the caller's global options applied.
@param gitPath - real Git executable
@param cwd - owning worktree directory
@param globalArgs - caller's kept global options
@param type - Git config value type
@param key - config key
@returns value, or the unset marker */async function readConfig({ gitPath, cwd, globalArgs, type, key,}: Readonly<{ gitPath: string; cwd: string; globalArgs: readonly string[]; type: 'path' | 'bool'; key: string;}>,): Promise<string | typeof CONFIG_UNSET> { /** Config lookup; exit 1 means unset. */ const result = await runTransactionGit({ gitPath, cwd, args: [ ...globalArgs, 'config', `--type=${type}`, '--get', key, ], allowFailure: true, },); if (result.exitCode === 1) return CONFIG_UNSET; if (result.exitCode !== 0) throw new TypeError(`git config --get ${key} failed: ${result.stderr .trim()}`,); /** Value with Git's single terminating newline. */ const text = DECODER.decode(result.stdout,); return text.endsWith('\n',) ? text.slice( 0, -1, ) : text;}
/** Computes the dispatch plan for one transaction.
@param gitPath - real Git executable
@param cwd - owning worktree directory
@param globalArgs - caller's global options kept for native Git, whose `-c` values join the config parameters
@param commonDir - absolute common Git directory
@param worktreeRoot - absolute worktree root
@param lease - preparation lease token
@param concurrentCommits - `hooks.concurrentCommits`
@param inheritedLeaseValid - whether this invocation runs under a valid outer preparation lease
@param environment - caller environment
@returns dispatch plan
@example ```ts await computeHookDispatchPlan({ gitPath: '/usr/bin/git', cwd: '/repo', globalArgs: [], commonDir: '/repo/.git', worktreeRoot: '/repo', lease: 'x', concurrentCommits: false, inheritedLeaseValid: false, environment: process.env }); ``` */export async function computeHookDispatchPlan({ gitPath, cwd, globalArgs, commonDir, worktreeRoot, lease, concurrentCommits, inheritedLeaseValid, environment,}: Readonly<{ gitPath: string; cwd: string; globalArgs: readonly string[]; commonDir: string; worktreeRoot: string; lease: string; concurrentCommits: boolean; inheritedLeaseValid: boolean; environment: Readonly<NodeJS.ProcessEnv>;}>,): Promise<HookDispatchPlan> { /** Tagged plan logger. */ const rl = tagged({ tag: computeHookDispatchPlan.name, l, },); /** Repository hooks path and each event switch, read concurrently. */ const [configuredHooksPath, ...eventSwitches] = await Promise.all([ readConfig({ gitPath, cwd, globalArgs, type: 'path', key: 'core.hooksPath', },), ...SILENCED_HOOK_EVENTS.map(function readEventSwitch(event,): Promise<string | typeof CONFIG_UNSET> { return readConfig({ gitPath, cwd, globalArgs, type: 'bool', key: `hook.${event}.enabled`, },); },), ],); /** Absolute hooks directory; Git resolves a relative `core.hooksPath` where hooks run, the worktree root. */ const hooksPath = configuredHooksPath === CONFIG_UNSET ? join( commonDir, 'hooks', ) : (isAbsolute(configuredHooksPath,) ? configuredHooksPath : resolve( worktreeRoot, configuredHooksPath, )); /** Plan with every value encoded later through `JSON.stringify`. */ const plan: HookDispatchPlan = { schemaVersion: 1, gitPath, hooksPath, disabledEvents: SILENCED_HOOK_EVENTS.filter(function isDisabled( _event, index, ): boolean { return eventSwitches[index] === 'false'; },), configParameters: combineConfigParameters({ inherited: environment.GIT_CONFIG_PARAMETERS ?? '', overrides: globalConfigOverrides(globalArgs,), },), worktreeRoot, lease, hookLockPath: join( commonDir, 'cli-git', 'hook.lock', ), skipHookLock: concurrentCommits || inheritedLeaseValid, }; rl.debug(`hook plan: hooksPath=${plan.hooksPath} disabled=${plan.disabledEvents .join(',',)} skipLock=${String(plan.skipHookLock,)}`,); return plan;}