summaryrefslogtreecommitdiff
path: root/builtin/branch.c
diff options
context:
space:
mode:
authorLibravatar Nguyễn Thái Ngọc Duy <pclouds@gmail.com>2018-05-26 15:55:21 +0200
committerLibravatar Junio C Hamano <gitster@pobox.com>2018-05-29 14:51:28 +0900
commita73b3680c4910866e3e215c3927a0c71f0b9229d (patch)
tree4c48aca0f71bba28888b19f3dec47088334ea7d1 /builtin/branch.c
parentMerge branch 'nd/command-list' into nd/complete-config-vars (diff)
downloadtgif-a73b3680c4910866e3e215c3927a0c71f0b9229d.tar.xz
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>
Diffstat (limited to 'builtin/branch.c')
-rw-r--r--builtin/branch.c28
1 files changed, 10 insertions, 18 deletions
diff --git a/builtin/branch.c b/builtin/branch.c
index efc9ac1922..136d57caa4 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -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)