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 136c8c8b8f (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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2017-10-03 09:46:06 -04:00 committed by Junio C Hamano
parent 0c88bf5050
commit 6be4595edb
3 changed files with 28 additions and 19 deletions

View File

@ -1052,10 +1052,10 @@ clean.requireForce::
color.branch:: color.branch::
A boolean to enable/disable color in the output of A boolean to enable/disable color in the output of
linkgit:git-branch[1]. May be set to `always`, linkgit:git-branch[1]. May be set to `false` (or `never`) to
`false` (or `never`) or `auto` (or `true`), in which case colors are used disable color entirely, `auto` (or `true` or `always`) in which
only when the output is to a terminal. If unset, then the case colors are used only when the output is to a terminal. If
value of `color.ui` is used (`auto` by default). unset, then the value of `color.ui` is used (`auto` by default).
color.branch.<slot>:: color.branch.<slot>::
Use customized color for branch coloration. `<slot>` is one of Use customized color for branch coloration. `<slot>` is one of
@ -1066,12 +1066,11 @@ color.branch.<slot>::
color.diff:: color.diff::
Whether to use ANSI escape sequences to add color to patches. Whether to use ANSI escape sequences to add color to patches.
If this is set to `always`, linkgit:git-diff[1], If this is set to `true` or `auto`, linkgit:git-diff[1],
linkgit:git-log[1], and linkgit:git-show[1] will use color linkgit:git-log[1], and linkgit:git-show[1] will use color
for all patches. If it is set to `true` or `auto`, those when output is to the terminal. The value `always` is a
commands will only use color when output is to the terminal. historical synonym for `auto`. If unset, then the value of
If unset, then the value of `color.ui` is used (`auto` by `color.ui` is used (`auto` by default).
default).
+ +
This does not affect linkgit:git-format-patch[1] or the This does not affect linkgit:git-format-patch[1] or the
'git-diff-{asterisk}' plumbing commands. Can be overridden on the 'git-diff-{asterisk}' plumbing commands. Can be overridden on the
@ -1124,12 +1123,12 @@ color.grep.<slot>::
-- --
color.interactive:: color.interactive::
When set to `always`, always use colors for interactive prompts When set to `true` or `auto`, use colors for interactive prompts
and displays (such as those used by "git-add --interactive" and and displays (such as those used by "git-add --interactive" and
"git-clean --interactive"). When false (or `never`), never. "git-clean --interactive") when the output is to the terminal.
When set to `true` or `auto`, use colors only when the output is When false (or `never`), never show colors. The value `always`
to the terminal. If unset, then the value of `color.ui` is is a historical synonym for `auto`. If unset, then the value of
used (`auto` by default). `color.ui` is used (`auto` by default).
color.interactive.<slot>:: color.interactive.<slot>::
Use customized color for 'git add --interactive' and 'git clean Use customized color for 'git add --interactive' and 'git clean
@ -1176,10 +1175,10 @@ color.ui::
configuration to set a default for the `--color` option. Set it configuration to set a default for the `--color` option. Set it
to `false` or `never` if you prefer Git commands not to use to `false` or `never` if you prefer Git commands not to use
color unless enabled explicitly with some other configuration color unless enabled explicitly with some other configuration
or the `--color` option. Set it to `always` if you want all or the `--color` option. Set it to `true` or `auto` to enable
output not intended for machine consumption to use color, to color when output is written to the terminal (this is also the
`true` or `auto` (this is the default since Git 1.8.4) if you default since Git 1.8.4). The value `always` is a historical
want such output to use color when written to the terminal. synonym for `auto`.
column.ui:: column.ui::
Specify whether supported commands should output in columns. Specify whether supported commands should output in columns.

View File

@ -308,7 +308,7 @@ int git_config_colorbool(const char *var, const char *value)
if (!strcasecmp(value, "never")) if (!strcasecmp(value, "never"))
return 0; return 0;
if (!strcasecmp(value, "always")) if (!strcasecmp(value, "always"))
return 1; return var ? GIT_COLOR_AUTO : 1;
if (!strcasecmp(value, "auto")) if (!strcasecmp(value, "auto"))
return GIT_COLOR_AUTO; return GIT_COLOR_AUTO;
} }

View File

@ -483,4 +483,14 @@ test_expect_success 'hunk-editing handles custom comment char' '
git diff --exit-code git diff --exit-code
' '
test_expect_success 'add -p works even with color.ui=always' '
git reset --hard &&
echo change >>file &&
test_config color.ui always &&
echo y | git add -p &&
echo file >expect &&
git diff --cached --name-only >actual &&
test_cmp expect actual
'
test_done test_done