Skip to content

Commit dce9648

Browse files
torvaldsgitster
authored andcommitted
Make the default abbrev length configurable
The default of 7 comes from fairly early in git development, when seven hex digits was a lot (it covers about 250+ million hash values). Back then I thought that 65k revisions was a lot (it was what we were about to hit in BK), and each revision tends to be about 5-10 new objects or so, so a million objects was a big number. These days, the kernel isn't even the largest git project, and even the kernel has about 220k revisions (_much_ bigger than the BK tree ever was) and we are approaching two million objects. At that point, seven hex digits is still unique for a lot of them, but when we're talking about just two orders of magnitude difference between number of objects and the hash size, there _will_ be collisions in truncated hash values. It's no longer even close to unrealistic - it happens all the time. We should both increase the default abbrev that was unrealistically small, _and_ add a way for people to set their own default per-project in the git config file. This is the first step to first make it configurable; the default of 7 is not raised yet. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7ed863a commit dce9648

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,12 @@ core.sparseCheckout::
567567
Enable "sparse checkout" feature. See section "Sparse checkout" in
568568
linkgit:git-read-tree[1] for more information.
569569

570+
core.abbrevLength::
571+
Set the length object names are abbreviated to. If unspecified,
572+
many commands abbreviate to 7 hexdigits, which may not be enough
573+
for abbreviated object names to stay unique for sufficiently long
574+
time.
575+
570576
add.ignore-errors::
571577
add.ignoreErrors::
572578
Tells 'git add' to continue adding files when some files cannot be

builtin/describe.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static int debug; /* Display lots of verbose info */
2121
static int all; /* Any valid ref can be used */
2222
static int tags; /* Allow lightweight tags */
2323
static int longformat;
24-
static int abbrev = DEFAULT_ABBREV;
24+
static int abbrev = -1; /* unspecified */
2525
static int max_candidates = 10;
2626
static struct hash_table names;
2727
static int have_util;
@@ -420,7 +420,11 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
420420
OPT_END(),
421421
};
422422

423+
git_config(git_default_config, NULL);
423424
argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
425+
if (abbrev < 0)
426+
abbrev = DEFAULT_ABBREV;
427+
424428
if (max_candidates < 0)
425429
max_candidates = 0;
426430
else if (max_candidates > MAX_TAGS)

cache.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ extern int trust_executable_bit;
540540
extern int trust_ctime;
541541
extern int quote_path_fully;
542542
extern int has_symlinks;
543+
extern int minimum_abbrev, default_abbrev;
543544
extern int ignore_case;
544545
extern int assume_unchanged;
545546
extern int prefer_symlink_refs;
@@ -758,8 +759,8 @@ static inline unsigned int hexval(unsigned char c)
758759
}
759760

760761
/* Convert to/from hex/sha1 representation */
761-
#define MINIMUM_ABBREV 4
762-
#define DEFAULT_ABBREV 7
762+
#define MINIMUM_ABBREV minimum_abbrev
763+
#define DEFAULT_ABBREV default_abbrev
763764

764765
struct object_context {
765766
unsigned char tree[20];

config.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,14 @@ static int git_default_core_config(const char *var, const char *value)
531531
return 0;
532532
}
533533

534+
if (!strcmp(var, "core.abbrevlength")) {
535+
int abbrev = git_config_int(var, value);
536+
if (abbrev < minimum_abbrev || abbrev > 40)
537+
return -1;
538+
default_abbrev = abbrev;
539+
return 0;
540+
}
541+
534542
if (!strcmp(var, "core.loosecompression")) {
535543
int level = git_config_int(var, value);
536544
if (level == -1)

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ int user_ident_explicitly_given;
1515
int trust_executable_bit = 1;
1616
int trust_ctime = 1;
1717
int has_symlinks = 1;
18+
int minimum_abbrev = 4, default_abbrev = 7;
1819
int ignore_case;
1920
int assume_unchanged;
2021
int prefer_symlink_refs;

0 commit comments

Comments
 (0)