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

[PATCH v3] pretty: fix lost and leaked colors at line breaks

From
Andy Koppe <andy.koppe@gmail.com>
Date
Nov 17, 2026, 09:10 UTC
Message-ID
<e9f88b533dfbd10528c9151e2e87207419d0253d.1794905511.git.andy.koppe@gmail.com>
In-Reply-To
<2f9f835e3f99d7de5bbf51be2e8ef4dc0485a46e.1794778250.git.andy.koppe@gmail.com>

Colors in custom log formats remain in effect until reset, including across line breaks. Lines therefore don't necessarily set their own colors, nor reset them at their end, which leads to two problems.

Firstly, lines that continue colors from preceding lines lose them when --graph edges or color-aware pagers such as less reset colors at line breaks. For example, "%C(bold)%w(72)%s" then only makes the first line of a wrapped subject bold, and "%C(cyan)%b" only colors the first line of the message body.

Secondly, colors that are still active at the end of a line can leak into what follows. For example, with --graph, "%C(red)%s" also colors a '*' commit marker at the start of the next line, because unlike the '|', '/' and '\' edges, it isn't colored. Attributes such as bold still leak into the edges, though, as the graph only resets colors after each one.

The default log formats as well as git grep and diff avoid both problems by ensuring that the coloring of each line is self-contained: they do not rely on colors carrying across line breaks and always reset colors at the end of each line. Custom formats can do the same at %n line breaks, but not within placeholders that expand to multiple lines.

Fix both problems by automating this for custom formats. When color output is enabled, scan their output for color escape sequences, collecting those since the last reset. If any are active at a newline, insert a reset before it, and replay them at the start of the next non-empty line, unless that begins with a reset. Also append a reset at the end if any colors are still active there.

Doing this in repo_format_commit_message() covers all color sequences and line breaks, whether they come from placeholders, auto colors, manual %x1b sequences, or message content.

Resets are only recognized as the first code of a sequence, which is where git puts them. A reset later in a sequence would cancel the codes before it, so it is unlikely to be used deliberately, and missing one merely adds to the replayed sequences.

To keep overhead to a minimum, avoid buffer inserts and use memchr() and strcspn() to skip to escapes and newlines. If there are no escapes, the only cost is an unsuccessful memchr() call. The buffer is only copied if colors are active at a line break.

Formats that have come to rely on --graph or a pager resetting colors at line breaks will need explicit resets, which they already require when neither is used.

Signed-off-by: Andy Koppe <andy.koppe@gmail.com>
---
Sorry, this code keeps on bouncing around my head, so here's one more
tweak to try to make it clearer. (The algorithm has been annoyingly
difficult to pin down considering it's a pretty simple idea.)
- Combine the reset and replay conditionals.
- Rename the reset variables, for consistency with 'replay'.
 pretty.c                      | 102 ++++++++++++++++++++++++++++++++++
 t/t4205-log-pretty-formats.sh |  80 ++++++++++++++++++++++++++
 2 files changed, 182 insertions(+)
diff --git a/pretty.c b/pretty.c
index d8a9f370f6..78be2ae034 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1997,6 +1997,104 @@ void userformat_find_requirements(const char *fmt, struct userformat_want *w)
 	}
 }
 
+/*
+ * Make the coloring of each line in 'sb' from offset 'start' onwards
+ * self-contained, so that it survives --graph and color-aware pagers such as
+ * 'less -R' resetting colors at line breaks, and doesn't leak into what
+ * follows, for example the graph.
+ *
+ * Color escape sequences since the last reset are collected, and if any are
+ * active at a newline, a reset is inserted before it. They are replayed at the
+ * start of the next non-empty line, unless that begins with a reset. A reset is
+ * also appended at the end if colors are still active there.
+ *
+ * Resets, which have a zero or empty code, are only recognized at the start of
+ * a sequence. They make little sense elsewhere, and missing one merely adds to
+ * the replayed sequences.
+ */
+static void make_line_colors_self_contained(struct strbuf *sb, size_t start)
+{
+	struct strbuf colors = STRBUF_INIT; /* colors since last reset */
+	struct strbuf tmp = STRBUF_INIT;
+
+	const char *copied = sb->buf;
+	const char *sb_end = sb->buf + sb->len;
+	const char *p = sb->buf + start;
+
+	while (p < sb_end) {
+		size_t esc_len;
+		bool replay = false, reset, reset_only;
+		char *reset_end;
+
+		if (!colors.len) {
+			/* look for colors to collect */
+			p = memchr(p, '\033', sb_end - p);
+			if (!p)
+				break;
+		} else {
+			/* look for more colors, the line end, or a NUL */
+			p += strcspn(p, "\033\n");
+
+			if (*p == '\n') {
+				if (!tmp.len) {
+					/* make room for resets and replays */
+					strbuf_grow(&tmp, sb->len * 3 / 2);
+				}
+
+				/* reset active colors at line end */
+				strbuf_add(&tmp, copied, p - copied);
+				copied = p;
+				strbuf_addstr(&tmp, GIT_COLOR_RESET);
+
+				while (*++p == '\n')
+					; /* skip empty lines */
+
+				if (p == sb_end) {
+					/* no line left to replay colors on */
+					strbuf_reset(&colors);
+					break;
+				}
+
+				replay = true;
+			}
+		}
+
+		esc_len = display_mode_esc_sequence_len(p);
+		reset = esc_len && !strtoul(p + 2, &reset_end, 10);
+		reset_only = reset && *reset_end == 'm';
+
+		if (reset) {
+			strbuf_reset(&colors);
+		} else if (replay) {
+			strbuf_add(&tmp, copied, p - copied);
+			copied = p;
+			strbuf_addbuf(&tmp, &colors);
+		}
+
+		if (esc_len) {
+			/* collect the sequence if it's more than a reset */
+			if (!reset_only)
+				strbuf_add(&colors, p, esc_len);
+			p += esc_len;
+		} else {
+			/* skip anything else, including NULs */
+			p++;
+		}
+	}
+
+	if (tmp.len) {
+		strbuf_add(&tmp, copied, sb_end - copied);
+		strbuf_swap(sb, &tmp);
+	}
+
+	/* reset active colors at buffer end */
+	if (colors.len)
+		strbuf_addstr(sb, GIT_COLOR_RESET);
+
+	strbuf_release(&tmp);
+	strbuf_release(&colors);
+}
+
 void repo_format_commit_message(struct repository *r,
 				const struct commit *commit,
 				const char *format, struct strbuf *sb,
@@ -2010,6 +2108,7 @@ void repo_format_commit_message(struct repository *r,
 	};
 	const char *output_enc = pretty_ctx->output_encoding;
 	const char *utf8 = "UTF-8";
+	size_t orig_len = sb->len;
 
 	while (strbuf_expand_step(sb, &format)) {
 		size_t len;
@@ -2023,6 +2122,9 @@ void repo_format_commit_message(struct repository *r,
 	}
 	rewrap_message_tail(sb, &context, 0, 0, 0);
 
+	if (want_color(pretty_ctx->color))
+		make_line_colors_self_contained(sb, orig_len);
+
 	/*
 	 * Convert output to an actual output encoding; note that
 	 * format_commit_item() will always use UTF-8, so we don't
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index 01b97c8888..7a3a3a4639 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -538,6 +538,86 @@ test_expect_success 'strbuf_utf8_replace() not producing NUL' '
 	test_grep ! Q actual
 '
 
+test_expect_success 'colors carry across lines' '
+	git log -1 --color=always \
+		--format="%C(bold)%C(red)a%n%C(green)b" >raw &&
+	test_decode_color <raw >actual &&
+	cat >expect <<-\EOF &&
+	<BOLD><RED>a<RESET>
+	<BOLD><RED><GREEN>b<RESET>
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'colors carry across wrapped lines' '
+	commit=$(echo "one two three" | git commit-tree HEAD^{tree}) &&
+	git log -1 --color=always --format="%C(bold)%w(7)%s" $commit >raw &&
+	test_decode_color <raw >actual &&
+	cat >expect <<-\EOF &&
+	<BOLD>one two<RESET>
+	<BOLD>three<RESET>
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'colors are reset at the end of each commit' '
+	git log -2 --color=always --format="%C(red)a" >raw &&
+	test_decode_color <raw >actual &&
+	cat >expect <<-\EOF &&
+	<RED>a<RESET>
+	<RED>a<RESET>
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'colors are not replayed on empty lines or before resets' '
+	git log -1 --color=always \
+		--format="%C(red)a%n%n%C(reset)b%C(green)%nc%n" >raw &&
+	test_decode_color <raw >actual &&
+	cat >expect <<-\EOF &&
+	<RED>a<RESET>
+
+	<RESET>b<GREEN><RESET>
+	<GREEN>c<RESET>
+
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'resets followed by colors carry across lines' '
+	git log -1 --color=always \
+		--format="%C(red)%C(blue reset)a%nb%x1b[0;31m%nc%x1b[0m%nd" \
+		>raw &&
+	test_decode_color <raw >actual &&
+	cat >expect <<-\EOF &&
+	<RED><;BLUE>a<RESET>
+	<;BLUE>b<RESET;RED><RESET>
+	<RESET;RED>c<RESET>
+	d
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'colors carry across lines with NULs' '
+	git log -1 --color=always --format="%C(red)a%x00%x00b%nc%x00" >raw &&
+	nul_to_q <raw | test_decode_color >actual &&
+	cat >expect <<-\EOF &&
+	<RED>aQQb<RESET>
+	<RED>cQ<RESET>
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success 'colors carry across lines with --graph' '
+	git log -1 --graph --color=always --format="%C(red)a%nb" >raw &&
+	test_decode_color <raw >actual &&
+	cat >expect <<-\EOF &&
+	* <RED>a<RESET>
+	<RED>|<RESET> <RED>b<RESET>
+	EOF
+	test_cmp expect actual
+'
+
 # --date=[XXX] and corresponding %a[X] %c[X] format equivalency
 test_expect_success '--date=iso-strict %ad%cd is the same as %aI%cI' '
 	git log --format=%ad%n%cd --date=iso-strict >expected &&

Range-diff against v2:
1:  845c622dc7 ! 1:  e9f88b533d pretty: fix lost and leaked colors at line breaks
    @@ pretty.c: void userformat_find_requirements(const char *fmt, struct userformat_w
     +
     +	while (p < sb_end) {
     +		size_t esc_len;
    -+		char *end;
    -+		bool replay = false, is_reset, is_reset_only;
    ++		bool replay = false, reset, reset_only;
    ++		char *reset_end;
     +
     +		if (!colors.len) {
     +			/* look for colors to collect */
    @@ pretty.c: void userformat_find_requirements(const char *fmt, struct userformat_w
     +		}
     +
     +		esc_len = display_mode_esc_sequence_len(p);
    -+		is_reset = esc_len && !strtoul(p + 2, &end, 10);
    -+		is_reset_only = is_reset && *end == 'm';
    ++		reset = esc_len && !strtoul(p + 2, &reset_end, 10);
    ++		reset_only = reset && *reset_end == 'm';
     +
    -+		if (replay && !is_reset) {
    ++		if (reset) {
    ++			strbuf_reset(&colors);
    ++		} else if (replay) {
     +			strbuf_add(&tmp, copied, p - copied);
     +			copied = p;
     +			strbuf_addbuf(&tmp, &colors);
     +		}
     +
     +		if (esc_len) {
    -+			if (is_reset)
    -+				strbuf_reset(&colors);
    -+			if (!is_reset_only)
    ++			/* collect the sequence if it's more than a reset */
    ++			if (!reset_only)
     +				strbuf_add(&colors, p, esc_len);
     +			p += esc_len;
     +		} else {

base-commit: 03b41523c576f26ad34085378717a0c1b287470f
-- 
2.56.GIT
Previous: Andy Koppe
Message 5 of 5 in “pretty: fix lost and leaked colors at line breaks”
  1. pretty: fix lost and leaked colors at line breaksAndy Koppe, Nov 15, 2026
  2. Junio C HamanoNov 16, 2026
  3. Andy KoppeNov 17, 2026
  4. pretty: fix lost and leaked colors at line breaksAndy Koppe, Nov 17, 2026
  5. pretty: fix lost and leaked colors at line breaksAndy Koppe, Nov 17, 2026

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.