builtin/cat-file: support "object:type=" objects filter

Implement support for the "object:type=" filter in git-cat-file(1), which causes us to omit all objects that don't match the provided object type. 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 8fa9fe171a43b10c47268b6508ad4f39f2f628d6
3 files changed +19 -2
Documentation/git-cat-file.adoc
+3
@@ -96,6 +96,9 @@ The form '--filter=blob:limit=<n>[kmg]' omits blobs of size at least n
96 bytes or units. n may be zero. The suffixes k, m, and g can be used to name
97 units in KiB, MiB, or GiB. For example, 'blob:limit=1k' is the same as
98 'blob:limit=1024'.
99 ++
100 +The form '--filter=object:type=(tag|commit|tree|blob)' omits all objects which
101 +are not of the requested type.
102
103 --path=<path>::
104 For use with `--textconv` or `--filters`, to allow specifying an object
builtin/cat-file.c
+11 -1
@@ -484,7 +484,8 @@ static void batch_object_write(const char *obj_name,
484
485 if (use_mailmap ||
486 opt->objects_filter.choice == LOFC_BLOB_NONE ||
487 - opt->objects_filter.choice == LOFC_BLOB_LIMIT)
487 + opt->objects_filter.choice == LOFC_BLOB_LIMIT ||
488 + opt->objects_filter.choice == LOFC_OBJECT_TYPE)
489 data->info.typep = &data->type;
490 if (opt->objects_filter.choice == LOFC_BLOB_LIMIT)
491 data->info.sizep = &data->size;
@@ -521,6 +522,14 @@ static void batch_object_write(const char *obj_name,
522 return;
523 }
524 break;
525 + case LOFC_OBJECT_TYPE:
526 + if (data->type != opt->objects_filter.object_type) {
527 + if (!opt->all_objects)
528 + report_object_status(opt, obj_name,
529 + &data->oid, "excluded");
530 + return;
531 + }
532 + break;
533 default:
534 BUG("unsupported objects filter");
535 }
@@ -1062,6 +1071,7 @@ int cmd_cat_file(int argc,
1071 break;
1072 case LOFC_BLOB_NONE:
1073 case LOFC_BLOB_LIMIT:
1074 + case LOFC_OBJECT_TYPE:
1075 if (!batch.enabled)
1076 usage(_("objects filter only supported in batch mode"));
1077 break;
t/t1006-cat-file.sh
+5 -1
@@ -1388,7 +1388,7 @@ test_expect_success 'objects filter with unknown option' '
1388 test_cmp expect err
1389 '
1390
1391 -for option in object:type=tag sparse:oid=1234 tree:1 sparse:path=x
1391 +for option in sparse:oid=1234 tree:1 sparse:path=x
1392 do
1393 test_expect_success "objects filter with unsupported option $option" '
1394 case "$option" in
@@ -1447,5 +1447,9 @@ test_objects_filter "blob:limit=1"
1447 test_objects_filter "blob:limit=500"
1448 test_objects_filter "blob:limit=1000"
1449 test_objects_filter "blob:limit=1k"
1450 +test_objects_filter "object:type=blob"
1451 +test_objects_filter "object:type=commit"
1452 +test_objects_filter "object:type=tag"
1453 +test_objects_filter "object:type=tree"
1454
1455 test_done