Skip to content

Commit f6c5a29

Browse files
peffgitster
authored andcommitted
color_parse: do not mention variable name in error message
Originally the color-parsing function was used only for config variables. It made sense to pass the variable name so that the die() message could be something like: $ git -c color.branch.plain=bogus branch fatal: bad color value 'bogus' for variable 'color.branch.plain' These days we call it in other contexts, and the resulting error messages are a little confusing: $ git log --pretty='%C(bogus)' fatal: bad color value 'bogus' for variable '--pretty format' $ git config --get-color foo.bar bogus fatal: bad color value 'bogus' for variable 'command line' This patch teaches color_parse to complain only about the value, and then return an error code. Config callers can then propagate that up to the config parser, which mentions the variable name. Other callers can provide a custom message. After this patch these three cases now look like: $ git -c color.branch.plain=bogus branch error: invalid color value: bogus fatal: unable to parse 'color.branch.plain' from command-line config $ git log --pretty='%C(bogus)' error: invalid color value: bogus fatal: unable to parse --pretty format $ git config --get-color foo.bar bogus error: invalid color value: bogus fatal: unable to parse default color value Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8852117 commit f6c5a29

File tree

11 files changed

+26
-28
lines changed

11 files changed

+26
-28
lines changed

builtin/branch.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ static int git_branch_config(const char *var, const char *value, void *cb)
9393
return 0;
9494
if (!value)
9595
return config_error_nonbool(var);
96-
color_parse(value, var, branch_colors[slot]);
97-
return 0;
96+
return color_parse(value, branch_colors[slot]);
9897
}
9998
return git_color_default_config(var, value, cb);
10099
}

builtin/clean.c

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

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

builtin/commit.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,8 +1295,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
12951295
return 0;
12961296
if (!v)
12971297
return config_error_nonbool(k);
1298-
color_parse(v, k, s->color_palette[slot]);
1299-
return 0;
1298+
return color_parse(v, s->color_palette[slot]);
13001299
}
13011300
if (!strcmp(k, "status.relativepaths")) {
13021301
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
@@ -673,7 +673,8 @@ static void populate_value(struct refinfo *ref)
673673
} else if (starts_with(name, "color:")) {
674674
char color[COLOR_MAXLEN] = "";
675675

676-
color_parse(name + 6, "--format", color);
676+
if (color_parse(name + 6, color) < 0)
677+
die(_("unable to parse format"));
677678
v->s = xstrdup(color);
678679
continue;
679680
} else if (!strcmp(name, "flag")) {
@@ -1007,7 +1008,8 @@ static void show_ref(struct refinfo *info, const char *format, int quote_style)
10071008
struct atom_value resetv;
10081009
char color[COLOR_MAXLEN] = "";
10091010

1010-
color_parse("reset", "--format", color);
1011+
if (color_parse("reset", color) < 0)
1012+
die("BUG: couldn't parse 'reset' as a color");
10111013
resetv.s = color;
10121014
print_value(&resetv, quote_style);
10131015
}

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
}

log-tree.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ int parse_decorate_color_config(const char *var, const char *slot_name, const ch
7373
return 0;
7474
if (!value)
7575
return config_error_nonbool(var);
76-
color_parse(value, var, decoration_colors[slot]);
77-
return 0;
76+
return color_parse(value, decoration_colors[slot]);
7877
}
7978

8079
/*

pretty.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -979,9 +979,8 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
979979
return end - placeholder + 1;
980980
begin += 5;
981981
}
982-
color_parse_mem(begin,
983-
end - begin,
984-
"--pretty format", color);
982+
if (color_parse_mem(begin, end - begin, color) < 0)
983+
die(_("unable to parse --pretty format"));
985984
strbuf_addstr(sb, color);
986985
return end - placeholder + 1;
987986
}

0 commit comments

Comments
 (0)