Skip to content

Commit 8fa9fe1

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

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Documentation/git-cat-file.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ The form '--filter=blob:limit=<n>[kmg]' omits blobs of size at least n
9696
bytes or units. n may be zero. The suffixes k, m, and g can be used to name
9797
units in KiB, MiB, or GiB. For example, 'blob:limit=1k' is the same as
9898
'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.
99102

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

builtin/cat-file.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@ static void batch_object_write(const char *obj_name,
484484

485485
if (use_mailmap ||
486486
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)
488489
data->info.typep = &data->type;
489490
if (opt->objects_filter.choice == LOFC_BLOB_LIMIT)
490491
data->info.sizep = &data->size;
@@ -521,6 +522,14 @@ static void batch_object_write(const char *obj_name,
521522
return;
522523
}
523524
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;
524533
default:
525534
BUG("unsupported objects filter");
526535
}
@@ -1062,6 +1071,7 @@ int cmd_cat_file(int argc,
10621071
break;
10631072
case LOFC_BLOB_NONE:
10641073
case LOFC_BLOB_LIMIT:
1074+
case LOFC_OBJECT_TYPE:
10651075
if (!batch.enabled)
10661076
usage(_("objects filter only supported in batch mode"));
10671077
break;

t/t1006-cat-file.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ test_expect_success 'objects filter with unknown option' '
13881388
test_cmp expect err
13891389
'
13901390

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
13921392
do
13931393
test_expect_success "objects filter with unsupported option $option" '
13941394
case "$option" in
@@ -1447,5 +1447,9 @@ test_objects_filter "blob:limit=1"
14471447
test_objects_filter "blob:limit=500"
14481448
test_objects_filter "blob:limit=1000"
14491449
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"
14501454

14511455
test_done

0 commit comments

Comments
 (0)