Skip to content

Commit bb188d0

Browse files
committed
Merge branch 'lt/abbrev-auto' into next
Allow the default abbreviation length, which has historically been 7, to scale as the repository grows. The logic suggests to use 12 hexdigits for the Linux kernel, and 9 to 10 for Git itself. * lt/abbrev-auto: abbrev: auto size the default abbreviation abbrev: prepare for new world order abbrev: add FALLBACK_DEFAULT_ABBREV to prepare for auto sizing
2 parents 6a0b946 + e6c587c commit bb188d0

File tree

7 files changed

+40
-7
lines changed

7 files changed

+40
-7
lines changed

builtin/fetch.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include "argv-array.h"
1818
#include "utf8.h"
1919

20+
#define TRANSPORT_SUMMARY(x) \
21+
(int)(TRANSPORT_SUMMARY_WIDTH + strlen(x) - gettext_width(x)), (x)
22+
2023
static const char * const builtin_fetch_usage[] = {
2124
N_("git fetch [<options>] [<repository> [<refspec>...]]"),
2225
N_("git fetch [<options>] <group>"),

builtin/rev-parse.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
671671
filter &= ~(DO_FLAGS|DO_NOREV);
672672
verify = 1;
673673
abbrev = DEFAULT_ABBREV;
674-
if (arg[7] == '=')
675-
abbrev = strtoul(arg + 8, NULL, 10);
674+
if (!arg[7])
675+
continue;
676+
abbrev = strtoul(arg + 8, NULL, 10);
676677
if (abbrev < MINIMUM_ABBREV)
677678
abbrev = MINIMUM_ABBREV;
678679
else if (40 <= abbrev)

cache.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,9 @@ static inline int hex2chr(const char *s)
11861186
#define MINIMUM_ABBREV minimum_abbrev
11871187
#define DEFAULT_ABBREV default_abbrev
11881188

1189+
/* used when the code does not know or care what the default abbrev is */
1190+
#define FALLBACK_DEFAULT_ABBREV 7
1191+
11891192
struct object_context {
11901193
unsigned char tree[20];
11911194
char path[PATH_MAX];
@@ -1204,6 +1207,7 @@ struct object_context {
12041207
#define GET_SHA1_TREEISH 020
12051208
#define GET_SHA1_BLOB 040
12061209
#define GET_SHA1_FOLLOW_SYMLINKS 0100
1210+
#define GET_SHA1_AUTOMATIC 0200
12071211
#define GET_SHA1_ONLY_TO_DIE 04000
12081212

12091213
#define GET_SHA1_DISAMBIGUATORS \

diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3421,7 +3421,7 @@ void diff_setup_done(struct diff_options *options)
34213421
*/
34223422
read_cache();
34233423
}
3424-
if (options->abbrev <= 0 || 40 < options->abbrev)
3424+
if (40 < options->abbrev)
34253425
options->abbrev = 40; /* full */
34263426

34273427
/*

environment.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ int trust_executable_bit = 1;
1616
int trust_ctime = 1;
1717
int check_stat = 1;
1818
int has_symlinks = 1;
19-
int minimum_abbrev = 4, default_abbrev = 7;
19+
int minimum_abbrev = 4, default_abbrev = -1;
2020
int ignore_case;
2121
int assume_unchanged;
2222
int prefer_symlink_refs;

sha1_name.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ typedef int (*disambiguate_hint_fn)(const unsigned char *, void *);
1515

1616
struct disambiguate_state {
1717
int len; /* length of prefix in hex chars */
18+
unsigned int nrobjects;
1819
char hex_pfx[GIT_SHA1_HEXSZ + 1];
1920
unsigned char bin_pfx[GIT_SHA1_RAWSZ];
2021

@@ -118,6 +119,14 @@ static void find_short_object_filename(struct disambiguate_state *ds)
118119

119120
if (strlen(de->d_name) != 38)
120121
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;
121130
if (memcmp(de->d_name, ds->hex_pfx + 2, ds->len - 2))
122131
continue;
123132
memcpy(hex + 2, de->d_name, 38);
@@ -151,6 +160,7 @@ static void unique_in_pack(struct packed_git *p,
151160

152161
open_pack_index(p);
153162
num = p->num_objects;
163+
ds->nrobjects += num;
154164
last = num;
155165
while (first < last) {
156166
uint32_t mid = (first + last) / 2;
@@ -380,6 +390,9 @@ static int show_ambiguous_object(const unsigned char *sha1, void *data)
380390
return 0;
381391
}
382392

393+
/* start from our historical default before the automatic abbreviation */
394+
static int default_automatic_abbrev = FALLBACK_DEFAULT_ABBREV;
395+
383396
static int get_short_sha1(const char *name, int len, unsigned char *sha1,
384397
unsigned flags)
385398
{
@@ -426,6 +439,14 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
426439
for_each_abbrev(ds.hex_pfx, show_ambiguous_object, &ds);
427440
}
428441

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+
429450
return status;
430451
}
431452

@@ -458,14 +479,19 @@ int for_each_abbrev(const char *prefix, each_abbrev_fn fn, void *cb_data)
458479
int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len)
459480
{
460481
int status, exists;
482+
int flags = GET_SHA1_QUIETLY;
461483

484+
if (len < 0) {
485+
flags |= GET_SHA1_AUTOMATIC;
486+
len = default_automatic_abbrev;
487+
}
462488
sha1_to_hex_r(hex, sha1);
463489
if (len == 40 || !len)
464490
return 40;
465491
exists = has_sha1_file(sha1);
466492
while (len < 40) {
467493
unsigned char sha1_ret[20];
468-
status = get_short_sha1(hex, len, sha1_ret, GET_SHA1_QUIETLY);
494+
status = get_short_sha1(hex, len, sha1_ret, flags);
469495
if (exists
470496
? !status
471497
: status == SHORT_NAME_NOT_FOUND) {

transport.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ struct transport {
147147
#define TRANSPORT_PUSH_ATOMIC 8192
148148
#define TRANSPORT_PUSH_OPTIONS 16384
149149

150-
#define TRANSPORT_SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
151-
#define TRANSPORT_SUMMARY(x) (int)(TRANSPORT_SUMMARY_WIDTH + strlen(x) - gettext_width(x)), (x)
150+
#define TRANSPORT_SUMMARY_WIDTH (2 * FALLBACK_DEFAULT_ABBREV + 3)
152151

153152
/* Returns a transport suitable for the url */
154153
struct transport *transport_get(struct remote *, const char *);

0 commit comments

Comments
 (0)