Skip to content

Commit 013f091

Browse files
committed
fix(hooks): one parse for a quoted git option value
A quoted git option value with no spaces matched both the quote branch and the bare branch, so each repeated -C, -c, --git-dir or --work-tree doubled the work on a command that was not git. The value's first character now picks its one alternative, as for an env assignment.
1 parent 64ddd9a commit 013f091

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

‎src/hooks_bash_commands.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ export function extractCommand(event: HookEvent): string | undefined {
276276
/** Matches git subcommands that can move HEAD and rewrite working-tree file content. */
277277
/** The leading `NAME=value` words a git command may carry (`FOO=1 BAR="x y" git ...`), the shape skipEnvAssignments reads on tokens. The git patterns take it as a prefix: stripCommandPrefix declines to peel an assignment that changes what runs (`PATH=`, `LD_*`), and such a command still runs git. A value's first character picks its one alternative (quote group, unclosed quote, bare), so an assignment parses one way and a non-matching command costs linear time. */
278278
const LEADING_ENV_ASSIGNMENTS = String.raw`(?:[A-Za-z_]\w*=(?:(?:"[^"]*"|'[^']*'|"(?![^"]*")|'(?![^']*')|[^\s"'])\S*)?\s+)*`
279-
/** A git option's value, bare or quoted. */
280-
const GIT_OPTION_VALUE = String.raw`(?:"[^"]*"|'[^']*'|\S+)`
279+
/** A git option's value, bare or quoted; its first character picks its one alternative, as for an assignment's value, so a quoted value has one parse. */
280+
const GIT_OPTION_VALUE = String.raw`(?:"[^"]*"|'[^']*'|"(?![^"]*")|'(?![^']*')|[^\s"'])\S*`
281281
/** The text before a git subcommand: leading `NAME=value` assignments, then git's global options (`-C <path>`, `-c <name>=<value>`, `--git-dir`, `--work-tree`, other flags), values bare or quoted, so `git -C ../b checkout other` and `GIT_DIR=... git checkout other` read as the checkout they run. Exported for the git patterns in shell_writes.ts. */
282282
export const GIT_PREFIX_RE_SRC = String.raw`^\s*${LEADING_ENV_ASSIGNMENTS}git(?:\s+(?:-[Cc]\s+${GIT_OPTION_VALUE}|--git-dir(?:=|\s+)${GIT_OPTION_VALUE}|--work-tree(?:=|\s+)${GIT_OPTION_VALUE}|--(?!(?:git-dir|work-tree)(?:=|\s))[A-Za-z][\w-]*|-(?![Cc]\s)[A-Za-z][\w-]*))*\s+`
283283
const HEAD_MOVING_GIT_RE = new RegExp(`${GIT_PREFIX_RE_SRC}(?:checkout|switch|pull|merge|rebase|reset|cherry-pick|stash\\s+branch)\\b`, 'i')

‎tests/hooks_bash.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4632,9 +4632,18 @@ describe('the git prefix pattern parses each env assignment and each global opti
46324632
expect(GIT_RESTORE.test('git ' + unit.repeat(REPEATS) + 'x'), `${REPEATS} repeats, no subcommand`).toBe(false)
46334633
})
46344634

4635+
it.each([['-C "a" '], ['--git-dir "a" '], ["-c 'k' "], ['--work-tree "a" ']])('git followed by the quoted option %s repeated is not a restore, at 3 and at 2000 repeats', (unit) => {
4636+
expect(GIT_RESTORE.test('git ' + unit.repeat(3) + 'x'), 'three repeats, no subcommand').toBe(false)
4637+
expect(GIT_RESTORE.test('git ' + unit.repeat(3) + 'restore q'), 'three repeats, then restore').toBe(true)
4638+
expect(GIT_RESTORE.test('git ' + unit.repeat(REPEATS) + 'x'), `${REPEATS} repeats, no subcommand`).toBe(false)
4639+
})
4640+
46354641
it.each([
46364642
['FOO="a b" git restore x', true],
46374643
["FOO='a b' BAR=1 git restore x", true],
4644+
['git -C "a b" -c \'k=v w\' restore x', true],
4645+
['git -C "a"b restore x', true],
4646+
['git -C "unclosed restore x', true],
46384647
['FOO="x"y git restore x', true],
46394648
['git -C a -c b=c restore q', true],
46404649
['git -C a -C b -C c restore q', true],

0 commit comments

Comments
 (0)