diff options
author | Jeff King <peff@peff.net> | 2017-07-13 18:09:32 +0300 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-07-13 22:42:51 +0300 |
commit | 11b087adfd469ca597f1d269314f8cad32d0d72f (patch) | |
tree | e0474bf5f8703ab737ca9fd59cae31c0813cc652 /ref-filter.c | |
parent | 18fb7ffc3dc9df081c241d6b7105b4058d5746d3 (diff) |
ref-filter: consult want_color() before emitting colors
When color placeholders like %(color:red) are used in a
ref-filter format, we unconditionally output the colors,
even if the user has asked us for no colors. This usually
isn't a problem when the user is constructing a --format on
the command line, but it means we may do the wrong thing
when the format is fed from a script or alias. For example:
$ git config alias.b 'branch --format=%(color:green)%(refname)'
$ git b --no-color
should probably omit the green color. Likewise, running:
$ git b >branches
should probably also omit the color, just as we would for
all baked-in coloring (and as we recently started to do for
user-specified colors in --pretty formats).
This commit makes both of those cases work by teaching
the ref-filter code to consult want_color() before
outputting any color. The color flag in ref_format defaults
to "-1", which means we'll consult color.ui, which in turn
defaults to the usual isatty() check on stdout. However,
callers like git-branch which support their own color config
(and command-line options) can override that.
The new tests independently cover all three of the callers
of ref-filter (for-each-ref, tag, and branch). Even though
these seem redundant, it confirms that we've correctly
plumbed through all of the necessary config to make colors
work by default.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'ref-filter.c')
-rw-r--r-- | ref-filter.c | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/ref-filter.c b/ref-filter.c index 129a636771..bc591f4f3d 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -104,6 +104,12 @@ static void color_atom_parser(const struct ref_format *format, struct used_atom die(_("expected format: %%(color:<color>)")); if (color_parse(color_value, atom->u.color) < 0) die(_("unrecognized color: %%(color:%s)"), color_value); + /* + * We check this after we've parsed the color, which lets us complain + * about syntactically bogus color names even if they won't be used. + */ + if (!want_color(format->use_color)) + color_parse("", atom->u.color); } static void refname_atom_parser_internal(struct refname_atom *atom, @@ -675,6 +681,8 @@ int verify_ref_format(struct ref_format *format) if (skip_prefix(used_atom[at].name, "color:", &color)) format->need_color_reset_at_eol = !!strcmp(color, "reset"); } + if (format->need_color_reset_at_eol && !want_color(format->use_color)) + format->need_color_reset_at_eol = 0; return 0; } |