Skip to content

Commit 67a20a0

Browse files
ttaylorrgitster
authored andcommitted
ref-filter.c: use trailer_opts to format trailers
Fill trailer_opts with "unfold" and "only" to match the sub-arguments given to the "%(trailers)" atom. Then, let's use the filled trailer_opts instance with 'format_trailers_from_commit' in order to format trailers in the desired manner. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 624b44d commit 67a20a0

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,10 @@ blank line. The optional GPG signature is `contents:signature`. The
218218
first `N` lines of the message is obtained using `contents:lines=N`.
219219
Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1]
220220
are obtained as `trailers` (or by using the historical alias
221-
`contents:trailers`).
221+
`contents:trailers`). Non-trailer lines from the trailer block can be omitted
222+
with `trailers:only`. Whitespace-continuations can be removed from trailers so
223+
that each trailer appears on a line by itself with its full content with
224+
`trailers:unfold`. Both can be used together as `trailers:unfold,only`.
222225

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

ref-filter.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ static struct used_atom {
8282
} remote_ref;
8383
struct {
8484
enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
85+
struct process_trailer_options trailer_opts;
8586
unsigned int nlines;
8687
} contents;
8788
struct {
@@ -182,9 +183,23 @@ static void subject_atom_parser(const struct ref_format *format, struct used_ato
182183

183184
static void trailers_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
184185
{
185-
if (arg)
186-
die(_("%%(trailers) does not take arguments"));
186+
struct string_list params = STRING_LIST_INIT_DUP;
187+
int i;
188+
189+
if (arg) {
190+
string_list_split(&params, arg, ',', -1);
191+
for (i = 0; i < params.nr; i++) {
192+
const char *s = params.items[i].string;
193+
if (!strcmp(s, "unfold"))
194+
atom->u.contents.trailer_opts.unfold = 1;
195+
else if (!strcmp(s, "only"))
196+
atom->u.contents.trailer_opts.only_trailers = 1;
197+
else
198+
die(_("unknown %%(trailers) argument: %s"), s);
199+
}
200+
}
187201
atom->u.contents.option = C_TRAILERS;
202+
string_list_clear(&params, 0);
188203
}
189204

190205
static void contents_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
@@ -1042,7 +1057,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
10421057
name++;
10431058
if (strcmp(name, "subject") &&
10441059
strcmp(name, "body") &&
1045-
strcmp(name, "trailers") &&
1060+
!starts_with(name, "trailers") &&
10461061
!starts_with(name, "contents"))
10471062
continue;
10481063
if (!subpos)
@@ -1067,13 +1082,12 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
10671082
append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
10681083
v->s = strbuf_detach(&s, NULL);
10691084
} else if (atom->u.contents.option == C_TRAILERS) {
1070-
struct trailer_info info;
1085+
struct strbuf s = STRBUF_INIT;
10711086

1072-
/* Search for trailer info */
1073-
trailer_info_get(&info, subpos);
1074-
v->s = xmemdupz(info.trailer_start,
1075-
info.trailer_end - info.trailer_start);
1076-
trailer_info_release(&info);
1087+
/* Format the trailer info according to the trailer_opts given */
1088+
format_trailers_from_commit(&s, subpos, &atom->u.contents.trailer_opts);
1089+
1090+
v->s = strbuf_detach(&s, NULL);
10771091
} else if (atom->u.contents.option == C_BARE)
10781092
v->s = xstrdup(subpos);
10791093
}

t/t6300-for-each-ref.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,9 @@ Acked-by: A U Thor
610610
611611
EOF
612612

613+
unfold () {
614+
perl -0pe 's/\n\s+/ /g'
615+
}
613616

614617
test_expect_success 'set up trailers for next test' '
615618
echo "Some contents" > two &&
@@ -623,6 +626,44 @@ test_expect_success 'set up trailers for next test' '
623626
EOF
624627
'
625628

629+
test_expect_success '%(trailers:unfold) unfolds trailers' '
630+
git for-each-ref --format="%(trailers:unfold)" refs/heads/master >actual &&
631+
{
632+
unfold <trailers
633+
echo
634+
} >expect &&
635+
test_cmp expect actual
636+
'
637+
638+
test_expect_success '%(trailers:only) shows only "key: value" trailers' '
639+
git for-each-ref --format="%(trailers:only)" refs/heads/master >actual &&
640+
{
641+
grep -v patch.description <trailers &&
642+
echo
643+
} >expect &&
644+
test_cmp expect actual
645+
'
646+
647+
test_expect_success '%(trailers:only) and %(trailers:unfold) work together' '
648+
git for-each-ref --format="%(trailers:only,unfold)" refs/heads/master >actual &&
649+
git for-each-ref --format="%(trailers:unfold,only)" refs/heads/master >reverse &&
650+
test_cmp actual reverse &&
651+
{
652+
grep -v patch.description <trailers | unfold &&
653+
echo
654+
} >expect &&
655+
test_cmp expect actual
656+
'
657+
658+
test_expect_success '%(trailers) rejects unknown trailers arguments' '
659+
# error message cannot be checked under i18n
660+
cat >expect <<-EOF &&
661+
fatal: unknown %(trailers) argument: unsupported
662+
EOF
663+
test_must_fail git for-each-ref --format="%(trailers:unsupported)" 2>actual &&
664+
test_i18ncmp expect actual
665+
'
666+
626667
test_expect_success 'basic atom: head contents:trailers' '
627668
git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
628669
sanitize_pgp <actual >actual.clean &&

0 commit comments

Comments
 (0)