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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Feb 23, 2026 at 12:26 UTC 2d4ab5a885f365cb66156ff4553f88c000dfa307
2 files changed +22 -16
builtin/config.c
+21 -6
@@ -351,6 +351,24 @@ static int format_config_expiry_date(struct strbuf *buf,
351 return 0;
352 }
353
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 +
372 /*
373 * Format the configuration key-value pair (`key_`, `value_`) and
374 * 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,
406 res = format_config_path(buf, key_, value_, gently);
407 else if (opts->type == TYPE_EXPIRY_DATE)
408 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_) {
412 strbuf_addstr(buf, value_);
413 } else {
414 /* Just show the key name; back out delimiter */
t/t1300-config.sh
+1 -10
@@ -2579,19 +2579,10 @@ test_expect_success 'list --type=color shows only canonicalizable color values'
2579 section.blue=<BLUE>
2580 EOF
2581
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 -
2582 git config ${mode_prefix}list --type=color >actual.raw 2>err &&
2583 test_decode_color <actual.raw >actual &&
2584 test_cmp expect actual &&
2594 - test_cmp expecterr err
2585 + test_must_be_empty err
2586 '
2587
2588 test_expect_success '--type rejects unknown specifiers' '