Skip to content

Commit a73b368

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 17b3e51 commit a73b368

File tree

7 files changed

+82
-112
lines changed

7 files changed

+82
-112
lines changed

builtin/branch.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,18 @@ enum color_branch {
5555
BRANCH_COLOR_UPSTREAM = 5
5656
};
5757

58+
static const char *color_branch_slots[] = {
59+
[BRANCH_COLOR_RESET] = "reset",
60+
[BRANCH_COLOR_PLAIN] = "plain",
61+
[BRANCH_COLOR_REMOTE] = "remote",
62+
[BRANCH_COLOR_LOCAL] = "local",
63+
[BRANCH_COLOR_CURRENT] = "current",
64+
[BRANCH_COLOR_UPSTREAM] = "upstream",
65+
};
66+
5867
static struct string_list output = STRING_LIST_INIT_DUP;
5968
static unsigned int colopts;
6069

61-
static int parse_branch_color_slot(const char *slot)
62-
{
63-
if (!strcasecmp(slot, "plain"))
64-
return BRANCH_COLOR_PLAIN;
65-
if (!strcasecmp(slot, "reset"))
66-
return BRANCH_COLOR_RESET;
67-
if (!strcasecmp(slot, "remote"))
68-
return BRANCH_COLOR_REMOTE;
69-
if (!strcasecmp(slot, "local"))
70-
return BRANCH_COLOR_LOCAL;
71-
if (!strcasecmp(slot, "current"))
72-
return BRANCH_COLOR_CURRENT;
73-
if (!strcasecmp(slot, "upstream"))
74-
return BRANCH_COLOR_UPSTREAM;
75-
return -1;
76-
}
77-
7870
static int git_branch_config(const char *var, const char *value, void *cb)
7971
{
8072
const char *slot_name;
@@ -86,7 +78,7 @@ static int git_branch_config(const char *var, const char *value, void *cb)
8678
return 0;
8779
}
8880
if (skip_prefix(var, "color.branch.", &slot_name)) {
89-
int slot = parse_branch_color_slot(slot_name);
81+
int slot = LOOKUP_CONFIG(color_branch_slots, slot_name);
9082
if (slot < 0)
9183
return 0;
9284
if (!value)

builtin/clean.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ enum color_clean {
4242
CLEAN_COLOR_ERROR = 5
4343
};
4444

45+
static const char *color_interactive_slots[] = {
46+
[CLEAN_COLOR_ERROR] = "error",
47+
[CLEAN_COLOR_HEADER] = "header",
48+
[CLEAN_COLOR_HELP] = "help",
49+
[CLEAN_COLOR_PLAIN] = "plain",
50+
[CLEAN_COLOR_PROMPT] = "prompt",
51+
[CLEAN_COLOR_RESET] = "reset",
52+
};
53+
4554
static int clean_use_color = -1;
4655
static char clean_colors[][COLOR_MAXLEN] = {
4756
[CLEAN_COLOR_ERROR] = GIT_COLOR_BOLD_RED,
@@ -82,23 +91,6 @@ struct menu_stuff {
8291
void *stuff;
8392
};
8493

85-
static int parse_clean_color_slot(const char *var)
86-
{
87-
if (!strcasecmp(var, "reset"))
88-
return CLEAN_COLOR_RESET;
89-
if (!strcasecmp(var, "plain"))
90-
return CLEAN_COLOR_PLAIN;
91-
if (!strcasecmp(var, "prompt"))
92-
return CLEAN_COLOR_PROMPT;
93-
if (!strcasecmp(var, "header"))
94-
return CLEAN_COLOR_HEADER;
95-
if (!strcasecmp(var, "help"))
96-
return CLEAN_COLOR_HELP;
97-
if (!strcasecmp(var, "error"))
98-
return CLEAN_COLOR_ERROR;
99-
return -1;
100-
}
101-
10294
static int git_clean_config(const char *var, const char *value, void *cb)
10395
{
10496
const char *slot_name;
@@ -113,7 +105,7 @@ static int git_clean_config(const char *var, const char *value, void *cb)
113105
return 0;
114106
}
115107
if (skip_prefix(var, "color.interactive.", &slot_name)) {
116-
int slot = parse_clean_color_slot(slot_name);
108+
int slot = LOOKUP_CONFIG(color_interactive_slots, slot_name);
117109
if (slot < 0)
118110
return 0;
119111
if (!value)

builtin/commit.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ N_("If you wish to skip this commit, use:\n"
6666
"Then \"git cherry-pick --continue\" will resume cherry-picking\n"
6767
"the remaining commits.\n");
6868

69+
static const char *color_status_slots[] = {
70+
[WT_STATUS_HEADER] = "header",
71+
[WT_STATUS_UPDATED] = "updated",
72+
[WT_STATUS_CHANGED] = "changed",
73+
[WT_STATUS_UNTRACKED] = "untracked",
74+
[WT_STATUS_NOBRANCH] = "noBranch",
75+
[WT_STATUS_UNMERGED] = "unmerged",
76+
[WT_STATUS_LOCAL_BRANCH] = "localBranch",
77+
[WT_STATUS_REMOTE_BRANCH] = "remoteBranch",
78+
[WT_STATUS_ONBRANCH] = "branch",
79+
};
80+
6981
static const char *use_message_buffer;
7082
static struct lock_file index_lock; /* real index */
7183
static struct lock_file false_lock; /* used only for partial commits */
@@ -1175,25 +1187,10 @@ static int dry_run_commit(int argc, const char **argv, const char *prefix,
11751187

11761188
static int parse_status_slot(const char *slot)
11771189
{
1178-
if (!strcasecmp(slot, "header"))
1179-
return WT_STATUS_HEADER;
1180-
if (!strcasecmp(slot, "branch"))
1181-
return WT_STATUS_ONBRANCH;
1182-
if (!strcasecmp(slot, "updated") || !strcasecmp(slot, "added"))
1190+
if (!strcasecmp(slot, "added"))
11831191
return WT_STATUS_UPDATED;
1184-
if (!strcasecmp(slot, "changed"))
1185-
return WT_STATUS_CHANGED;
1186-
if (!strcasecmp(slot, "untracked"))
1187-
return WT_STATUS_UNTRACKED;
1188-
if (!strcasecmp(slot, "nobranch"))
1189-
return WT_STATUS_NOBRANCH;
1190-
if (!strcasecmp(slot, "unmerged"))
1191-
return WT_STATUS_UNMERGED;
1192-
if (!strcasecmp(slot, "localBranch"))
1193-
return WT_STATUS_LOCAL_BRANCH;
1194-
if (!strcasecmp(slot, "remoteBranch"))
1195-
return WT_STATUS_REMOTE_BRANCH;
1196-
return -1;
1192+
1193+
return LOOKUP_CONFIG(color_status_slots, slot);
11971194
}
11981195

11991196
static int git_status_config(const char *k, const char *v, void *cb)

config.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,3 +3245,16 @@ enum config_scope current_config_scope(void)
32453245
else
32463246
return current_parsing_scope;
32473247
}
3248+
3249+
int lookup_config(const char **mapping, int nr_mapping, const char *var)
3250+
{
3251+
int i;
3252+
3253+
for (i = 0; i < nr_mapping; i++) {
3254+
const char *name = mapping[i];
3255+
3256+
if (name && !strcasecmp(var, name))
3257+
return i;
3258+
}
3259+
return -1;
3260+
}

config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,8 @@ struct key_value_info {
257257
extern NORETURN void git_die_config(const char *key, const char *err, ...) __attribute__((format(printf, 2, 3)));
258258
extern NORETURN void git_die_config_linenr(const char *key, const char *filename, int linenr);
259259

260+
#define LOOKUP_CONFIG(mapping, var) \
261+
lookup_config(mapping, ARRAY_SIZE(mapping), var)
262+
int lookup_config(const char **mapping, int nr_mapping, const char *var);
263+
260264
#endif /* CONFIG_H */

diff.c

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,46 +69,35 @@ static char diff_colors[][COLOR_MAXLEN] = {
6969
GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */
7070
};
7171

72+
static const char *color_diff_slots[] = {
73+
[DIFF_CONTEXT] = "context",
74+
[DIFF_METAINFO] = "meta",
75+
[DIFF_FRAGINFO] = "frag",
76+
[DIFF_FILE_OLD] = "old",
77+
[DIFF_FILE_NEW] = "new",
78+
[DIFF_COMMIT] = "commit",
79+
[DIFF_WHITESPACE] = "whitespace",
80+
[DIFF_FUNCINFO] = "func",
81+
[DIFF_FILE_OLD_MOVED] = "oldMoved",
82+
[DIFF_FILE_OLD_MOVED_ALT] = "oldMovedAlternative",
83+
[DIFF_FILE_OLD_MOVED_DIM] = "oldMovedDimmed",
84+
[DIFF_FILE_OLD_MOVED_ALT_DIM] = "oldMovedAlternativeDimmed",
85+
[DIFF_FILE_NEW_MOVED] = "newMoved",
86+
[DIFF_FILE_NEW_MOVED_ALT] = "newMovedAlternative",
87+
[DIFF_FILE_NEW_MOVED_DIM] = "newMovedDimmed",
88+
[DIFF_FILE_NEW_MOVED_ALT_DIM] = "newMovedAlternativeDimmed",
89+
};
90+
7291
static NORETURN void die_want_option(const char *option_name)
7392
{
7493
die(_("option '%s' requires a value"), option_name);
7594
}
7695

7796
static int parse_diff_color_slot(const char *var)
7897
{
79-
if (!strcasecmp(var, "context") || !strcasecmp(var, "plain"))
98+
if (!strcasecmp(var, "plain"))
8099
return DIFF_CONTEXT;
81-
if (!strcasecmp(var, "meta"))
82-
return DIFF_METAINFO;
83-
if (!strcasecmp(var, "frag"))
84-
return DIFF_FRAGINFO;
85-
if (!strcasecmp(var, "old"))
86-
return DIFF_FILE_OLD;
87-
if (!strcasecmp(var, "new"))
88-
return DIFF_FILE_NEW;
89-
if (!strcasecmp(var, "commit"))
90-
return DIFF_COMMIT;
91-
if (!strcasecmp(var, "whitespace"))
92-
return DIFF_WHITESPACE;
93-
if (!strcasecmp(var, "func"))
94-
return DIFF_FUNCINFO;
95-
if (!strcasecmp(var, "oldmoved"))
96-
return DIFF_FILE_OLD_MOVED;
97-
if (!strcasecmp(var, "oldmovedalternative"))
98-
return DIFF_FILE_OLD_MOVED_ALT;
99-
if (!strcasecmp(var, "oldmoveddimmed"))
100-
return DIFF_FILE_OLD_MOVED_DIM;
101-
if (!strcasecmp(var, "oldmovedalternativedimmed"))
102-
return DIFF_FILE_OLD_MOVED_ALT_DIM;
103-
if (!strcasecmp(var, "newmoved"))
104-
return DIFF_FILE_NEW_MOVED;
105-
if (!strcasecmp(var, "newmovedalternative"))
106-
return DIFF_FILE_NEW_MOVED_ALT;
107-
if (!strcasecmp(var, "newmoveddimmed"))
108-
return DIFF_FILE_NEW_MOVED_DIM;
109-
if (!strcasecmp(var, "newmovedalternativedimmed"))
110-
return DIFF_FILE_NEW_MOVED_ALT_DIM;
111-
return -1;
100+
return LOOKUP_CONFIG(color_diff_slots, var);
112101
}
113102

114103
static int parse_dirstat_params(struct diff_options *options, const char *params_string,

log-tree.c

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,24 @@ static char decoration_colors[][COLOR_MAXLEN] = {
2727
GIT_COLOR_BOLD_BLUE, /* GRAFTED */
2828
};
2929

30+
static const char *color_decorate_slots[] = {
31+
[DECORATION_REF_LOCAL] = "branch",
32+
[DECORATION_REF_REMOTE] = "remoteBranch",
33+
[DECORATION_REF_TAG] = "tag",
34+
[DECORATION_REF_STASH] = "stash",
35+
[DECORATION_REF_HEAD] = "HEAD",
36+
};
37+
3038
static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
3139
{
3240
if (want_color(decorate_use_color))
3341
return decoration_colors[ix];
3442
return "";
3543
}
3644

37-
static int parse_decorate_color_slot(const char *slot)
38-
{
39-
/*
40-
* We're comparing with 'ignore-case' on
41-
* (because config.c sets them all tolower),
42-
* but let's match the letters in the literal
43-
* string values here with how they are
44-
* documented in Documentation/config.txt, for
45-
* consistency.
46-
*
47-
* We love being consistent, don't we?
48-
*/
49-
if (!strcasecmp(slot, "branch"))
50-
return DECORATION_REF_LOCAL;
51-
if (!strcasecmp(slot, "remoteBranch"))
52-
return DECORATION_REF_REMOTE;
53-
if (!strcasecmp(slot, "tag"))
54-
return DECORATION_REF_TAG;
55-
if (!strcasecmp(slot, "stash"))
56-
return DECORATION_REF_STASH;
57-
if (!strcasecmp(slot, "HEAD"))
58-
return DECORATION_REF_HEAD;
59-
return -1;
60-
}
61-
6245
int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
6346
{
64-
int slot = parse_decorate_color_slot(slot_name);
47+
int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
6548
if (slot < 0)
6649
return 0;
6750
if (!value)

0 commit comments

Comments
 (0)