builtin/cat-file: support "blob:limit=" objects filter

Implement support for the "blob:limit=" filter in git-cat-file(1), which causes us to omit all blobs that are bigger than a certain size. 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 dbe1b32d59699092d549150e2db7af07e3cbfaf3
3 files changed +34 -4
Documentation/git-cat-file.adoc
+5
@@ -91,6 +91,11 @@ OPTIONS
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 +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 --path=<path>::
101 For use with `--textconv` or `--filters`, to allow specifying an object
builtin/cat-file.c
+14 -1
@@ -483,8 +483,11 @@ static void batch_object_write(const char *obj_name,
483 int ret;
484
485 if (use_mailmap ||
486 - opt->objects_filter.choice == LOFC_BLOB_NONE)
486 + opt->objects_filter.choice == LOFC_BLOB_NONE ||
487 + opt->objects_filter.choice == LOFC_BLOB_LIMIT)
488 data->info.typep = &data->type;
489 + if (opt->objects_filter.choice == LOFC_BLOB_LIMIT)
490 + data->info.sizep = &data->size;
491
492 if (pack)
493 ret = packed_object_info(the_repository, pack, offset,
@@ -509,6 +512,15 @@ static void batch_object_write(const char *obj_name,
512 return;
513 }
514 break;
515 + case LOFC_BLOB_LIMIT:
516 + if (data->type == OBJ_BLOB &&
517 + data->size >= opt->objects_filter.blob_limit_value) {
518 + if (!opt->all_objects)
519 + report_object_status(opt, obj_name,
520 + &data->oid, "excluded");
521 + return;
522 + }
523 + break;
524 default:
525 BUG("unsupported objects filter");
526 }
@@ -1049,6 +1061,7 @@ int cmd_cat_file(int argc,
1061 case LOFC_DISABLED:
1062 break;
1063 case LOFC_BLOB_NONE:
1064 + case LOFC_BLOB_LIMIT:
1065 if (!batch.enabled)
1066 usage(_("objects filter only supported in batch mode"));
1067 break;
t/t1006-cat-file.sh
+15 -3
@@ -1356,11 +1356,12 @@ test_expect_success PERL '--batch-command info is unbuffered by default' '
1356 test_expect_success 'setup for objects filter' '
1357 git init repo &&
1358 (
1359 - # Seed the repository with three different sets of objects:
1359 + # Seed the repository with four 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 + # - The fourth set is loose and contains big objects.
1365 #
1366 # This ensures that we cover all these types as expected.
1367 cd repo &&
@@ -1368,7 +1369,14 @@ test_expect_success 'setup for objects filter' '
1369 git repack -Adb &&
1370 test_commit second &&
1371 git repack -d &&
1371 - test_commit third
1372 + test_commit third &&
1373 +
1374 + for n in 1000 10000
1375 + do
1376 + printf "%"$n"s" X >large.$n || return 1
1377 + done &&
1378 + git add large.* &&
1379 + git commit -m fourth
1380 )
1381 '
1382
@@ -1380,7 +1388,7 @@ test_expect_success 'objects filter with unknown option' '
1388 test_cmp expect err
1389 '
1390
1383 -for option in blob:limit=1 object:type=tag sparse:oid=1234 tree:1 sparse:path=x
1391 +for option in object:type=tag sparse:oid=1234 tree:1 sparse:path=x
1392 do
1393 test_expect_success "objects filter with unsupported option $option" '
1394 case "$option" in
@@ -1435,5 +1443,9 @@ test_objects_filter () {
1443 }
1444
1445 test_objects_filter "blob:none"
1446 +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
1451 test_done