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

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

From
Jeff King <peff@peff.net>
Date
Dec 17, 2012, 12:40 UTC
Message-ID
<20121217124029.GC21858@sigill.intra.peff.net>
In-Reply-To
<7vobhtm5pk.fsf_-_@alter.siamese.dyndns.org>
On Mon, Dec 17, 2012 at 12:40:55AM -0800, Junio C Hamano wrote:
Show 13 quoted lines
> +# %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

Hmm. I would have expected this to output colors. In other words, for "auto" to work just like the config-respecting colorization that is built into git.

Yes, in this toy example, one could always just drop the "auto" when using --color=always. But part of the point of this is that I could do:

  git config pretty.fake-oneline "%C(auto,yellow)%h%C(auto,reset) %s"

and have it behave like "--oneline", no matter what the user has in their color.ui config or --color option on the command line.

I think the patch below (on top of yours) does the right thing by copying the color option from the rev_info diff options into the pretty-print context, which is the same place that the graph and log-tree code look. That means you'll get consistent colorization (either all or nothing) with:

  git log --graph -p --format='%C(auto,blue)%s%C(auto,reset)'

no matter what your setting of color.diff, color.ui, or --color on the command line.

It also means that pretty-print defaults to "no colors, do not even check stdout" when people initialize it to all-zeroes. That's probably a good thing, as it means any callers of format_commit_message have to consciously opt into allowing colorization in their format messages.

diff --git a/commit.h b/commit.h
index b6ad8f3..0f469e5 100644
--- a/commit.h
+++ b/commit.h
@@ -89,6 +89,7 @@ struct pretty_print_context {
 	char *notes_message;
 	struct reflog_walk_info *reflog_info;
 	const char *output_encoding;
+	int color;
 };
 
 struct userformat_want {
diff --git a/log-tree.c b/log-tree.c
index 4f86def..8876c73 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -671,6 +671,7 @@ void show_log(struct rev_info *opt)
 	ctx.preserve_subject = opt->preserve_subject;
 	ctx.reflog_info = opt->reflog_info;
 	ctx.fmt = opt->commit_format;
+	ctx.color = opt->diffopt.use_color;
 	pretty_print_commit(&ctx, commit, &msgbuf);
 
 	if (opt->add_signoff)
diff --git a/pretty.c b/pretty.c
index 9e51fec..e6a2886 100644
--- a/pretty.c
+++ b/pretty.c
@@ -967,7 +967,7 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
 			if (!end)
 				return 0;
 			if (!memcmp(begin, "auto,", 5)) {
-				if (!want_color(-1))
+				if (!want_color(c->pretty_ctx->color))
 					return end - placeholder + 1;
 				begin += 5;
 			}
Previous: Jeff King
Message 18 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.