@@ -15,7 +15,6 @@ typedef int (*disambiguate_hint_fn)(const unsigned char *, void *);
15
15
16
16
struct disambiguate_state {
17
17
int len ; /* length of prefix in hex chars */
18
- unsigned int nrobjects ;
19
18
char hex_pfx [GIT_SHA1_HEXSZ + 1 ];
20
19
unsigned char bin_pfx [GIT_SHA1_RAWSZ ];
21
20
@@ -119,14 +118,6 @@ static void find_short_object_filename(struct disambiguate_state *ds)
119
118
120
119
if (strlen (de -> d_name ) != 38 )
121
120
continue ;
122
-
123
- /*
124
- * We only look at the one subdirectory, and we assume
125
- * each subdirectory is roughly similar, so each
126
- * object we find probably has 255 other objects in
127
- * the other fan-out directories.
128
- */
129
- ds -> nrobjects += 256 ;
130
121
if (memcmp (de -> d_name , ds -> hex_pfx + 2 , ds -> len - 2 ))
131
122
continue ;
132
123
memcpy (hex + 2 , de -> d_name , 38 );
@@ -160,7 +151,6 @@ static void unique_in_pack(struct packed_git *p,
160
151
161
152
open_pack_index (p );
162
153
num = p -> num_objects ;
163
- ds -> nrobjects += num ;
164
154
last = num ;
165
155
while (first < last ) {
166
156
uint32_t mid = (first + last ) / 2 ;
@@ -390,9 +380,6 @@ static int show_ambiguous_object(const unsigned char *sha1, void *data)
390
380
return 0 ;
391
381
}
392
382
393
- /* start from our historical default before the automatic abbreviation */
394
- static int default_automatic_abbrev = FALLBACK_DEFAULT_ABBREV ;
395
-
396
383
static int get_short_sha1 (const char * name , int len , unsigned char * sha1 ,
397
384
unsigned flags )
398
385
{
@@ -439,14 +426,6 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
439
426
for_each_abbrev (ds .hex_pfx , show_ambiguous_object , & ds );
440
427
}
441
428
442
- if (len < 16 && !status && (flags & GET_SHA1_AUTOMATIC )) {
443
- unsigned int expect_collision = 1 << (len * 2 );
444
- if (ds .nrobjects > expect_collision ) {
445
- default_automatic_abbrev = len + 1 ;
446
- return SHORT_NAME_AMBIGUOUS ;
447
- }
448
- }
449
-
450
429
return status ;
451
430
}
452
431
@@ -476,22 +455,53 @@ int for_each_abbrev(const char *prefix, each_abbrev_fn fn, void *cb_data)
476
455
return ret ;
477
456
}
478
457
458
+ /*
459
+ * Return the slot of the most-significant bit set in "val". There are various
460
+ * ways to do this quickly with fls() or __builtin_clzl(), but speed is
461
+ * probably not a big deal here.
462
+ */
463
+ static unsigned msb (unsigned long val )
464
+ {
465
+ unsigned r = 0 ;
466
+ while (val >>= 1 )
467
+ r ++ ;
468
+ return r ;
469
+ }
470
+
479
471
int find_unique_abbrev_r (char * hex , const unsigned char * sha1 , int len )
480
472
{
481
473
int status , exists ;
482
- int flags = GET_SHA1_QUIETLY ;
483
474
484
475
if (len < 0 ) {
485
- flags |= GET_SHA1_AUTOMATIC ;
486
- len = default_automatic_abbrev ;
476
+ unsigned long count = approximate_object_count ();
477
+ /*
478
+ * Add one because the MSB only tells us the highest bit set,
479
+ * not including the value of all the _other_ bits (so "15"
480
+ * is only one off of 2^4, but the MSB is the 3rd bit.
481
+ */
482
+ len = msb (count ) + 1 ;
483
+ /*
484
+ * We now know we have on the order of 2^len objects, which
485
+ * expects a collision at 2^(len/2). But we also care about hex
486
+ * chars, not bits, and there are 4 bits per hex. So all
487
+ * together we need to divide by 2; but we also want to round
488
+ * odd numbers up, hence adding one before dividing.
489
+ */
490
+ len = (len + 1 ) / 2 ;
491
+ /*
492
+ * For very small repos, we stick with our regular fallback.
493
+ */
494
+ if (len < FALLBACK_DEFAULT_ABBREV )
495
+ len = FALLBACK_DEFAULT_ABBREV ;
487
496
}
497
+
488
498
sha1_to_hex_r (hex , sha1 );
489
499
if (len == 40 || !len )
490
500
return 40 ;
491
501
exists = has_sha1_file (sha1 );
492
502
while (len < 40 ) {
493
503
unsigned char sha1_ret [20 ];
494
- status = get_short_sha1 (hex , len , sha1_ret , flags );
504
+ status = get_short_sha1 (hex , len , sha1_ret , GET_SHA1_QUIETLY );
495
505
if (exists
496
506
? !status
497
507
: status == SHORT_NAME_NOT_FOUND ) {
0 commit comments