git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: Taylor Blau <me@ttaylorr.com>
Cc: git@vger.kernel.org, gitster@pobox.com
Subject: Re: [PATCH 1/5] pretty.c: delimit "%(trailers)" arguments with ","
Date: Sat, 30 Sep 2017 02:49:49 -0400	[thread overview]
Message-ID: <20170930064949.23plbzjtw2qupj6b@sigill.intra.peff.net> (raw)
In-Reply-To: <20170930062238.87077-2-me@ttaylorr.com>

On Fri, Sep 29, 2017 at 11:22:34PM -0700, Taylor Blau wrote:

> In preparation for adding consistent "%(trailers)" atom options to
> `git-for-each-ref(1)`'s "--format" argument, change "%(trailers)" in pretty.c
> to separate sub-arguments with a ",", instead of a ":".
> 
> Multiple sub-arguments are given either as "%(trailers:unfold,only)" or
> "%(trailers:only,unfold)".
> 
> This change disambiguates between "top-level" arguments, and arguments given to
> the trailers atom itself. It is consistent with the behavior of "%(upstream)"
> and "%(push)" atoms.

Great. I hope we'll eventually unify the pretty.c and ref-filter.c
syntaxes and implementations, so consistency is good. I'm sure there
will be some hackery to keep backwards compatibility in all cases, but
the fewer the better.  The "every option gets its own colon" scheme was
just something I invented, because there was no prior art in pretty.c. I
didn't think to check for similar cases in ref-filter.c.

We should be safe to change this unconditionally, without worrying about
breaking compatibility. These options were added in 58311c66fd (pretty:
support normalization options for %(trailers), 2017-08-15), which hasn't
been in any released version yet.

> ---
>  pretty.c                      | 13 ++++++++-----
>  t/t4205-log-pretty-formats.sh |  4 ++--
>  2 files changed, 10 insertions(+), 7 deletions(-)

I think this needs an update to Documentation/pretty-formats.txt. Maybe:

diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 6b4a12f028..40948d925b 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -199,11 +199,13 @@ endif::git-rev-list[]
   than given and there are spaces on its left, use those spaces
 - '%><(<N>)', '%><|(<N>)': similar to '% <(<N>)', '%<|(<N>)'
   respectively, but padding both sides (i.e. the text is centered)
-- %(trailers): display the trailers of the body as interpreted by
-  linkgit:git-interpret-trailers[1]. If the `:only` option is given,
-  omit non-trailer lines from the trailer block.  If the `:unfold`
-  option is given, behave as if interpret-trailer's `--unfold` option
-  was given. E.g., `%(trailers:only:unfold)` to do both.
+- %(trailers[:options]): display the trailers of the body as interpreted
+  by linkgit:git-interpret-trailers[1]. The `trailers` string may be
+  followed by a colon and zero or more comma-separated options. If the
+  `only` option is given, omit non-trailer lines from the trailer block.
+  If the `unfold` option is given, behave as if interpret-trailer's
+  `--unfold` option was given.  E.g., `%(trailers:only,unfold)` to do
+  both.
 
 NOTE: Some placeholders may depend on other options given to the
 revision traversal engine. For example, the `%g*` reflog options will

diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index ec5f530102..977472f539 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -588,8 +588,8 @@ test_expect_success '%(trailers:unfold) unfolds trailers' '
 '
 
 test_expect_success ':only and :unfold work together' '
-	git log --no-walk --pretty="%(trailers:only:unfold)" >actual &&
-	git log --no-walk --pretty="%(trailers:unfold:only)" >reverse &&
+	git log --no-walk --pretty="%(trailers:only,unfold)" >actual &&
+	git log --no-walk --pretty="%(trailers:unfold,only)" >reverse &&
 	test_cmp actual reverse &&
 	{
 		grep -v patch.description <trailers | unfold &&

> diff --git a/pretty.c b/pretty.c
> index 0e23fe3c0..68f736912 100644
> --- a/pretty.c
> +++ b/pretty.c
> @@ -1265,11 +1265,14 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
>  
>  	if (skip_prefix(placeholder, "(trailers", &arg)) {
>  		struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
> -		while (*arg == ':') {
> -			if (skip_prefix(arg, ":only", &arg))
> -				opts.only_trailers = 1;
> -			else if (skip_prefix(arg, ":unfold", &arg))
> -				opts.unfold = 1;
> +		if (skip_prefix(arg, ":", &arg)) {
> +			while (*arg != ')') {
> +				skip_prefix(arg, ",", &arg);
> +				if (skip_prefix(arg, "only", &arg))
> +					opts.only_trailers = 1;
> +				else if (skip_prefix(arg, "unfold", &arg))
> +					opts.unfold = 1;
> +			}
>  		}

Do we always make forward progress with this loop condition?

If arg doesn't start with a close-paren, we'll keep looping. But if it's
not a comma or one of the option strings, then skip_prefix() won't move
the pointer forward. So I think "%(trailers:foo)" would loop forever.

The original would complain about that and even almost-right things like
"%(trailers:only-the-lonely", though it's pretty subtle. The atom
parsers in ref-filter.c use string_list_split() which is way more
readable and obvious. But I don't think we can do that here. ref-filter
has a separate parsing step, so it can afford to be slow. Here we're
parsing the pretty format individually for each commit in "git log", and
allocations would be noticeable.

Here's what I came up with:

  static int match_placeholder_arg(const char *to_parse,
  				 const char *candidate,
  				 const char **end)
  {
  	const char *p;
  	if (!skip_prefix(to_parse, candidate, &p))
  		return 0;
  	if (*p == ',') {
  		*end = p + 1;
  		return 1;
  	}
  	if (*p == ')') {
  		*end = p;
  		return 1;
  	}
  	return 0;
  }

  ...

  	if (skip_prefix(placeholder, "(trailers", &arg)) {
  		struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
  		if (*arg == ':') {
  			arg++;
  			for (;;) {
  				if (match_placeholder_arg(arg, "only", &arg))
  					opts.only_trailers = 1;
  				else if (match_placeholder_arg(arg, "unfold", &arg))
  					opts.unfold = 1;
  				else
  					break;
  			}
  		}
  		if (*arg == ')') {
  			format_trailers_from_commit(sb, msg + c->subject_off, &opts);
  			return arg - placeholder + 1;
  		}
  	}

I hate the "for (;;)", but at least it's easy to see that each iteration
either makes progress or breaks out of the loop.

-Peff

  reply	other threads:[~2017-09-30  6:49 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-30  6:22 [PATCH 0/5] Support %(trailers) arguments in for-each-ref(1) Taylor Blau
2017-09-30  6:22 ` [PATCH 1/5] pretty.c: delimit "%(trailers)" arguments with "," Taylor Blau
2017-09-30  6:49   ` Jeff King [this message]
2017-09-30  6:22 ` [PATCH 2/5] t6300: refactor %(trailers) tests Taylor Blau
2017-09-30  7:01   ` Jeff King
2017-09-30  6:22 ` [PATCH 3/5] ref-filter.c: add trailer options to used_atom Taylor Blau
2017-09-30  7:10   ` Jeff King
2017-09-30  6:22 ` [PATCH 4/5] ref-filter.c: use trailer_opts to format trailers Taylor Blau
2017-09-30  7:21   ` Jeff King
2017-10-01  9:08     ` Junio C Hamano
2017-10-01  9:00   ` Junio C Hamano
2017-10-02  5:05     ` Jeff King
2017-10-02  5:11       ` Taylor Blau
2017-09-30  6:22 ` [PATCH 5/5] ref-filter.c: parse trailers arguments with %(contents) atom Taylor Blau
2017-09-30  7:36   ` Jeff King
2017-09-30  7:38 ` [PATCH 0/5] Support %(trailers) arguments in for-each-ref(1) Jeff King
2017-09-30 18:41 ` [PATCH v2 0/6] " Taylor Blau
2017-09-30 18:46   ` [PATCH v2 1/6] pretty.c: delimit "%(trailers)" arguments with "," Taylor Blau
2017-09-30 18:46     ` [PATCH v2 2/6] t6300: refactor %(trailers) tests Taylor Blau
2017-09-30 18:46     ` [PATCH v2 3/6] doc: 'trailers' is the preferred way to format trailers Taylor Blau
2017-09-30 18:46     ` [PATCH v2 4/6] doc: use modern "`"-style code fencing Taylor Blau
2017-09-30 18:46     ` [PATCH v2 5/6] ref-filter.c: use trailer_opts to format trailers Taylor Blau
2017-09-30 18:46     ` [PATCH v2 6/6] ref-filter.c: parse trailers arguments with %(contents) atom Taylor Blau
2017-10-01  0:06   ` [PATCH v2 0/6] Support %(trailers) arguments in for-each-ref(1) Taylor Blau
2017-10-01  0:10     ` [PATCH v3 1/6] pretty.c: delimit "%(trailers)" arguments with "," Taylor Blau
2017-10-01  0:10       ` [PATCH v3 2/6] t6300: refactor %(trailers) tests Taylor Blau
2017-10-01  0:10       ` [PATCH v3 3/6] doc: 'trailers' is the preferred way to format trailers Taylor Blau
2017-10-01  0:10       ` [PATCH v3 4/6] doc: use modern "`"-style code fencing Taylor Blau
2017-10-01  0:10       ` [PATCH v3 5/6] ref-filter.c: use trailer_opts to format trailers Taylor Blau
2017-10-01  0:10       ` [PATCH v3 6/6] ref-filter.c: parse trailers arguments with %(contents) atom Taylor Blau
2017-10-01 16:17     ` [PATCH v4 0/6] Support %(trailers) arguments in for-each-ref(1) Taylor Blau
2017-10-01 16:18       ` [PATCH v4 1/6] pretty.c: delimit "%(trailers)" arguments with "," Taylor Blau
2017-10-01 16:18         ` [PATCH v4 2/6] t6300: refactor %(trailers) tests Taylor Blau
2017-10-02  0:12           ` Junio C Hamano
2017-10-01 16:18         ` [PATCH v4 3/6] doc: 'trailers' is the preferred way to format trailers Taylor Blau
2017-10-01 16:18         ` [PATCH v4 4/6] doc: use modern "`"-style code fencing Taylor Blau
2017-10-01 23:55           ` Junio C Hamano
2017-10-02  0:06             ` Taylor Blau
2017-10-02  1:35               ` Junio C Hamano
2017-10-02  4:53                 ` Jeff King
2017-10-01 16:18         ` [PATCH v4 5/6] ref-filter.c: use trailer_opts to format trailers Taylor Blau
2017-10-02  0:13           ` Junio C Hamano
2017-10-01 16:18         ` [PATCH v4 6/6] ref-filter.c: parse trailers arguments with %(contents) atom Taylor Blau
2017-10-02  0:19           ` Junio C Hamano
2017-10-02  0:11         ` [PATCH v4 1/6] pretty.c: delimit "%(trailers)" arguments with "," Junio C Hamano
2017-10-02  5:00           ` Jeff King
2017-10-02  0:31       ` [PATCH v5 0/6] Support %(trailers) arguments in for-each-ref(1) Taylor Blau
2017-10-02  0:32         ` [PATCH v5 1/6] pretty.c: delimit "%(trailers)" arguments with "," Taylor Blau
2017-10-02  0:33           ` [PATCH v5 2/6] t6300: refactor %(trailers) tests Taylor Blau
2017-10-02  0:33           ` [PATCH v5 3/6] doc: 'trailers' is the preferred way to format trailers Taylor Blau
2017-10-02  0:33           ` [PATCH v5 4/6] doc: use modern "`"-style code quoting Taylor Blau
2017-10-02  0:33           ` [PATCH v5 5/6] ref-filter.c: use trailer_opts to format trailers Taylor Blau
2017-10-02  0:33           ` [PATCH v5 6/6] ref-filter.c: parse trailers arguments with %(contents) atom Taylor Blau
2017-10-02  4:26             ` Junio C Hamano
2017-10-02  4:51             ` Junio C Hamano
2017-10-02  5:13               ` Taylor Blau
2017-10-02  5:03             ` Jeff King
2017-10-02  5:12               ` Taylor Blau
2017-10-02  5:14                 ` Jeff King
2017-10-02  5:23         ` [PATCH v6 0/7] Support %(trailers) arguments in for-each-ref(1) Taylor Blau
2017-10-02  5:25           ` [PATCH v6 1/7] pretty.c: delimit "%(trailers)" arguments with "," Taylor Blau
2017-10-02  5:25             ` [PATCH v6 2/7] t4205: unfold across multiple lines Taylor Blau
2017-10-02  5:25             ` [PATCH v6 3/7] doc: 'trailers' is the preferred way to format trailers Taylor Blau
2017-10-02  5:25             ` [PATCH v6 4/7] doc: use modern "`"-style code quoting Taylor Blau
2017-10-02  5:25             ` [PATCH v6 5/7] t6300: refactor %(trailers) tests Taylor Blau
2017-10-02  5:25             ` [PATCH v6 6/7] ref-filter.c: use trailer_opts to format trailers Taylor Blau
2017-10-02  5:25             ` [PATCH v6 7/7] ref-filter.c: parse trailers arguments with %(contents) atom Taylor Blau
2017-10-02  6:51               ` Jeff King
2017-10-02  9:52                 ` Junio C Hamano
2017-10-02 15:49                 ` Taylor Blau
2017-10-02 23:44                   ` Junio C Hamano
2017-10-02  6:56           ` [PATCH v6 0/7] Support %(trailers) arguments in for-each-ref(1) Jeff King
2017-10-03  6:24             ` Junio C Hamano
2017-10-03  6:36               ` Jeff King
2017-10-03  6:40                 ` Junio C Hamano
2017-10-02  8:07           ` Junio C Hamano
2017-10-02 12:15             ` Junio C Hamano
2017-10-02 16:07               ` Taylor Blau

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

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170930064949.23plbzjtw2qupj6b@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=me@ttaylorr.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).