Git development
 help / color / mirror / Atom feed
From: Adrian Ratiu <adrian.ratiu@collabora.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Patrick Steinhardt <ps@pks.im>,
	"brian m . carlson" <sandals@crustytoothpaste.net>,
	"Eric W . Biederman" <ebiederm@xmission.com>,
	Axel Rasmussen <axelrasmussen@google.com>,
	Emily Shaffer <emilyshaffer@google.com>,
	Josh Steadmon <steadmon@google.com>,
	Adrian Ratiu <adrian.ratiu@collabora.com>
Subject: [PATCH 3/3] object-file-convert: copy unknown commit headers unchanged
Date: Thu, 12 Nov 2026 21:57:40 +0200	[thread overview]
Message-ID: <20261112195740.2834341-4-adrian.ratiu@collabora.com> (raw)
In-Reply-To: <20261112195740.2834341-1-adrian.ratiu@collabora.com>

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
records a compat id for it that can't be read back:
   "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
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.

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.

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

The test writes a commit with an unknown header with "git hash-object",
amends it with "git commit", and checks that both commits map to the
same objects in the SHA-1 and the SHA-256 repo.

Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
---
 object-file-convert.c         | 21 +++++++++++----------
 t/t1016-compatObjectFormat.sh | 18 ++++++++++++++++++
 2 files changed, 29 insertions(+), 10 deletions(-)

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,
+			 * 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
+			 * 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)
 	'
+	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
@@ -298,6 +314,8 @@ compare_oids 'blob' more "$more_sha1_oid" "$more_sha256_oid"
 compare_oids 'blob' another "$another_sha1_oid" "$another_sha256_oid"
 compare_oids 'tree' tree2 "$tree2_sha1_oid" "$tree2_sha256_oid"
 compare_oids 'commit' commit2 "$commit2_sha1_oid" "$commit2_sha256_oid"
+compare_oids 'commit' unknownheader "$unknownheader_sha1_oid" "$unknownheader_sha256_oid"
+compare_oids 'commit' unknownheader2 "$unknownheader2_sha1_oid" "$unknownheader2_sha256_oid"
 compare_oids GPG2 'tag' signedtag2 "$signedtag2_sha1_oid" "$signedtag2_sha256_oid"
 compare_oids GPG2 'commit' signedcommit2 "$signedcommit2_sha1_oid" "$signedcommit2_sha256_oid"
 compare_oids GPG2 'commit' signedcommit3 "$signedcommit3_sha1_oid" "$signedcommit3_sha256_oid"
-- 
2.56.1


  parent reply	other threads:[~2026-11-12 19:59 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 19:57 [PATCH 0/3] compatObjectFormat: fix a BUG(), wrong id and unknown headers Adrian Ratiu
2026-11-12 19:57 ` [PATCH 1/3] setup: reject compatObjectFormat == objectFormat Adrian Ratiu
2026-11-16  7:36   ` Patrick Steinhardt
2026-11-17 10:42     ` Adrian Ratiu
2026-11-12 19:57 ` [PATCH 2/3] rev-parse: die when --output-object-format cannot map an object Adrian Ratiu
2026-11-16  7:37   ` Patrick Steinhardt
2026-11-17 11:55     ` Adrian Ratiu
2026-11-12 19:57 ` Adrian Ratiu [this message]
2026-11-16  7:37   ` [PATCH 3/3] object-file-convert: copy unknown commit headers unchanged Patrick Steinhardt
2026-11-17 15:27     ` Adrian Ratiu

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=20261112195740.2834341-4-adrian.ratiu@collabora.com \
    --to=adrian.ratiu@collabora.com \
    --cc=axelrasmussen@google.com \
    --cc=ebiederm@xmission.com \
    --cc=emilyshaffer@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=ps@pks.im \
    --cc=sandals@crustytoothpaste.net \
    --cc=steadmon@google.com \
    /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