builtin/rev-parse: allow shortening to more than 40 hex characters
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>
Patrick Steinhardt committed
May 7, 2024 at 06:53 UTC
07658e9ce5b156876eebf81f39e0672a5347eae5
2 files changed
+8
-3
builtin/rev-parse.c
+2
-3
@@ -687,7 +687,6 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
687
const char *name = NULL;
688
struct object_context unused;
689
struct strbuf buf = STRBUF_INIT;
690
- const int hexsz = the_hash_algo->hexsz;
690
int seen_end_of_options = 0;
691
enum format_type format = FORMAT_DEFAULT;
692
@@ -863,8 +862,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
862
abbrev = strtoul(arg, NULL, 10);
863
if (abbrev < MINIMUM_ABBREV)
864
abbrev = MINIMUM_ABBREV;
866
- else if (hexsz <= abbrev)
867
- abbrev = hexsz;
865
+ else if ((int)the_hash_algo->hexsz <= abbrev)
866
+ abbrev = the_hash_algo->hexsz;
867
continue;
868
}
869
if (!strcmp(arg, "--sq")) {
t/t1500-rev-parse.sh
+6
@@ -304,4 +304,10 @@ test_expect_success 'rev-parse --bisect includes bad, excludes good' '
304
test_cmp expect actual
305
'
306
307
+test_expect_success '--short= truncates to the actual hash length' '
308
+ git rev-parse HEAD >expect &&
309
+ git rev-parse --short=100 HEAD >actual &&
310
+ test_cmp expect actual
311
+'
312
+
313
test_done