Re: [PATCH v2] grep: improve errors for unmatched ( and )
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Mar 23, 2024, 18:06 UTC
- Message-ID
- <xmqqzfuox1oi.fsf@gitster.g>
- In-Reply-To
- <tkz3a5jkalcz5ajemx4b4x42pe6kv45sfmgpin4zeai3moq42o@tarta.nabijaczleweli.xyz>
Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz> writes:
Show 44 quoted lines
> Imagine you want to grep for (. Easy:
>
> $ git grep '('
> fatal: unmatched parenthesis
>
> uhoh. This is plainly wrong. Unless you know specifically that
> (a) git grep has expression groups and that
> (b) the only way to work around them is by doing -- '(' or -e '('
>
> Similarly,
>
> $ git grep ')'
> fatal: incomplete pattern expression: )
>
> is somehow worse. ")" is a complete regular expression pattern.
> Of course, the error wants to say "group" here.
> In this case it's also not "incomplete", it's unmatched.
> But whatever.
>
> Make them return
>
> $ ./git grep '('
> fatal: unmatched ( for expression group
> $ ./git grep ')'
> fatal: incomplete pattern expression group: )
>
> which hopefully are clearer in indicating that it's not the expression
> that's wrong (since no pattern had been parsed at all), but rather that
> it's been misconstrued as a grouping operator.
>
> Link: https://bugs.debian.org/1051205
> Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
> ---
> On Fri, Mar 22, 2024 at 09:41:40AM -0700, Junio C Hamano wrote:
>> Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz> writes:
>> > uhoh. This is plainly wrong. Unless you know specifically that
>> > (a) git grep has expression groups and that
>> > (b) the only way to work around them is by doing -- '(' or -e '('
>> I do not think "--" (end of options and beginning of pathspec)
>> marker would work for that purpose, UNLESS you are talking about a
>> file whose name is an open parenthesis.
> False. -- turns all subsequent parameters into arguments, and if
> there is no -e, the first argument is the pattern, and all the
> subsequent ones are paths. This is normal [git] grep behaviour.Ah, thanks.
I forgot that "git grep" was a oddball that allows revs come after "--" in some cases. As long as the user understands this may not work for other commands, it is OK. "-e" is the only officially supported way (which is why I mentioned it in the review comments I gave you here), so guiding users in that direction would be a better idea anyway, though.
Thanks.
Show 22 quoted lines
> diff --git a/grep.c b/grep.c
> index 5f23d1a..ac34bfe 100644
> --- a/grep.c
> +++ b/grep.c
> @@ -621,7 +621,7 @@ static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
> *list = p->next;
> x = compile_pattern_or(list);
> if (!*list || (*list)->token != GREP_CLOSE_PAREN)
> - die("unmatched parenthesis");
> + die("unmatched ( for expression group");
> *list = (*list)->next;
> return x;
> default:
> @@ -792,7 +792,7 @@ void compile_grep_patterns(struct grep_opt *opt)
> if (p)
> opt->pattern_expression = compile_pattern_expr(&p);
> if (p)
> - die("incomplete pattern expression: %s", p->pattern);
> + die("incomplete pattern expression group: %s", p->pattern);
>
> if (opt->no_body_match && opt->pattern_expression)
> opt->pattern_expression = grep_not_expr(opt->pattern_expression);