]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: format colors quietly
authorDerrick Stolee <stolee@gmail.com>
Mon, 23 Feb 2026 12:26:53 +0000 (12:26 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 23 Feb 2026 21:23:41 +0000 (13:23 -0800)
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>
builtin/config.c
t/t1300-config.sh

index ee77ddc87c629cdb1d4774d685a0a4c4a0e2c112..45304076dc47267a99b6472558155c8809462f77 100644 (file)
@@ -351,6 +351,24 @@ static int format_config_expiry_date(struct strbuf *buf,
        return 0;
 }
 
+static int format_config_color(struct strbuf *buf,
+                              const char *key_,
+                              const char *value_,
+                              int gently)
+{
+       char v[COLOR_MAXLEN];
+
+       if (gently) {
+               if (color_parse_quietly(value_, v) < 0)
+                       return -1;
+       } else if (git_config_color(v, key_, value_) < 0) {
+               return -1;
+       }
+
+       strbuf_addstr(buf, v);
+       return 0;
+}
+
 /*
  * Format the configuration key-value pair (`key_`, `value_`) and
  * 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,
                        res = format_config_path(buf, key_, value_, gently);
                else if (opts->type == TYPE_EXPIRY_DATE)
                        res = format_config_expiry_date(buf, key_, value_, gently);
-               else if (opts->type == TYPE_COLOR) {
-                       char v[COLOR_MAXLEN];
-                       if (git_config_color(v, key_, value_) < 0)
-                               return -1;
-                       strbuf_addstr(buf, v);
-               } else if (value_) {
+               else if (opts->type == TYPE_COLOR)
+                       res = format_config_color(buf, key_, value_, gently);
+               else if (value_) {
                        strbuf_addstr(buf, value_);
                } else {
                        /* Just show the key name; back out delimiter */
index 72bdd6ab03fd75c9d4fd073a5d926bca14409f49..128971ee12fa6c510c4110d9b958037fbf578f42 100755 (executable)
@@ -2579,19 +2579,10 @@ test_expect_success 'list --type=color shows only canonicalizable color values'
        section.blue=<BLUE>
        EOF
 
-       cat >expecterr <<-EOF &&
-       error: invalid color value: True
-       error: invalid color value: 1M
-       error: invalid color value: ~/dir
-       error: invalid color value: Fri Jun 4 15:46:55 2010
-       error: invalid color value: :(optional)no-such-path
-       error: invalid color value: :(optional)expect
-       EOF
-
        git config ${mode_prefix}list --type=color >actual.raw 2>err &&
        test_decode_color <actual.raw >actual &&
        test_cmp expect actual &&
-       test_cmp expecterr err
+       test_must_be_empty err
 '
 
 test_expect_success '--type rejects unknown specifiers' '