From 2abc7f0304606e23199416ffa3dbc84fa2d431d3 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 28 Jul 2026 11:00:31 -0400 Subject: [PATCH] cat-file: handle content request for --batch-command without type The batch mode of cat-file needs to know the object's type in order to print the contents (because it decides whether to stream or not based on object type). The default batch output contains %(objecttype), so we get the type info automatically. But when it doesn't, we have to ask for it explicitly. In the --batch code path, we check while setting up the object_info struct whether we will print the contents, and if so set "typep" to get the value. This comes from 6554dfa97a (cat-file: handle --batch format with missing type/size, 2013-12-12). But later we added a --batch-command mode, which does not do the same trick. The decision about whether to retrieve the contents is made per-command (a "contents" vs "info" command), so we can't decide when building the object_info originally. As a result, asking for: echo "contents HEAD" | git cat-file --batch-command="%(objectname)" will fail the assertion in print_object_or_die() that the type was actually filled in. We can fix it by tweaking the object_info on the fly as we receive each command. But we should be careful to restore it afterwards; otherwise a sequence of commands like: contents $one info $two info $three will pay the type-lookup price for $two and $three when it does not need to. This wouldn't be incorrect, but just slightly inefficient (and hence there are no tests for that part, because the externally-visible behavior is the same). Reported-by: Alan Stokes Helped-by: Pablo Sabater Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin/cat-file.c | 3 +++ t/t1006-cat-file.sh | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index d6ef8414ee..826a1c6620 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -687,8 +687,11 @@ static void parse_cmd_contents(struct batch_options *opt, struct strbuf *output, struct expand_data *data) { + enum object_type *saved_typep = data->info.typep; + data->info.typep = &data->type; opt->batch_mode = BATCH_MODE_CONTENTS; batch_one_object(line, output, opt, data); + data->info.typep = saved_typep; } static void parse_cmd_info(struct batch_options *opt, diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index 8e2c52652c..d0db1f2a29 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -1351,6 +1351,14 @@ test_expect_success 'batch-command flush without --buffer' ' grep "^fatal:.*flush is only for --buffer mode.*" err ' +test_expect_success 'batch-command contents auto-handles type' ' + echo "HEAD" | + git cat-file --batch="%(objectname)" >expect && + echo "contents HEAD" | + git cat-file --batch-command="%(objectname)" >actual && + test_cmp expect actual +' + perl_script=' use warnings; use strict; -- 2.47.3