Add and use generic name->id mapping code for color slot parsing

Instead of hard coding the name-to-id mapping in C code, keep it in an
array and use a common function to do the parsing. This reduces code
and also allows us to list all possible color slots later.

This starts using C99 designated initializers more for convenience
(the first designated initializers have been introduced in builtin/clean.c
for some time without complaints)

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Nguyễn Thái Ngọc Duy
2018-05-26 15:55:21 +02:00
committed by Junio C Hamano
parent 17b3e51505
commit a73b3680c4
7 changed files with 82 additions and 112 deletions

View File

@ -55,26 +55,18 @@ enum color_branch {
BRANCH_COLOR_UPSTREAM = 5
};
static const char *color_branch_slots[] = {
[BRANCH_COLOR_RESET] = "reset",
[BRANCH_COLOR_PLAIN] = "plain",
[BRANCH_COLOR_REMOTE] = "remote",
[BRANCH_COLOR_LOCAL] = "local",
[BRANCH_COLOR_CURRENT] = "current",
[BRANCH_COLOR_UPSTREAM] = "upstream",
};
static struct string_list output = STRING_LIST_INIT_DUP;
static unsigned int colopts;
static int parse_branch_color_slot(const char *slot)
{
if (!strcasecmp(slot, "plain"))
return BRANCH_COLOR_PLAIN;
if (!strcasecmp(slot, "reset"))
return BRANCH_COLOR_RESET;
if (!strcasecmp(slot, "remote"))
return BRANCH_COLOR_REMOTE;
if (!strcasecmp(slot, "local"))
return BRANCH_COLOR_LOCAL;
if (!strcasecmp(slot, "current"))
return BRANCH_COLOR_CURRENT;
if (!strcasecmp(slot, "upstream"))
return BRANCH_COLOR_UPSTREAM;
return -1;
}
static int git_branch_config(const char *var, const char *value, void *cb)
{
const char *slot_name;
@ -86,7 +78,7 @@ static int git_branch_config(const char *var, const char *value, void *cb)
return 0;
}
if (skip_prefix(var, "color.branch.", &slot_name)) {
int slot = parse_branch_color_slot(slot_name);
int slot = LOOKUP_CONFIG(color_branch_slots, slot_name);
if (slot < 0)
return 0;
if (!value)