]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/rev-parse: allow shortening to more than 40 hex characters
authorPatrick Steinhardt <ps@pks.im>
Tue, 7 May 2024 04:53:15 +0000 (06:53 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 7 May 2024 05:50:49 +0000 (22:50 -0700)
The `--short=` option for git-rev-parse(1) allows the user to specify
to how many characters object IDs should be shortened to. The option is
broken though for SHA256 repositories because we set the maximum allowed
hash size to `the_hash_algo->hexsz` before we have even set up the repo.
Consequently, `the_hash_algo` will always be SHA1 and thus we truncate
every hash after at most 40 characters.

Fix this by accessing `the_hash_algo` only after we have set up the
repo.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rev-parse.c
t/t1500-rev-parse.sh

index af795386323a34d1fa8659669749857316cf58f5..7d10ee33c4950700c3bafa0542262e7b85608b63 100644 (file)
@@ -687,7 +687,6 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
        const char *name = NULL;
        struct object_context unused;
        struct strbuf buf = STRBUF_INIT;
-       const int hexsz = the_hash_algo->hexsz;
        int seen_end_of_options = 0;
        enum format_type format = FORMAT_DEFAULT;
 
@@ -863,8 +862,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                abbrev = strtoul(arg, NULL, 10);
                                if (abbrev < MINIMUM_ABBREV)
                                        abbrev = MINIMUM_ABBREV;
-                               else if (hexsz <= abbrev)
-                                       abbrev = hexsz;
+                               else if ((int)the_hash_algo->hexsz <= abbrev)
+                                       abbrev = the_hash_algo->hexsz;
                                continue;
                        }
                        if (!strcmp(arg, "--sq")) {
index a669e592f1d95ca7e0dc575697342326a7a6654f..30c31918fde6539d52800e18dfbb3423b5b73491 100755 (executable)
@@ -304,4 +304,10 @@ test_expect_success 'rev-parse --bisect includes bad, excludes good' '
        test_cmp expect actual
 '
 
+test_expect_success '--short= truncates to the actual hash length' '
+       git rev-parse HEAD >expect &&
+       git rev-parse --short=100 HEAD >actual &&
+       test_cmp expect actual
+'
+
 test_done