pretty: respect color settings for %C placeholders

The color placeholders have traditionally been unconditional, showing colors even when git is not otherwise configured to do so. This was not so bad for their original use, which was on the command-line (and the user could decide at that moment whether to add colors or not). But these days we have configured formats via pretty.*, and those should operate correctly in multiple contexts. In 3082517 (log --format: teach %C(auto,black) to respect color config, 2012-12-17), we gave an extended placeholder that could be used to accomplish this. But it's rather clunky to use, because you have to specify it individually for each color (and their matching resets) in the format. We shied away from just switching the default to auto, because it is technically breaking backwards compatibility. However, there's not really a use case for unconditional colors. The most plausible reason you would want them is to redirect "git log" output to a file. But there, the right answer is --color=always, as it does the right thing both with custom user-format colors and git-generated colors. So let's switch to the more useful default. In the off-chance that somebody really does find a use for unconditional colors without wanting to enable the rest of git's colors, we provide a new %C(always,...) to enable the old behavior. And we can remind them of --color=always in the documentation. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 13, 2017 at 11:08 UTC 18fb7ffc3dc9df081c241d6b7105b4058d5746d3
3 files changed +84 -45
Documentation/pretty-formats.txt
+11 -7
@@ -173,13 +173,17 @@ endif::git-rev-list[]
173 - '%Cblue': switch color to blue
174 - '%Creset': reset color
175 - '%C(...)': color specification, as described under Values in the
176 - "CONFIGURATION FILE" section of linkgit:git-config[1];
177 - adding `auto,` at the beginning (e.g. `%C(auto,red)`) will emit
178 - color only when colors are enabled for log output (by `color.diff`,
179 - `color.ui`, or `--color`, and respecting the `auto` settings of the
180 - former if we are going to a terminal). `auto` alone (i.e.
181 - `%C(auto)`) will turn on auto coloring on the next placeholders
182 - until the color is switched again.
176 + "CONFIGURATION FILE" section of linkgit:git-config[1].
177 + By default, colors are shown only when enabled for log output (by
178 + `color.diff`, `color.ui`, or `--color`, and respecting the `auto`
179 + settings of the former if we are going to a terminal). `%C(auto,...)`
180 + is accepted as a historical synonym for the default (e.g.,
181 + `%C(auto,red)`). Specifying `%C(always,...) will show the colors
182 + even when color is not otherwise enabled (though consider
183 + just using `--color=always` to enable color for the whole output,
184 + including this format and anything else git might color). `auto`
185 + alone (i.e. `%C(auto)`) will turn on auto coloring on the next
186 + placeholders until the color is switched again.
187 - '%m': left (`<`), right (`>`) or boundary (`-`) mark
188 - '%n': newline
189 - '%%': a raw '%'
pretty.c
+23 -4
@@ -947,6 +947,7 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
947 struct format_commit_context *c)
948 {
949 const char *rest = placeholder;
950 + const char *basic_color = NULL;
951
952 if (placeholder[1] == '(') {
953 const char *begin = placeholder + 2;
@@ -955,23 +956,41 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
956
957 if (!end)
958 return 0;
959 +
960 if (skip_prefix(begin, "auto,", &begin)) {
961 if (!want_color(c->pretty_ctx->color))
962 return end - placeholder + 1;
963 + } else if (skip_prefix(begin, "always,", &begin)) {
964 + /* nothing to do; we do not respect want_color at all */
965 + } else {
966 + /* the default is the same as "auto" */
967 + if (!want_color(c->pretty_ctx->color))
968 + return end - placeholder + 1;
969 }
970 +
971 if (color_parse_mem(begin, end - begin, color) < 0)
972 die(_("unable to parse --pretty format"));
973 strbuf_addstr(sb, color);
974 return end - placeholder + 1;
975 }
976 +
977 + /*
978 + * We handle things like "%C(red)" above; for historical reasons, there
979 + * are a few colors that can be specified without parentheses (and
980 + * they cannot support things like "auto" or "always" at all).
981 + */
982 if (skip_prefix(placeholder + 1, "red", &rest))
968 - strbuf_addstr(sb, GIT_COLOR_RED);
983 + basic_color = GIT_COLOR_RED;
984 else if (skip_prefix(placeholder + 1, "green", &rest))
970 - strbuf_addstr(sb, GIT_COLOR_GREEN);
985 + basic_color = GIT_COLOR_GREEN;
986 else if (skip_prefix(placeholder + 1, "blue", &rest))
972 - strbuf_addstr(sb, GIT_COLOR_BLUE);
987 + basic_color = GIT_COLOR_BLUE;
988 else if (skip_prefix(placeholder + 1, "reset", &rest))
974 - strbuf_addstr(sb, GIT_COLOR_RESET);
989 + basic_color = GIT_COLOR_RESET;
990 +
991 + if (basic_color && want_color(c->pretty_ctx->color))
992 + strbuf_addstr(sb, basic_color);
993 +
994 return rest - placeholder;
995 }
996
t/t6006-rev-list-format.sh
+50 -34
@@ -59,7 +59,10 @@ test_format () {
59 }
60
61 # Feed to --format to provide predictable colored sequences.
62 +BASIC_COLOR='%Credfoo%Creset'
63 +COLOR='%C(red)foo%C(reset)'
64 AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
65 +ALWAYS_COLOR='%C(always,red)foo%C(always,reset)'
66 has_color () {
67 test_decode_color <"$1" >decoded &&
68 echo "<RED>foo<RESET>" >expect &&
@@ -177,7 +180,7 @@ test_expect_success 'basic colors' '
180 <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
181 EOF
182 format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
180 - git rev-list --format="$format" -1 master >actual.raw &&
183 + git rev-list --color --format="$format" -1 master >actual.raw &&
184 test_decode_color <actual.raw >actual &&
185 test_cmp expect actual
186 '
@@ -188,48 +191,61 @@ test_expect_success 'advanced colors' '
191 <BOLD;RED;BYELLOW>foo<RESET>
192 EOF
193 format="%C(red yellow bold)foo%C(reset)" &&
191 - git rev-list --format="$format" -1 master >actual.raw &&
194 + git rev-list --color --format="$format" -1 master >actual.raw &&
195 test_decode_color <actual.raw >actual &&
196 test_cmp expect actual
197 '
198
196 -test_expect_success '%C(auto,...) does not enable color by default' '
197 - git log --format=$AUTO_COLOR -1 >actual &&
198 - has_no_color actual
199 -'
200 -
201 -test_expect_success '%C(auto,...) enables colors for color.diff' '
202 - git -c color.diff=always log --format=$AUTO_COLOR -1 >actual &&
203 - has_color actual
204 -'
205 -
206 -test_expect_success '%C(auto,...) enables colors for color.ui' '
207 - git -c color.ui=always log --format=$AUTO_COLOR -1 >actual &&
208 - has_color actual
209 -'
199 +for spec in \
200 + "%Cred:$BASIC_COLOR" \
201 + "%C(...):$COLOR" \
202 + "%C(auto,...):$AUTO_COLOR"
203 +do
204 + desc=${spec%%:*}
205 + color=${spec#*:}
206 + test_expect_success "$desc does not enable color by default" '
207 + git log --format=$color -1 >actual &&
208 + has_no_color actual
209 + '
210
211 -test_expect_success '%C(auto,...) respects --color' '
212 - git log --format=$AUTO_COLOR -1 --color >actual &&
213 - has_color actual
214 -'
211 + test_expect_success "$desc enables colors for color.diff" '
212 + git -c color.diff=always log --format=$color -1 >actual &&
213 + has_color actual
214 + '
215
216 -test_expect_success '%C(auto,...) respects --no-color' '
217 - git -c color.ui=always log --format=$AUTO_COLOR -1 --no-color >actual &&
218 - has_no_color actual
219 -'
216 + test_expect_success "$desc enables colors for color.ui" '
217 + git -c color.ui=always log --format=$color -1 >actual &&
218 + has_color actual
219 + '
220
221 -test_expect_success TTY '%C(auto,...) respects --color=auto (stdout is tty)' '
222 - test_terminal env TERM=vt100 \
223 - git log --format=$AUTO_COLOR -1 --color=auto >actual &&
224 - has_color actual
225 -'
221 + test_expect_success "$desc respects --color" '
222 + git log --format=$color -1 --color >actual &&
223 + has_color actual
224 + '
225
227 -test_expect_success '%C(auto,...) respects --color=auto (stdout not tty)' '
228 - (
229 - TERM=vt100 && export TERM &&
230 - git log --format=$AUTO_COLOR -1 --color=auto >actual &&
226 + test_expect_success "$desc respects --no-color" '
227 + git -c color.ui=always log --format=$color -1 --no-color >actual &&
228 has_no_color actual
232 - )
229 + '
230 +
231 + test_expect_success TTY "$desc respects --color=auto (stdout is tty)" '
232 + test_terminal env TERM=vt100 \
233 + git log --format=$color -1 --color=auto >actual &&
234 + has_color actual
235 + '
236 +
237 + test_expect_success "$desc respects --color=auto (stdout not tty)" '
238 + (
239 + TERM=vt100 && export TERM &&
240 + git log --format=$color -1 --color=auto >actual &&
241 + has_no_color actual
242 + )
243 + '
244 +done
245 +
246 +test_expect_success '%C(always,...) enables color even without tty' '
247 + git log --format=$ALWAYS_COLOR -1 >actual &&
248 + has_color actual
249 '
250
251 test_expect_success '%C(auto) respects --color' '