Skip to content

Commit 2abc7f0

Browse files
peffgitster
authored andcommitted
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 6554dfa (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 <alan@source.dev> Helped-by: Pablo Sabater <pabloosabaterr@gmail.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e9019fc commit 2abc7f0

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

‎builtin/cat-file.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,8 +687,11 @@ static void parse_cmd_contents(struct batch_options *opt,
687687
struct strbuf *output,
688688
struct expand_data *data)
689689
{
690+
enum object_type *saved_typep = data->info.typep;
691+
data->info.typep = &data->type;
690692
opt->batch_mode = BATCH_MODE_CONTENTS;
691693
batch_one_object(line, output, opt, data);
694+
data->info.typep = saved_typep;
692695
}
693696

694697
static void parse_cmd_info(struct batch_options *opt,

‎t/t1006-cat-file.sh‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,14 @@ test_expect_success 'batch-command flush without --buffer' '
13511351
grep "^fatal:.*flush is only for --buffer mode.*" err
13521352
'
13531353

1354+
test_expect_success 'batch-command contents auto-handles type' '
1355+
echo "HEAD" |
1356+
git cat-file --batch="%(objectname)" >expect &&
1357+
echo "contents HEAD" |
1358+
git cat-file --batch-command="%(objectname)" >actual &&
1359+
test_cmp expect actual
1360+
'
1361+
13541362
perl_script='
13551363
use warnings;
13561364
use strict;

0 commit comments

Comments
 (0)