Skip to content

Commit d14937e

Browse files
committed
config: format colors gently
Move the logic for formatting color config value into a helper method and use gentle 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 ddf6131 commit d14937e

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

builtin/config.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,24 @@ static int format_config_expiry_date(struct strbuf *buf,
354354
return 0;
355355
}
356356

357+
static int format_config_color(struct strbuf *buf,
358+
const char *key_,
359+
const char *value_,
360+
int gently)
361+
{
362+
char v[COLOR_MAXLEN];
363+
364+
if (gently) {
365+
if (color_parse_gently(value_, v) < 0)
366+
return -1;
367+
} else if (git_config_color(v, key_, value_) < 0) {
368+
return -1;
369+
}
370+
371+
strbuf_addstr(buf, v);
372+
return 0;
373+
}
374+
357375
/*
358376
* Format the configuration key-value pair (`key_`, `value_`) and
359377
* append it into strbuf `buf`. Returns a negative value on failure,
@@ -391,12 +409,9 @@ static int format_config(const struct config_display_options *opts,
391409
res = format_config_path(buf, key_, value_, gently);
392410
else if (opts->type == TYPE_EXPIRY_DATE)
393411
res = format_config_expiry_date(buf, key_, value_, gently);
394-
else if (opts->type == TYPE_COLOR) {
395-
char v[COLOR_MAXLEN];
396-
if (git_config_color(v, key_, value_) < 0)
397-
return -1;
398-
strbuf_addstr(buf, v);
399-
} else if (value_) {
412+
else if (opts->type == TYPE_COLOR)
413+
res = format_config_color(buf, key_, value_, gently);
414+
else if (value_) {
400415
strbuf_addstr(buf, value_);
401416
} else {
402417
/* Just show the key name; back out delimiter */

t/t1300-config.sh

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2553,17 +2553,10 @@ test_expect_success 'list --type=color shows only canonicalizable color values'
25532553
section.blue=<BLUE>
25542554
EOF
25552555
2556-
cat >expecterr <<-EOF &&
2557-
error: invalid color value: True
2558-
error: invalid color value: 1M
2559-
error: invalid color value: ~/dir
2560-
error: invalid color value: Fri Jun 4 15:46:55 2010
2561-
EOF
2562-
25632556
git config ${mode_prefix}list --type=color >actual.raw 2>err &&
25642557
test_decode_color <actual.raw >actual &&
25652558
test_cmp expect actual &&
2566-
test_cmp expecterr err
2559+
test_must_be_empty err
25672560
'
25682561

25692562
test_expect_success '--type rejects unknown specifiers' '

0 commit comments

Comments
 (0)