Skip to content

Commit 6be4595

Browse files
peffgitster
authored andcommitted
color: make "always" the same as "auto" in config
It can be handy to use `--color=always` (or it's synonym `--color`) on the command-line to convince a command to produce color even if it's stdout isn't going to the terminal or a pager. What's less clear is whether it makes sense to set config variables like color.ui to `always`. For a one-shot like: git -c color.ui=always ... it's potentially useful (especially if the command doesn't directly support the `--color` option). But setting `always` in your on-disk config is much muddier, as you may be surprised when piped commands generate colors (and send them to whatever is consuming the pipe downstream). Some people have done this anyway, because: 1. The documentation for color.ui makes it sound like using `always` is a good idea, when you almost certainly want `auto`. 2. Traditionally not every command (and especially not plumbing) respected color.ui in the first place. So the confusion came up less frequently than it might have. The situation changed in 136c8c8 (color: check color.ui in git_default_config(), 2017-07-13), which negated point (2): now scripts using only plumbing commands (like add-interactive) are broken by this setting. That commit was fixing real issues (e.g., by making `color.ui=never` work, since `auto` is the default), so we don't want to just revert it. We could turn `always` into a noop in plumbing commands, but that creates a hard-to-explain inconsistency between the plumbing and other commands. Instead, let's just turn `always` into `auto` for all config. This does break the "one-shot" config shown above, but again, we're probably better to have simple and consistent rules than to try to special-case command-line config. There is one place where `always` should retain its meaning: on the command line, `--color=always` should continue to be the same as `--color`, overriding any isatty checks. Since the command-line parser also depends on git_config_colorbool(), we can use the existence of the "var" string to deterine whether we are serving the command-line or the config. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0c88bf5 commit 6be4595

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

Documentation/config.txt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,10 +1052,10 @@ clean.requireForce::
10521052

10531053
color.branch::
10541054
A boolean to enable/disable color in the output of
1055-
linkgit:git-branch[1]. May be set to `always`,
1056-
`false` (or `never`) or `auto` (or `true`), in which case colors are used
1057-
only when the output is to a terminal. If unset, then the
1058-
value of `color.ui` is used (`auto` by default).
1055+
linkgit:git-branch[1]. May be set to `false` (or `never`) to
1056+
disable color entirely, `auto` (or `true` or `always`) in which
1057+
case colors are used only when the output is to a terminal. If
1058+
unset, then the value of `color.ui` is used (`auto` by default).
10591059

10601060
color.branch.<slot>::
10611061
Use customized color for branch coloration. `<slot>` is one of
@@ -1066,12 +1066,11 @@ color.branch.<slot>::
10661066

10671067
color.diff::
10681068
Whether to use ANSI escape sequences to add color to patches.
1069-
If this is set to `always`, linkgit:git-diff[1],
1069+
If this is set to `true` or `auto`, linkgit:git-diff[1],
10701070
linkgit:git-log[1], and linkgit:git-show[1] will use color
1071-
for all patches. If it is set to `true` or `auto`, those
1072-
commands will only use color when output is to the terminal.
1073-
If unset, then the value of `color.ui` is used (`auto` by
1074-
default).
1071+
when output is to the terminal. The value `always` is a
1072+
historical synonym for `auto`. If unset, then the value of
1073+
`color.ui` is used (`auto` by default).
10751074
+
10761075
This does not affect linkgit:git-format-patch[1] or the
10771076
'git-diff-{asterisk}' plumbing commands. Can be overridden on the
@@ -1124,12 +1123,12 @@ color.grep.<slot>::
11241123
--
11251124

11261125
color.interactive::
1127-
When set to `always`, always use colors for interactive prompts
1126+
When set to `true` or `auto`, use colors for interactive prompts
11281127
and displays (such as those used by "git-add --interactive" and
1129-
"git-clean --interactive"). When false (or `never`), never.
1130-
When set to `true` or `auto`, use colors only when the output is
1131-
to the terminal. If unset, then the value of `color.ui` is
1132-
used (`auto` by default).
1128+
"git-clean --interactive") when the output is to the terminal.
1129+
When false (or `never`), never show colors. The value `always`
1130+
is a historical synonym for `auto`. If unset, then the value of
1131+
`color.ui` is used (`auto` by default).
11331132

11341133
color.interactive.<slot>::
11351134
Use customized color for 'git add --interactive' and 'git clean
@@ -1176,10 +1175,10 @@ color.ui::
11761175
configuration to set a default for the `--color` option. Set it
11771176
to `false` or `never` if you prefer Git commands not to use
11781177
color unless enabled explicitly with some other configuration
1179-
or the `--color` option. Set it to `always` if you want all
1180-
output not intended for machine consumption to use color, to
1181-
`true` or `auto` (this is the default since Git 1.8.4) if you
1182-
want such output to use color when written to the terminal.
1178+
or the `--color` option. Set it to `true` or `auto` to enable
1179+
color when output is written to the terminal (this is also the
1180+
default since Git 1.8.4). The value `always` is a historical
1181+
synonym for `auto`.
11831182

11841183
column.ui::
11851184
Specify whether supported commands should output in columns.

color.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ int git_config_colorbool(const char *var, const char *value)
308308
if (!strcasecmp(value, "never"))
309309
return 0;
310310
if (!strcasecmp(value, "always"))
311-
return 1;
311+
return var ? GIT_COLOR_AUTO : 1;
312312
if (!strcasecmp(value, "auto"))
313313
return GIT_COLOR_AUTO;
314314
}

t/t3701-add-interactive.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,14 @@ test_expect_success 'hunk-editing handles custom comment char' '
483483
git diff --exit-code
484484
'
485485

486+
test_expect_success 'add -p works even with color.ui=always' '
487+
git reset --hard &&
488+
echo change >>file &&
489+
test_config color.ui always &&
490+
echo y | git add -p &&
491+
echo file >expect &&
492+
git diff --cached --name-only >actual &&
493+
test_cmp expect actual
494+
'
495+
486496
test_done

0 commit comments

Comments
 (0)