threads / discuss / 51635

git grep -F (fixed string) requires matching parenthesis?

Subject: git grep -F (fixed string) requires matching parenthesis?

## tl;dr

3 messages between Aug 13, 2019 and Aug 13, 2019.

replies: 2people: 2as markdown or json

nanaya· Aug 13, 2019, 06:01 UTC · lore
Hi,
I observed this today:

$ git grep -F '(' fatal: unmatched parenthesis

Which doesn't make sense and I believe shouldn't happen.

$ git --version git version 2.22.0

Jeff King· Aug 13, 2019, 12:17 UTC · re: nanaya · lore

Re: git grep -F (fixed string) requires matching parenthesis?

On Tue, Aug 13, 2019 at 03:01:04PM +0900, nanaya wrote:
Show 6 quoted lines
> I observed this today:
> 
> $ git grep -F '('
> fatal: unmatched parenthesis
> 
> Which doesn't make sense and I believe shouldn't happen.

At first glance this looks like we're feeding a fixed-string pattern to the regex compiler, which would indeed be a bug.

But I think it's actually happening at a level above that. git-grep supports multiple patterns, which can be joined with --and, --or, --not, etc. And they can be grouped with parentheses.

What you're seeing is the argument parser thinking your '(' is part of the construction of a boolean match formula, and complaining about the lack of closing ')'. You can use "-e" to make it clear that it's a pattern (just as you'd need to for a pattern that starts with "-"):

  git grep -F -e '('

So I think everything is working as designed, though I admit the implication was slightly surprising to me (and note that it only happens with that _specific_ pattern; something like "(foo" would not be matched by the option parser).

-Peff
nanaya· Aug 13, 2019, 12:33 UTC · re: Jeff King · lore

Re: git grep -F (fixed string) requires matching parenthesis?

On Tue, Aug 13, 2019, at 21:17, Jeff King wrote:
Show 16 quoted lines
> But I think it's actually happening at a level above that. git-grep
> supports multiple patterns, which can be joined with --and, --or, --not,
> etc. And they can be grouped with parentheses.
> 
> What you're seeing is the argument parser thinking your '(' is part of
> the construction of a boolean match formula, and complaining about the
> lack of closing ')'. You can use "-e" to make it clear that it's a
> pattern (just as you'd need to for a pattern that starts with "-"):
> 
>   git grep -F -e '('
> 
> So I think everything is working as designed, though I admit the
> implication was slightly surprising to me (and note that it only happens
> with that _specific_ pattern; something like "(foo" would not be matched
> by the option parser).
> 
Thanks. That was confusing (and the error message didn't help). I've always assumed the grep is similar to posix grep so it was weird the same syntax work for fgrep but not git-grep -F.

← back to recent threads