Skip to content

Commit 9cf68b2

Browse files
pks-tgitster
authored andcommitted
rev-list: allow filtering of provided items
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-objects` option to the git-rev-parse(1) command. If given, then all user-provided references will be subject to filtering. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 169a15e commit 9cf68b2

File tree

8 files changed

+104
-14
lines changed

8 files changed

+104
-14
lines changed

Documentation/rev-list-options.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,11 @@ equivalent.
933933
--no-filter::
934934
Turn off any previous `--filter=` argument.
935935

936+
--filter-provided-objects::
937+
Filter the list of explicitly provided objects, which would otherwise
938+
always be printed even if they did not match any of the filters. Only
939+
useful with `--filter=`.
940+
936941
--filter-print-omitted::
937942
Only useful with `--filter=`; prints a list of the objects omitted
938943
by the filter. Object IDs are prefixed with a ``~'' character.

builtin/pack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3516,7 +3516,7 @@ static int pack_options_allow_reuse(void)
35163516

35173517
static int get_object_list_from_bitmap(struct rev_info *revs)
35183518
{
3519-
if (!(bitmap_git = prepare_bitmap_walk(revs, &filter_options)))
3519+
if (!(bitmap_git = prepare_bitmap_walk(revs, &filter_options, 0)))
35203520
return -1;
35213521

35223522
if (pack_options_allow_reuse() &&

builtin/rev-list.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,8 @@ static inline int parse_missing_action_value(const char *value)
398398
}
399399

400400
static int try_bitmap_count(struct rev_info *revs,
401-
struct list_objects_filter_options *filter)
401+
struct list_objects_filter_options *filter,
402+
int filter_provided_objects)
402403
{
403404
uint32_t commit_count = 0,
404405
tag_count = 0,
@@ -433,7 +434,7 @@ static int try_bitmap_count(struct rev_info *revs,
433434
*/
434435
max_count = revs->max_count;
435436

436-
bitmap_git = prepare_bitmap_walk(revs, filter);
437+
bitmap_git = prepare_bitmap_walk(revs, filter, filter_provided_objects);
437438
if (!bitmap_git)
438439
return -1;
439440

@@ -450,7 +451,8 @@ static int try_bitmap_count(struct rev_info *revs,
450451
}
451452

452453
static int try_bitmap_traversal(struct rev_info *revs,
453-
struct list_objects_filter_options *filter)
454+
struct list_objects_filter_options *filter,
455+
int filter_provided_objects)
454456
{
455457
struct bitmap_index *bitmap_git;
456458

@@ -461,7 +463,7 @@ static int try_bitmap_traversal(struct rev_info *revs,
461463
if (revs->max_count >= 0)
462464
return -1;
463465

464-
bitmap_git = prepare_bitmap_walk(revs, filter);
466+
bitmap_git = prepare_bitmap_walk(revs, filter, filter_provided_objects);
465467
if (!bitmap_git)
466468
return -1;
467469

@@ -471,14 +473,15 @@ static int try_bitmap_traversal(struct rev_info *revs,
471473
}
472474

473475
static int try_bitmap_disk_usage(struct rev_info *revs,
474-
struct list_objects_filter_options *filter)
476+
struct list_objects_filter_options *filter,
477+
int filter_provided_objects)
475478
{
476479
struct bitmap_index *bitmap_git;
477480

478481
if (!show_disk_usage)
479482
return -1;
480483

481-
bitmap_git = prepare_bitmap_walk(revs, filter);
484+
bitmap_git = prepare_bitmap_walk(revs, filter, filter_provided_objects);
482485
if (!bitmap_git)
483486
return -1;
484487

@@ -499,6 +502,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
499502
int bisect_show_vars = 0;
500503
int bisect_find_all = 0;
501504
int use_bitmap_index = 0;
505+
int filter_provided_objects = 0;
502506
const char *show_progress = NULL;
503507

504508
if (argc == 2 && !strcmp(argv[1], "-h"))
@@ -599,6 +603,10 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
599603
list_objects_filter_set_no_filter(&filter_options);
600604
continue;
601605
}
606+
if (!strcmp(arg, "--filter-provided-objects")) {
607+
filter_provided_objects = 1;
608+
continue;
609+
}
602610
if (!strcmp(arg, "--filter-print-omitted")) {
603611
arg_print_omitted = 1;
604612
continue;
@@ -665,11 +673,11 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
665673
progress = start_delayed_progress(show_progress, 0);
666674

667675
if (use_bitmap_index) {
668-
if (!try_bitmap_count(&revs, &filter_options))
676+
if (!try_bitmap_count(&revs, &filter_options, filter_provided_objects))
669677
return 0;
670-
if (!try_bitmap_disk_usage(&revs, &filter_options))
678+
if (!try_bitmap_disk_usage(&revs, &filter_options, filter_provided_objects))
671679
return 0;
672-
if (!try_bitmap_traversal(&revs, &filter_options))
680+
if (!try_bitmap_traversal(&revs, &filter_options, filter_provided_objects))
673681
return 0;
674682
}
675683

@@ -694,6 +702,16 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
694702
return show_bisect_vars(&info, reaches, all);
695703
}
696704

705+
if (filter_provided_objects) {
706+
struct commit_list *c;
707+
for (i = 0; i < revs.pending.nr; i++) {
708+
struct object_array_entry *pending = revs.pending.objects + i;
709+
pending->item->flags |= NOT_USER_GIVEN;
710+
}
711+
for (c = revs.commits; c; c = c->next)
712+
c->item->object.flags |= NOT_USER_GIVEN;
713+
}
714+
697715
if (arg_print_omitted)
698716
oidset_init(&omitted_objects, DEFAULT_OIDSET_SIZE);
699717
if (arg_missing_action == MA_PRINT)

pack-bitmap.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,8 @@ static int can_filter_bitmap(struct list_objects_filter_options *filter)
986986
}
987987

988988
struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
989-
struct list_objects_filter_options *filter)
989+
struct list_objects_filter_options *filter,
990+
int filter_provided_objects)
990991
{
991992
unsigned int i;
992993

@@ -1081,7 +1082,8 @@ struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
10811082
if (haves_bitmap)
10821083
bitmap_and_not(wants_bitmap, haves_bitmap);
10831084

1084-
filter_bitmap(bitmap_git, wants, wants_bitmap, filter);
1085+
filter_bitmap(bitmap_git, (filter && filter_provided_objects) ? NULL : wants,
1086+
wants_bitmap, filter);
10851087

10861088
bitmap_git->result = wants_bitmap;
10871089
bitmap_git->haves = haves_bitmap;

pack-bitmap.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ void traverse_bitmap_commit_list(struct bitmap_index *,
5050
show_reachable_fn show_reachable);
5151
void test_bitmap_walk(struct rev_info *revs);
5252
struct bitmap_index *prepare_bitmap_walk(struct rev_info *revs,
53-
struct list_objects_filter_options *filter);
53+
struct list_objects_filter_options *filter,
54+
int filter_provided_objects);
5455
int reuse_partial_packfile_from_bitmap(struct bitmap_index *,
5556
struct packed_git **packfile,
5657
uint32_t *entries,

reachable.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
223223
cp.progress = progress;
224224
cp.count = 0;
225225

226-
bitmap_git = prepare_bitmap_walk(revs, NULL);
226+
bitmap_git = prepare_bitmap_walk(revs, NULL, 0);
227227
if (bitmap_git) {
228228
traverse_bitmap_commit_list(bitmap_git, revs, mark_object_seen);
229229
free_bitmap_index(bitmap_git);

t/t6112-rev-list-filters-objects.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,34 @@ test_expect_success 'verify object:type=tag prints tag' '
203203
test_cmp expected actual
204204
'
205205

206+
test_expect_success 'verify object:type=blob prints only blob with --filter-provided-objects' '
207+
printf "%s blob\n" $(git -C object-type rev-parse HEAD:blob) >expected &&
208+
git -C object-type rev-list --objects \
209+
--filter=object:type=blob --filter-provided-objects HEAD >actual &&
210+
test_cmp expected actual
211+
'
212+
213+
test_expect_success 'verify object:type=tree prints only tree with --filter-provided-objects' '
214+
printf "%s \n" $(git -C object-type rev-parse HEAD^{tree}) >expected &&
215+
git -C object-type rev-list --objects \
216+
--filter=object:type=tree HEAD --filter-provided-objects >actual &&
217+
test_cmp expected actual
218+
'
219+
220+
test_expect_success 'verify object:type=commit prints only commit with --filter-provided-objects' '
221+
git -C object-type rev-parse HEAD >expected &&
222+
git -C object-type rev-list --objects \
223+
--filter=object:type=commit --filter-provided-objects HEAD >actual &&
224+
test_cmp expected actual
225+
'
226+
227+
test_expect_success 'verify object:type=tag prints only tag with --filter-provided-objects' '
228+
printf "%s tag\n" $(git -C object-type rev-parse tag) >expected &&
229+
git -C object-type rev-list --objects \
230+
--filter=object:type=tag --filter-provided-objects tag >actual &&
231+
test_cmp expected actual
232+
'
233+
206234
# Test sparse:path=<path> filter.
207235
# !!!!
208236
# NOTE: sparse:path filter support has been dropped for security reasons,

t/t6113-rev-list-bitmap-filters.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,47 @@ test_expect_success 'object:type filter' '
9898
test_bitmap_traversal expect actual
9999
'
100100

101+
test_expect_success 'object:type filter with --filter-provided-objects' '
102+
git rev-list --objects --filter-provided-objects --filter=object:type=tag tag >expect &&
103+
git rev-list --use-bitmap-index \
104+
--objects --filter-provided-objects --filter=object:type=tag tag >actual &&
105+
test_cmp expect actual &&
106+
107+
git rev-list --objects --filter-provided-objects --filter=object:type=commit tag >expect &&
108+
git rev-list --use-bitmap-index \
109+
--objects --filter-provided-objects --filter=object:type=commit tag >actual &&
110+
test_bitmap_traversal expect actual &&
111+
112+
git rev-list --objects --filter-provided-objects --filter=object:type=tree tag >expect &&
113+
git rev-list --use-bitmap-index \
114+
--objects --filter-provided-objects --filter=object:type=tree tag >actual &&
115+
test_bitmap_traversal expect actual &&
116+
117+
git rev-list --objects --filter-provided-objects --filter=object:type=blob tag >expect &&
118+
git rev-list --use-bitmap-index \
119+
--objects --filter-provided-objects --filter=object:type=blob tag >actual &&
120+
test_bitmap_traversal expect actual
121+
'
122+
101123
test_expect_success 'combine filter' '
102124
git rev-list --objects --filter=blob:limit=1000 --filter=object:type=blob tag >expect &&
103125
git rev-list --use-bitmap-index \
104126
--objects --filter=blob:limit=1000 --filter=object:type=blob tag >actual &&
105127
test_bitmap_traversal expect actual
106128
'
107129

130+
test_expect_success 'combine filter with --filter-provided-objects' '
131+
git rev-list --objects --filter-provided-objects --filter=blob:limit=1000 --filter=object:type=blob tag >expect &&
132+
git rev-list --use-bitmap-index \
133+
--objects --filter-provided-objects --filter=blob:limit=1000 --filter=object:type=blob tag >actual &&
134+
test_bitmap_traversal expect actual &&
135+
136+
git cat-file --batch-check="%(objecttype) %(objectsize)" <actual >objects &&
137+
while read objecttype objectsize
138+
do
139+
test "$objecttype" = blob || return 1
140+
test "$objectsize" -le 1000 || return 1
141+
done <objects
142+
'
143+
108144
test_done

0 commit comments

Comments
 (0)