Skip to content

Commit f008159

Browse files
committed
Merge branch 'jk/trailers-placeholder-in-pretty'
In addition to %(subject), %(body), "log --pretty=format:..." learned a new placeholder %(trailers). * jk/trailers-placeholder-in-pretty: ref-filter: add support to display trailers as part of contents pretty: add %(trailers) format for displaying trailers of a commit message
2 parents 3aead1c + b1d31c8 commit f008159

6 files changed

Lines changed: 93 additions & 1 deletion

File tree

Documentation/git-for-each-ref.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ of all lines of the commit message up to the first blank line. The next
168168
line is 'contents:body', where body is all of the lines after the first
169169
blank line. The optional GPG signature is `contents:signature`. The
170170
first `N` lines of the message is obtained using `contents:lines=N`.
171+
Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1]
172+
are obtained as 'contents:trailers'.
171173

172174
For sorting purposes, fields with numeric values sort in numeric order
173175
(`objectsize`, `authordate`, `committerdate`, `creatordate`, `taggerdate`).

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

ref-filter.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "utf8.h"
1414
#include "git-compat-util.h"
1515
#include "version.h"
16+
#include "trailer.h"
1617

1718
typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
1819

@@ -40,7 +41,7 @@ static struct used_atom {
4041
enum { RR_NORMAL, RR_SHORTEN, RR_TRACK, RR_TRACKSHORT }
4142
remote_ref;
4243
struct {
43-
enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB } option;
44+
enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
4445
unsigned int nlines;
4546
} contents;
4647
enum { O_FULL, O_SHORT } objectname;
@@ -85,6 +86,13 @@ static void subject_atom_parser(struct used_atom *atom, const char *arg)
8586
atom->u.contents.option = C_SUB;
8687
}
8788

89+
static void trailers_atom_parser(struct used_atom *atom, const char *arg)
90+
{
91+
if (arg)
92+
die(_("%%(trailers) does not take arguments"));
93+
atom->u.contents.option = C_TRAILERS;
94+
}
95+
8896
static void contents_atom_parser(struct used_atom *atom, const char *arg)
8997
{
9098
if (!arg)
@@ -95,6 +103,8 @@ static void contents_atom_parser(struct used_atom *atom, const char *arg)
95103
atom->u.contents.option = C_SIG;
96104
else if (!strcmp(arg, "subject"))
97105
atom->u.contents.option = C_SUB;
106+
else if (!strcmp(arg, "trailers"))
107+
atom->u.contents.option = C_TRAILERS;
98108
else if (skip_prefix(arg, "lines=", &arg)) {
99109
atom->u.contents.option = C_LINES;
100110
if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
@@ -194,6 +204,7 @@ static struct {
194204
{ "creatordate", FIELD_TIME },
195205
{ "subject", FIELD_STR, subject_atom_parser },
196206
{ "body", FIELD_STR, body_atom_parser },
207+
{ "trailers", FIELD_STR, trailers_atom_parser },
197208
{ "contents", FIELD_STR, contents_atom_parser },
198209
{ "upstream", FIELD_STR, remote_ref_atom_parser },
199210
{ "push", FIELD_STR, remote_ref_atom_parser },
@@ -785,6 +796,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
785796
name++;
786797
if (strcmp(name, "subject") &&
787798
strcmp(name, "body") &&
799+
strcmp(name, "trailers") &&
788800
!starts_with(name, "contents"))
789801
continue;
790802
if (!subpos)
@@ -808,6 +820,14 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
808820
/* Size is the length of the message after removing the signature */
809821
append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
810822
v->s = strbuf_detach(&s, NULL);
823+
} else if (atom->u.contents.option == C_TRAILERS) {
824+
struct trailer_info info;
825+
826+
/* Search for trailer info */
827+
trailer_info_get(&info, subpos);
828+
v->s = xmemdupz(info.trailer_start,
829+
info.trailer_end - info.trailer_start);
830+
trailer_info_release(&info);
811831
} else if (atom->u.contents.option == C_BARE)
812832
v->s = xstrdup(subpos);
813833
}

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

t/t6300-for-each-ref.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,4 +563,29 @@ test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
563563
test_cmp expect actual
564564
'
565565

566+
cat >trailers <<EOF
567+
Reviewed-by: A U Thor <author@example.com>
568+
Signed-off-by: A U Thor <author@example.com>
569+
EOF
570+
571+
test_expect_success 'basic atom: head contents:trailers' '
572+
echo "Some contents" > two &&
573+
git add two &&
574+
git commit -F - <<-EOF &&
575+
trailers: this commit message has trailers
576+
577+
Some message contents
578+
579+
$(cat trailers)
580+
EOF
581+
git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
582+
sanitize_pgp <actual >actual.clean &&
583+
# git for-each-ref ends with a blank line
584+
cat >expect <<-EOF &&
585+
$(cat trailers)
586+
587+
EOF
588+
test_cmp expect actual.clean
589+
'
590+
566591
test_done

0 commit comments

Comments
 (0)