Our documentation for git-config(1) has a section where it explains how to parse and use colors as Git would configure them. In order to get the ANSI color escape sequence to reset the colors to normal we recommend the following command: $ git config get --type=color --default="reset" "" What this command is supposed to do is to not parse any configuration key at all. Instead, it is expected to parse the "reset" default value and turn it into a proper ANSI color escape sequence. It was reported though [1] that this command doesn't work: $ git config get --type=color --default="reset" "" error: key does not contain a section: This error was introduced with 4e51389000 (builtin/config: introduce "get" subcommand, 2024-05-06), where we introduced the new "get" subcommand to retrieve configuration values. The preimage of that commit used `git config --get-color "" "reset"` instead, which still works nowadays. This use case is really quite specific to parsing colors, as it wouldn't make sense to give git-config(1) a default value and an empty config key only to return that default value unmodified. But with `--type=color` we don't return the value directly, but we instead parse the value into an ANSI escape sequence. As such, we can easily special-case this one use case: if the provided config key is empty, the user is asking for a color code and the user has provided a value, then we call `get_color()` directly. Do so to make the documented command work as expected. [1]: <aI+oQvQgnNtC6DVw@xxxxxxxxxx> Reported-by: SZEDER Gábor <szeder.dev@xxxxxxxxx> Signed-off-by: Patrick Steinhardt <ps@xxxxxx> --- builtin/config.c | 2 ++ t/t1300-config.sh | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/builtin/config.c b/builtin/config.c index afd48bfa51..f50c11df57 100644 --- a/builtin/config.c +++ b/builtin/config.c @@ -923,6 +923,8 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix, if (url) ret = get_urlmatch(&location_opts, &display_opts, argv[0], url); + else if (display_opts.type == TYPE_COLOR && !strlen(argv[0]) && display_opts.default_value) + ret = get_color(&location_opts, "", display_opts.default_value); else ret = get_value(&location_opts, &display_opts, argv[0], value_pattern, get_value_flags, flags); diff --git a/t/t1300-config.sh b/t/t1300-config.sh index 9c405e9532..40f170cf40 100755 --- a/t/t1300-config.sh +++ b/t/t1300-config.sh @@ -1084,11 +1084,22 @@ test_expect_success 'get --type=color' ' rm .git/config && git config ${mode_set} foo.color "red" && git config --get --type=color foo.color >actual.raw && + git config get --type=color foo.color >actual-subcommand.raw && + test_cmp actual.raw actual-subcommand.raw && test_decode_color <actual.raw >actual && echo "<RED>" >expect && test_cmp expect actual ' +test_expect_success 'get --type=color with default value only' ' + git config --get-color "" "red" >actual.raw && + test_decode_color <actual.raw >actual && + echo "<RED>" >expect && + test_cmp expect actual && + git config get --type=color --default="red" "" >actual-subcommand.raw && + test_cmp actual.raw actual-subcommand.raw +' + test_expect_success 'set --type=color' ' cat >expect <<EOF && [foo] -- 2.51.0.450.g87641ccf93.dirty