git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: "git@vger.kernel.org" <git@vger.kernel.org>,
	Nguyen Thai Ngoc Duy <pclouds@gmail.com>,
	"Srb, Michal" <michal.srb11@imperial.ac.uk>
Subject: Re: [PATCH] log --format: teach %C(auto,black) to paint it black only on terminals
Date: Mon, 17 Dec 2012 07:40:29 -0500	[thread overview]
Message-ID: <20121217124029.GC21858@sigill.intra.peff.net> (raw)
In-Reply-To: <7vobhtm5pk.fsf_-_@alter.siamese.dyndns.org>

On Mon, Dec 17, 2012 at 12:40:55AM -0800, Junio C Hamano wrote:

> +# %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;
 			}

      parent reply	other threads:[~2012-12-17 12:40 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <72BB37CB88C48F4B925365539F1EE46C182613A9@icexch-m3.ic.ac.uk>
2012-12-12 17:35 ` FW: Git log --graph doesn't output color when redirected Srb, Michal
2012-12-13 13:13   ` Jeff King
2012-12-13 15:34     ` Srb, Michal
2012-12-15  3:23     ` Nguyen Thai Ngoc Duy
2012-12-15 10:16       ` Jeff King
2012-12-15 18:30         ` Junio C Hamano
2012-12-17  8:40           ` [PATCH] log --format: teach %C(auto,black) to paint it black only on terminals Junio C Hamano
2012-12-17 11:44             ` Nguyen Thai Ngoc Duy
2012-12-17 12:13               ` Jeff King
2012-12-17 19:34                 ` Junio C Hamano
2012-12-17 19:49                   ` Jeff King
2012-12-17 20:03                     ` Junio C Hamano
2012-12-17 22:55                       ` Jeff King
2012-12-17 22:55                         ` [PATCH 1/2] t6006: clean up whitespace Junio C Hamano
2012-12-17 22:59                           ` Jeff King
2012-12-17 22:56                         ` Jeff King
2012-12-17 22:56                         ` [PATCH 2/2] log --format: teach %C(auto,black) to respect color config Jeff King
2012-12-17 12:40             ` Jeff King [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20121217124029.GC21858@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=michal.srb11@imperial.ac.uk \
    --cc=pclouds@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).