color_parse: do not mention variable name in error message
authorJeff King <peff@peff.net>
Tue, 7 Oct 2014 19:33:09 +0000 (15:33 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 14 Oct 2014 18:01:21 +0000 (11:01 -0700)
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>
builtin/branch.c
builtin/clean.c
builtin/commit.c
builtin/config.c
builtin/for-each-ref.c
color.c
color.h
diff.c
grep.c
log-tree.c
pretty.c
index b2e1895ca92ec2037349d88b945ba64ebf16d62d..0a8ed9d22a410d51c44c3f93ade8bf746f071331 100644 (file)
@@ -93,8 +93,7 @@ static int git_branch_config(const char *var, const char *value, void *cb)
                        return 0;
                if (!value)
                        return config_error_nonbool(var);
-               color_parse(value, var, branch_colors[slot]);
-               return 0;
+               return color_parse(value, branch_colors[slot]);
        }
        return git_color_default_config(var, value, cb);
 }
index 1032563e5fae880df9256c9eafaa96d60462ecd2..035ea391a2ec0c9399fa9868888d32e84c93b227 100644 (file)
@@ -116,8 +116,7 @@ static int git_clean_config(const char *var, const char *value, void *cb)
                        return 0;
                if (!value)
                        return config_error_nonbool(var);
-               color_parse(value, var, clean_colors[slot]);
-               return 0;
+               return color_parse(value, clean_colors[slot]);
        }
 
        if (!strcmp(var, "clean.requireforce")) {
index 5a8a29e07534237aee822cdee26f7c7f6a612c02..8dab44d93362553f4a9a55b66ba1d4a947795afd 100644 (file)
@@ -1295,8 +1295,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
                        return 0;
                if (!v)
                        return config_error_nonbool(k);
-               color_parse(v, k, s->color_palette[slot]);
-               return 0;
+               return color_parse(v, s->color_palette[slot]);
        }
        if (!strcmp(k, "status.relativepaths")) {
                s->relative_paths = git_config_bool(k, v);
index 7bba5163834c87272a85ba324821beb1c9bca2c1..842809b0b3b0907a883d651a72891a071367ab9e 100644 (file)
@@ -296,7 +296,8 @@ static int git_get_color_config(const char *var, const char *value, void *cb)
        if (!strcmp(var, get_color_slot)) {
                if (!value)
                        config_error_nonbool(var);
-               color_parse(value, var, parsed_color);
+               if (color_parse(value, parsed_color) < 0)
+                       return -1;
                get_color_found = 1;
        }
        return 0;
@@ -309,8 +310,10 @@ static void get_color(const char *def_color)
        git_config_with_options(git_get_color_config, NULL,
                                &given_config_source, respect_includes);
 
-       if (!get_color_found && def_color)
-               color_parse(def_color, "command line", parsed_color);
+       if (!get_color_found && def_color) {
+               if (color_parse(def_color, parsed_color) < 0)
+                       die(_("unable to parse default color value"));
+       }
 
        fputs(parsed_color, stdout);
 }
index 47bd624696d5e94295dda8846f00dcbb80a6ca5a..d41920d382eae1e3e6ee92467a9591e4bea24d21 100644 (file)
@@ -673,7 +673,8 @@ static void populate_value(struct refinfo *ref)
                } else if (starts_with(name, "color:")) {
                        char color[COLOR_MAXLEN] = "";
 
-                       color_parse(name + 6, "--format", color);
+                       if (color_parse(name + 6, color) < 0)
+                               die(_("unable to parse format"));
                        v->s = xstrdup(color);
                        continue;
                } else if (!strcmp(name, "flag")) {
@@ -1007,7 +1008,8 @@ static void show_ref(struct refinfo *info, const char *format, int quote_style)
                struct atom_value resetv;
                char color[COLOR_MAXLEN] = "";
 
-               color_parse("reset", "--format", color);
+               if (color_parse("reset", color) < 0)
+                       die("BUG: couldn't parse 'reset' as a color");
                resetv.s = color;
                print_value(&resetv, quote_style);
        }
diff --git a/color.c b/color.c
index f672885b71acef4108c8cefc9cf887c220f614d3..7941e932d2fb7cb0af5d15dcb2754bc8c96ec657 100644 (file)
--- a/color.c
+++ b/color.c
@@ -60,13 +60,12 @@ static int parse_attr(const char *name, int len)
        return -1;
 }
 
-void color_parse(const char *value, const char *var, char *dst)
+int color_parse(const char *value, char *dst)
 {
-       color_parse_mem(value, strlen(value), var, dst);
+       return color_parse_mem(value, strlen(value), dst);
 }
 
-void color_parse_mem(const char *value, int value_len, const char *var,
-               char *dst)
+int color_parse_mem(const char *value, int value_len, char *dst)
 {
        const char *ptr = value;
        int len = value_len;
@@ -76,7 +75,7 @@ void color_parse_mem(const char *value, int value_len, const char *var,
 
        if (!strncasecmp(value, "reset", len)) {
                strcpy(dst, GIT_COLOR_RESET);
-               return;
+               return 0;
        }
 
        /* [fg [bg]] [attr]... */
@@ -153,9 +152,9 @@ void color_parse_mem(const char *value, int value_len, const char *var,
                *dst++ = 'm';
        }
        *dst = 0;
-       return;
+       return 0;
 bad:
-       die("bad color value '%.*s' for variable '%s'", value_len, value, var);
+       return error(_("invalid color value: %.*s"), value_len, value);
 }
 
 int git_config_colorbool(const char *var, const char *value)
diff --git a/color.h b/color.h
index 9a8495bb7ff06eb4e94e190d902b48c23fc021f9..f5beab1ed782549eb08ad11392ed8e6331bdd653 100644 (file)
--- a/color.h
+++ b/color.h
@@ -77,8 +77,8 @@ int git_color_default_config(const char *var, const char *value, void *cb);
 
 int git_config_colorbool(const char *var, const char *value);
 int want_color(int var);
-void color_parse(const char *value, const char *var, char *dst);
-void color_parse_mem(const char *value, int len, const char *var, char *dst);
+int color_parse(const char *value, char *dst);
+int color_parse_mem(const char *value, int len, char *dst);
 __attribute__((format (printf, 3, 4)))
 int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
 __attribute__((format (printf, 3, 4)))
diff --git a/diff.c b/diff.c
index 867f034b8ffc052d28f28a86fd9f52a5aa5b2c82..4493dde7495c91346d9d164df63fbb61ed1bf00b 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -248,8 +248,7 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
                        return 0;
                if (!value)
                        return config_error_nonbool(var);
-               color_parse(value, var, diff_colors[slot]);
-               return 0;
+               return color_parse(value, diff_colors[slot]);
        }
 
        /* like GNU diff's --suppress-blank-empty option  */
diff --git a/grep.c b/grep.c
index 99217dc04f5d04c761f094069f1053cb090baaf1..4dc31ea38656f72c51bae96155053cd1cd0f6d64 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -111,7 +111,7 @@ int grep_config(const char *var, const char *value, void *cb)
        if (color) {
                if (!value)
                        return config_error_nonbool(var);
-               color_parse(value, var, color);
+               return color_parse(value, color);
        }
        return 0;
 }
index 479b1d2a5b51fec78706467ac8dc0821d018478c..a21ef30d0cb6a664665adf77a7c924572d600712 100644 (file)
@@ -73,8 +73,7 @@ int parse_decorate_color_config(const char *var, const char *slot_name, const ch
                return 0;
        if (!value)
                return config_error_nonbool(var);
-       color_parse(value, var, decoration_colors[slot]);
-       return 0;
+       return color_parse(value, decoration_colors[slot]);
 }
 
 /*
index 31fc76b2fde8297298a7d9ffd38ea7f00eede2fa..6182ca9aed79503a4c24ed267b8cf69673cac16b 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -979,9 +979,8 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
                                return end - placeholder + 1;
                        begin += 5;
                }
-               color_parse_mem(begin,
-                               end - begin,
-                               "--pretty format", color);
+               if (color_parse_mem(begin, end - begin, color) < 0)
+                       die(_("unable to parse --pretty format"));
                strbuf_addstr(sb, color);
                return end - placeholder + 1;
        }