Skip to content

Commit 62ce40d

Browse files
committed
Merge branch 'jn/parse-config-slot' into jk/colors
* jn/parse-config-slot: color_parse: do not mention variable name in error message pass config slots as pointers instead of offsets
2 parents cb35722 + f6c5a29 commit 62ce40d

File tree

13 files changed

+47
-50
lines changed

13 files changed

+47
-50
lines changed

builtin/branch.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,19 @@ static unsigned char merge_filter_ref[20];
6262
static struct string_list output = STRING_LIST_INIT_DUP;
6363
static unsigned int colopts;
6464

65-
static int parse_branch_color_slot(const char *var, int ofs)
65+
static int parse_branch_color_slot(const char *slot)
6666
{
67-
if (!strcasecmp(var+ofs, "plain"))
67+
if (!strcasecmp(slot, "plain"))
6868
return BRANCH_COLOR_PLAIN;
69-
if (!strcasecmp(var+ofs, "reset"))
69+
if (!strcasecmp(slot, "reset"))
7070
return BRANCH_COLOR_RESET;
71-
if (!strcasecmp(var+ofs, "remote"))
71+
if (!strcasecmp(slot, "remote"))
7272
return BRANCH_COLOR_REMOTE;
73-
if (!strcasecmp(var+ofs, "local"))
73+
if (!strcasecmp(slot, "local"))
7474
return BRANCH_COLOR_LOCAL;
75-
if (!strcasecmp(var+ofs, "current"))
75+
if (!strcasecmp(slot, "current"))
7676
return BRANCH_COLOR_CURRENT;
77-
if (!strcasecmp(var+ofs, "upstream"))
77+
if (!strcasecmp(slot, "upstream"))
7878
return BRANCH_COLOR_UPSTREAM;
7979
return -1;
8080
}
@@ -88,13 +88,12 @@ static int git_branch_config(const char *var, const char *value, void *cb)
8888
return 0;
8989
}
9090
if (starts_with(var, "color.branch.")) {
91-
int slot = parse_branch_color_slot(var, 13);
91+
int slot = parse_branch_color_slot(var + 13);
9292
if (slot < 0)
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: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,22 +1238,21 @@ static int dry_run_commit(int argc, const char **argv, const char *prefix,
12381238
return commitable ? 0 : 1;
12391239
}
12401240

1241-
static int parse_status_slot(const char *var, int offset)
1241+
static int parse_status_slot(const char *slot)
12421242
{
1243-
if (!strcasecmp(var+offset, "header"))
1243+
if (!strcasecmp(slot, "header"))
12441244
return WT_STATUS_HEADER;
1245-
if (!strcasecmp(var+offset, "branch"))
1245+
if (!strcasecmp(slot, "branch"))
12461246
return WT_STATUS_ONBRANCH;
1247-
if (!strcasecmp(var+offset, "updated")
1248-
|| !strcasecmp(var+offset, "added"))
1247+
if (!strcasecmp(slot, "updated") || !strcasecmp(slot, "added"))
12491248
return WT_STATUS_UPDATED;
1250-
if (!strcasecmp(var+offset, "changed"))
1249+
if (!strcasecmp(slot, "changed"))
12511250
return WT_STATUS_CHANGED;
1252-
if (!strcasecmp(var+offset, "untracked"))
1251+
if (!strcasecmp(slot, "untracked"))
12531252
return WT_STATUS_UNTRACKED;
1254-
if (!strcasecmp(var+offset, "nobranch"))
1253+
if (!strcasecmp(slot, "nobranch"))
12551254
return WT_STATUS_NOBRANCH;
1256-
if (!strcasecmp(var+offset, "unmerged"))
1255+
if (!strcasecmp(slot, "unmerged"))
12571256
return WT_STATUS_UNMERGED;
12581257
return -1;
12591258
}
@@ -1291,13 +1290,12 @@ static int git_status_config(const char *k, const char *v, void *cb)
12911290
return 0;
12921291
}
12931292
if (starts_with(k, "status.color.") || starts_with(k, "color.status.")) {
1294-
int slot = parse_status_slot(k, 13);
1293+
int slot = parse_status_slot(k + 13);
12951294
if (slot < 0)
12961295
return 0;
12971296
if (!v)
12981297
return config_error_nonbool(k);
1299-
color_parse(v, k, s->color_palette[slot]);
1300-
return 0;
1298+
return color_parse(v, s->color_palette[slot]);
13011299
}
13021300
if (!strcmp(k, "status.relativepaths")) {
13031301
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;
@@ -310,8 +311,10 @@ static void get_color(const char *var, const char *def_color)
310311
git_config_with_options(git_get_color_config, NULL,
311312
&given_config_source, respect_includes);
312313

313-
if (!get_color_found && def_color)
314-
color_parse(def_color, "command line", parsed_color);
314+
if (!get_color_found && def_color) {
315+
if (color_parse(def_color, parsed_color) < 0)
316+
die(_("unable to parse default color value"));
317+
}
315318

316319
fputs(parsed_color, stdout);
317320
}

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
}

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ static int git_log_config(const char *var, const char *value, void *cb)
389389
return 0;
390390
}
391391
if (starts_with(var, "color.decorate."))
392-
return parse_decorate_color_config(var, 15, value);
392+
return parse_decorate_color_config(var, var + 15, value);
393393
if (!strcmp(var, "log.mailmap")) {
394394
use_mailmap_config = git_config_bool(var, value);
395395
return 0;

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: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,14 @@ static int parse_decorate_color_slot(const char *slot)
6666
return -1;
6767
}
6868

69-
int parse_decorate_color_config(const char *var, const int ofs, const char *value)
69+
int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
7070
{
71-
int slot = parse_decorate_color_slot(var + ofs);
71+
int slot = parse_decorate_color_slot(slot_name);
7272
if (slot < 0)
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
/*

log-tree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct log_info {
77
struct commit *commit, *parent;
88
};
99

10-
int parse_decorate_color_config(const char *var, const int ofs, const char *value);
10+
int parse_decorate_color_config(const char *var, const char *slot_name, const char *value);
1111
void init_log_tree_opt(struct rev_info *);
1212
int log_tree_diff_flush(struct rev_info *);
1313
int log_tree_commit(struct rev_info *, struct commit *);

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)