Skip to content

Commit d9f31fb

Browse files
jacob-kellergitster
authored andcommitted
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>
1 parent 967dfd4 commit d9f31fb

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

Documentation/pretty-formats.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ endif::git-rev-list[]
199199
than given and there are spaces on its left, use those spaces
200200
- '%><(<N>)', '%><|(<N>)': similar to '% <(<N>)', '%<|(<N>)'
201201
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]
202204

203205
NOTE: Some placeholders may depend on other options given to the
204206
revision traversal engine. For example, the `%g*` reflog options will

pretty.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "color.h"
1111
#include "reflog-walk.h"
1212
#include "gpg-interface.h"
13+
#include "trailer.h"
1314

1415
static char *user_format;
1516
static struct cmt_fmt_map {
@@ -889,6 +890,16 @@ const char *format_subject(struct strbuf *sb, const char *msg,
889890
return msg;
890891
}
891892

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+
892903
static void parse_commit_message(struct format_commit_context *c)
893904
{
894905
const char *msg = c->message + c->message_off;
@@ -1292,6 +1303,12 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
12921303
strbuf_addstr(sb, msg + c->body_off);
12931304
return 1;
12941305
}
1306+
1307+
if (starts_with(placeholder, "(trailers)")) {
1308+
format_trailers(sb, msg + c->subject_off);
1309+
return strlen("(trailers)");
1310+
}
1311+
12951312
return 0; /* unknown placeholder */
12961313
}
12971314

t/t4205-log-pretty-formats.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,4 +535,30 @@ test_expect_success 'clean log decoration' '
535535
test_cmp expected actual1
536536
'
537537

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+
538564
test_done

0 commit comments

Comments
 (0)