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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Apr 2, 2025 at 13:13 UTC
3794e9bf982cde754a48b569a639bd2e180e754c
3 files changed
+62
-4
Documentation/git-cat-file.adoc
+3
-1
@@ -88,7 +88,9 @@ OPTIONS
88
been explicitly requested via any of the batch modes that read objects
89
via standard input (`--batch`, `--batch-check`) will be reported as
90
"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.
94
95
--path=<path>::
96
For use with `--textconv` or `--filters`, to allow specifying an object
builtin/cat-file.c
+14
-1
@@ -482,7 +482,8 @@ static void batch_object_write(const char *obj_name,
482
if (!data->skip_object_info) {
483
int ret;
484
485
- if (use_mailmap)
485
+ if (use_mailmap ||
486
+ opt->objects_filter.choice == LOFC_BLOB_NONE)
487
data->info.typep = &data->type;
488
489
if (pack)
@@ -500,6 +501,14 @@ static void batch_object_write(const char *obj_name,
501
switch (opt->objects_filter.choice) {
502
case LOFC_DISABLED:
503
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;
512
default:
513
BUG("unsupported objects filter");
514
}
@@ -1039,6 +1048,10 @@ int cmd_cat_file(int argc,
1048
switch (batch.objects_filter.choice) {
1049
case LOFC_DISABLED:
1050
break;
1051
+ case LOFC_BLOB_NONE:
1052
+ if (!batch.enabled)
1053
+ usage(_("objects filter only supported in batch mode"));
1054
+ break;
1055
default:
1056
usagef(_("objects filter not supported: '%s'"),
1057
list_object_filter_config_name(batch.objects_filter.choice));
t/t1006-cat-file.sh
+45
-2
@@ -1354,7 +1354,22 @@ test_expect_success PERL '--batch-command info is unbuffered by default' '
1354
'
1355
1356
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
+ )
1373
'
1374
1375
test_expect_success 'objects filter with unknown option' '
@@ -1365,7 +1380,7 @@ test_expect_success 'objects filter with unknown option' '
1380
test_cmp expect err
1381
'
1382
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
1384
do
1385
test_expect_success "objects filter with unsupported option $option" '
1386
case "$option" in
@@ -1393,4 +1408,32 @@ test_expect_success 'objects filter: disabled' '
1408
test_cmp expect.sorted actual.sorted
1409
'
1410
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
+
1439
test_done