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

[PATCH] pretty: add %(body:trailers=<bool>)

From
Andy Koppe <andy.koppe@gmail.com>
Date
Nov 20, 2026, 06:09 UTC
Message-ID
<4e6e2c256ef7d13dfc31cf0235f6163cc1509fca.1795153464.git.andy.koppe@gmail.com>

While pretty formatting has the "%(trailers)" placeholder for extracting the trailer block, it does not have a complementary placeholder for extracting the message body without trailers. That can be useful for:

- Hiding trailers in a compact log format.
- Reflowing the body with "%w" without affecting the trailers.
- Coloring the trailers separately from the rest of the body.
- Getting the body without trailers in scripts and UIs without having
  to post-process "%b".

Add a "%(body)" placeholder that is equivalent to "%b", with a boolean "trailers" option. With "trailers=no", the trailer block as found by "%(trailers)" is omitted.

The blank line separating the trailer block from the text before it is dropped too. Content after the trailer block that "%(trailers)" ignores, such as comment lines or an old-style "Conflicts:" section, is kept. When there are no trailers, "%(body:trailers=no)" is the same as "%b".

The placeholder name matches the "%(contents:body)" field of git-for-each-ref, which also excludes the subject, and the option syntax follows the options of "%(trailers)" and "%(describe)".

As an example, "%w(72)%(body:trailers=no)%w()%C(dim)%+(trailers)%Creset" reflows the message body but not the trailers, which are dimmed.

Signed-off-by: Andy Koppe <andy.koppe@gmail.com>
---
Coloring the trailers separately as mentioned in the use cases would
greatly benefit from the "pretty: fix lost and leaked colors at line
breaks" patch [1], which in fact was motivated by it. I think the two
patches are independently useful though, which is why I didn't make
them a series.
[1] https://lore.kernel.org/git/e9f88b533dfbd10528c9151e2e87207419d0253d.1794905511.git.andy.koppe@gmail.com
 Documentation/pretty-formats.adoc |  3 ++
 pretty.c                          | 58 +++++++++++++++++++++++++++++++
 t/t4205-log-pretty-formats.sh     | 55 +++++++++++++++++++++++++++++
 3 files changed, 116 insertions(+)
diff --git a/Documentation/pretty-formats.adoc b/Documentation/pretty-formats.adoc
index 2ae0eb11a9..39d66d9f73 100644
--- a/Documentation/pretty-formats.adoc
+++ b/Documentation/pretty-formats.adoc
@@ -301,6 +301,9 @@ tags are added or removed at the same time.
 +%f+:: sanitized subject line, suitable for a filename
 +%b+:: body
 +%B+:: raw body (unwrapped subject and body)
+++%(body++`[:trailers[=<bool>]]`++)++:: body, like `%b`. With
+	`trailers=no`, the trailer block (see `%(trailers)` below) and the
+	blank line before it are omitted.
 ifndef::git-rev-list[]
 +%N+:: commit notes
 endif::git-rev-list[]
diff --git a/pretty.c b/pretty.c
index d8a9f370f6..c67c9160c7 100644
--- a/pretty.c
+++ b/pretty.c
@@ -997,6 +997,36 @@ const char *format_subject(struct strbuf *sb, const char *msg,
 	return msg;
 }
 
+static void format_body_without_trailers(struct strbuf *sb, const char *msg,
+					 const char *body)
+{
+	struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
+	struct trailer_block *trailer_block;
+	const char *trailer_start, *trailer_end;
+	LIST_HEAD(trailers);
+
+	opts.no_divider = 1;
+	trailer_block = parse_trailers(&opts, msg, &trailers);
+	trailer_start = msg + trailer_block_start(trailer_block);
+	trailer_end = msg + trailer_block_end(trailer_block);
+	trailer_block_release(trailer_block);
+	free_trailers(&trailers);
+
+	if (trailer_start == trailer_end) {
+		strbuf_addstr(sb, body);
+		return;
+	}
+
+	if (trailer_start > body) {
+		/* Include the preceding blank line in the chunk to cut. */
+		while (--trailer_start > body && trailer_start[-1] != '\n')
+			; /* back to the start of the line */
+	}
+
+	strbuf_add(sb, body, trailer_start - body);
+	strbuf_addstr(sb, trailer_end);
+}
+
 static void parse_commit_message(struct format_commit_context *c)
 {
 	const char *msg = c->message + c->message_off;
@@ -1251,6 +1281,14 @@ static int match_placeholder_bool_arg(const char *to_parse, const char *candidat
 	return 1;
 }
 
+static int parse_body_options(const char **p, int *trailers)
+{
+	while (**p != ')')
+		if (!match_placeholder_bool_arg(*p, "trailers", p, trailers))
+			return -1;
+	return 0;
+}
+
 static int format_trailer_match_cb(const struct strbuf *key, void *ud)
 {
 	const struct string_list *list = ud;
@@ -1771,6 +1809,26 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 		return 1;
 	}
 
+	if (skip_prefix(placeholder, "(body", &arg)) {
+		int trailers = 1;
+
+		if (*arg == ':') {
+			arg++;
+			if (parse_body_options(&arg, &trailers))
+				return 0;
+		}
+		if (*arg != ')')
+			return 0;
+
+		if (trailers)
+			strbuf_addstr(sb, msg + c->body_off);
+		else
+			format_body_without_trailers(sb, msg + c->subject_off,
+						     msg + c->body_off);
+
+		return arg - placeholder + 1;
+	}
+
 	if (skip_prefix(placeholder, "(trailers", &arg)) {
 		struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
 		struct string_list filter_list = STRING_LIST_INIT_NODUP;
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index 01b97c8888..4e90243616 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -662,6 +662,61 @@ test_expect_success 'set up trailer tests' '
 	EOF
 '
 
+test_expect_success '%(body) is equivalent to %b' '
+	git log --no-walk --pretty="%(body)" >actual &&
+	git log --no-walk --pretty="%b" >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success '%(body:trailers) is equivalent to %b' '
+	git log --no-walk --pretty="%(body:trailers)" >actual &&
+	git log --no-walk --pretty="%b" >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success '%(body) with unknown option is left as is' '
+	git log --no-walk --pretty="tformat:%(body:bogus)" >actual &&
+	echo "%(body:bogus)" >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success '%(body:trailers=no) omits trailers' '
+	git log --no-walk --pretty="%(body:trailers=no)" >actual &&
+	cat >expect <<-\EOF &&
+	This commit is a test commit with trailers at the end. We parse this
+	message and display the trailers using %(trailers).
+
+	EOF
+	test_cmp expect actual
+'
+
+test_expect_success '%(body:trailers=no) ignores Conflicts: section' '
+	test_when_finished "git reset --hard HEAD^" &&
+	printf "subject\n\nConflicts:\n\tfile.c\n" |
+	git commit --allow-empty --cleanup=verbatim -F - &&
+	git log --no-walk --pretty="%(body:trailers=no)" >actual &&
+	git log --no-walk --pretty="%b" >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success '%(body:trailers=no) keeps text after trailers' '
+	test_when_finished "git reset --hard HEAD^" &&
+	printf "subject\n\nbody\n\nA: b\n\nConflicts:\n\tfile.c\n" |
+	git commit --allow-empty --cleanup=verbatim -F - &&
+	git log --no-walk --pretty="%(body:trailers=no)" >actual &&
+	printf "body\n\nConflicts:\n\tfile.c\n\n" >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success '%(body:trailers=no) is empty with only trailers' '
+	test_when_finished "git reset --hard HEAD^" &&
+	printf "subject\n\nA: b\n" |
+	git commit --allow-empty --cleanup=verbatim -F - &&
+	git log --no-walk --pretty="%(body:trailers=no)" >actual &&
+	echo >expect &&
+	test_cmp expect actual
+'
+
 test_expect_success 'pretty format %(trailers) shows trailers' '
 	git log --no-walk --pretty="%(trailers)" >actual &&
 	{

base-commit: f7de81456940dd6f3274e461149bba12965de609
-- 
2.56.GIT
Message 1 of 1 in “pretty: add %(body:trailers=<bool>)”
  1. pretty: add %(body:trailers=<bool>)Andy Koppe, Nov 20, 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.