Skip to content

Commit b1d31c8

Browse files
jacob-kellergitster
authored andcommitted
ref-filter: add support to display trailers as part of contents
Add %(trailers) and %(contents:trailers) to display the trailers as interpreted by trailer_info_get. Update documentation and add a test for the new feature. Signed-off-by: Jacob Keller <jacob.keller@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent d9f31fb commit b1d31c8

3 files changed

Lines changed: 49 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
@@ -165,6 +165,8 @@ of all lines of the commit message up to the first blank line. The next
165165
line is 'contents:body', where body is all of the lines after the first
166166
blank line. The optional GPG signature is `contents:signature`. The
167167
first `N` lines of the message is obtained using `contents:lines=N`.
168+
Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1]
169+
are obtained as 'contents:trailers'.
168170

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

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/t6300-for-each-ref.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,4 +553,30 @@ test_expect_success 'Verify sort with multiple keys' '
553553
refs/tags/bogo refs/tags/master > actual &&
554554
test_cmp expected actual
555555
'
556+
557+
cat >trailers <<EOF
558+
Reviewed-by: A U Thor <author@example.com>
559+
Signed-off-by: A U Thor <author@example.com>
560+
EOF
561+
562+
test_expect_success 'basic atom: head contents:trailers' '
563+
echo "Some contents" > two &&
564+
git add two &&
565+
git commit -F - <<-EOF &&
566+
trailers: this commit message has trailers
567+
568+
Some message contents
569+
570+
$(cat trailers)
571+
EOF
572+
git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
573+
sanitize_pgp <actual >actual.clean &&
574+
# git for-each-ref ends with a blank line
575+
cat >expect <<-EOF &&
576+
$(cat trailers)
577+
578+
EOF
579+
test_cmp expect actual.clean
580+
'
581+
556582
test_done

0 commit comments

Comments
 (0)