Skip to content

Commit 46a6c26

Browse files
author
Junio C Hamano
committed
abbrev cleanup: use symbolic constants
The minimum length of abbreviated object name was hardcoded in different places to be 4, risking inconsistencies in the future. Also there were three different "default abbreviation precision". Use two C preprocessor symbols to clean up this mess. Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 6b94f1e commit 46a6c26

File tree

5 files changed

+7
-8
lines changed

5 files changed

+7
-8
lines changed

cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ extern int has_pack_file(const unsigned char *sha1);
221221
extern int has_pack_index(const unsigned char *sha1);
222222

223223
/* Convert to/from hex/sha1 representation */
224+
#define MINIMUM_ABBREV 4
225+
#define DEFAULT_ABBREV 7
226+
224227
extern int get_sha1(const char *str, unsigned char *sha1);
225228
extern int get_sha1_hex(const char *hex, unsigned char *sha1);
226229
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */

describe.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ static const char describe_usage[] =
1111
static int all = 0; /* Default to annotated tags only */
1212
static int tags = 0; /* But allow any tags if --tags is specified */
1313

14-
#define DEFAULT_ABBREV 8 /* maybe too many */
1514
static int abbrev = DEFAULT_ABBREV;
1615

1716
static int names = 0, allocs = 0;
@@ -155,7 +154,7 @@ int main(int argc, char **argv)
155154
tags = 1;
156155
else if (!strncmp(arg, "--abbrev=", 9)) {
157156
abbrev = strtoul(arg + 9, NULL, 10);
158-
if (abbrev < 4 || 40 <= abbrev)
157+
if (abbrev < MINIMUM_ABBREV || 40 <= abbrev)
159158
abbrev = DEFAULT_ABBREV;
160159
}
161160
else

diff.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ static void run_diff(struct diff_filepair *p, struct diff_options *o)
723723

724724
if (memcmp(one->sha1, two->sha1, 20)) {
725725
char one_sha1[41];
726-
int abbrev = o->full_index ? 40 : DIFF_DEFAULT_INDEX_ABBREV;
726+
int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
727727
memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
728728

729729
len += snprintf(msg + len, sizeof(msg) - len,
@@ -846,7 +846,7 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
846846
else if (!strcmp(arg, "--find-copies-harder"))
847847
options->find_copies_harder = 1;
848848
else if (!strcmp(arg, "--abbrev"))
849-
options->abbrev = DIFF_DEFAULT_ABBREV;
849+
options->abbrev = DEFAULT_ABBREV;
850850
else if (!strncmp(arg, "--abbrev=", 9))
851851
options->abbrev = strtoul(arg + 9, NULL, 10);
852852
else

diff.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ extern int diff_setup_done(struct diff_options *);
8888

8989
#define DIFF_PICKAXE_ALL 1
9090

91-
#define DIFF_DEFAULT_INDEX_ABBREV 7 /* hex digits */
92-
#define DIFF_DEFAULT_ABBREV 7 /* hex digits */
93-
9491
extern void diffcore_std(struct diff_options *);
9592

9693
extern void diffcore_std_no_resolve(struct diff_options *);

sha1_name.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ static int get_short_sha1(const char *name, int len, unsigned char *sha1,
155155
char canonical[40];
156156
unsigned char res[20];
157157

158-
if (len < 4)
158+
if (len < MINIMUM_ABBREV)
159159
return -1;
160160
memset(res, 0, 20);
161161
memset(canonical, 'x', 40);

0 commit comments

Comments
 (0)