find_unique_abbrev: move logic out of get_short_sha1()

The get_short_sha1() is only about reading short sha1s; we do call it in a loop to check "is this long enough" for each object, but otherwise it should not need to know about things like our default_abbrev setting. So instead of asking it to set default_automatic_abbrev as a side-effect, let's just have find_unique_abbrev() pick the right place to start its loop. This requires a separate approximate_object_count() function, but that naturally belongs with the rest of sha1_file.c. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 3, 2016 at 19:47 UTC 8e3f52d77854a19cb3fd2adee40be84c8a8bdacc
3 files changed +68 -26
cache.h
+6 -1
@@ -1204,7 +1204,6 @@ struct object_context {
1204 #define GET_SHA1_TREEISH 020
1205 #define GET_SHA1_BLOB 040
1206 #define GET_SHA1_FOLLOW_SYMLINKS 0100
1207 -#define GET_SHA1_AUTOMATIC 0200
1207 #define GET_SHA1_ONLY_TO_DIE 04000
1208
1209 #define GET_SHA1_DISAMBIGUATORS \
@@ -1456,6 +1455,12 @@ extern void prepare_packed_git(void);
1455 extern void reprepare_packed_git(void);
1456 extern void install_packed_git(struct packed_git *pack);
1457
1458 +/*
1459 + * Give a rough count of objects in the repository. This sacrifices accuracy
1460 + * for speed.
1461 + */
1462 +unsigned long approximate_object_count(void);
1463 +
1464 extern struct packed_git *find_sha1_pack(const unsigned char *sha1,
1465 struct packed_git *packs);
1466
sha1_file.c
+27
@@ -1381,6 +1381,32 @@ static void prepare_packed_git_one(char *objdir, int local)
1381 strbuf_release(&path);
1382 }
1383
1384 +static int approximate_object_count_valid;
1385 +
1386 +/*
1387 + * Give a fast, rough count of the number of objects in the repository. This
1388 + * ignores loose objects completely. If you have a lot of them, then either
1389 + * you should repack because your performance will be awful, or they are
1390 + * all unreachable objects about to be pruned, in which case they're not really
1391 + * interesting as a measure of repo size in the first place.
1392 + */
1393 +unsigned long approximate_object_count(void)
1394 +{
1395 + static unsigned long count;
1396 + if (!approximate_object_count_valid) {
1397 + struct packed_git *p;
1398 +
1399 + prepare_packed_git();
1400 + count = 0;
1401 + for (p = packed_git; p; p = p->next) {
1402 + if (open_pack_index(p))
1403 + continue;
1404 + count += p->num_objects;
1405 + }
1406 + }
1407 + return count;
1408 +}
1409 +
1410 static void *get_next_packed_git(const void *p)
1411 {
1412 return ((const struct packed_git *)p)->next;
@@ -1455,6 +1481,7 @@ void prepare_packed_git(void)
1481
1482 void reprepare_packed_git(void)
1483 {
1484 + approximate_object_count_valid = 0;
1485 prepare_packed_git_run_once = 0;
1486 prepare_packed_git();
1487 }
sha1_name.c
+35 -25
@@ -15,7 +15,6 @@ typedef int (*disambiguate_hint_fn)(const unsigned char *, void *);
15
16 struct disambiguate_state {
17 int len; /* length of prefix in hex chars */
18 - unsigned int nrobjects;
18 char hex_pfx[GIT_SHA1_HEXSZ + 1];
19 unsigned char bin_pfx[GIT_SHA1_RAWSZ];
20
@@ -119,14 +118,6 @@ static void find_short_object_filename(struct disambiguate_state *ds)
118
119 if (strlen(de->d_name) != 38)
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;
121 if (memcmp(de->d_name, ds->hex_pfx + 2, ds->len - 2))
122 continue;
123 memcpy(hex + 2, de->d_name, 38);
@@ -160,7 +151,6 @@ static void unique_in_pack(struct packed_git *p,
151
152 open_pack_index(p);
153 num = p->num_objects;
163 - ds->nrobjects += num;
154 last = num;
155 while (first < last) {
156 uint32_t mid = (first + last) / 2;
@@ -390,9 +380,6 @@ static int show_ambiguous_object(const unsigned char *sha1, void *data)
380 return 0;
381 }
382
393 -/* start from our historical default before the automatic abbreviation */
394 -static int default_automatic_abbrev = FALLBACK_DEFAULT_ABBREV;
395 -
383 static int get_short_sha1(const char *name, int len, unsigned char *sha1,
384 unsigned flags)
385 {
@@ -439,14 +426,6 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
426 for_each_abbrev(ds.hex_pfx, show_ambiguous_object, &ds);
427 }
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 -
429 return status;
430 }
431
@@ -476,22 +455,53 @@ int for_each_abbrev(const char *prefix, each_abbrev_fn fn, void *cb_data)
455 return ret;
456 }
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 +
471 int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len)
472 {
473 int status, exists;
482 - int flags = GET_SHA1_QUIETLY;
474
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;
496 }
497 +
498 sha1_to_hex_r(hex, sha1);
499 if (len == 40 || !len)
500 return 40;
501 exists = has_sha1_file(sha1);
502 while (len < 40) {
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);
505 if (exists
506 ? !status
507 : status == SHORT_NAME_NOT_FOUND) {