abbrev: prepare for new world order
authorJunio C Hamano <gitster@pobox.com>
Sat, 1 Oct 2016 00:19:36 +0000 (17:19 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 3 Oct 2016 19:54:22 +0000 (12:54 -0700)
The code that sets custom abbreviation length, in response to
command line argument, often does something like this:

if (skip_prefix(arg, "--abbrev=", &arg))
abbrev = atoi(arg);
else if (!strcmp("--abbrev", &arg))
abbrev = DEFAULT_ABBREV;
/* make the value sane */
if (abbrev < 0 || 40 < abbrev)
abbrev = ... some sane value ...

However, it is pointless to sanity-check and tweak the value
obtained from DEFAULT_ABBREV. We are going to allow it to be
initially set to -1 to signal that the default abbreviation length
must be auto sized upon the first request to abbreviate, based on
the number of objects in the repository, and when that happens,
rejecting or tweaking a negative value to a "saner" one will
negatively interfere with the auto sizing. The codepaths for

git rev-parse --short <object>
git diff --raw --abbrev

do exactly that; allow them to pass possibly negative abbrevs
intact, that will come from DEFAULT_ABBREV in the future.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rev-parse.c
diff.c
index 76cf05e2ade4348b8ad59b1541b58f671e53e1ce..17cbfabdde3845099e0ffd303db5e4a730e5262b 100644 (file)
@@ -643,8 +643,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                filter &= ~(DO_FLAGS|DO_NOREV);
                                verify = 1;
                                abbrev = DEFAULT_ABBREV;
-                               if (arg[7] == '=')
-                                       abbrev = strtoul(arg + 8, NULL, 10);
+                               if (!arg[7])
+                                       continue;
+                               abbrev = strtoul(arg + 8, NULL, 10);
                                if (abbrev < MINIMUM_ABBREV)
                                        abbrev = MINIMUM_ABBREV;
                                else if (40 <= abbrev)
diff --git a/diff.c b/diff.c
index c6da383c563c6cf248843e59ccb38c4ea24fd1f7..cefc13eb8e811a5a5021fcd9573d5fd3c9a37bae 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -3399,7 +3399,7 @@ void diff_setup_done(struct diff_options *options)
                         */
                        read_cache();
        }
-       if (options->abbrev <= 0 || 40 < options->abbrev)
+       if (40 < options->abbrev)
                options->abbrev = 40; /* full */
 
        /*