Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Andy Koppe <andy.koppe@gmail.com>
Cc: git@vger.kernel.org, "Jeff King" <peff@peff.net>,
	"René Scharfe" <l.s.r@web.de>
Subject: Re: [PATCH] pretty: add %(body:trailers=<bool>)
Date: Tue, 24 Nov 2026 13:10:43 -0800	[thread overview]
Message-ID: <xmqqse0qlyuk.fsf@gitster.g> (raw)
In-Reply-To: <4e6e2c256ef7d13dfc31cf0235f6163cc1509fca.1795153464.git.andy.koppe@gmail.com> (Andy Koppe's message of "Fri, 20 Nov 2026 06:09:30 +0000")

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.

      reply	other threads:[~2026-11-24 21:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  6:09 [PATCH] pretty: add %(body:trailers=<bool>) Andy Koppe
2026-11-24 21:10 ` Junio C Hamano [this message]

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=xmqqse0qlyuk.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=andy.koppe@gmail.com \
    --cc=git@vger.kernel.org \
    --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