* [PATCH] pretty: add %(body:trailers=<bool>)
@ 2026-11-20 6:09 Andy Koppe
2026-11-24 21:10 ` Junio C Hamano
0 siblings, 1 reply; 2+ messages in thread
From: Andy Koppe @ 2026-11-20 6:09 UTC (permalink / raw)
To: git; +Cc: Jeff King, René Scharfe
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
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] pretty: add %(body:trailers=<bool>)
2026-11-20 6:09 [PATCH] pretty: add %(body:trailers=<bool>) Andy Koppe
@ 2026-11-24 21:10 ` Junio C Hamano
0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2026-11-24 21:10 UTC (permalink / raw)
To: Andy Koppe; +Cc: git, Jeff King, René Scharfe
Andy Koppe <andy.koppe@gmail.com> writes:
> 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.
Having a counterpart to %(trailers) that strips the trailer block
without needing external post-processing makes "%w" rewrapping and
separate coloring much cleaner. Modeling the syntax after %(trailers)
and %(describe) also fits the existing placeholders well.
> 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);
Calling parse_trailers() with '.no_divider' set to 1 at the subject
offset mirrors format_commit_one()'s %(trailers) branch, so both agree
on where the trailer block starts and ends. It does populate and
immediately free the trailer list, which looks a bit curious. I guess
this is because trailer_block_get() is file-local to trailer.c, which is
not available to us here. We could expose it as a helper function if we
wanted to, but the trade off there is unclear. I'd say we can leave it
as-is at least for now.
> + 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 */
> + }
Do we need an "else" clause here, IOW, can trailer_start be less than
body at this point?
The caller in format_commit_one() does
> + format_body_without_trailers(sb, msg + c->subject_off,
> + msg + c->body_off);
where msg+c->subject_off is "msg" and msg+c->body_off is "body" for us.
IOW, "msg" is the first line of the first paragraph in the commit object
body (which is taken as "commit subject"), and "body" is the first line
of the second paragraph, with each paragraph separated by blank lines.
trailer.c:find_trailer_block_start() clearly says that the first
paragraph cannot be trailers and skips it, so we should be safe. There
is no need for an "else" clause there.
> @@ -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;
> +}
Use of match_placeholder_bool_arg() here is nice. This handles bare
"trailers", "trailers=<bool>", repeated options (last wins), and
unclosed placeholders cleanly, since the function returns 0 at NUL.
> +test_expect_success '%(body:trailers=no) ignores Conflicts: section' '
> + test_when_finished "git reset --hard HEAD^" &&
This is not quite right way to set up test_when_finished. You are
assuming that at least up to your "git commit" things will not break and
you will successfully create one commit, hence HEAD^ is the right place
to go back to. If you fail before managing to create the commit, then
the following tests will run in a repository with one commit before
where this test started.
Perhaps remember where you are and prepare to go back exactly to that
commit, i.e.,
go_back_to=$(git rev-parse --verify HEAD) &&
test_when_finished "git reset --hard $go_back_to) &&
would be safer?
> + 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
> +'
It might also be nice to exercise "trailers=no,trailers=yes" and an
invalid boolean value such as "%(body:trailers=bogus)".
Overall this looks nicely done, except for these minor nits in the
tests.
Thanks.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-24 21:10 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 6:09 [PATCH] pretty: add %(body:trailers=<bool>) Andy Koppe
2026-11-24 21:10 ` Junio C Hamano
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox