Skip to content

Commit 078065c

Browse files
committed
config: format colors quietly
Move the logic for formatting color config value into a helper method and use quiet parsing when needed. This removes error messages when parsing a list of config values that do not match color formats. Signed-off-by: Derrick Stolee <stolee@gmail.com>
1 parent d98966f commit 078065c

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

builtin/config.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,24 @@ static int format_config_expiry_date(struct strbuf *buf,
351351
return 0;
352352
}
353353

354+
static int format_config_color(struct strbuf *buf,
355+
const char *key_,
356+
const char *value_,
357+
int gently)
358+
{
359+
char v[COLOR_MAXLEN];
360+
361+
if (gently) {
362+
if (color_parse_quietly(value_, v) < 0)
363+
return -1;
364+
} else if (git_config_color(v, key_, value_) < 0) {
365+
return -1;
366+
}
367+
368+
strbuf_addstr(buf, v);
369+
return 0;
370+
}
371+
354372
/*
355373
* Format the configuration key-value pair (`key_`, `value_`) and
356374
* append it into strbuf `buf`. Returns a negative value on failure,
@@ -388,12 +406,9 @@ static int format_config(const struct config_display_options *opts,
388406
res = format_config_path(buf, key_, value_, gently);
389407
else if (opts->type == TYPE_EXPIRY_DATE)
390408
res = format_config_expiry_date(buf, key_, value_, gently);
391-
else if (opts->type == TYPE_COLOR) {
392-
char v[COLOR_MAXLEN];
393-
if (git_config_color(v, key_, value_) < 0)
394-
return -1;
395-
strbuf_addstr(buf, v);
396-
} else if (value_) {
409+
else if (opts->type == TYPE_COLOR)
410+
res = format_config_color(buf, key_, value_, gently);
411+
else if (value_) {
397412
strbuf_addstr(buf, value_);
398413
} else {
399414
/* Just show the key name; back out delimiter */

t/t1300-config.sh

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,19 +2579,10 @@ test_expect_success 'list --type=color shows only canonicalizable color values'
25792579
section.blue=<BLUE>
25802580
EOF
25812581
2582-
cat >expecterr <<-EOF &&
2583-
error: invalid color value: True
2584-
error: invalid color value: 1M
2585-
error: invalid color value: ~/dir
2586-
error: invalid color value: Fri Jun 4 15:46:55 2010
2587-
error: invalid color value: :(optional)no-such-path
2588-
error: invalid color value: :(optional)expect
2589-
EOF
2590-
25912582
git config ${mode_prefix}list --type=color >actual.raw 2>err &&
25922583
test_decode_color <actual.raw >actual &&
25932584
test_cmp expect actual &&
2594-
test_cmp expecterr err
2585+
test_must_be_empty err
25952586
'
25962587

25972588
test_expect_success '--type rejects unknown specifiers' '

0 commit comments

Comments
 (0)