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>
This commit is contained in:
Jeff King
2014-10-07 15:33:09 -04:00
committed by Junio C Hamano
parent 8852117a60
commit f6c5a2968c
11 changed files with 26 additions and 28 deletions

13
color.c
View File

@ -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)