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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311import { tagged, } from '@monochromatic-dev/module-logger/ts';
import { classifyEffectiveTarget, type EffectiveTarget,} from '../effective-target.ts';import { stripEscapeHatch, WORKTREE_ENFORCEMENT_ESCAPE_HATCH,} from '../escape-hatch.ts';import { parseGlobalOptions, } from '../parse-global-options.ts';import { cleanChangesWorktree, parseCleanRegion,} from '../parser/clean.ts';import { parseResetRegion, resetChangesWorktree,} from '../parser/reset.ts';import { parseStashRegion, } from '../parser/stash.ts';import { CLEAN_SUBCOMMAND, type GuardedCommand, RESET_SUBCOMMAND, STASH_SUBCOMMAND,} from './linked-worktree-constants.ts';import { mainWorktreeMessage, outsideWorktreeMessage,} from './linked-worktree-messages.ts';
/** Logger root for cli-git after removing the package log shim. @example ```ts const rl = tagged({ tag: someFunction.name, l, },); ``` */const l = tagged({ tag: 'cli-git', },);
//region Guarded command predicates and policy facts
/** Per-subcommand fact set produced by an optique-based region parser. */type GuardedRegion = { /** True when wrapper-only escape hatch appears as a real flag. */ readonly hasEscapeHatch: boolean; /** True when invocation can change worktree filesystem state. */ readonly changesWorktree: boolean; /** Options that consume the *next* argv token; used to strip flag-position escape hatches without disturbing values. */ readonly separateValueOptions: ReadonlySet<string>;};
/** Stash options that consume the next argv token. */const STASH_VALUE_OPTIONS: ReadonlySet<string> = new Set([ '-m', '--message', '--pathspec-from-file',],);
/** Clean options that consume the next argv token (long form is matched via abbreviation table inside the parser). */const CLEAN_VALUE_OPTIONS: ReadonlySet<string> = new Set([ '-e', '--exclude',],);
/** Reset options that consume the next argv token. */const RESET_VALUE_OPTIONS: ReadonlySet<string> = new Set([ '--pathspec-from-file',],);
/** Narrows subcommand string to commands covered by linked-worktree policy. @param subcommand - Subcommand located by global option parser. @returns `true` when subcommand has linked-worktree policy. @example ```ts isGuardedCommand('stash'); // => true ``` */function isGuardedCommand(subcommand: string,): subcommand is GuardedCommand { return (subcommand === STASH_SUBCOMMAND) || (subcommand === CLEAN_SUBCOMMAND) || (subcommand === RESET_SUBCOMMAND);}
/** Options for computing per-subcommand facts. */type ComputeRegionOptions = { /** Guarded git subcommand. */ readonly subcommand: GuardedCommand; /** Arguments strictly after subcommand. */ readonly postSubcommandArgs: readonly string[];};
/** Selects the appropriate optique-based parser for the subcommand ({@link parseStashRegion}, {@link parseCleanRegion} with {@link cleanChangesWorktree}, or {@link parseResetRegion} with {@link resetChangesWorktree}) and turns its result into the common {@link GuardedRegion} shape used by the rule. @param subcommand - Guarded git subcommand. @param postSubcommandArgs - Arguments strictly after subcommand. @returns Region facts. @example ```ts computeRegion({ subcommand: 'clean', postSubcommandArgs: ['-ndX'] }); // changesWorktree = false (dry-run) ``` */function computeRegion({ subcommand, postSubcommandArgs,}: ComputeRegionOptions,): GuardedRegion { if (subcommand === STASH_SUBCOMMAND) { /** Stash region facts; stash is always destructive when forwarded. */ const stash = parseStashRegion(postSubcommandArgs,); return { hasEscapeHatch: stash.hasEscapeHatch, changesWorktree: true, separateValueOptions: STASH_VALUE_OPTIONS, }; }
if (subcommand === CLEAN_SUBCOMMAND) { /** Clean region facts; destructiveness depends on dry-run / interactive flags. */ const clean = parseCleanRegion(postSubcommandArgs,); return { hasEscapeHatch: clean.hasEscapeHatch, changesWorktree: cleanChangesWorktree(clean,), separateValueOptions: CLEAN_VALUE_OPTIONS, }; }
/** Reset region facts; destructive when --hard/--merge/--keep (or abbreviation) appears. */ const reset = parseResetRegion(postSubcommandArgs,); return { hasEscapeHatch: reset.hasEscapeHatch, changesWorktree: resetChangesWorktree(reset,), separateValueOptions: RESET_VALUE_OPTIONS, };}
//endregion Guarded command predicates and policy facts
/** Expected linked-worktree policy violation. */export class LinkedWorktreeViolationError extends Error { /** Creates expected policy violation. @param message - safe rejection explanation */ public constructor(message: string,) { super(message,); this.name = 'LinkedWorktreeViolationError'; }}
//region Linked worktree enforcement entry
/** Rejects state-changing guarded commands unless effective working directory is a linked git worktree root. This covers all `git stash`, non-dry-run `git clean`, and destructive `git reset` modes (`--hard`, `--merge`, `--keep`). Per-subcommand optique parsers model long-option abbreviations and option arity, so neither the destructiveness decision nor the wrapper-only escape hatch can be bypassed by abbreviations or by hiding the escape hatch in a value position. Worktree classification is delegated to {@link classifyEffectiveTarget}, which replays the caller's pre-subcommand region and inherited environment against real git's `rev-parse` so `--git-dir`, `--work-tree`, `GIT_DIR`, and `GIT_WORK_TREE` cannot make the wrapper validate one worktree while the destructive command operates on another. Subcommand location comes from {@link parseGlobalOptions} and per-command facts from {@link computeRegion}; the escape hatch is stripped by {@link stripEscapeHatch}, and rejections are rendered by {@link outsideWorktreeMessage} and {@link mainWorktreeMessage}. @param args - Git argv to inspect after wrapper invocation. @returns Original argv when command is unguarded, linked-worktree-safe, or targets a baked-in allowlisted tool-cache directory; argv with flag-position `--no-enforce-worktree` stripped when escape hatch is in use. @throws When guarded state-changing invocation is requested outside a linked git worktree. @example ```ts await checkLinkedWorktree(['-C', '/repo-linked', 'stash']); // passes when real git reports /repo-linked is a linked worktree await checkLinkedWorktree(['-C', '/repo-main', 'clean', '-fd']); // throws when /repo-main is the main worktree await checkLinkedWorktree(['-C', '/repo-main', 'clean', '-ndX']); // passes because dry-run clean does not delete files ``` */export async function checkLinkedWorktree( args: readonly string[],): Promise<readonly string[]> { /** Effective cwd and subcommand index after walking pre-subcommand `-C` chaining. */ const { effectiveCwd, subcommandIndex, } = parseGlobalOptions(args,); /** Subcommand at the located index; `undefined` when args have no subcommand. */ const subcommand = args[subcommandIndex];
if (subcommand === undefined) return args;
if (!isGuardedCommand(subcommand,)) return args;
/** Tagged logger for the linked-worktree-only rule. */ const rl = tagged({ tag: checkLinkedWorktree.name, l, },);
/** Slice of args strictly after guarded subcommand. */ const postSubcommandArgs = args.slice(subcommandIndex + 1,); /** Facts about the post-subcommand region produced by optique. */ const region = computeRegion({ subcommand, postSubcommandArgs, },);
if (region.hasEscapeHatch) { rl.debug( '--no-enforce-worktree present in flag position, stripping and skipping linked-worktree check', ); return stripEscapeHatch({ args, subcommandIndex, separateValueOptions: region.separateValueOptions, escapeHatchToken: WORKTREE_ENFORCEMENT_ESCAPE_HATCH, },); }
if (!region.changesWorktree) return args;
rl.debug(`effective cwd: ${effectiveCwd}, subcommand: ${subcommand}`,);
/** Worktree classification driven by shared identity resolver. */ const target: EffectiveTarget = await classifyEffectiveTarget({ args, },);
if (target === 'outside-worktree') throw new LinkedWorktreeViolationError(outsideWorktreeMessage({ command: subcommand, },),);
if (target === 'main-worktree') throw new LinkedWorktreeViolationError(mainWorktreeMessage({ command: subcommand, },),);
rl.debug(`effective target ${target}; linked-worktree enforcement not triggered`,); return args;}
//endregion Linked worktree enforcement entry