Skip to content

Commit 07658e9

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bd455ce commit 07658e9

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

builtin/rev-parse.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,6 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
687687
const char *name = NULL;
688688
struct object_context unused;
689689
struct strbuf buf = STRBUF_INIT;
690-
const int hexsz = the_hash_algo->hexsz;
691690
int seen_end_of_options = 0;
692691
enum format_type format = FORMAT_DEFAULT;
693692

@@ -863,8 +862,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
863862
abbrev = strtoul(arg, NULL, 10);
864863
if (abbrev < MINIMUM_ABBREV)
865864
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;
868867
continue;
869868
}
870869
if (!strcmp(arg, "--sq")) {

t/t1500-rev-parse.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,10 @@ test_expect_success 'rev-parse --bisect includes bad, excludes good' '
304304
test_cmp expect actual
305305
'
306306

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+
307313
test_done

0 commit comments

Comments
 (0)