Skip to content

Commit b946576

Browse files
committed
Merge branch 'jn/parse-config-slot'
Code cleanup. * jn/parse-config-slot: color_parse: do not mention variable name in error message pass config slots as pointers instead of offsets
2 parents b67588d + f6c5a29 commit b946576

File tree

13 files changed

+47
-50
lines changed

13 files changed

+47
-50
lines changed

builtin/branch.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,19 @@ static unsigned char merge_filter_ref[20];
6262
static struct string_list output = STRING_LIST_INIT_DUP;
6363
static unsigned int colopts;
6464

65-
static int parse_branch_color_slot(const char *var, int ofs)
65+
static int parse_branch_color_slot(const char *slot)
6666
{
67-
if (!strcasecmp(var+ofs, "plain"))
67+
if (!strcasecmp(slot, "plain"))
6868
return BRANCH_COLOR_PLAIN;
69-
if (!strcasecmp(var+ofs, "reset"))
69+
if (!strcasecmp(slot, "reset"))
7070
return BRANCH_COLOR_RESET;
71-
if (!strcasecmp(var+ofs, "remote"))
71+
if (!strcasecmp(slot, "remote"))
7272
return BRANCH_COLOR_REMOTE;
73-
if (!strcasecmp(var+ofs, "local"))
73+
if (!strcasecmp(slot, "local"))
7474
return BRANCH_COLOR_LOCAL;
75-
if (!strcasecmp(var+ofs, "current"))
75+
if (!strcasecmp(slot, "current"))
7676
return BRANCH_COLOR_CURRENT;
77-
if (!strcasecmp(var+ofs, "upstream"))
77+
if (!strcasecmp(slot, "upstream"))
7878
return BRANCH_COLOR_UPSTREAM;
7979
return -1;
8080
}
@@ -90,13 +90,12 @@ static int git_branch_config(const char *var, const char *value, void *cb)
9090
return 0;
9191
}
9292
if (skip_prefix(var, "color.branch.", &slot_name)) {
93-
int slot = parse_branch_color_slot(var, slot_name - var);
93+
int slot = parse_branch_color_slot(slot_name);
9494
if (slot < 0)
9595
return 0;
9696
if (!value)
9797
return config_error_nonbool(var);
98-
color_parse(value, var, branch_colors[slot]);
99-
return 0;
98+
return color_parse(value, branch_colors[slot]);
10099
}
101100
return git_color_default_config(var, value, cb);
102101
}

builtin/clean.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ static int git_clean_config(const char *var, const char *value, void *cb)
117117
return 0;
118118
if (!value)
119119
return config_error_nonbool(var);
120-
color_parse(value, var, clean_colors[slot]);
121-
return 0;
120+
return color_parse(value, clean_colors[slot]);
122121
}
123122

124123
if (!strcmp(var, "clean.requireforce")) {

builtin/commit.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,22 +1272,21 @@ static int dry_run_commit(int argc, const char **argv, const char *prefix,
12721272
return commitable ? 0 : 1;
12731273
}
12741274

1275-
static int parse_status_slot(const char *var, int offset)
1275+
static int parse_status_slot(const char *slot)
12761276
{
1277-
if (!strcasecmp(var+offset, "header"))
1277+
if (!strcasecmp(slot, "header"))
12781278
return WT_STATUS_HEADER;
1279-
if (!strcasecmp(var+offset, "branch"))
1279+
if (!strcasecmp(slot, "branch"))
12801280
return WT_STATUS_ONBRANCH;
1281-
if (!strcasecmp(var+offset, "updated")
1282-
|| !strcasecmp(var+offset, "added"))
1281+
if (!strcasecmp(slot, "updated") || !strcasecmp(slot, "added"))
12831282
return WT_STATUS_UPDATED;
1284-
if (!strcasecmp(var+offset, "changed"))
1283+
if (!strcasecmp(slot, "changed"))
12851284
return WT_STATUS_CHANGED;
1286-
if (!strcasecmp(var+offset, "untracked"))
1285+
if (!strcasecmp(slot, "untracked"))
12871286
return WT_STATUS_UNTRACKED;
1288-
if (!strcasecmp(var+offset, "nobranch"))
1287+
if (!strcasecmp(slot, "nobranch"))
12891288
return WT_STATUS_NOBRANCH;
1290-
if (!strcasecmp(var+offset, "unmerged"))
1289+
if (!strcasecmp(slot, "unmerged"))
12911290
return WT_STATUS_UNMERGED;
12921291
return -1;
12931292
}
@@ -1327,13 +1326,12 @@ static int git_status_config(const char *k, const char *v, void *cb)
13271326
}
13281327
if (skip_prefix(k, "status.color.", &slot_name) ||
13291328
skip_prefix(k, "color.status.", &slot_name)) {
1330-
int slot = parse_status_slot(k, slot_name - k);
1329+
int slot = parse_status_slot(slot_name);
13311330
if (slot < 0)
13321331
return 0;
13331332
if (!v)
13341333
return config_error_nonbool(k);
1335-
color_parse(v, k, s->color_palette[slot]);
1336-
return 0;
1334+
return color_parse(v, s->color_palette[slot]);
13371335
}
13381336
if (!strcmp(k, "status.relativepaths")) {
13391337
s->relative_paths = git_config_bool(k, v);

builtin/config.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ static int git_get_color_config(const char *var, const char *value, void *cb)
296296
if (!strcmp(var, get_color_slot)) {
297297
if (!value)
298298
config_error_nonbool(var);
299-
color_parse(value, var, parsed_color);
299+
if (color_parse(value, parsed_color) < 0)
300+
return -1;
300301
get_color_found = 1;
301302
}
302303
return 0;
@@ -309,8 +310,10 @@ static void get_color(const char *def_color)
309310
git_config_with_options(git_get_color_config, NULL,
310311
&given_config_source, respect_includes);
311312

312-
if (!get_color_found && def_color)
313-
color_parse(def_color, "command line", parsed_color);
313+
if (!get_color_found && def_color) {
314+
if (color_parse(def_color, parsed_color) < 0)
315+
die(_("unable to parse default color value"));
316+
}
314317

315318
fputs(parsed_color, stdout);
316319
}

builtin/for-each-ref.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,8 @@ static void populate_value(struct refinfo *ref)
671671
} else if (starts_with(name, "color:")) {
672672
char color[COLOR_MAXLEN] = "";
673673

674-
color_parse(name + 6, "--format", color);
674+
if (color_parse(name + 6, color) < 0)
675+
die(_("unable to parse format"));
675676
v->s = xstrdup(color);
676677
continue;
677678
} else if (!strcmp(name, "flag")) {
@@ -1004,7 +1005,8 @@ static void show_ref(struct refinfo *info, const char *format, int quote_style)
10041005
struct atom_value resetv;
10051006
char color[COLOR_MAXLEN] = "";
10061007

1007-
color_parse("reset", "--format", color);
1008+
if (color_parse("reset", color) < 0)
1009+
die("BUG: couldn't parse 'reset' as a color");
10081010
resetv.s = color;
10091011
print_value(&resetv, quote_style);
10101012
}

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ static int git_log_config(const char *var, const char *value, void *cb)
391391
return 0;
392392
}
393393
if (skip_prefix(var, "color.decorate.", &slot_name))
394-
return parse_decorate_color_config(var, slot_name - var, value);
394+
return parse_decorate_color_config(var, slot_name, value);
395395
if (!strcmp(var, "log.mailmap")) {
396396
use_mailmap_config = git_config_bool(var, value);
397397
return 0;

color.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ static int parse_attr(const char *name, int len)
6060
return -1;
6161
}
6262

63-
void color_parse(const char *value, const char *var, char *dst)
63+
int color_parse(const char *value, char *dst)
6464
{
65-
color_parse_mem(value, strlen(value), var, dst);
65+
return color_parse_mem(value, strlen(value), dst);
6666
}
6767

68-
void color_parse_mem(const char *value, int value_len, const char *var,
69-
char *dst)
68+
int color_parse_mem(const char *value, int value_len, char *dst)
7069
{
7170
const char *ptr = value;
7271
int len = value_len;
@@ -76,7 +75,7 @@ void color_parse_mem(const char *value, int value_len, const char *var,
7675

7776
if (!strncasecmp(value, "reset", len)) {
7877
strcpy(dst, GIT_COLOR_RESET);
79-
return;
78+
return 0;
8079
}
8180

8281
/* [fg [bg]] [attr]... */
@@ -153,9 +152,9 @@ void color_parse_mem(const char *value, int value_len, const char *var,
153152
*dst++ = 'm';
154153
}
155154
*dst = 0;
156-
return;
155+
return 0;
157156
bad:
158-
die("bad color value '%.*s' for variable '%s'", value_len, value, var);
157+
return error(_("invalid color value: %.*s"), value_len, value);
159158
}
160159

161160
int git_config_colorbool(const char *var, const char *value)

color.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ int git_color_default_config(const char *var, const char *value, void *cb);
7777

7878
int git_config_colorbool(const char *var, const char *value);
7979
int want_color(int var);
80-
void color_parse(const char *value, const char *var, char *dst);
81-
void color_parse_mem(const char *value, int len, const char *var, char *dst);
80+
int color_parse(const char *value, char *dst);
81+
int color_parse_mem(const char *value, int len, char *dst);
8282
__attribute__((format (printf, 3, 4)))
8383
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
8484
__attribute__((format (printf, 3, 4)))

diff.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
248248
return 0;
249249
if (!value)
250250
return config_error_nonbool(var);
251-
color_parse(value, var, diff_colors[slot]);
252-
return 0;
251+
return color_parse(value, diff_colors[slot]);
253252
}
254253

255254
/* like GNU diff's --suppress-blank-empty option */

grep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ int grep_config(const char *var, const char *value, void *cb)
111111
if (color) {
112112
if (!value)
113113
return config_error_nonbool(var);
114-
color_parse(value, var, color);
114+
return color_parse(value, color);
115115
}
116116
return 0;
117117
}

0 commit comments

Comments
 (0)