Skip to content

Commit 54b24b1

Browse files
pks-tgitster
authored andcommitted
builtin/config: special-case retrieving colors without a key
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" "" This command is not supposed to parse any configuration keys. 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 in 4e51389 (builtin/config: introduce "get" subcommand, 2024-05-06), where we introduced the "get" subcommand to retrieve configuration values. The preimage of that commit used `git config --get-color "" "reset"` instead, which still works. 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; 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 default value, then we call `get_color()` directly. Do so to make the documented command work as expected. [1]: <[email protected]> Reported-by: SZEDER Gábor <[email protected]> Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6e6ed3e commit 54b24b1

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

builtin/config.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,8 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix,
923923

924924
if (url)
925925
ret = get_urlmatch(&location_opts, &display_opts, argv[0], url);
926+
else if (display_opts.type == TYPE_COLOR && !strlen(argv[0]) && display_opts.default_value)
927+
ret = get_color(&location_opts, "", display_opts.default_value);
926928
else
927929
ret = get_value(&location_opts, &display_opts, argv[0], value_pattern,
928930
get_value_flags, flags);

t/t1300-config.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,11 +1083,22 @@ test_expect_success 'get --type=color' '
10831083
rm .git/config &&
10841084
git config ${mode_set} foo.color "red" &&
10851085
git config --get --type=color foo.color >actual.raw &&
1086+
git config get --type=color foo.color >actual-subcommand.raw &&
1087+
test_cmp actual.raw actual-subcommand.raw &&
10861088
test_decode_color <actual.raw >actual &&
10871089
echo "<RED>" >expect &&
10881090
test_cmp expect actual
10891091
'
10901092

1093+
test_expect_success 'get --type=color with default value only' '
1094+
git config --get-color "" "red" >actual.raw &&
1095+
test_decode_color <actual.raw >actual &&
1096+
echo "<RED>" >expect &&
1097+
test_cmp expect actual &&
1098+
git config get --type=color --default="red" "" >actual-subcommand.raw &&
1099+
test_cmp actual.raw actual-subcommand.raw
1100+
'
1101+
10911102
test_expect_success 'set --type=color' '
10921103
cat >expect <<\EOF &&
10931104
[foo]

0 commit comments

Comments
 (0)