Skip to content

Commit 776d6fb

Browse files
peffgitster
authored andcommitted
add-interactive: manually fall back color config to color.ui
Color options like color.interactive and color.diff should fall back to the value of color.ui if they aren't set. In add-interactive, we check the specific options (e.g., color.diff) via repo_config_get_value(), which does not depend on the main command having loaded any color config via the git_config() callback mechanism. But then we call want_color() on the result; if our specific config is unset then that function uses the value of git_use_color_default. That variable is typically set from color.ui by the git_color_config() callback, which is called by the main command in its own git_config() callback function. This works fine for "add -p", whose add_config() callback calls into git_color_config(). But it doesn't work for other commands like "checkout -p", which is otherwise unaware of color at all. People tend not to notice because the default is "auto", and that's what they'd set color.ui to as well. But something like: git -c color.ui=false checkout -p should disable color, and it doesn't. This regression goes back to 0527ccb (add -i: default to the built-in implementation, 2021-11-30). In the perl version we got the color config from "git config --get-colorbool", which did the full lookup for us. The obvious fix is for git-checkout to add a call to git_color_config() to its own config callback. But we'd have to do so for every command with this problem, which is error-prone. Let's see if we can fix it more centrally. It is tempting to teach want_color() to look up the value of repo_config_get_value("color.ui") itself. But I think that would have disastrous consequences. Plumbing commands, especially older ones, avoid porcelain config like "color.*" by simply not parsing it in their config callbacks. Looking up the value of color.ui under the hood would undermine that. Instead, let's do that lookup in the add-interactive setup code. We're already demand-loading other color config there, which is probably fine (even in a plumbing command like "git reset", the interactive mode is inherently porcelain-ish). That catches all commands that use the interactive code, whether they were calling git_color_config() themselves or not. Reported-by: Isaac Oscar Gariano <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8c78b5c commit 776d6fb

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

add-interactive.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ static int check_color_config(struct repository *r, const char *var)
4545
ret = -1;
4646
else
4747
ret = git_config_colorbool(var, value);
48+
49+
/*
50+
* Do not rely on want_color() to fall back to color.ui for us. It uses
51+
* the value parsed by git_color_config(), which may not have been
52+
* called by the main command.
53+
*/
54+
if (ret < 0 && !repo_config_get_value(r, "color.ui", &value))
55+
ret = git_config_colorbool("color.ui", value);
56+
4857
return want_color(ret);
4958
}
5059

t/t3701-add-interactive.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,12 @@ test_expect_success 'stash accepts -U and --inter-hunk-context' '
13211321
test_grep "@@ -2,20 +2,20 @@" actual
13221322
'
13231323

1324+
test_expect_success 'set up base for -p color tests' '
1325+
echo commit >file &&
1326+
git commit -am "commit state" &&
1327+
git tag patch-base
1328+
'
1329+
13241330
for cmd in add checkout commit reset restore "stash save" "stash push"
13251331
do
13261332
test_expect_success "$cmd rejects invalid context options" '
@@ -1337,6 +1343,15 @@ do
13371343
test_must_fail git $cmd --inter-hunk-context 2 2>actual &&
13381344
test_grep -E ".--inter-hunk-context. requires .(--interactive/)?--patch." actual
13391345
'
1346+
1347+
test_expect_success "$cmd falls back to color.ui" '
1348+
git reset --hard patch-base &&
1349+
echo working-tree >file &&
1350+
test_write_lines y |
1351+
force_color git -c color.ui=false $cmd -p >output.raw 2>&1 &&
1352+
test_decode_color <output.raw >output &&
1353+
test_cmp output.raw output
1354+
'
13401355
done
13411356

13421357
test_done

0 commit comments

Comments
 (0)