Subject: Re: Git bug: Filter ignored when "--invert-grep" option is used.

From: Junio C Hamano <[email-masked]>
Date: Fri, 17 Dec 2021 10:16:00 -0800

René Scharfe <[email-masked]> writes:

> Subject: [PATCH] log: let --invert-grep only invert --grep
>
> The option --invert-grep is documented to filter out commits whose
> messages match the --grep filters.  However, it also affects the
> header matches (--author, --committer), which is not intended.

I re-read the log message that introduced this feature, and I agree
with the "not intended" part.  I do not think the change itself was
even done with awareness that the header matches may also be
affected, and there is no test for it to see the interaction.

> Move the handling of that option to grep.c, as only the code there can
> distinguish between matches in the header from those in the message
> body.  If --invert-grep is given then enable extended expressions (not
> the regex type, we just need git grep's --not to work), negate the body
> patterns and check if any of them match by piggy-backing on the
> collect_hits mechanism of grep_source_1().

Nice.  The original says that --files-without-matches being a
negation of --files-with-matches was what triggered them to have the
bit in the revisions, not in grep_opt, by the way.

> Collecting the matches in struct grep_opt is a bit iffy, but with
> "last_shown" we have a precedent for writing state information to that
> struct.

I think this is perfectly fine.  apply_state, grep_opt,
diff_options, and rev_info are used the same way within their
subsystems to carry in options that affect behaviour, carry around
the state of the machinery, and carry out the result.  The word
"option" does make it sound it is an input-only thing, but others
are not much better ;-).

> diff --git a/grep.c b/grep.c
> index fe847a0111..beef5fe47e 100644
> --- a/grep.c
> +++ b/grep.c
> @@ -699,6 +699,14 @@ static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
>  	return compile_pattern_or(list);
>  }
>
> +static struct grep_expr *grep_not_expr(struct grep_expr *expr)
> +{
> +	struct grep_expr *z = xcalloc(1, sizeof(*z));
> +	z->node = GREP_NODE_NOT;
> +	z->u.unary = expr;
> +	return z;
> +}

A bit surprising to see that we already had GREP_NODE_NOT without a
helper to create a node.  Not updating compile_pattern_not() to use
this new helper does make this patch simpler to read by allowing
readers to focus on what matters, which is very much appreciaed.

The rest of the patch looks good to me, too.