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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 7, 2014 at 15:33 UTC f6c5a2968c103621adf6928a29e4895361eaa23b
11 files changed +26 -28
builtin/branch.c
+1 -2
@@ -93,8 +93,7 @@ static int git_branch_config(const char *var, const char *value, void *cb)
93 return 0;
94 if (!value)
95 return config_error_nonbool(var);
96 - color_parse(value, var, branch_colors[slot]);
97 - return 0;
96 + return color_parse(value, branch_colors[slot]);
97 }
98 return git_color_default_config(var, value, cb);
99 }
builtin/clean.c
+1 -2
@@ -116,8 +116,7 @@ static int git_clean_config(const char *var, const char *value, void *cb)
116 return 0;
117 if (!value)
118 return config_error_nonbool(var);
119 - color_parse(value, var, clean_colors[slot]);
120 - return 0;
119 + return color_parse(value, clean_colors[slot]);
120 }
121
122 if (!strcmp(var, "clean.requireforce")) {
builtin/commit.c
+1 -2
@@ -1295,8 +1295,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
1295 return 0;
1296 if (!v)
1297 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]);
1299 }
1300 if (!strcmp(k, "status.relativepaths")) {
1301 s->relative_paths = git_config_bool(k, v);
builtin/config.c
+6 -3
@@ -296,7 +296,8 @@ static int git_get_color_config(const char *var, const char *value, void *cb)
296 if (!strcmp(var, get_color_slot)) {
297 if (!value)
298 config_error_nonbool(var);
299 - color_parse(value, var, parsed_color);
299 + if (color_parse(value, parsed_color) < 0)
300 + return -1;
301 get_color_found = 1;
302 }
303 return 0;
@@ -309,8 +310,10 @@ static void get_color(const char *def_color)
310 git_config_with_options(git_get_color_config, NULL,
311 &given_config_source, respect_includes);
312
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 + }
317
318 fputs(parsed_color, stdout);
319 }
builtin/for-each-ref.c
+4 -2
@@ -673,7 +673,8 @@ static void populate_value(struct refinfo *ref)
673 } else if (starts_with(name, "color:")) {
674 char color[COLOR_MAXLEN] = "";
675
676 - color_parse(name + 6, "--format", color);
676 + if (color_parse(name + 6, color) < 0)
677 + die(_("unable to parse format"));
678 v->s = xstrdup(color);
679 continue;
680 } else if (!strcmp(name, "flag")) {
@@ -1007,7 +1008,8 @@ static void show_ref(struct refinfo *info, const char *format, int quote_style)
1008 struct atom_value resetv;
1009 char color[COLOR_MAXLEN] = "";
1010
1010 - color_parse("reset", "--format", color);
1011 + if (color_parse("reset", color) < 0)
1012 + die("BUG: couldn't parse 'reset' as a color");
1013 resetv.s = color;
1014 print_value(&resetv, quote_style);
1015 }
color.c
+6 -7
@@ -60,13 +60,12 @@ static int parse_attr(const char *name, int len)
60 return -1;
61 }
62
63 -void color_parse(const char *value, const char *var, char *dst)
63 +int color_parse(const char *value, char *dst)
64 {
65 - color_parse_mem(value, strlen(value), var, dst);
65 + return color_parse_mem(value, strlen(value), dst);
66 }
67
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)
69 {
70 const char *ptr = value;
71 int len = value_len;
@@ -76,7 +75,7 @@ void color_parse_mem(const char *value, int value_len, const char *var,
75
76 if (!strncasecmp(value, "reset", len)) {
77 strcpy(dst, GIT_COLOR_RESET);
79 - return;
78 + return 0;
79 }
80
81 /* [fg [bg]] [attr]... */
@@ -153,9 +152,9 @@ void color_parse_mem(const char *value, int value_len, const char *var,
152 *dst++ = 'm';
153 }
154 *dst = 0;
156 - return;
155 + return 0;
156 bad:
158 - die("bad color value '%.*s' for variable '%s'", value_len, value, var);
157 + return error(_("invalid color value: %.*s"), value_len, value);
158 }
159
160 int git_config_colorbool(const char *var, const char *value)
color.h
+2 -2
@@ -77,8 +77,8 @@ int git_color_default_config(const char *var, const char *value, void *cb);
77
78 int git_config_colorbool(const char *var, const char *value);
79 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);
82 __attribute__((format (printf, 3, 4)))
83 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
84 __attribute__((format (printf, 3, 4)))
diff.c
+1 -2
@@ -248,8 +248,7 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
248 return 0;
249 if (!value)
250 return config_error_nonbool(var);
251 - color_parse(value, var, diff_colors[slot]);
252 - return 0;
251 + return color_parse(value, diff_colors[slot]);
252 }
253
254 /* like GNU diff's --suppress-blank-empty option */
grep.c
+1 -1
@@ -111,7 +111,7 @@ int grep_config(const char *var, const char *value, void *cb)
111 if (color) {
112 if (!value)
113 return config_error_nonbool(var);
114 - color_parse(value, var, color);
114 + return color_parse(value, color);
115 }
116 return 0;
117 }
log-tree.c
+1 -2
@@ -73,8 +73,7 @@ int parse_decorate_color_config(const char *var, const char *slot_name, const ch
73 return 0;
74 if (!value)
75 return config_error_nonbool(var);
76 - color_parse(value, var, decoration_colors[slot]);
77 - return 0;
76 + return color_parse(value, decoration_colors[slot]);
77 }
78
79 /*
pretty.c
+2 -3
@@ -979,9 +979,8 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
979 return end - placeholder + 1;
980 begin += 5;
981 }
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"));
984 strbuf_addstr(sb, color);
985 return end - placeholder + 1;
986 }