1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-23 23:06:12 +02:00

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
This commit is contained in:
Junio C Hamano 2014-10-20 12:23:48 -07:00
commit b946576839
13 changed files with 47 additions and 50 deletions

View File

@ -62,19 +62,19 @@ static unsigned char merge_filter_ref[20];
static struct string_list output = STRING_LIST_INIT_DUP; static struct string_list output = STRING_LIST_INIT_DUP;
static unsigned int colopts; static unsigned int colopts;
static int parse_branch_color_slot(const char *var, int ofs) static int parse_branch_color_slot(const char *slot)
{ {
if (!strcasecmp(var+ofs, "plain")) if (!strcasecmp(slot, "plain"))
return BRANCH_COLOR_PLAIN; return BRANCH_COLOR_PLAIN;
if (!strcasecmp(var+ofs, "reset")) if (!strcasecmp(slot, "reset"))
return BRANCH_COLOR_RESET; return BRANCH_COLOR_RESET;
if (!strcasecmp(var+ofs, "remote")) if (!strcasecmp(slot, "remote"))
return BRANCH_COLOR_REMOTE; return BRANCH_COLOR_REMOTE;
if (!strcasecmp(var+ofs, "local")) if (!strcasecmp(slot, "local"))
return BRANCH_COLOR_LOCAL; return BRANCH_COLOR_LOCAL;
if (!strcasecmp(var+ofs, "current")) if (!strcasecmp(slot, "current"))
return BRANCH_COLOR_CURRENT; return BRANCH_COLOR_CURRENT;
if (!strcasecmp(var+ofs, "upstream")) if (!strcasecmp(slot, "upstream"))
return BRANCH_COLOR_UPSTREAM; return BRANCH_COLOR_UPSTREAM;
return -1; return -1;
} }
@ -90,13 +90,12 @@ static int git_branch_config(const char *var, const char *value, void *cb)
return 0; return 0;
} }
if (skip_prefix(var, "color.branch.", &slot_name)) { if (skip_prefix(var, "color.branch.", &slot_name)) {
int slot = parse_branch_color_slot(var, slot_name - var); int slot = parse_branch_color_slot(slot_name);
if (slot < 0) if (slot < 0)
return 0; return 0;
if (!value) if (!value)
return config_error_nonbool(var); return config_error_nonbool(var);
color_parse(value, var, branch_colors[slot]); return color_parse(value, branch_colors[slot]);
return 0;
} }
return git_color_default_config(var, value, cb); return git_color_default_config(var, value, cb);
} }

View File

@ -117,8 +117,7 @@ static int git_clean_config(const char *var, const char *value, void *cb)
return 0; return 0;
if (!value) if (!value)
return config_error_nonbool(var); return config_error_nonbool(var);
color_parse(value, var, clean_colors[slot]); return color_parse(value, clean_colors[slot]);
return 0;
} }
if (!strcmp(var, "clean.requireforce")) { if (!strcmp(var, "clean.requireforce")) {

View File

@ -1272,22 +1272,21 @@ static int dry_run_commit(int argc, const char **argv, const char *prefix,
return commitable ? 0 : 1; return commitable ? 0 : 1;
} }
static int parse_status_slot(const char *var, int offset) static int parse_status_slot(const char *slot)
{ {
if (!strcasecmp(var+offset, "header")) if (!strcasecmp(slot, "header"))
return WT_STATUS_HEADER; return WT_STATUS_HEADER;
if (!strcasecmp(var+offset, "branch")) if (!strcasecmp(slot, "branch"))
return WT_STATUS_ONBRANCH; return WT_STATUS_ONBRANCH;
if (!strcasecmp(var+offset, "updated") if (!strcasecmp(slot, "updated") || !strcasecmp(slot, "added"))
|| !strcasecmp(var+offset, "added"))
return WT_STATUS_UPDATED; return WT_STATUS_UPDATED;
if (!strcasecmp(var+offset, "changed")) if (!strcasecmp(slot, "changed"))
return WT_STATUS_CHANGED; return WT_STATUS_CHANGED;
if (!strcasecmp(var+offset, "untracked")) if (!strcasecmp(slot, "untracked"))
return WT_STATUS_UNTRACKED; return WT_STATUS_UNTRACKED;
if (!strcasecmp(var+offset, "nobranch")) if (!strcasecmp(slot, "nobranch"))
return WT_STATUS_NOBRANCH; return WT_STATUS_NOBRANCH;
if (!strcasecmp(var+offset, "unmerged")) if (!strcasecmp(slot, "unmerged"))
return WT_STATUS_UNMERGED; return WT_STATUS_UNMERGED;
return -1; return -1;
} }
@ -1327,13 +1326,12 @@ static int git_status_config(const char *k, const char *v, void *cb)
} }
if (skip_prefix(k, "status.color.", &slot_name) || if (skip_prefix(k, "status.color.", &slot_name) ||
skip_prefix(k, "color.status.", &slot_name)) { skip_prefix(k, "color.status.", &slot_name)) {
int slot = parse_status_slot(k, slot_name - k); int slot = parse_status_slot(slot_name);
if (slot < 0) if (slot < 0)
return 0; return 0;
if (!v) if (!v)
return config_error_nonbool(k); return config_error_nonbool(k);
color_parse(v, k, s->color_palette[slot]); return color_parse(v, s->color_palette[slot]);
return 0;
} }
if (!strcmp(k, "status.relativepaths")) { if (!strcmp(k, "status.relativepaths")) {
s->relative_paths = git_config_bool(k, v); s->relative_paths = git_config_bool(k, v);

View 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 (!strcmp(var, get_color_slot)) {
if (!value) if (!value)
config_error_nonbool(var); config_error_nonbool(var);
color_parse(value, var, parsed_color); if (color_parse(value, parsed_color) < 0)
return -1;
get_color_found = 1; get_color_found = 1;
} }
return 0; return 0;
@ -309,8 +310,10 @@ static void get_color(const char *def_color)
git_config_with_options(git_get_color_config, NULL, git_config_with_options(git_get_color_config, NULL,
&given_config_source, respect_includes); &given_config_source, respect_includes);
if (!get_color_found && def_color) if (!get_color_found && def_color) {
color_parse(def_color, "command line", parsed_color); if (color_parse(def_color, parsed_color) < 0)
die(_("unable to parse default color value"));
}
fputs(parsed_color, stdout); fputs(parsed_color, stdout);
} }

View File

@ -671,7 +671,8 @@ static void populate_value(struct refinfo *ref)
} else if (starts_with(name, "color:")) { } else if (starts_with(name, "color:")) {
char color[COLOR_MAXLEN] = ""; 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); v->s = xstrdup(color);
continue; continue;
} else if (!strcmp(name, "flag")) { } else if (!strcmp(name, "flag")) {
@ -1004,7 +1005,8 @@ static void show_ref(struct refinfo *info, const char *format, int quote_style)
struct atom_value resetv; struct atom_value resetv;
char color[COLOR_MAXLEN] = ""; 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; resetv.s = color;
print_value(&resetv, quote_style); print_value(&resetv, quote_style);
} }

View File

@ -391,7 +391,7 @@ static int git_log_config(const char *var, const char *value, void *cb)
return 0; return 0;
} }
if (skip_prefix(var, "color.decorate.", &slot_name)) if (skip_prefix(var, "color.decorate.", &slot_name))
return parse_decorate_color_config(var, slot_name - var, value); return parse_decorate_color_config(var, slot_name, value);
if (!strcmp(var, "log.mailmap")) { if (!strcmp(var, "log.mailmap")) {
use_mailmap_config = git_config_bool(var, value); use_mailmap_config = git_config_bool(var, value);
return 0; return 0;

13
color.c
View File

@ -60,13 +60,12 @@ static int parse_attr(const char *name, int len)
return -1; 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, int color_parse_mem(const char *value, int value_len, char *dst)
char *dst)
{ {
const char *ptr = value; const char *ptr = value;
int len = value_len; 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)) { if (!strncasecmp(value, "reset", len)) {
strcpy(dst, GIT_COLOR_RESET); strcpy(dst, GIT_COLOR_RESET);
return; return 0;
} }
/* [fg [bg]] [attr]... */ /* [fg [bg]] [attr]... */
@ -153,9 +152,9 @@ void color_parse_mem(const char *value, int value_len, const char *var,
*dst++ = 'm'; *dst++ = 'm';
} }
*dst = 0; *dst = 0;
return; return 0;
bad: 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) int git_config_colorbool(const char *var, const char *value)

View File

@ -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 git_config_colorbool(const char *var, const char *value);
int want_color(int var); int want_color(int var);
void color_parse(const char *value, const char *var, char *dst); int color_parse(const char *value, char *dst);
void color_parse_mem(const char *value, int len, const char *var, char *dst); int color_parse_mem(const char *value, int len, char *dst);
__attribute__((format (printf, 3, 4))) __attribute__((format (printf, 3, 4)))
int color_fprintf(FILE *fp, const char *color, const char *fmt, ...); int color_fprintf(FILE *fp, const char *color, const char *fmt, ...);
__attribute__((format (printf, 3, 4))) __attribute__((format (printf, 3, 4)))

3
diff.c
View File

@ -248,8 +248,7 @@ int git_diff_basic_config(const char *var, const char *value, void *cb)
return 0; return 0;
if (!value) if (!value)
return config_error_nonbool(var); return config_error_nonbool(var);
color_parse(value, var, diff_colors[slot]); return color_parse(value, diff_colors[slot]);
return 0;
} }
/* like GNU diff's --suppress-blank-empty option */ /* like GNU diff's --suppress-blank-empty option */

2
grep.c
View File

@ -111,7 +111,7 @@ int grep_config(const char *var, const char *value, void *cb)
if (color) { if (color) {
if (!value) if (!value)
return config_error_nonbool(var); return config_error_nonbool(var);
color_parse(value, var, color); return color_parse(value, color);
} }
return 0; return 0;
} }

View File

@ -56,15 +56,14 @@ static int parse_decorate_color_slot(const char *slot)
return -1; return -1;
} }
int parse_decorate_color_config(const char *var, const int ofs, const char *value) int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
{ {
int slot = parse_decorate_color_slot(var + ofs); int slot = parse_decorate_color_slot(slot_name);
if (slot < 0) if (slot < 0)
return 0; return 0;
if (!value) if (!value)
return config_error_nonbool(var); return config_error_nonbool(var);
color_parse(value, var, decoration_colors[slot]); return color_parse(value, decoration_colors[slot]);
return 0;
} }
/* /*

View File

@ -7,7 +7,7 @@ struct log_info {
struct commit *commit, *parent; struct commit *commit, *parent;
}; };
int parse_decorate_color_config(const char *var, const int ofs, const char *value); int parse_decorate_color_config(const char *var, const char *slot_name, const char *value);
void init_log_tree_opt(struct rev_info *); void init_log_tree_opt(struct rev_info *);
int log_tree_diff_flush(struct rev_info *); int log_tree_diff_flush(struct rev_info *);
int log_tree_commit(struct rev_info *, struct commit *); int log_tree_commit(struct rev_info *, struct commit *);

View File

@ -964,9 +964,8 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
if (!want_color(c->pretty_ctx->color)) if (!want_color(c->pretty_ctx->color))
return end - placeholder + 1; return end - placeholder + 1;
} }
color_parse_mem(begin, if (color_parse_mem(begin, end - begin, color) < 0)
end - begin, die(_("unable to parse --pretty format"));
"--pretty format", color);
strbuf_addstr(sb, color); strbuf_addstr(sb, color);
return end - placeholder + 1; return end - placeholder + 1;
} }