git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] pretty: Add %(trailer:X) to display single trailer
@ 2018-10-28 12:50 Anders Waldenborg
  2018-10-29  4:49 ` Junio C Hamano
                   ` (4 more replies)
  0 siblings, 5 replies; 69+ messages in thread
From: Anders Waldenborg @ 2018-10-28 12:50 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Jeff King, Anders Waldenborg

This new format placeholder allows displaying only a single
trailer. The formatting done is similar to what is done for
--decorate/%d using parentheses and comma separation.

It's intended use is for things like ticket references in trailers.

So with a commit with a message like:

 > Some good commit
 >
 > Ticket: XYZ-123

running:

 $ git log --pretty="%H %s% (trailer:Ticket)"

will give:

 > 123456789a Some good commit (Ticket: XYZ-123)

Signed-off-by: Anders Waldenborg <anders@0x63.nu>
---
 Documentation/pretty-formats.txt |  4 ++++
 pretty.c                         | 16 +++++++++++++
 t/t4205-log-pretty-formats.sh    | 40 ++++++++++++++++++++++++++++++++
 trailer.c                        | 18 ++++++++++++--
 trailer.h                        |  1 +
 5 files changed, 77 insertions(+), 2 deletions(-)

diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 6109ef09aa..a46d0c0717 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -211,6 +211,10 @@ endif::git-rev-list[]
   If the `unfold` option is given, behave as if interpret-trailer's
   `--unfold` option was given.  E.g., `%(trailers:only,unfold)` to do
   both.
+- %(trailer:<t>): display the specified trailer in parentheses (like
+  %d does for refnames). If there are multiple entries of that trailer
+  they are shown comma separated. If there are no matching trailers
+  nothing is displayed.
 
 NOTE: Some placeholders may depend on other options given to the
 revision traversal engine. For example, the `%g*` reflog options will
diff --git a/pretty.c b/pretty.c
index 8ca29e9281..61ae34ced4 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1324,6 +1324,22 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 		}
 	}
 
+	if (skip_prefix(placeholder, "(trailer:", &arg)) {
+		struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
+		opts.no_divider = 1;
+		opts.only_trailers = 1;
+		opts.unfold = 1;
+
+		const char *end = strchr(arg, ')');
+		if (!end)
+			return 0;
+
+		opts.filter_trailer = xstrndup(arg, end - arg);
+		format_trailers_from_commit(sb, msg + c->subject_off, &opts);
+		free(opts.filter_trailer);
+		return end - placeholder + 1;
+	}
+
 	return 0;	/* unknown placeholder */
 }
 
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index 978a8a66ff..e929f820e7 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -598,6 +598,46 @@ test_expect_success ':only and :unfold work together' '
 	test_cmp expect actual
 '
 
+test_expect_success 'pretty format %(trailer:foo) shows that trailer' '
+	git log --no-walk --pretty="%(trailer:Acked-By)" >actual &&
+	{
+		echo "(Acked-By: A U Thor <author@example.com>)"
+	} >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success '%(trailer:nonexistant) becomes empty' '
+	git log --no-walk --pretty="x%(trailer:Nacked-By)x" >actual &&
+	{
+		echo "xx"
+	} >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success '% (trailer:nonexistant) with space becomes empty' '
+	git log --no-walk --pretty="x% (trailer:Nacked-By)x" >actual &&
+	{
+		echo "xx"
+	} >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success '% (trailer:foo) with space adds space before' '
+	git log --no-walk --pretty="x% (trailer:Acked-By)x" >actual &&
+	{
+		echo "x (Acked-By: A U Thor <author@example.com>)x"
+	} >expect &&
+	test_cmp expect actual
+'
+
+test_expect_success '%(trailer:foo) with multiple lines becomes comma separated and unwrapped' '
+	git log --no-walk --pretty="%(trailer:Signed-Off-By)" >actual &&
+	{
+		echo "(Signed-Off-By: A U Thor <author@example.com>, A U Thor <author@example.com>)"
+	} >expect &&
+	test_cmp expect actual
+'
+
 test_expect_success 'trailer parsing not fooled by --- line' '
 	git commit --allow-empty -F - <<-\EOF &&
 	this is the subject
diff --git a/trailer.c b/trailer.c
index 0796f326b3..d337bca8dd 100644
--- a/trailer.c
+++ b/trailer.c
@@ -1138,6 +1138,7 @@ static void format_trailer_info(struct strbuf *out,
 		return;
 	}
 
+	int printed_first = 0;
 	for (i = 0; i < info->trailer_nr; i++) {
 		char *trailer = info->trailers[i];
 		ssize_t separator_pos = find_separator(trailer, separators);
@@ -1150,7 +1151,19 @@ static void format_trailer_info(struct strbuf *out,
 			if (opts->unfold)
 				unfold_value(&val);
 
-			strbuf_addf(out, "%s: %s\n", tok.buf, val.buf);
+			if (opts->filter_trailer) {
+				if (!strcasecmp (tok.buf, opts->filter_trailer)) {
+					if (!printed_first) {
+						strbuf_addf(out, "(%s: ", opts->filter_trailer);
+						printed_first = 1;
+					} else {
+						strbuf_addstr(out, ", ");
+					}
+					strbuf_addstr(out, val.buf);
+				}
+			} else {
+				strbuf_addf(out, "%s: %s\n", tok.buf, val.buf);
+			}
 			strbuf_release(&tok);
 			strbuf_release(&val);
 
@@ -1158,7 +1171,8 @@ static void format_trailer_info(struct strbuf *out,
 			strbuf_addstr(out, trailer);
 		}
 	}
-
+	if (printed_first)
+		strbuf_addstr(out, ")");
 }
 
 void format_trailers_from_commit(struct strbuf *out, const char *msg,
diff --git a/trailer.h b/trailer.h
index b997739649..852c79d449 100644
--- a/trailer.h
+++ b/trailer.h
@@ -72,6 +72,7 @@ struct process_trailer_options {
 	int only_input;
 	int unfold;
 	int no_divider;
+	char *filter_trailer;
 };
 
 #define PROCESS_TRAILER_OPTIONS_INIT {0}
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 69+ messages in thread

end of thread, other threads:[~2019-02-02  9:14 UTC | newest]

Thread overview: 69+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-28 12:50 [PATCH] pretty: Add %(trailer:X) to display single trailer Anders Waldenborg
2018-10-29  4:49 ` Junio C Hamano
2018-10-29 14:14 ` Jeff King
2018-10-29 17:05   ` Anders Waldenborg
2018-10-31 20:27     ` Jeff King
2018-10-31 23:01       ` Anders Waldenborg
2018-11-01 18:42         ` Jeff King
2018-11-04 15:22 ` [PATCH v2 0/5] %(trailers) improvements in pretty format Anders Waldenborg
2018-11-04 15:22   ` [PATCH v2 1/5] pretty: single return path in %(trailers) handling Anders Waldenborg
2018-11-04 15:22   ` [PATCH v2 2/5] pretty: allow showing specific trailers Anders Waldenborg
2018-11-04 18:14     ` Eric Sunshine
2018-11-05  3:48       ` Junio C Hamano
2018-11-05  3:52         ` Eric Sunshine
2018-11-05  8:26       ` Anders Waldenborg
2018-11-05  9:00         ` Eric Sunshine
2018-11-05  5:14     ` Junio C Hamano
2018-11-04 15:22   ` [PATCH v2 3/5] pretty: add support for "nokey" option in %(trailers) Anders Waldenborg
2018-11-04 15:22   ` [PATCH v2 4/5] pretty: extract fundamental placeholders to separate function Anders Waldenborg
2018-11-05  2:06     ` Junio C Hamano
2018-11-05  8:32       ` Anders Waldenborg
2018-11-06  1:46         ` Junio C Hamano
2018-11-04 15:22   ` [PATCH v2 5/5] pretty: add support for separator option in %(trailers) Anders Waldenborg
2018-11-05  2:10     ` Junio C Hamano
2018-11-05 18:24       ` Anders Waldenborg
2018-11-06  1:48         ` Junio C Hamano
2018-11-05  5:18     ` Junio C Hamano
2018-11-04 17:40   ` [PATCH v2 0/5] %(trailers) improvements in pretty format Eric Sunshine
2018-11-05  7:09     ` Anders Waldenborg
2018-11-18 11:44   ` [PATCH v3 " Anders Waldenborg
2018-11-18 11:44     ` [PATCH v3 1/5] pretty: single return path in %(trailers) handling Anders Waldenborg
2018-11-18 11:44     ` [PATCH v3 2/5] pretty: allow showing specific trailers Anders Waldenborg
2018-11-20  5:45       ` Junio C Hamano
2018-11-20  5:59       ` Junio C Hamano
2018-11-25 23:02         ` Anders Waldenborg
2018-11-26  3:13           ` Junio C Hamano
2018-11-26  6:56             ` Anders Waldenborg
2018-11-26  7:52               ` Junio C Hamano
2018-11-18 11:44     ` [PATCH v3 3/5] pretty: add support for "valueonly" option in %(trailers) Anders Waldenborg
2018-11-20  8:14       ` Eric Sunshine
2018-11-18 11:44     ` [PATCH v3 4/5] strbuf: separate callback for strbuf_expand:ing literals Anders Waldenborg
2018-11-18 11:44     ` [PATCH v3 5/5] pretty: add support for separator option in %(trailers) Anders Waldenborg
2018-11-20  8:25       ` Eric Sunshine
2018-12-08 16:36 ` [PATCH v4 0/7] %(trailers) improvements in pretty format Anders Waldenborg
2018-12-08 16:36   ` [PATCH v4 1/7] doc: group pretty-format.txt placeholders descriptions Anders Waldenborg
2018-12-08 16:36   ` [PATCH v4 2/7] pretty: allow %(trailers) options with explicit value Anders Waldenborg
2018-12-10  8:45     ` Junio C Hamano
2018-12-18 21:30       ` Anders Waldenborg
2019-01-29 16:55         ` Jeff King
2019-01-29 21:23           ` Anders Waldenborg
     [not found]             ` <CAL21Bmmx=EO+R2t+KviNekDhU3fc0wjCcmUmbzLa14bb0PAmHA@mail.gmail.com>
2019-01-31 18:46               ` Anders Waldenborg
2019-02-02  9:14                 ` Оля Тележная
2018-12-08 16:36   ` [PATCH v4 3/7] pretty: single return path in %(trailers) handling Anders Waldenborg
2018-12-08 16:36   ` [PATCH v4 4/7] pretty: allow showing specific trailers Anders Waldenborg
2018-12-10  8:56     ` Junio C Hamano
2018-12-08 16:36   ` [PATCH v4 5/7] pretty: add support for "valueonly" option in %(trailers) Anders Waldenborg
2018-12-08 16:36   ` [PATCH v4 6/7] strbuf: separate callback for strbuf_expand:ing literals Anders Waldenborg
2018-12-08 16:36   ` [PATCH v4 7/7] pretty: add support for separator option in %(trailers) Anders Waldenborg
2019-01-28 21:33 ` [PATCH v5 0/7] %(trailers) improvements in pretty format Anders Waldenborg
2019-01-28 21:33   ` [PATCH v5 1/7] doc: group pretty-format.txt placeholders descriptions Anders Waldenborg
2019-01-28 21:33   ` [PATCH v5 2/7] pretty: Allow %(trailers) options with explicit value Anders Waldenborg
2019-01-28 22:38     ` Junio C Hamano
2019-01-29  6:45       ` Anders Waldenborg
2019-01-29 16:57         ` Jeff King
2019-01-29  6:49       ` [PATCH v5 2/7 update] pretty: allow " Anders Waldenborg
2019-01-28 21:33   ` [PATCH v5 3/7] pretty: single return path in %(trailers) handling Anders Waldenborg
2019-01-28 21:33   ` [PATCH v5 4/7] pretty: allow showing specific trailers Anders Waldenborg
2019-01-28 21:33   ` [PATCH v5 5/7] pretty: add support for "valueonly" option in %(trailers) Anders Waldenborg
2019-01-28 21:33   ` [PATCH v5 6/7] strbuf: separate callback for strbuf_expand:ing literals Anders Waldenborg
2019-01-28 21:33   ` [PATCH v5 7/7] pretty: add support for separator option in %(trailers) Anders Waldenborg

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).