From: Jeff King <peff@peff.net>
To: Alan Stokes <alan@source.dev>
Cc: git@vger.kernel.org
Subject: Re: Assertion failure with git cat-file --batch-command
Date: Mon, 27 Jul 2026 05:57:35 -0400 [thread overview]
Message-ID: <20260727095735.GA1153453@coredump.intra.peff.net> (raw)
In-Reply-To: <CAFZW3h0K6vi15HhMEX30Ab+pjRc3mQr2Myv9KJUH=MWzsvt0FQ@mail.gmail.com>
On Mon, Jul 27, 2026 at 10:30:43AM +0100, Alan Stokes wrote:
> I first observed this in 2.43.0, but it still seems to be present in
> 2.54.0.
Yeah, I think this has been there since --batch-command was added.
> Note that if I ask git cat-file --batch-command to include the
> objecttype in the output it is fine (which gives me a workaround). Or
> if I use git cat-file --batch.
>
> IIUC git only fetches the metadata that it needs for each object, and
> that is determined from the format. For --batch I guess the type is
> always requested, since it is needed to print the object contents. But
> for --batch-command that doesn't seem to happen.
Yes, exactly. In the normal --batch code path we have this code:
/*
* If we are printing out the object, then always fill in the type,
* since we will want to decide whether or not to stream.
*/
if (opt->batch_mode == BATCH_MODE_CONTENTS)
data.info.typep = &data.type;
But for command mode, we don't do the same. This makes your case work:
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index 1458dd76d6..78eab9723d 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -690,6 +690,7 @@ static void parse_cmd_contents(struct batch_options *opt,
struct expand_data *data)
{
opt->batch_mode = BATCH_MODE_CONTENTS;
+ data->info.typep = &data->type;
batch_one_object(line, output, opt, data);
}
but there's a slight catch. That expand_data is used for every request,
not just the current one. In normal --batch mode, every request wants
the same data (the user-specified format plus the object contents). But
in command mode, some may be "contents" requests and some may just be
"info". The code above turns on type-checking for every request, making
the "info" ones pay to look up the type.
A type lookup isn't all that expensive, but it might matter for some
formats (e.g., just "%(objectname)" does an existence check and nothing
else, so we never even access the object data).
I guess saving and restore data->info.typep would work.
> I'm not sure what the correct fix is - always request the type in
> --batch-command, or perhaps only if a "contents" command is issued?
Yeah, in general if you are asking about "contents" I'd expect you to
get the full name/type/size triple. But it's not wrong to ask for less,
and certainly we should never hit a BUG(). So I think we'd want a fix
along the lines above.
Do you want to try your hand at a patch? It would need to do the
save/restore, and most importantly add a new test to t1006.
-Peff
next prev parent reply other threads:[~2026-07-27 9:57 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-27 9:30 Assertion failure with git cat-file --batch-command Alan Stokes
2026-07-27 9:57 ` Jeff King [this message]
2026-07-27 20:26 ` Pablo Sabater
2026-07-28 9:08 ` Alan Stokes
2026-07-28 15:00 ` [PATCH] cat-file: handle content request for --batch-command without type Jeff King
2026-07-28 17:36 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260727095735.GA1153453@coredump.intra.peff.net \
--to=peff@peff.net \
--cc=alan@source.dev \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox