Skip to content

Commit e4dabf4

Browse files
pks-tgitster
authored andcommitted
builtin/config: do not spawn pager when printing color codes
With `git config get --type=color` the user asks us to parse a specific configuration key and turn the value into an ANSI color escape sequence. The printed string can then for example be used as part of shell scripts to reuse the same colors as Git. Right now though we set up the auto-pager, which means that the string may be written to the pager instead of directly to the terminal. This behaviour is problematic for two reasons: - Color codes are meant for direct terminal output; writing them into a pager does not seem like a sensible thing to do without additional text. - It is inconsistent with `git config --get-color`, which never uses a pager, despite the fact that we claim `git config get --type=color` to be a drop-in replacement in git-config(1). Fix this by disabling the pager when outputting color sequences. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 54b24b1 commit e4dabf4

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

builtin/config.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,8 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix,
919919
location_options_init(&location_opts, prefix);
920920
display_options_init(&display_opts);
921921

922-
setup_auto_pager("config", 1);
922+
if (display_opts.type != TYPE_COLOR)
923+
setup_auto_pager("config", 1);
923924

924925
if (url)
925926
ret = get_urlmatch(&location_opts, &display_opts, argv[0], url);

t/t1300-config.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
99
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
1010

1111
. ./test-lib.sh
12+
. "$TEST_DIRECTORY"/lib-terminal.sh
1213

1314
for mode in legacy subcommands
1415
do
@@ -1099,6 +1100,14 @@ test_expect_success 'get --type=color with default value only' '
10991100
test_cmp actual.raw actual-subcommand.raw
11001101
'
11011102

1103+
test_expect_success TTY 'get --type=color does not use a pager' '
1104+
test_config core.pager "echo foobar" &&
1105+
test_terminal git config get --type=color --default="red" "" >actual.raw &&
1106+
test_decode_color <actual.raw >actual &&
1107+
echo "<RED>" >expect &&
1108+
test_cmp expect actual
1109+
'
1110+
11021111
test_expect_success 'set --type=color' '
11031112
cat >expect <<\EOF &&
11041113
[foo]

0 commit comments

Comments
 (0)