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

[PATCH v2 2/3] rev-parse: die when --output-object-format cannot map an object

From
Adrian Ratiu <adrian.ratiu@collabora.com>
Date
Nov 19, 2026, 14:18 UTC
Message-ID
<5b9137274818319747b3eeafaf800ec6874df1d4.1794932695.git.adrian.ratiu@collabora.com>
In-Reply-To
<cover.1794932695.git.adrian.ratiu@collabora.com>

"git rev-parse --output-object-format=<compat-format>" should print the compat ID of an object when extensions.compatObjectFormat is enabled.

Git however doesn't record a compat ID for every object, e.g. for objects received by a fetch that goes through index-pack. For such objects, rev-parse silently prints the wrong ID: it prints the storage ID where the compat ID is expected and exits with 0.

This happens because rev-parse ignores the repo_oid_to_algop() return value. When no mapping is found, that function fails and leaves the ID unchanged, so the storage ID is what gets printed.

Fix it to die with the same message as pack-objects. The other callers of repo_oid_to_algop() already check its result.

Note that this only covers the case where the user asks for a single revision, as it is the only case where rev-parse passes the desired output hash explicitly. All the other cases, like for example when the user passes a range, do not use the desired output hash at all. Fixing that would be a bigger change though, so it is left for a future patch series.

Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
---
 builtin/rev-parse.c  |  8 +++++---
 t/t1500-rev-parse.sh | 14 ++++++++++++++
 2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index d2154eac8d..f4b5a31aff 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -1171,9 +1171,11 @@ int cmd_rev_parse(int argc,
 		}
 		if (!repo_get_oid_with_flags(the_repository, name, &oid,
 					     flags)) {
-			if (output_algo)
-				repo_oid_to_algop(the_repository, &oid,
-						  output_algo, &oid);
+			if (output_algo &&
+			    repo_oid_to_algop(the_repository, &oid,
+					      output_algo, &oid) < 0)
+				die(_("cannot map object %s to %s"),
+				    oid_to_hex(&oid), output_algo->name);
 			if (verify)
 				revs_count++;
 			else
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
index 30bf8a4d0a..a945f421da 100755
--- a/t/t1500-rev-parse.sh
+++ b/t/t1500-rev-parse.sh
@@ -241,6 +241,20 @@ test_expect_success RUST 'rev-parse --show-object-format in repo with compat mod
 	)
 '
 
+test_expect_success RUST 'rev-parse --output-object-format with unmapped object' '
+	test_when_finished "rm -rf unmapped" &&
+	git init --object-format=sha256 unmapped &&
+	# write the object before enabling compat, so it gets no compat mapping
+	oid=$(echo unmapped | git -C unmapped hash-object -w --stdin) &&
+	git -C unmapped config set extensions.compatObjectFormat sha1 &&
+	echo $oid >expect &&
+	git -C unmapped rev-parse --output-object-format=sha256 $oid >actual &&
+	test_cmp expect actual &&
+	test_must_fail git -C unmapped rev-parse --output-object-format=sha1 $oid >actual 2>err &&
+	test_must_be_empty actual &&
+	test_grep "cannot map object $oid to sha1" err
+'
+
 test_expect_success 'rev-parse --show-ref-storage-format' '
 	test_detect_ref_format >expect &&
 	git rev-parse --show-ref-storage-format >actual &&
-- 
2.56.1
Previous: Adrian RatiuNext: Adrian Ratiu
Message 13 of 14 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
  11. 0/3 compatObjectFormat: fix a BUG(), wrong ID and unknown headersAdrian Ratiu, Nov 19, 2026
  12. 1/3 setup: reject compatObjectFormat == objectFormatAdrian Ratiu, Nov 19, 2026
  13. 2/3 rev-parse: die when --output-object-format cannot map an objectAdrian Ratiu, Nov 19, 2026
  14. 3/3 object-file-convert: copy unknown commit headers unchangedAdrian Ratiu, Nov 19, 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.