import { expandAbbreviations, } from '../abbrev.ts'; import { PATHSPEC_SEPARATOR, WORKTREE_ENFORCEMENT_ESCAPE_HATCH, } from '../escape-hatch.ts'; import { ARGV_REFUSED, type ArgvSpec, tryParseArgv, } from './argv.ts'; //region Reset abbreviation tables (git accepts unambiguous prefixes) /** Aliases for `--hard`, generated by {@link expandAbbreviations}. No other reset long option starts with `--h`. */ const HARD_ALIASES = expandAbbreviations({ longOption: '--hard', },); /** Aliases for `--merge`, generated by {@link expandAbbreviations}. Requires `--me` because `--m` is ambiguous with `--mixed`. */ const MERGE_ALIASES = expandAbbreviations({ longOption: '--merge', minStemLength: 2, },); /** Aliases for `--keep`, generated by {@link expandAbbreviations}. No other reset long option starts with `--k`. */ const KEEP_ALIASES = expandAbbreviations({ longOption: '--keep', },); //endregion Reset abbreviation tables //region Reset post-subcommand optique parser /** Declared option surface of the post-`reset` argv region. Models destructive reset modes plus the value-taking `--pathspec-from-file ` so the wrapper-only escape hatch cannot be misread when it appears in the value position. Other reset options stay undeclared since they do not influence destructiveness. */ const resetRegionSpec: ArgvSpec = { flags: { hardFlags: { names: [...HARD_ALIASES,], }, mergeFlags: { names: [...MERGE_ALIASES,], }, keepFlags: { names: [...KEEP_ALIASES,], }, escape: { names: [WORKTREE_ENFORCEMENT_ESCAPE_HATCH,], }, }, valueOptions: { pathspecFromFile: { names: ['--pathspec-from-file',], }, }, }; //endregion Reset post-subcommand optique parser //region Reset region facts derived from optique parse /** Facts about the post-`reset` argv region used by linked-worktree policy. */ export type ResetRegion = { /** True when any destructive reset mode (any abbreviation) appears before pathspecs. */ readonly hasDestructiveMode: boolean; /** True when wrapper-only escape hatch appears as a real flag. */ readonly hasEscapeHatch: boolean; /** True when optique parse failed; rule should be conservative. */ readonly parseFailed: boolean; }; /** Splits `args` at the {@link PATHSPEC_SEPARATOR} and returns only the option region. @param args - Post-subcommand argv tokens. @returns Argv slice strictly before `--`. @example ```ts optionRegion(['--hard', '--', 'HEAD']); // => ['--hard'] ``` */ function optionRegion(args: readonly string[],): readonly string[] { /** Position of pathspec separator inside post-subcommand region. */ const separatorIndex = args.indexOf(PATHSPEC_SEPARATOR,); if (separatorIndex === (-1)) return args; return args.slice( 0, separatorIndex, ); } /** Parses the post-`reset` argv region into a structured fact set used by the linked-worktree rule. Splits the region with {@link optionRegion}, then closes parser-mismatch bypasses by declaring every accepted abbreviation of the destructive modes and by modelling `--pathspec-from-file` arity so the escape-hatch token cannot be hidden in the value slot. Parse failures leave `parseFailed: true` so the rule can default to conservative enforcement. @param postSubcommandArgs - Arguments strictly after `reset` subcommand. @returns Fact record consumed by reset-worktree policy. @example ```ts parseResetRegion(['--har', 'HEAD~1']).hasDestructiveMode; // => true (git accepts --har as --hard) ``` */ export function parseResetRegion( postSubcommandArgs: readonly string[], ): ResetRegion { /** Argv slice handed to optique; pathspec region is excluded. */ const region = optionRegion(postSubcommandArgs,); /** Parsed facts over the cleaned option region, or refusal. */ const parsed = tryParseArgv({ args: region, spec: resetRegionSpec, },); if (parsed === ARGV_REFUSED) { return { hasDestructiveMode: false, hasEscapeHatch: false, parseFailed: true, }; } /** Occurrence count per declared flag group. */ const { flagCounts, } = parsed; /** Sum of destructive-mode flag occurrences (`--hard` + `--merge` + `--keep`, any abbreviation). */ const destructiveModeCount = (flagCounts.hardFlags ?? 0) + (flagCounts.mergeFlags ?? 0) + (flagCounts.keepFlags ?? 0); return { hasDestructiveMode: destructiveModeCount > 0, hasEscapeHatch: (flagCounts.escape ?? 0) > 0, parseFailed: false, }; } //endregion Reset region facts derived from optique parse //region Reset destructiveness policy /** Determines whether a `git reset` invocation can change worktree files. Destructive when any of `--hard`, `--merge`, or `--keep` (or accepted abbreviations) appears before the pathspec separator. Conservative under parse failure: treats unknown cases as destructive so the guard runs. @param region - Parsed reset region. @returns `true` when invocation can change worktree filesystem state. @example ```ts resetChangesWorktree(parseResetRegion(['--mixed', 'HEAD~1'])); // => false (mixed mode is index-only) ``` */ export function resetChangesWorktree(region: ResetRegion,): boolean { if (region.parseFailed) return true; return region.hasDestructiveMode; } //endregion Reset destructiveness policy