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 <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Taylor Blau committed
Oct 1, 2017 at 22:25 UTC
67a20a0010224255bbd5bb9fa4f95595c3e1ba7c
3 files changed
+68
-10
Documentation/git-for-each-ref.txt
+4
-1
@@ -218,7 +218,10 @@ blank line. The optional GPG signature is `contents:signature`. The
218
first `N` lines of the message is obtained using `contents:lines=N`.
219
Additionally, the trailers as interpreted by linkgit:git-interpret-trailers[1]
220
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`.
225
226
For sorting purposes, fields with numeric values sort in numeric order
227
(`objectsize`, `authordate`, `committerdate`, `creatordate`, `taggerdate`).
ref-filter.c
+23
-9
@@ -82,6 +82,7 @@ static struct used_atom {
82
} remote_ref;
83
struct {
84
enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
85
+ struct process_trailer_options trailer_opts;
86
unsigned int nlines;
87
} contents;
88
struct {
@@ -182,9 +183,23 @@ static void subject_atom_parser(const struct ref_format *format, struct used_ato
183
184
static void trailers_atom_parser(const struct ref_format *format, struct used_atom *atom, const char *arg)
185
{
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(¶ms, 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
+ }
201
atom->u.contents.option = C_TRAILERS;
202
+ string_list_clear(¶ms, 0);
203
}
204
205
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
1057
name++;
1058
if (strcmp(name, "subject") &&
1059
strcmp(name, "body") &&
1045
- strcmp(name, "trailers") &&
1060
+ !starts_with(name, "trailers") &&
1061
!starts_with(name, "contents"))
1062
continue;
1063
if (!subpos)
@@ -1067,13 +1082,12 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj
1082
append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
1083
v->s = strbuf_detach(&s, NULL);
1084
} else if (atom->u.contents.option == C_TRAILERS) {
1070
- struct trailer_info info;
1085
+ struct strbuf s = STRBUF_INIT;
1086
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);
1091
} else if (atom->u.contents.option == C_BARE)
1092
v->s = xstrdup(subpos);
1093
}
t/t6300-for-each-ref.sh
+41
@@ -610,6 +610,9 @@ Acked-by: A U Thor
610
<author@example.com>
611
EOF
612
613
+unfold () {
614
+ perl -0pe 's/\n\s+/ /g'
615
+}
616
617
test_expect_success 'set up trailers for next test' '
618
echo "Some contents" > two &&
@@ -623,6 +626,44 @@ test_expect_success 'set up trailers for next test' '
626
EOF
627
'
628
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
+
667
test_expect_success 'basic atom: head contents:trailers' '
668
git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
669
sanitize_pgp <actual >actual.clean &&