From: Adrian Ratiu Date: Thu, 12 Nov 2026 19:57:39 GMT Subject: [PATCH 2/3] rev-parse: die when --output-object-format cannot map an object Message-ID: <20261112195740.2834341-3-adrian.ratiu@collabora.com> In-Reply-To: <20261112195740.2834341-1-adrian.ratiu@collabora.com> "git rev-parse --output-object-format=" should print an object compat id 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. This only covers a single revision argument, the one case where rev-parse converts its output. Other forms, such as "A..B" or "A^@", print storage ids whatever --output-object-format says. That is left for a separate change. The test copies a loose object into the repository by hand, so it doesn't depend on whether fetch writes compat mappings. Signed-off-by: Adrian Ratiu --- builtin/rev-parse.c | 8 +++++--- t/t1500-rev-parse.sh | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index d2154eac8d..ab2898ba52 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)) + 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..c15d9953a8 100755 --- a/t/t1500-rev-parse.sh +++ b/t/t1500-rev-parse.sh @@ -241,6 +241,24 @@ 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 plain compat" && + git init --object-format=sha256 plain && + git init --object-format=sha256 compat && + git -C compat config extensions.compatobjectformat sha1 && + oid=$(echo unmapped | git -C plain hash-object -w --stdin) && + # copy the object by hand, so that no compat mapping is written for it + path=$(test_oid_to_path $oid) && + mkdir compat/.git/objects/${path%/*} && + cp plain/.git/objects/$path compat/.git/objects/$path && + echo $oid >expect && + git -C compat rev-parse --output-object-format=sha256 $oid >actual && + test_cmp expect actual && + test_must_fail git -C compat 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