From: Andy Koppe <andy.koppe@gmail.com>
To: git@vger.kernel.org
Cc: "Andy Koppe" <andy.koppe@gmail.com>,
"Junio C Hamano" <gitster@pobox.com>, "Jeff King" <peff@peff.net>,
"René Scharfe" <l.s.r@web.de>
Subject: [PATCH v2] pretty: fix lost and leaked colors at line breaks
Date: Tue, 17 Nov 2026 00:00:36 +0000 [thread overview]
Message-ID: <845c622dc7fd33306beca5488a74a89dee8a2e38.1794872505.git.andy.koppe@gmail.com> (raw)
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>
---
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
next prev parent reply other threads:[~2026-11-17 0:00 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Andy Koppe [this message]
2026-11-17 9:10 ` [PATCH v3] " Andy Koppe
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=845c622dc7fd33306beca5488a74a89dee8a2e38.1794872505.git.andy.koppe@gmail.com \
--to=andy.koppe@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=l.s.r@web.de \
--cc=peff@peff.net \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox