Skip to content

Commit dbe1b32

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3794e9b commit dbe1b32

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

Documentation/git-cat-file.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ OPTIONS
9191
printed at all. The '<filter-spec>' may be one of the following:
9292
+
9393
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'.
9499

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

builtin/cat-file.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,11 @@ static void batch_object_write(const char *obj_name,
483483
int ret;
484484

485485
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)
487488
data->info.typep = &data->type;
489+
if (opt->objects_filter.choice == LOFC_BLOB_LIMIT)
490+
data->info.sizep = &data->size;
488491

489492
if (pack)
490493
ret = packed_object_info(the_repository, pack, offset,
@@ -509,6 +512,15 @@ static void batch_object_write(const char *obj_name,
509512
return;
510513
}
511514
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;
512524
default:
513525
BUG("unsupported objects filter");
514526
}
@@ -1049,6 +1061,7 @@ int cmd_cat_file(int argc,
10491061
case LOFC_DISABLED:
10501062
break;
10511063
case LOFC_BLOB_NONE:
1064+
case LOFC_BLOB_LIMIT:
10521065
if (!batch.enabled)
10531066
usage(_("objects filter only supported in batch mode"));
10541067
break;

t/t1006-cat-file.sh

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,19 +1356,27 @@ test_expect_success PERL '--batch-command info is unbuffered by default' '
13561356
test_expect_success 'setup for objects filter' '
13571357
git init repo &&
13581358
(
1359-
# Seed the repository with three different sets of objects:
1359+
# Seed the repository with four different sets of objects:
13601360
#
13611361
# - The first set is fully packed and has a bitmap.
13621362
# - The second set is packed, but has no bitmap.
13631363
# - The third set is loose.
1364+
# - The fourth set is loose and contains big objects.
13641365
#
13651366
# This ensures that we cover all these types as expected.
13661367
cd repo &&
13671368
test_commit first &&
13681369
git repack -Adb &&
13691370
test_commit second &&
13701371
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
13721380
)
13731381
'
13741382

@@ -1380,7 +1388,7 @@ test_expect_success 'objects filter with unknown option' '
13801388
test_cmp expect err
13811389
'
13821390

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
13841392
do
13851393
test_expect_success "objects filter with unsupported option $option" '
13861394
case "$option" in
@@ -1435,5 +1443,9 @@ test_objects_filter () {
14351443
}
14361444

14371445
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"
14381450

14391451
test_done

0 commit comments

Comments
 (0)