pretty: add %(trailers) format for displaying trailers of a commit message

Recent patches have expanded on the trailers.c code and we have the builtin commant git-interpret-trailers which can be used to add or modify trailer lines. However, there is no easy way to simply display the trailers of a commit message. Add support for %(trailers) format modifier which will use the trailer_info_get() calls to read trailers in an identical way as git interpret-trailers does. Use a long format option instead of a short name so that future work can more easily unify ref-filter and pretty formats. Add documentation and tests for the same. Signed-off-by: Jacob Keller <jacob.keller@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jacob Keller committed Nov 18, 2016 at 16:58 UTC d9f31fbfe98e0e363a3c69aaf6badecc746afe6b
3 files changed +45
Documentation/pretty-formats.txt
+2
@@ -199,6 +199,8 @@ endif::git-rev-list[]
199 than given and there are spaces on its left, use those spaces
200 - '%><(<N>)', '%><|(<N>)': similar to '% <(<N>)', '%<|(<N>)'
201 respectively, but padding both sides (i.e. the text is centered)
202 +-%(trailers): display the trailers of the body as interpreted by
203 + linkgit:git-interpret-trailers[1]
204
205 NOTE: Some placeholders may depend on other options given to the
206 revision traversal engine. For example, the `%g*` reflog options will
pretty.c
+17
@@ -10,6 +10,7 @@
10 #include "color.h"
11 #include "reflog-walk.h"
12 #include "gpg-interface.h"
13 +#include "trailer.h"
14
15 static char *user_format;
16 static struct cmt_fmt_map {
@@ -889,6 +890,16 @@ const char *format_subject(struct strbuf *sb, const char *msg,
890 return msg;
891 }
892
893 +static void format_trailers(struct strbuf *sb, const char *msg)
894 +{
895 + struct trailer_info info;
896 +
897 + trailer_info_get(&info, msg);
898 + strbuf_add(sb, info.trailer_start,
899 + info.trailer_end - info.trailer_start);
900 + trailer_info_release(&info);
901 +}
902 +
903 static void parse_commit_message(struct format_commit_context *c)
904 {
905 const char *msg = c->message + c->message_off;
@@ -1292,6 +1303,12 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1303 strbuf_addstr(sb, msg + c->body_off);
1304 return 1;
1305 }
1306 +
1307 + if (starts_with(placeholder, "(trailers)")) {
1308 + format_trailers(sb, msg + c->subject_off);
1309 + return strlen("(trailers)");
1310 + }
1311 +
1312 return 0; /* unknown placeholder */
1313 }
1314
t/t4205-log-pretty-formats.sh
+26
@@ -535,4 +535,30 @@ test_expect_success 'clean log decoration' '
535 test_cmp expected actual1
536 '
537
538 +cat >trailers <<EOF
539 +Signed-off-by: A U Thor <author@example.com>
540 +Acked-by: A U Thor <author@example.com>
541 +[ v2 updated patch description ]
542 +Signed-off-by: A U Thor <author@example.com>
543 +EOF
544 +
545 +test_expect_success 'pretty format %(trailers) shows trailers' '
546 + echo "Some contents" >trailerfile &&
547 + git add trailerfile &&
548 + git commit -F - <<-EOF &&
549 + trailers: this commit message has trailers
550 +
551 + This commit is a test commit with trailers at the end. We parse this
552 + message and display the trailers using %bT
553 +
554 + $(cat trailers)
555 + EOF
556 + git log --no-walk --pretty="%(trailers)" >actual &&
557 + cat >expect <<-EOF &&
558 + $(cat trailers)
559 +
560 + EOF
561 + test_cmp expect actual
562 +'
563 +
564 test_done