git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] log --format: teach %C(auto,black) to paint it black only on terminals

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 17, 2012, 08:40 UTC
Message-ID
<7vobhtm5pk.fsf_-_@alter.siamese.dyndns.org>
In-Reply-To
<7v7gojtbgr.fsf@alter.siamese.dyndns.org>

Traditionally, %C(color attr) always emitted the ANSI color sequence; it was up to the scripts that wanted to conditionally color their output to omit %C(...) specifier when they do not want colors.

Optionally allow "auto," to be prefixed to the color, so that the output is colored iff it goes to the terminal.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * This time with minimum test and documentation.
 Documentation/pretty-formats.txt |  4 +++-
 pretty.c                         | 13 ++++++++++---
 t/t6006-rev-list-format.sh       | 30 ++++++++++++++++++++++++++----
 3 files changed, 39 insertions(+), 8 deletions(-)
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index d9edded..2af017c 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -144,7 +144,9 @@ The placeholders are:
 - '%Cgreen': switch color to green
 - '%Cblue': switch color to blue
 - '%Creset': reset color
-- '%C(...)': color specification, as described in color.branch.* config option
+- '%C(...)': color specification, as described in color.branch.* config option;
+  adding `auto,` at the beginning will emit color only when the
+  output is going to a terminal
 - '%m': left, right or boundary mark
 - '%n': newline
 - '%%': a raw '%'
diff --git a/pretty.c b/pretty.c
index dba6828..b9fd972 100644
--- a/pretty.c
+++ b/pretty.c
@@ -960,12 +960,19 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
 	switch (placeholder[0]) {
 	case 'C':
 		if (placeholder[1] == '(') {
-			const char *end = strchr(placeholder + 2, ')');
+			const char *begin = placeholder + 2;
+			const char *end = strchr(begin, ')');
 			char color[COLOR_MAXLEN];
+
 			if (!end)
 				return 0;
-			color_parse_mem(placeholder + 2,
-					end - (placeholder + 2),
+			if (!memcmp(begin, "auto,", 5)) {
+				if (!want_color(-1))
+					return end - placeholder + 1;
+				begin += 5;
+			}
+			color_parse_mem(begin,
+					end - begin,
 					"--pretty format", color);
 			strbuf_addstr(sb, color);
 			return end - placeholder + 1;
diff --git a/t/t6006-rev-list-format.sh b/t/t6006-rev-list-format.sh
index f94f0c4..bfcc1c6 100755
--- a/t/t6006-rev-list-format.sh
+++ b/t/t6006-rev-list-format.sh
@@ -11,12 +11,12 @@ touch foo && git add foo && git commit -m "added foo" &&
 '
 
 # usage: test_format name format_string <expected_output
-test_format() {
+test_format () {
 	cat >expect.$1
 	test_expect_success "format $1" "
-git rev-list --pretty=format:'$2' master >output.$1 &&
-test_cmp expect.$1 output.$1
-"
+		git rev-list --pretty=format:'$2'${3:+ $3} master >output.$1 &&
+		test_cmp expect.$1 output.$1
+	"
 }
 
 test_format percent %%h <<'EOF'
@@ -124,6 +124,28 @@ commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
 foo
 EOF
 
+test_format advanced-colors-auto \
+	'%C(auto,red yellow bold)foo%C(auto,reset)' --color <<'EOF'
+commit 131a310eb913d107dd3c09a65d1651175898735d
+foo
+commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
+foo
+EOF
+
+# %C(auto,...) should trump --color=always
+#
+# NEEDSWORK: --color=never should also be tested but we need to run a
+# similar test under pseudo-terminal with test_terminal which is too
+# much hassle for its worth.
+
+test_format advanced-colors-forced \
+	'%C(auto,red yellow bold)foo%C(auto,reset)' --color=always <<'EOF'
+commit 131a310eb913d107dd3c09a65d1651175898735d
+foo
+commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
+foo
+EOF
+
 cat >commit-msg <<'EOF'
 Test printing of complex bodies
 
-- 
1.8.1.rc2.126.ge9b7782
Previous: Junio C HamanoNext: Nguyen Thai Ngoc Duy
Message 7 of 18 in “FW: Git log --graph doesn't output color when redirected”
  1. Srb, MichalDec 12, 2012
  2. Jeff KingDec 13, 2012
  3. Srb, MichalDec 13, 2012
  4. Nguyen Thai Ngoc DuyDec 15, 2012
  5. Jeff KingDec 15, 2012
  6. Junio C HamanoDec 15, 2012
  7. log --format: teach %C(auto,black) to paint it black only on terminalsJunio C Hamano, Dec 17, 2012
  8. Nguyen Thai Ngoc DuyDec 17, 2012
  9. Jeff KingDec 17, 2012
  10. Junio C HamanoDec 17, 2012
  11. Jeff KingDec 17, 2012
  12. Junio C HamanoDec 17, 2012
  13. Jeff KingDec 17, 2012
  14. 1/2 t6006: clean up whitespaceJunio C Hamano, Dec 17, 2012
  15. Jeff KingDec 17, 2012
  16. 1/2 t6006: clean up whitespaceJeff King, Dec 17, 2012
  17. 2/2 log --format: teach %C(auto,black) to respect color configJeff King, Dec 17, 2012
  18. Jeff KingDec 17, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.