]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/config: do not spawn pager when printing color codes
authorPatrick Steinhardt <ps@pks.im>
Mon, 22 Sep 2025 13:06:22 +0000 (15:06 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 22 Sep 2025 16:32:57 +0000 (09:32 -0700)
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 <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/config.c
t/t1300-config.sh

index cfd89a4186bdd1ccb3f54d42614f9562421e00a4..c8776ad6519178b06fec15e0c9c9e39c8c34f682 100644 (file)
@@ -919,7 +919,8 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix,
        location_options_init(&location_opts, prefix);
        display_options_init(&display_opts);
 
-       setup_auto_pager("config", 1);
+       if (display_opts.type != TYPE_COLOR)
+               setup_auto_pager("config", 1);
 
        if (url)
                ret = get_urlmatch(&location_opts, &display_opts, argv[0], url);
index 3cf5d17abab0bc5c8b901f8770d422595f0e9d96..358d6363796f48c8c39c89c8a7ea6f589bc02fb8 100755 (executable)
@@ -9,6 +9,7 @@ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
 . ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
 
 for mode in legacy subcommands
 do
@@ -1099,6 +1100,14 @@ test_expect_success 'get --type=color with default value only' '
        test_cmp actual.raw actual-subcommand.raw
 '
 
+test_expect_success TTY 'get --type=color does not use a pager' '
+       test_config core.pager "echo foobar" &&
+       test_terminal git config get --type=color --default="red" "" >actual.raw &&
+       test_decode_color <actual.raw >actual &&
+       echo "<RED>" >expect &&
+       test_cmp expect actual
+'
+
 test_expect_success 'set --type=color' '
        cat >expect <<\EOF &&
 [foo]