# [PATCH] format_commit_message: honor `color=auto` for `%C(auto)`

3 messages from 2016-05-27 to 2016-05-27. Participants: Edward Thomson, Jeff King, Junio C Hamano.
Thread: https://gitlist.dev/t/42464

## Edward Thomson, 2016-05-27 03:46

Subject: [PATCH] format_commit_message: honor `color=auto` for `%C(auto)`
Message-ID: <20160527034610.GA31629@zoidberg>
URL: https://gitlist.dev/e/20160527034610.GA31629%40zoidberg

```
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)`.

Signed-off-by: Edward Thomson <ethomson@edwardthomson.com>

Tests from Jeff King.

Signed-off-by: Jeff King <peff@peff.net>
---
 pretty.c                   |  2 +-
 t/t6006-rev-list-format.sh | 26 +++++++++++++++++++-------
 2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/pretty.c b/pretty.c
index 87c4497..c3ec430 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1063,7 +1063,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 	switch (placeholder[0]) {
 	case 'C':
 		if (starts_with(placeholder + 1, "(auto)")) {
-			c->auto_color = 1;
+			c->auto_color = want_color(c->pretty_ctx->color);
 			return 7; /* consumed 7 bytes, "C(auto)" */
 		} else {
 			int ret = parse_color(sb, placeholder, c);
diff --git a/t/t6006-rev-list-format.sh b/t/t6006-rev-list-format.sh
index b77d4c9..a1dcdb8 100755
--- a/t/t6006-rev-list-format.sh
+++ b/t/t6006-rev-list-format.sh
@@ -184,38 +184,38 @@ commit $head1
 [1;31;43mfoo[m
 EOF
 
-test_expect_success '%C(auto) does not enable color by default' '
+test_expect_success '%C(auto,...) does not enable color by default' '
 	git log --format=$AUTO_COLOR -1 >actual &&
 	has_no_color actual
 '
 
-test_expect_success '%C(auto) enables colors for color.diff' '
+test_expect_success '%C(auto,...) enables colors for color.diff' '
 	git -c color.diff=always log --format=$AUTO_COLOR -1 >actual &&
 	has_color actual
 '
 
-test_expect_success '%C(auto) enables colors for color.ui' '
+test_expect_success '%C(auto,...) enables colors for color.ui' '
 	git -c color.ui=always log --format=$AUTO_COLOR -1 >actual &&
 	has_color actual
 '
 
-test_expect_success '%C(auto) respects --color' '
+test_expect_success '%C(auto,...) respects --color' '
 	git log --format=$AUTO_COLOR -1 --color >actual &&
 	has_color actual
 '
 
-test_expect_success '%C(auto) respects --no-color' '
+test_expect_success '%C(auto,...) respects --no-color' '
 	git -c color.ui=always log --format=$AUTO_COLOR -1 --no-color >actual &&
 	has_no_color actual
 '
 
-test_expect_success TTY '%C(auto) respects --color=auto (stdout is tty)' '
+test_expect_success TTY '%C(auto,...) respects --color=auto (stdout is tty)' '
 	test_terminal env TERM=vt100 \
 		git log --format=$AUTO_COLOR -1 --color=auto >actual &&
 	has_color actual
 '
 
-test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
+test_expect_success '%C(auto,...) respects --color=auto (stdout not tty)' '
 	(
 		TERM=vt100 && export TERM &&
 		git log --format=$AUTO_COLOR -1 --color=auto >actual &&
@@ -223,6 +223,18 @@ test_expect_success '%C(auto) respects --color=auto (stdout not tty)' '
 	)
 '
 
+test_expect_success '%C(auto) respects --color' '
+	git log --color --format="%C(auto)%H" -1 >actual &&
+	printf "\\033[33m%s\\033[m\\n" $(git rev-parse HEAD) >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success '%C(auto) respects --no-color' '
+	git log --no-color --format="%C(auto)%H" -1 >actual &&
+	git rev-parse HEAD >expect &&
+	test_cmp expect actual
+'
+
 iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
 Test printing of complex bodies
 
-- 
2.7.4 (Apple Git-66)

```

## Jeff King, 2016-05-27 03:55

Subject: Re: [PATCH] format_commit_message: honor `color=auto` for `%C(auto)`
Message-ID: <20160527035553.GA24972@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20160527035553.GA24972%40sigill.intra.peff.net
In-Reply-To: <20160527034610.GA31629@zoidberg>

```
On Thu, May 26, 2016 at 10:46:10PM -0500, Edward Thomson wrote:

> 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)`.

Explanation and the patch look good.

> Signed-off-by: Edward Thomson <ethomson@edwardthomson.com>
> 
> Tests from Jeff King.
> 
> Signed-off-by: Jeff King <peff@peff.net>

Trailers should all go at the bottom in a single stanza, and should
generally be in chronological order (so you got the bits from with an
s-o-b, and then you signed off the whole thing). IOW:

> Tests from Jeff King.
>
> Signed-off-by: Jeff King <peff@peff.net>
> Signed-off-by: Edward Thomson <ethomson@edwardthomson.com>

I suspect Junio can just tweak that while applying, unless there's
another reason to re-roll.

(Also for anybody watching, Ed did not just make up my signoff; I gave
it to him off-list).

-Peff

```

## Junio C Hamano, 2016-05-27 06:22

Subject: Re: [PATCH] format_commit_message: honor `color=auto` for `%C(auto)`
Message-ID: <xmqqwpmgrpeb.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqwpmgrpeb.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <20160527035553.GA24972@sigill.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> I suspect Junio can just tweak that while applying, unless there's
> another reason to re-roll.
>
> (Also for anybody watching, Ed did not just make up my signoff; I gave
> it to him off-list).

Understood.  Thanks.

```
