Subject: Re: [PATCH v2 8/8] rev-list: allow filtering of provided items

From: Jeff King <[email-masked]>
Date: Tue, 6 Apr 2021 14:04:15 -0400

On Mon, Mar 15, 2021 at 02:15:05PM +0100, Patrick Steinhardt wrote:

> When providing an object filter, it is currently impossible to also
> filter provided items. E.g. when executing `git rev-list HEAD` , the
> commit this reference points to will be treated as user-provided and is
> thus excluded from the filtering mechanism. This makes it harder than
> necessary to properly use the new `--filter=object:type` filter given
> that even if the user wants to only see blobs, he'll still see commits
> of provided references.
> 
> Improve this by introducing a new `--filter-provided` option to the
> git-rev-parse(1) command. If given, then all user-provided references
> will be subject to filtering.

I think this option is a good thing to have.

The name seems a little confusing to me, as I can read is as both
"please filter the provided objects" and "a filter has been provided".
I guess "--filter-print-provided" would be more clear. And also the
default, so you'd want "--no-filter-print-provided". That's kind of
clunky, though. Maybe "--filter-omit-provided"?

> @@ -694,6 +698,16 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
>  			return show_bisect_vars(&info, reaches, all);
>  	}
>  
> +	if (filter_options.filter_wants) {
> +		struct commit_list *c;
> +		for (i = 0; i < revs.pending.nr; i++) {
> +			struct object_array_entry *pending = revs.pending.objects + i;
> +			pending->item->flags |= NOT_USER_GIVEN;
> +		}
> +		for (c = revs.commits; c; c = c->next)
> +			c->item->object.flags |= NOT_USER_GIVEN;
> +	}

You store the flag inside the filter_options struct, which implies to me
that it's something that could be applied per-filter (at least in
theory; the command line option doesn't allow us to distinguish).

But here you treat it as a global flag that munges the NOT_USER_GIVEN
flags. Given that it's inside the filter_options struct, and that you
propagate it via transform_to_combine_type(), I'd have expected the LOFC
code to look at the flag and decide to ignore the whole user-given
concept completely.

To be clear, I don't mind at all having it as a global that applies to
all filters. I don't think the flexibility buys us anything. But since
it only applies to rev-list, why not just make it a global option within
rev-list?

And then these hunks:

> diff --git a/list-objects-filter-options.c b/list-objects-filter-options.c
> index bb6f6577d5..2877aa9e96 100644
> --- a/list-objects-filter-options.c
> +++ b/list-objects-filter-options.c
> @@ -242,6 +242,7 @@ static void transform_to_combine_type(
>  		memset(filter_options, 0, sizeof(*filter_options));
>  		filter_options->sub = sub_array;
>  		filter_options->sub_alloc = initial_sub_alloc;
> +		filter_options->filter_wants = sub_array[0].filter_wants;
>  	}
>  	filter_options->sub_nr = 1;
>  	filter_options->choice = LOFC_COMBINE;
> @@ -290,6 +291,9 @@ void parse_list_objects_filter(
>  		parse_error = gently_parse_list_objects_filter(
>  			&filter_options->sub[filter_options->sub_nr - 1], arg,
>  			&errbuf);
> +		if (!parse_error)
> +			filter_options->sub[filter_options->sub_nr - 1].filter_wants =
> +				filter_options->filter_wants;
>  	}
>  	if (parse_error)
>  		die("%s", errbuf.buf);
> diff --git a/list-objects-filter-options.h b/list-objects-filter-options.h
> index 4d0d0588cc..5e609e307a 100644
> --- a/list-objects-filter-options.h
> +++ b/list-objects-filter-options.h
> @@ -42,6 +42,12 @@ struct list_objects_filter_options {
>  	 */
>  	enum list_objects_filter_choice choice;
>  
> +	/*
> +	 * "--filter-provided" was given by the user, instructing us to also
> +	 * filter all explicitly provided objects.
> +	 */
> +	unsigned int filter_wants : 1;
> +
>  	/*
>  	 * Choice is LOFC_DISABLED because "--no-filter" was requested.
>  	 */

would not be needed at all.

> diff --git a/pack-bitmap.c b/pack-bitmap.c
> index e33805e076..5ff800316b 100644
> --- a/pack-bitmap.c
> +++ b/pack-bitmap.c
> @@ -1101,7 +1101,8 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
>  	if (haves_bitmap)
>  		bitmap_and_not(wants_bitmap, haves_bitmap);
>  
> -	filter_bitmap(bitmap_git, wants, wants_bitmap, filter);
> +	filter_bitmap(bitmap_git, (filter && filter->filter_wants) ? NULL : wants,
> +		      wants_bitmap, filter);
>  
>  	bitmap_git->result = wants_bitmap;
>  	bitmap_git->haves = haves_bitmap;

I guess we'd need to pass that flag into prepare_bitmap_walk() here so
it knows not to bother with the wants-filtering. But that seems less bad
that stuffing it into the filter struct.

-Peff