]> git.ipfire.org Git - thirdparty/git.git/commitdiff
rev-parse: allow printing compatibility hash
authorbrian m. carlson <sandals@crustytoothpaste.net>
Thu, 2 Oct 2025 22:38:52 +0000 (22:38 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 3 Oct 2025 16:58:54 +0000 (09:58 -0700)
Right now, we have a way to print the storage hash, the input hash, and
the output hash, but we lack a way to print the compatibility hash.  Add
a new type to --show-object-format, compat, which prints this value.

If no compatibility hash exists, simply print a newline.  This is
important to allow users to use multiple options at once while still
getting unambiguous output.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-rev-parse.adoc
builtin/rev-parse.c
t/t1500-rev-parse.sh

index cc32b4b4f0f999a15f035cd2375182fe6d776fd5..465ae3e29d071425595d40da77a3e319fe16f8f0 100644 (file)
@@ -324,11 +324,12 @@ The following options are unaffected by `--path-format`:
        path of the current directory relative to the top-level
        directory.
 
---show-object-format[=(storage|input|output)]::
-       Show the object format (hash algorithm) used for the repository
-       for storage inside the `.git` directory, input, or output. For
-       input, multiple algorithms may be printed, space-separated.
-       If not specified, the default is "storage".
+--show-object-format[=(storage|input|output|compat)]::
+       Show the object format (hash algorithm) used for the repository for storage
+       inside the `.git` directory, input, output, or compatibility. For input,
+       multiple algorithms may be printed, space-separated. If `compat` is
+       requested and no compatibility algorithm is enabled, prints an empty line. If
+       not specified, the default is "storage".
 
 --show-ref-format::
        Show the reference storage format used for the repository.
index 44ff1b8342acaea76d7d001075aaa7ea19cf1b53..187b7e8be93f3bbd9f8339e39fea409fbf7f1350 100644 (file)
@@ -1108,11 +1108,20 @@ int cmd_rev_parse(int argc,
                                const char *val = arg ? arg : "storage";
 
                                if (strcmp(val, "storage") &&
+                                   strcmp(val, "compat") &&
                                    strcmp(val, "input") &&
                                    strcmp(val, "output"))
                                        die(_("unknown mode for --show-object-format: %s"),
                                            arg);
-                               puts(the_hash_algo->name);
+
+                               if (!strcmp(val, "compat")) {
+                                       if (the_repository->compat_hash_algo)
+                                               puts(the_repository->compat_hash_algo->name);
+                                       else
+                                               putchar('\n');
+                               } else {
+                                       puts(the_hash_algo->name);
+                               }
                                continue;
                        }
                        if (!strcmp(arg, "--show-ref-format")) {
index 58a4583088b859b4349f7266bd0b8836a369f385..7739ab611bc1c0161731f11addd9b59c9d398408 100755 (executable)
@@ -207,6 +207,40 @@ test_expect_success 'rev-parse --show-object-format in repo' '
        grep "unknown mode for --show-object-format: squeamish-ossifrage" err
 '
 
+
+test_expect_success 'rev-parse --show-object-format in repo with compat mode' '
+       mkdir repo &&
+       (
+               sane_unset GIT_DEFAULT_HASH &&
+               cd repo &&
+               git init --object-format=sha256 &&
+               git config extensions.compatobjectformat sha1 &&
+               echo sha256 >expect &&
+               git rev-parse --show-object-format >actual &&
+               test_cmp expect actual &&
+               git rev-parse --show-object-format=storage >actual &&
+               test_cmp expect actual &&
+               git rev-parse --show-object-format=input >actual &&
+               test_cmp expect actual &&
+               git rev-parse --show-object-format=output >actual &&
+               test_cmp expect actual &&
+               echo sha1 >expect &&
+               git rev-parse --show-object-format=compat >actual &&
+               test_cmp expect actual &&
+               test_must_fail git rev-parse --show-object-format=squeamish-ossifrage 2>err &&
+               grep "unknown mode for --show-object-format: squeamish-ossifrage" err
+       ) &&
+       mkdir repo2 &&
+       (
+               sane_unset GIT_DEFAULT_HASH &&
+               cd repo2 &&
+               git init --object-format=sha256 &&
+               echo >expect &&
+               git rev-parse --show-object-format=compat >actual &&
+               test_cmp expect actual
+       )
+'
+
 test_expect_success 'rev-parse --show-ref-format' '
        test_detect_ref_format >expect &&
        git rev-parse --show-ref-format >actual &&