Skip to content
/ git Public
forked from git/git

Commit 3794e9b

Browse files
pks-tgitster
authored andcommitted
builtin/cat-file: support "blob:none" objects filter
Implement support for the "blob:none" filter in git-cat-file(1), which causes us to omit all blobs. Note that this new filter requires us to read the object type via `oid_object_info_extended()` in `batch_object_write()`. But as we try to optimize away reading objects from the database the `data->info.typep` pointer may not be set. We thus have to adapt the logic to conditionally set the pointer in cases where the filter is given. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent eb83e4c commit 3794e9b

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

Documentation/git-cat-file.adoc

+3-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ OPTIONS
8888
been explicitly requested via any of the batch modes that read objects
8989
via standard input (`--batch`, `--batch-check`) will be reported as
9090
"filtered". Excluded objects in `--batch-all-objects` mode will not be
91-
printed at all. No filters are supported yet.
91+
printed at all. The '<filter-spec>' may be one of the following:
92+
+
93+
The form '--filter=blob:none' omits all blobs.
9294

9395
--path=<path>::
9496
For use with `--textconv` or `--filters`, to allow specifying an object

builtin/cat-file.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,8 @@ static void batch_object_write(const char *obj_name,
482482
if (!data->skip_object_info) {
483483
int ret;
484484

485-
if (use_mailmap)
485+
if (use_mailmap ||
486+
opt->objects_filter.choice == LOFC_BLOB_NONE)
486487
data->info.typep = &data->type;
487488

488489
if (pack)
@@ -500,6 +501,14 @@ static void batch_object_write(const char *obj_name,
500501
switch (opt->objects_filter.choice) {
501502
case LOFC_DISABLED:
502503
break;
504+
case LOFC_BLOB_NONE:
505+
if (data->type == OBJ_BLOB) {
506+
if (!opt->all_objects)
507+
report_object_status(opt, obj_name,
508+
&data->oid, "excluded");
509+
return;
510+
}
511+
break;
503512
default:
504513
BUG("unsupported objects filter");
505514
}
@@ -1039,6 +1048,10 @@ int cmd_cat_file(int argc,
10391048
switch (batch.objects_filter.choice) {
10401049
case LOFC_DISABLED:
10411050
break;
1051+
case LOFC_BLOB_NONE:
1052+
if (!batch.enabled)
1053+
usage(_("objects filter only supported in batch mode"));
1054+
break;
10421055
default:
10431056
usagef(_("objects filter not supported: '%s'"),
10441057
list_object_filter_config_name(batch.objects_filter.choice));

t/t1006-cat-file.sh

+45-2
Original file line numberDiff line numberDiff line change
@@ -1354,7 +1354,22 @@ test_expect_success PERL '--batch-command info is unbuffered by default' '
13541354
'
13551355

13561356
test_expect_success 'setup for objects filter' '
1357-
git init repo
1357+
git init repo &&
1358+
(
1359+
# Seed the repository with three different sets of objects:
1360+
#
1361+
# - The first set is fully packed and has a bitmap.
1362+
# - The second set is packed, but has no bitmap.
1363+
# - The third set is loose.
1364+
#
1365+
# This ensures that we cover all these types as expected.
1366+
cd repo &&
1367+
test_commit first &&
1368+
git repack -Adb &&
1369+
test_commit second &&
1370+
git repack -d &&
1371+
test_commit third
1372+
)
13581373
'
13591374

13601375
test_expect_success 'objects filter with unknown option' '
@@ -1365,7 +1380,7 @@ test_expect_success 'objects filter with unknown option' '
13651380
test_cmp expect err
13661381
'
13671382

1368-
for option in blob:none blob:limit=1 object:type=tag sparse:oid=1234 tree:1 sparse:path=x
1383+
for option in blob:limit=1 object:type=tag sparse:oid=1234 tree:1 sparse:path=x
13691384
do
13701385
test_expect_success "objects filter with unsupported option $option" '
13711386
case "$option" in
@@ -1393,4 +1408,32 @@ test_expect_success 'objects filter: disabled' '
13931408
test_cmp expect.sorted actual.sorted
13941409
'
13951410

1411+
test_objects_filter () {
1412+
filter="$1"
1413+
1414+
test_expect_success "objects filter: $filter" '
1415+
git -C repo cat-file --batch-check="%(objectname)" --batch-all-objects --filter="$filter" >actual &&
1416+
sort actual >actual.sorted &&
1417+
git -C repo rev-list --objects --no-object-names --all --filter="$filter" --filter-provided-objects >expect &&
1418+
sort expect >expect.sorted &&
1419+
test_cmp expect.sorted actual.sorted
1420+
'
1421+
1422+
test_expect_success "objects filter prints excluded objects: $filter" '
1423+
# Find all objects that would be excluded by the current filter.
1424+
git -C repo rev-list --objects --no-object-names --all >all &&
1425+
git -C repo rev-list --objects --no-object-names --all --filter="$filter" --filter-provided-objects >filtered &&
1426+
sort all >all.sorted &&
1427+
sort filtered >filtered.sorted &&
1428+
comm -23 all.sorted filtered.sorted >expected.excluded &&
1429+
test_line_count -gt 0 expected.excluded &&
1430+
1431+
git -C repo cat-file --batch-check="%(objectname)" --filter="$filter" <expected.excluded >actual &&
1432+
awk "/excluded/{ print \$1 }" actual | sort >actual.excluded &&
1433+
test_cmp expected.excluded actual.excluded
1434+
'
1435+
}
1436+
1437+
test_objects_filter "blob:none"
1438+
13961439
test_done

0 commit comments

Comments
 (0)