All of lore.kernel.org
 help / color / mirror / Atom feed
From: Raphael Bauer <raphael@pdev.de>
To: git@vger.kernel.org
Cc: Raphael Bauer <raphael@pdev.de>
Subject: [PATCH] pretty format: fix colors on %+ placeholder newline
Date: Tue,  5 Apr 2022 17:45:29 +0200	[thread overview]
Message-ID: <20220405154529.966434-1-raphael@pdev.de> (raw)
In-Reply-To: <e8417ad5-f9f2-c1de-75f6-45be49f0011b@pdev.de>

Previously, the color escape codes were not printed again when a %+
placeholder was expanded, which could lead to missing colors.

In particular, the following command on any commit history exercised the
problem:

git log --pretty=format:'%h%Cred%+d test' --graph

The string 'test' should always be in red, but is not when commits have
ref names associated and the %+d placeholder is expanded.
This is also not a problem of any pager since the --graph option adds a
colored pipe symbol at the beginning of the line, which makes re-setting
the color again afterwards necessary.
---
 pretty.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/pretty.c b/pretty.c
index ee6114e3f0..0aabba89ca 100644
--- a/pretty.c
+++ b/pretty.c
@@ -863,6 +863,7 @@ struct format_commit_context {
 	size_t width, indent1, indent2;
 	int auto_color;
 	int padding;
+	char curr_color_escape[COLOR_MAXLEN];
 
 	/* These offsets are relative to the start of the commit message. */
 	struct chunk author;
@@ -1050,6 +1051,7 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
 		if (color_parse_mem(begin, end - begin, color) < 0)
 			die(_("unable to parse --pretty format"));
 		strbuf_addstr(sb, color);
+		strlcpy(c->curr_color_escape, color, COLOR_MAXLEN);
 		return end - placeholder + 1;
 	}
 
@@ -1067,8 +1069,10 @@ static size_t parse_color(struct strbuf *sb, /* in UTF-8 */
 	else if (skip_prefix(placeholder + 1, "reset", &rest))
 		basic_color = GIT_COLOR_RESET;
 
-	if (basic_color && want_color(c->pretty_ctx->color))
+	if (basic_color && want_color(c->pretty_ctx->color)) {
 		strbuf_addstr(sb, basic_color);
+		strlcpy(c->curr_color_escape, basic_color, COLOR_MAXLEN);
+	}
 
 	return rest - placeholder;
 }
@@ -1360,8 +1364,10 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 	case 'C':
 		if (starts_with(placeholder + 1, "(auto)")) {
 			c->auto_color = want_color(c->pretty_ctx->color);
-			if (c->auto_color && sb->len)
+			if (c->auto_color && sb->len) {
 				strbuf_addstr(sb, GIT_COLOR_RESET);
+				c->curr_color_escape[0] = 0;
+			}
 			return 7; /* consumed 7 bytes, "C(auto)" */
 		} else {
 			int ret = parse_color(sb, placeholder, c);
@@ -1813,9 +1819,11 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
 		while (sb->len && sb->buf[sb->len - 1] == '\n')
 			strbuf_setlen(sb, sb->len - 1);
 	} else if (orig_len != sb->len) {
-		if (magic == ADD_LF_BEFORE_NON_EMPTY)
+		if (magic == ADD_LF_BEFORE_NON_EMPTY) {
 			strbuf_insertstr(sb, orig_len, "\n");
-		else if (magic == ADD_SP_BEFORE_NON_EMPTY)
+			strbuf_insertstr(sb, orig_len + 1,
+				((struct format_commit_context *)context)->curr_color_escape);
+		} else if (magic == ADD_SP_BEFORE_NON_EMPTY)
 			strbuf_insertstr(sb, orig_len, " ");
 	}
 	return consumed + 1;
-- 
2.35.1


  reply	other threads:[~2022-04-05 21:50 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-21 22:38 Pretty format color bug Raphael Bauer
2022-02-21 23:29 ` Ævar Arnfjörð Bjarmason
2022-02-22  0:15   ` Raphael Bauer
2022-03-22 23:42     ` BUG: %+ format placeholder and colors Raphael Bauer
2022-03-23  2:49       ` Ævar Arnfjörð Bjarmason
2022-03-23 18:12         ` Raphael
2022-04-05 15:45           ` Raphael Bauer [this message]
2022-04-06 21:16             ` [PATCH] pretty format: fix colors on %+ placeholder newline Junio C Hamano
2022-04-08 13:18               ` Raphael
2022-04-08 15:14                 ` Ævar Arnfjörð Bjarmason
2022-04-08 23:40                 ` Junio C Hamano

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

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

  git send-email \
    --in-reply-to=20220405154529.966434-1-raphael@pdev.de \
    --to=raphael@pdev.de \
    --cc=git@vger.kernel.org \
    /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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.