format_commit_message: honor `color=auto` for `%C(auto)`

git-log(1) documents that when specifying the `%C(auto)` format placeholder will "turn on auto coloring on the next %placeholders until the color is switched again." However, when `%C(auto)` is used, the present implementation will turn colors on unconditionally (even if the color configuration is turned off for the current context - for example, `--no-color` was specified or the color is `auto` and the output is not a tty). Update `format_commit_one` to examine the current context when a format string of `%C(auto)` is specified, which ensures that we will not unconditionally write colors. This brings that behavior in line with the behavior of `%C(auto,<colorname>)`, and allows the user the ability to specify that color should be displayed only when the output is a tty. Additionally, add a test for `%C(auto)` and update the existing tests for `%C(auto,...)` as they were misidentified as being applicable to `%C(auto)`. Tests from Jeff King. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Edward Thomson <ethomson@edwardthomson.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Edward Thomson committed May 26, 2016 at 22:46 UTC b15a3e005af07843116c73205742adfbab3d2e82
2 files changed +20 -8
pretty.c
+1 -1
@@ -1061,7 +1061,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1061 switch (placeholder[0]) {
1062 case 'C':
1063 if (starts_with(placeholder + 1, "(auto)")) {
1064 - c->auto_color = 1;
1064 + c->auto_color = want_color(c->pretty_ctx->color);
1065 return 7; /* consumed 7 bytes, "C(auto)" */
1066 } else {
1067 int ret = parse_color(sb, placeholder, c);
t/t6006-rev-list-format.sh
+19 -7
@@ -184,38 +184,38 @@ commit $head1
184 foo
185 EOF
186
187 -test_expect_success '%C(auto) does not enable color by default' '
187 +test_expect_success '%C(auto,...) does not enable color by default' '
188 git log --format=$AUTO_COLOR -1 >actual &&
189 has_no_color actual
190 '
191
192 -test_expect_success '%C(auto) enables colors for color.diff' '
192 +test_expect_success '%C(auto,...) enables colors for color.diff' '
193 git -c color.diff=always log --format=$AUTO_COLOR -1 >actual &&
194 has_color actual
195 '
196
197 -test_expect_success '%C(auto) enables colors for color.ui' '
197 +test_expect_success '%C(auto,...) enables colors for color.ui' '
198 git -c color.ui=always log --format=$AUTO_COLOR -1 >actual &&
199 has_color actual
200 '
201
202 -test_expect_success '%C(auto) respects --color' '
202 +test_expect_success '%C(auto,...) respects --color' '
203 git log --format=$AUTO_COLOR -1 --color >actual &&
204 has_color actual
205 '
206
207 -test_expect_success '%C(auto) respects --no-color' '
207 +test_expect_success '%C(auto,...) respects --no-color' '
208 git -c color.ui=always log --format=$AUTO_COLOR -1 --no-color >actual &&
209 has_no_color actual
210 '
211
212 -test_expect_success TTY '%C(auto) respects --color=auto (stdout is tty)' '
212 +test_expect_success TTY '%C(auto,...) respects --color=auto (stdout is tty)' '
213 test_terminal env TERM=vt100 \
214 git log --format=$AUTO_COLOR -1 --color=auto >actual &&
215 has_color actual
216 '
217
218 -test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
218 +test_expect_success '%C(auto,...) respects --color=auto (stdout not tty)' '
219 (
220 TERM=vt100 && export TERM &&
221 git log --format=$AUTO_COLOR -1 --color=auto >actual &&
@@ -223,6 +223,18 @@ test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
223 )
224 '
225
226 +test_expect_success '%C(auto) respects --color' '
227 + git log --color --format="%C(auto)%H" -1 >actual &&
228 + printf "\\033[33m%s\\033[m\\n" $(git rev-parse HEAD) >expect &&
229 + test_cmp expect actual
230 +'
231 +
232 +test_expect_success '%C(auto) respects --no-color' '
233 + git log --no-color --format="%C(auto)%H" -1 >actual &&
234 + git rev-parse HEAD >expect &&
235 + test_cmp expect actual
236 +'
237 +
238 iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
239 Test printing of complex bodies
240