git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 3/3] object-file-convert: copy unknown commit headers unchanged

From
Patrick Steinhardt <ps@pks.im>
Date
Nov 16, 2026, 07:37 UTC
Message-ID
<avqypRLPLZZFNqRs@pks.im>
In-Reply-To
<20261112195740.2834341-4-adrian.ratiu@collabora.com>
On Thu, Nov 12, 2026 at 09:57:40PM +0200, Adrian Ratiu wrote:
Show 13 quoted lines
> In a repo with extensions.compatObjectFormat, Git converts commits
> to the compat format in two places which disagree about headers they
> don't know:
> 
> 1. When a commit is created with commit_tree_extended(), as
>    "git commit" does, convert_commit_extra_headers() copies unknown
>    headers unchanged.
> 
> 2. When an existing commit object is converted (git hash-object -w)
>    or when it's read by its compat id, convert_commit_object() fails
>    on them instead.
> 
> So git hash-object refuses such a commit, while git commit --amend
s/git hash-object/git-hash-object(1)/
> records a compat id for it that can't be read back:
s/compat id/compat ID/
Show 5 quoted lines
>    "Failed to convert object from sha256 to sha1".
> 
> The rejection was added in 08a45903cb (object-file-convert: convert
> commits that embed signed tags, 2023-10-01). Git can't know what an
> unknown header holds. It might hold an object id that needs
s/object id/object ID/
Show 16 quoted lines
> converting, so convert_commit_object() rejects all of them.
> 
> Some tools rely on such headers though. Jujutsu, for example, writes
> a "change-id" header in every commit, so none of its commits can be
> converted.
> 
> Make convert_commit_object() copy unknown headers and their
> continuation lines unchanged, just like convert_commit_extra_headers()
> so both give the same compat object.
> 
> The author, committer, encoding and gpgsig headers were already copied
> the same, so their separate cases are no longer needed.
> 
> A "tree", "parent" or "mergetag" line with nothing after the keyword
> used to be rejected as unknown and is now copied as well. Such a
> commit is malformed in both formats anyway.

Hmm. I guess this here might raise some eyebrows. I guess it makes the code simpler, but I wonder what the benefit is of converting an object that is so obviously corrupt...

... yeah, after reading the code we'd have to introduce a bunch of extra logic to make this work as expected. As far as I can see though we _do_ reject "tree " without trailing data, but we wouldn't reject "tree" without the trailing space.

We could fix that by introducing a generic format check:
  - The line must have at least a single space separating key and value
    of the header.
  - The key must only contain characters of a specific set.

Otherwise we might be too lenient and also accept data that doesn't even look close to an object header.

But whether that's all worth it is a different question, as said. It's a corrupt commit anyway, and we cannot address the root cause of that corruption here anyway.

Show 5 quoted lines
> Alternatives considered:
> 
>  - Reject unknown headers in convert_commit_extra_headers() too.
>    Both paths would then agree, but commits from tools that add
>    their own headers could not be used in a compat repo.

Yeah, that's certainly a direction we shouldn't go. There's way too many tools nowadays with widespread usage that write such unknown headers.

>  - Copy only headers known to hold no object ids. Every tool would
>    first need to get its headers added to that list.

Agreed, that doesn't seem sensible, either. We don't want to be the gatekeeper of who is or who isn't allowed to have their own custom header.

A third alternative could have been to try and recognize commit IDs automatically. E.g. if you see a 40-character hex string then we treat it as a commit ID. But I don't think that's sensible, either, as we shouldn't interpret what we don't know.

A bit of a side note, but I have been wondering multiple times already whether we should fix this gap by introducing a well-known object header prefix that tells us that the value of that header is indeed an object ID, for example something like "x-obj-foobar <oid>". That would make our commit graph extensible by third-party-tools without us having to understand what that tool even intends to do with that, and we would know to treat such object IDs as reachable.

But that's of course a topic that is well outside the scope of this patch series.

Show 22 quoted lines
> diff --git a/object-file-convert.c b/object-file-convert.c
> index 63ee18630b..1a0f98c9ff 100644
> --- a/object-file-convert.c
> +++ b/object-file-convert.c
> @@ -223,17 +223,18 @@ static int convert_commit_object(struct repository *repo,
>  			strbuf_release(&tag);
>  			strbuf_release(&new_tag);
>  		}
> -		else if (((bufptr + 7) < tail) && !memcmp(bufptr, "author ", 7))
> -			strbuf_add(out, bufptr, (eol - bufptr) + 1);
> -		else if (((bufptr + 10) < tail) && !memcmp(bufptr, "committer ", 10))
> -			strbuf_add(out, bufptr, (eol - bufptr) + 1);
> -		else if (((bufptr + 9) < tail) && !memcmp(bufptr, "encoding ", 9))
> -			strbuf_add(out, bufptr, (eol - bufptr) + 1);
> -		else if (((bufptr + 6) < tail) && !memcmp(bufptr, "gpgsig", 6))
> -			strbuf_add(out, bufptr, (eol - bufptr) + 1);
>  		else {
> -			/* Unknown line fail it might embed an oid */
> -			return -1;
> +			/*
> +			 * Copy all other headers unchanged. author, committer, encoding and gpgsig
> +			 * hold no object ids. For unknown headers we can't tell, so copy them too,
s/object ids/object IDs/
> +			 * like convert_commit_extra_headers() does when the commit is written. Both
> +			 * must give the same compat object.
> +			 *
> +			 * This means that a header holding object ids is copied without converting
Likewise.
Show 15 quoted lines
> +			 * them. Any such header added to Git needs its own case above, like parent
> +			 * or mergetag, and the same conversion in convert_commit_extra_headers().
> +			 */
> +			strbuf_add(out, bufptr, (eol - bufptr) + 1);
>  		}
>  		/* Consume any trailing continuation lines */
>  		bufptr = eol + 1;
> diff --git a/t/t1016-compatObjectFormat.sh b/t/t1016-compatObjectFormat.sh
> index 9cafcee509..fde092a017 100755
> --- a/t/t1016-compatObjectFormat.sh
> +++ b/t/t1016-compatObjectFormat.sh
> @@ -187,6 +187,22 @@ do
>  		eval signedtag3_${hash}_oid=$(git hash-object -t tag -w ../${hash}_signedtag3) &&
>  		eval signedtag4_${hash}_oid=$(git hash-object -t tag -w ../${hash}_signedtag4)
>  	'
Let's have an empty line between tests.
Show 19 quoted lines
> +	test_expect_success "create a $hash commit with an unknown header" '
> +		git cat-file commit $(commit2_oid $hash) >../${hash}_commit2 &&
> +		awk "{ print } /^committer / { print \"x-unknown one\"; print \" two\" }" \
> +			<../${hash}_commit2 >../${hash}_unknownheader &&
> +		oid=$(git hash-object -t commit -w ../${hash}_unknownheader) &&
> +		eval unknownheader_${hash}_oid=$oid &&
> +		git branch unknownheader $oid
> +	'
> +	test_expect_success "amend a $hash commit with an unknown header" '
> +		git checkout unknownheader &&
> +		git commit --amend -m "Amend a commit with an unknown header" &&
> +		git cat-file commit HEAD >actual &&
> +		test_grep "^x-unknown one$" actual &&
> +		oid=$(git rev-parse HEAD) &&
> +		eval unknownheader2_${hash}_oid=$oid
> +	'
>  
>  	test_expect_success 'rev-parse maps oid of object borrowed from alternate' '
>  		for repo in alt borrow
Thanks!
Patrick
Previous: Adrian RatiuNext: Adrian Ratiu
Message 9 of 10 in “compatObjectFormat: fix a BUG(), wrong id and unknown headers”
  1. 0/3 compatObjectFormat: fix a BUG(), wrong id and unknown headersAdrian Ratiu, Nov 12, 2026
  2. 1/3 setup: reject compatObjectFormat == objectFormatAdrian Ratiu, Nov 12, 2026
  3. Patrick SteinhardtNov 16, 2026
  4. Adrian RatiuNov 17, 2026
  5. 2/3 rev-parse: die when --output-object-format cannot map an objectAdrian Ratiu, Nov 12, 2026
  6. Patrick SteinhardtNov 16, 2026
  7. Adrian RatiuNov 17, 2026
  8. 3/3 object-file-convert: copy unknown commit headers unchangedAdrian Ratiu, Nov 12, 2026
  9. Patrick SteinhardtNov 16, 2026
  10. Adrian RatiuNov 17, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.