git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] pretty: fix lost and leaked colors at line breaks
@ 2026-11-15 21:35 Andy Koppe
  2026-11-16 16:25 ` Junio C Hamano
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Andy Koppe @ 2026-11-15 21:35 UTC (permalink / raw)
  To: git; +Cc: Andy Koppe, Jeff King, René Scharfe

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
shown without that context. Notably, --graph and color-aware pagers such
as less reset colors at line breaks, so for example "%C(bold)%w(72)%s"
only makes the first line of a wrapped subject bold, and "%C(cyan)%b"
only colors the first line of the message body. The same can happen to
lines picked out by grepping colored output.

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 the graph does
not reset colors before drawing. Attributes such as bold also leak into
the '|', '/' and '\' edges, as their colors don't override them. When
grepping colored output, colors can similarly leak into unrelated lines.

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.

Fix both problems by taking the same approach 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>
---
 pretty.c                      | 98 +++++++++++++++++++++++++++++++++++
 t/t4205-log-pretty-formats.sh | 70 +++++++++++++++++++++++++
 2 files changed, 168 insertions(+)

diff --git a/pretty.c b/pretty.c
index d8a9f370f6..58ccf304be 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1997,6 +1997,100 @@ 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;
+		char *end;
+		bool replay = false, is_reset, is_reset_only;
+
+		if (!colors.len) {
+			/* look for colors to collect */
+			p = memchr(p, '\033', sb_end - p);
+			if (!p)
+				break;
+		} else {
+			/* look for more colors or the line end */
+			p += strcspn(p, "\033\n");
+		}
+
+		if (*p == '\n') {
+			/* leave room for added resets and colors */
+			if (!tmp.len)
+				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);
+		is_reset = esc_len && !strtoul(p + 2, &end, 10);
+		is_reset_only = is_reset && *end == 'm';
+
+		if (replay && !is_reset) {
+			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)
+				strbuf_add(&colors, p, esc_len);
+			p += esc_len;
+		} else {
+			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 +2104,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 +2118,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..43a7de3f37 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -538,6 +538,76 @@ 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 --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 &&

base-commit: 7fb6055e678f800320d8ab8cd8f201d9493d1ba0
-- 
2.56.GIT


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] pretty: fix lost and leaked colors at line breaks
  2026-11-15 21:35 [PATCH] pretty: fix lost and leaked colors at line breaks Andy Koppe
@ 2026-11-16 16:25 ` Junio C Hamano
  2026-11-17  0:12   ` Andy Koppe
  2026-11-17  0:00 ` [PATCH v2] " Andy Koppe
  2026-11-17  9:10 ` [PATCH v3] " Andy Koppe
  2 siblings, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2026-11-16 16:25 UTC (permalink / raw)
  To: Andy Koppe; +Cc: git, Jeff King, René Scharfe

Andy Koppe <andy.koppe@gmail.com> writes:

> 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
> shown without that context. Notably, --graph and color-aware pagers such
> as less reset colors at line breaks, so for example "%C(bold)%w(72)%s"
> only makes the first line of a wrapped subject bold, and "%C(cyan)%b"
> only colors the first line of the message body.

And custom format writers have no way to control what happens at the
(artificial) line boundary that wrapping at 72 columns %w(72)
introduces, %b may include many lines but there is nothing for them
to say "at the end of each and every line, do this".  And that is
why you decided that runtime detection is the only way to do this to
help custom formats?

OK.  My initial reaction to the "let's do the same as the default
format output" was "isn't that the responsibility of those who write
their own custom format?", but there clearly is a gap in the custom
format feature that makes it impossible for them to do so.

> The same can happen to
> lines picked out by grepping colored output.

I am not sure if that is a problem.

> Secondly, colors that are still active at the end of a line can leak
> into what follows.

That somehow feels contradictory with the previous point, though.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] pretty: fix lost and leaked colors at line breaks
  2026-11-15 21:35 [PATCH] pretty: fix lost and leaked colors at line breaks Andy Koppe
  2026-11-16 16:25 ` Junio C Hamano
@ 2026-11-17  0:00 ` Andy Koppe
  2026-11-17  9:10 ` [PATCH v3] " Andy Koppe
  2 siblings, 0 replies; 5+ messages in thread
From: Andy Koppe @ 2026-11-17  0:00 UTC (permalink / raw)
  To: git; +Cc: Andy Koppe, Junio C Hamano, Jeff King, René Scharfe

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>
---
Changes since v1 in response to Junio's review:

- Point out that custom formats cannot make lines self-contained within
  placeholders that expand to multiple lines, which is why the formatted
  output is processed instead.
- Drop the mentions of grepping colored output.
- Clarify the --graph examples, which looked contradictory: the graph
  only resets colors after its colored edges, so colors carried across
  a line break are lost after an edge, yet leak into an uncolored '*'
  commit marker.
- Rebase onto maint, where v1 was queued.

Other changes, from looking at the code with fresh eyes again:

- Move the line break handling into the branch for active colors, as
  it can't trigger otherwise.
- Comment on the handling of NULs, which also stop strcspn().
- Add a test with embedded and trailing NULs.

 pretty.c                      | 101 ++++++++++++++++++++++++++++++++++
 t/t4205-log-pretty-formats.sh |  80 +++++++++++++++++++++++++++
 2 files changed, 181 insertions(+)

diff --git a/pretty.c b/pretty.c
index d8a9f370f6..983bd4fc71 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1997,6 +1997,103 @@ 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;
+		char *end;
+		bool replay = false, is_reset, is_reset_only;
+
+		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);
+		is_reset = esc_len && !strtoul(p + 2, &end, 10);
+		is_reset_only = is_reset && *end == 'm';
+
+		if (replay && !is_reset) {
+			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)
+				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 +2107,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 +2121,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 v1:
1:  1e5e3188cd ! 1:  845c622dc7 pretty: fix lost and leaked colors at line breaks
    @@ Commit message
         colors, nor reset them at their end, which leads to two problems.
     
         Firstly, lines that continue colors from preceding lines lose them when
    -    shown without that context. Notably, --graph and color-aware pagers such
    -    as less reset colors at line breaks, so for example "%C(bold)%w(72)%s"
    -    only makes the first line of a wrapped subject bold, and "%C(cyan)%b"
    -    only colors the first line of the message body. The same can happen to
    -    lines picked out by grepping colored output.
    +    --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 the graph does
    -    not reset colors before drawing. Attributes such as bold also leak into
    -    the '|', '/' and '\' edges, as their colors don't override them. When
    -    grepping colored output, colors can similarly leak into unrelated lines.
    +    '*' 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.
    +    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 taking the same approach for custom formats. When
    -    color output is enabled, scan their output for color escape sequences,
    +    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
    @@ pretty.c: void userformat_find_requirements(const char *fmt, struct userformat_w
     +			if (!p)
     +				break;
     +		} else {
    -+			/* look for more colors or the line end */
    ++			/* look for more colors, the line end, or a NUL */
     +			p += strcspn(p, "\033\n");
    -+		}
     +
    -+		if (*p == '\n') {
    -+			/* leave room for added resets and colors */
    -+			if (!tmp.len)
    -+				strbuf_grow(&tmp, sb->len * 3 / 2);
    ++			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);
    ++				/* 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 */
    ++				while (*++p == '\n')
    ++					; /* skip empty lines */
     +
    -+			if (p == sb_end) {
    -+				/* no line left to replay colors on */
    -+				strbuf_reset(&colors);
    -+				break;
    ++				if (p == sb_end) {
    ++					/* no line left to replay colors on */
    ++					strbuf_reset(&colors);
    ++					break;
    ++				}
    ++
    ++				replay = true;
     +			}
    -+			replay = true;
     +		}
     +
     +		esc_len = display_mode_esc_sequence_len(p);
    @@ pretty.c: void userformat_find_requirements(const char *fmt, struct userformat_w
     +				strbuf_add(&colors, p, esc_len);
     +			p += esc_len;
     +		} else {
    ++			/* skip anything else, including NULs */
     +			p++;
     +		}
     +	}
    @@ t/t4205-log-pretty-formats.sh: test_expect_success 'strbuf_utf8_replace() not pr
     +	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 &&

base-commit: 03b41523c576f26ad34085378717a0c1b287470f
-- 
2.56.GIT


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] pretty: fix lost and leaked colors at line breaks
  2026-11-16 16:25 ` Junio C Hamano
@ 2026-11-17  0:12   ` Andy Koppe
  0 siblings, 0 replies; 5+ messages in thread
From: Andy Koppe @ 2026-11-17  0:12 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King, René Scharfe

Junio C Hamano <gitster@pobox.com> writes:

> And custom format writers have no way to control what happens at the
> (artificial) line boundary that wrapping at 72 columns %w(72)
> introduces, %b may include many lines but there is nothing for them
> to say "at the end of each and every line, do this".  And that is
> why you decided that runtime detection is the only way to do this to
> help custom formats?

Yes, exactly. I've tried to make that clearer in the commit message of v2.

> > The same can happen to
> > lines picked out by grepping colored output.
>
> I am not sure if that is a problem.

Fair enough. It's a minor point, so I've dropped it.

> > Secondly, colors that are still active at the end of a line can leak
> > into what follows.
>
> That somehow feels contradictory with the previous point, though.

You're right, I didn't put that well. The graph doesn't reset colors
at the start of a line, but after each of its colored edges. So a
color carried across a line break is lost once an edge has been drawn,
but it leaks into anything drawn before that, notably the uncolored
'*' commit marker. Attributes such as bold even leak into the edges
themselves, as their colors only replace the foreground color. v2
rewords this.

Thanks,
Andy

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v3] pretty: fix lost and leaked colors at line breaks
  2026-11-15 21:35 [PATCH] pretty: fix lost and leaked colors at line breaks Andy Koppe
  2026-11-16 16:25 ` Junio C Hamano
  2026-11-17  0:00 ` [PATCH v2] " Andy Koppe
@ 2026-11-17  9:10 ` Andy Koppe
  2 siblings, 0 replies; 5+ messages in thread
From: Andy Koppe @ 2026-11-17  9:10 UTC (permalink / raw)
  To: git; +Cc: Andy Koppe, Junio C Hamano, Jeff King, René Scharfe

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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-17  9:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-15 21:35 [PATCH] pretty: fix lost and leaked colors at line breaks Andy Koppe
2026-11-16 16:25 ` Junio C Hamano
2026-11-17  0:12   ` Andy Koppe
2026-11-17  0:00 ` [PATCH v2] " Andy Koppe
2026-11-17  9:10 ` [PATCH v3] " Andy Koppe

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 NNTP newsgroup(s).