{"thread":{"id":"66894","subject":"[PATCH] pretty: add %(body:trailers=<bool>)","startedAt":"2026-11-20T06:09:30Z","lastAt":"2026-11-20T06:09:30Z","messageCount":1,"participants":["Andy Koppe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"558909","messageId":"4e6e2c256ef7d13dfc31cf0235f6163cc1509fca.1795153464.git.andy.koppe@gmail.com","threadId":"66894","inReplyTo":null,"subject":"[PATCH] pretty: add %(body:trailers=<bool>)","fromName":"Andy Koppe","fromEmail":"andy.koppe@gmail.com","sentAt":"2026-11-20T06:09:30Z","receivedAt":"2026-11-20T06:09:30Z","isPatch":true,"sender":{"key":"andy.koppe@gmail.com","avatar":"https://avatars.githubusercontent.com/u/223411?v=4"},"body":"While pretty formatting has the \"%(trailers)\" placeholder for extracting\nthe trailer block, it does not have a complementary placeholder for\nextracting the message body without trailers. That can be useful for:\n\n- Hiding trailers in a compact log format.\n- Reflowing the body with \"%w\" without affecting the trailers.\n- Coloring the trailers separately from the rest of the body.\n- Getting the body without trailers in scripts and UIs without having\n  to post-process \"%b\".\n\nAdd a \"%(body)\" placeholder that is equivalent to \"%b\", with a boolean\n\"trailers\" option. With \"trailers=no\", the trailer block as found by\n\"%(trailers)\" is omitted.\n\nThe blank line separating the trailer block from the text before it is\ndropped too. Content after the trailer block that \"%(trailers)\" ignores,\nsuch as comment lines or an old-style \"Conflicts:\" section, is kept.\nWhen there are no trailers, \"%(body:trailers=no)\" is the same as \"%b\".\n\nThe placeholder name matches the \"%(contents:body)\" field of\ngit-for-each-ref, which also excludes the subject, and the option syntax\nfollows the options of \"%(trailers)\" and \"%(describe)\".\n\nAs an example, \"%w(72)%(body:trailers=no)%w()%C(dim)%+(trailers)%Creset\"\nreflows the message body but not the trailers, which are dimmed.\n\nSigned-off-by: Andy Koppe <andy.koppe@gmail.com>\n---\nColoring the trailers separately as mentioned in the use cases would\ngreatly benefit from the \"pretty: fix lost and leaked colors at line\nbreaks\" patch [1], which in fact was motivated by it. I think the two\npatches are independently useful though, which is why I didn't make\nthem a series.\n\n[1] https://lore.kernel.org/git/e9f88b533dfbd10528c9151e2e87207419d0253d.1794905511.git.andy.koppe@gmail.com\n\n Documentation/pretty-formats.adoc |  3 ++\n pretty.c                          | 58 +++++++++++++++++++++++++++++++\n t/t4205-log-pretty-formats.sh     | 55 +++++++++++++++++++++++++++++\n 3 files changed, 116 insertions(+)\n\ndiff --git a/Documentation/pretty-formats.adoc b/Documentation/pretty-formats.adoc\nindex 2ae0eb11a9..39d66d9f73 100644\n--- a/Documentation/pretty-formats.adoc\n+++ b/Documentation/pretty-formats.adoc\n@@ -301,6 +301,9 @@ tags are added or removed at the same time.\n +%f+:: sanitized subject line, suitable for a filename\n +%b+:: body\n +%B+:: raw body (unwrapped subject and body)\n+++%(body++`[:trailers[=<bool>]]`++)++:: body, like `%b`. With\n+\t`trailers=no`, the trailer block (see `%(trailers)` below) and the\n+\tblank line before it are omitted.\n ifndef::git-rev-list[]\n +%N+:: commit notes\n endif::git-rev-list[]\ndiff --git a/pretty.c b/pretty.c\nindex d8a9f370f6..c67c9160c7 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -997,6 +997,36 @@ const char *format_subject(struct strbuf *sb, const char *msg,\n \treturn msg;\n }\n \n+static void format_body_without_trailers(struct strbuf *sb, const char *msg,\n+\t\t\t\t\t const char *body)\n+{\n+\tstruct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;\n+\tstruct trailer_block *trailer_block;\n+\tconst char *trailer_start, *trailer_end;\n+\tLIST_HEAD(trailers);\n+\n+\topts.no_divider = 1;\n+\ttrailer_block = parse_trailers(&opts, msg, &trailers);\n+\ttrailer_start = msg + trailer_block_start(trailer_block);\n+\ttrailer_end = msg + trailer_block_end(trailer_block);\n+\ttrailer_block_release(trailer_block);\n+\tfree_trailers(&trailers);\n+\n+\tif (trailer_start == trailer_end) {\n+\t\tstrbuf_addstr(sb, body);\n+\t\treturn;\n+\t}\n+\n+\tif (trailer_start > body) {\n+\t\t/* Include the preceding blank line in the chunk to cut. */\n+\t\twhile (--trailer_start > body && trailer_start[-1] != '\\n')\n+\t\t\t; /* back to the start of the line */\n+\t}\n+\n+\tstrbuf_add(sb, body, trailer_start - body);\n+\tstrbuf_addstr(sb, trailer_end);\n+}\n+\n static void parse_commit_message(struct format_commit_context *c)\n {\n \tconst char *msg = c->message + c->message_off;\n@@ -1251,6 +1281,14 @@ static int match_placeholder_bool_arg(const char *to_parse, const char *candidat\n \treturn 1;\n }\n \n+static int parse_body_options(const char **p, int *trailers)\n+{\n+\twhile (**p != ')')\n+\t\tif (!match_placeholder_bool_arg(*p, \"trailers\", p, trailers))\n+\t\t\treturn -1;\n+\treturn 0;\n+}\n+\n static int format_trailer_match_cb(const struct strbuf *key, void *ud)\n {\n \tconst struct string_list *list = ud;\n@@ -1771,6 +1809,26 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */\n \t\treturn 1;\n \t}\n \n+\tif (skip_prefix(placeholder, \"(body\", &arg)) {\n+\t\tint trailers = 1;\n+\n+\t\tif (*arg == ':') {\n+\t\t\targ++;\n+\t\t\tif (parse_body_options(&arg, &trailers))\n+\t\t\t\treturn 0;\n+\t\t}\n+\t\tif (*arg != ')')\n+\t\t\treturn 0;\n+\n+\t\tif (trailers)\n+\t\t\tstrbuf_addstr(sb, msg + c->body_off);\n+\t\telse\n+\t\t\tformat_body_without_trailers(sb, msg + c->subject_off,\n+\t\t\t\t\t\t     msg + c->body_off);\n+\n+\t\treturn arg - placeholder + 1;\n+\t}\n+\n \tif (skip_prefix(placeholder, \"(trailers\", &arg)) {\n \t\tstruct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;\n \t\tstruct string_list filter_list = STRING_LIST_INIT_NODUP;\ndiff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh\nindex 01b97c8888..4e90243616 100755\n--- a/t/t4205-log-pretty-formats.sh\n+++ b/t/t4205-log-pretty-formats.sh\n@@ -662,6 +662,61 @@ test_expect_success 'set up trailer tests' '\n \tEOF\n '\n \n+test_expect_success '%(body) is equivalent to %b' '\n+\tgit log --no-walk --pretty=\"%(body)\" >actual &&\n+\tgit log --no-walk --pretty=\"%b\" >expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '%(body:trailers) is equivalent to %b' '\n+\tgit log --no-walk --pretty=\"%(body:trailers)\" >actual &&\n+\tgit log --no-walk --pretty=\"%b\" >expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '%(body) with unknown option is left as is' '\n+\tgit log --no-walk --pretty=\"tformat:%(body:bogus)\" >actual &&\n+\techo \"%(body:bogus)\" >expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '%(body:trailers=no) omits trailers' '\n+\tgit log --no-walk --pretty=\"%(body:trailers=no)\" >actual &&\n+\tcat >expect <<-\\EOF &&\n+\tThis commit is a test commit with trailers at the end. We parse this\n+\tmessage and display the trailers using %(trailers).\n+\n+\tEOF\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '%(body:trailers=no) ignores Conflicts: section' '\n+\ttest_when_finished \"git reset --hard HEAD^\" &&\n+\tprintf \"subject\\n\\nConflicts:\\n\\tfile.c\\n\" |\n+\tgit commit --allow-empty --cleanup=verbatim -F - &&\n+\tgit log --no-walk --pretty=\"%(body:trailers=no)\" >actual &&\n+\tgit log --no-walk --pretty=\"%b\" >expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '%(body:trailers=no) keeps text after trailers' '\n+\ttest_when_finished \"git reset --hard HEAD^\" &&\n+\tprintf \"subject\\n\\nbody\\n\\nA: b\\n\\nConflicts:\\n\\tfile.c\\n\" |\n+\tgit commit --allow-empty --cleanup=verbatim -F - &&\n+\tgit log --no-walk --pretty=\"%(body:trailers=no)\" >actual &&\n+\tprintf \"body\\n\\nConflicts:\\n\\tfile.c\\n\\n\" >expect &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '%(body:trailers=no) is empty with only trailers' '\n+\ttest_when_finished \"git reset --hard HEAD^\" &&\n+\tprintf \"subject\\n\\nA: b\\n\" |\n+\tgit commit --allow-empty --cleanup=verbatim -F - &&\n+\tgit log --no-walk --pretty=\"%(body:trailers=no)\" >actual &&\n+\techo >expect &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'pretty format %(trailers) shows trailers' '\n \tgit log --no-walk --pretty=\"%(trailers)\" >actual &&\n \t{\n\nbase-commit: f7de81456940dd6f3274e461149bba12965de609\n-- \n2.56.GIT\n\n\n"}]}