Skip to content

Commit 99eb1de

Browse files
committed
abbrev: allow extending beyond 20 chars to disambiguate
When you have two or more objects with object names that share more than half the length of the hash algorithm in use (e.g. 10 bytes for SHA-1 that produces 20-byte/160-bit hash), find_unique_abbrev() fails to show disambiguation. To see how many leading letters of a given full object name is sufficiently unambiguous, the algorithm starts from a initial length, guessed based on the estimated number of objects in the repository, and see if another object that shares the prefix, and keeps extending the abbreviation. The loop stops at GIT_MAX_RAWSZ, which is counted as the number of bytes, since 5b20ace (sha1_name: unroll len loop in find_unique_abbrev_r(), 2017-10-08); before that change, it extended up to GIT_SHA1_HEXSZ, which was the correct limit because the loop is adding one output letter per iteration and back then SHA256 was not in the picture. Pass the max length of the hash being in use in the current repository down the code path, and use it to compute the code to update the abbreviation length required to make it unique. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2c2ba49 commit 99eb1de

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

object-name.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,7 @@ static unsigned msb(unsigned long val)
680680
struct min_abbrev_data {
681681
unsigned int init_len;
682682
unsigned int cur_len;
683+
unsigned int max_len;
683684
char *hex;
684685
struct repository *repo;
685686
const struct object_id *oid;
@@ -699,12 +700,12 @@ static inline char get_hex_char_from_oid(const struct object_id *oid,
699700
static int extend_abbrev_len(const struct object_id *oid, void *cb_data)
700701
{
701702
struct min_abbrev_data *mad = cb_data;
702-
703703
unsigned int i = mad->init_len;
704+
704705
while (mad->hex[i] && mad->hex[i] == get_hex_char_from_oid(oid, i))
705706
i++;
706707

707-
if (i < GIT_MAX_RAWSZ && i >= mad->cur_len)
708+
if (mad->cur_len <= i && i < mad->max_len)
708709
mad->cur_len = i + 1;
709710

710711
return 0;
@@ -864,6 +865,7 @@ int repo_find_unique_abbrev_r(struct repository *r, char *hex,
864865
mad.repo = r;
865866
mad.init_len = len;
866867
mad.cur_len = len;
868+
mad.max_len = hexsz;
867869
mad.hex = hex;
868870
mad.oid = oid;
869871

0 commit comments

Comments
 (0)