Skip to content

Commit 7a5edbd

Browse files
ttaylorrgitster
authored andcommitted
ref-filter.c: parse trailers arguments with %(contents) atom
The %(contents) atom takes a contents "field" as its argument. Since "trailers" is one of those fields, extend contents_atom_parser to parse "trailers"'s arguments when used through "%(contents)", like: %(contents:trailers:unfold,only) A caveat: trailers_atom_parser expects NULL when no arguments are given (see: `parse_ref_filter_atom`). This is because string_list_split (given a maxsplit of -1) returns a 1-ary string_list* containing the given string if the delimiter could not be found using `strchr`. To simulate this behavior without teaching trailers_atom_parser to accept strings with length zero, conditionally pass NULL to trailers_atom_parser if the arguments portion of the argument to %(contents) is empty. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 67a20a0 commit 7a5edbd

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

ref-filter.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,10 @@ static void contents_atom_parser(const struct ref_format *format, struct used_at
212212
atom->u.contents.option = C_SIG;
213213
else if (!strcmp(arg, "subject"))
214214
atom->u.contents.option = C_SUB;
215-
else if (!strcmp(arg, "trailers"))
216-
atom->u.contents.option = C_TRAILERS;
217-
else if (skip_prefix(arg, "lines=", &arg)) {
215+
else if (skip_prefix(arg, "trailers", &arg)) {
216+
skip_prefix(arg, ":", &arg);
217+
trailers_atom_parser(format, atom, *arg ? arg : NULL);
218+
} else if (skip_prefix(arg, "lines=", &arg)) {
218219
atom->u.contents.option = C_LINES;
219220
if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
220221
die(_("positive value expected contents:lines=%s"), arg);

t/t6300-for-each-ref.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,35 @@ test_expect_success '%(trailers:only) and %(trailers:unfold) work together' '
655655
test_cmp expect actual
656656
'
657657

658+
test_expect_success '%(contents:trailers:unfold) unfolds trailers' '
659+
git for-each-ref --format="%(contents:trailers:unfold)" refs/heads/master >actual &&
660+
{
661+
unfold <trailers
662+
echo
663+
} >expect &&
664+
test_cmp expect actual
665+
'
666+
667+
test_expect_success '%(contents:trailers:only) shows only "key: value" trailers' '
668+
git for-each-ref --format="%(contents:trailers:only)" refs/heads/master >actual &&
669+
{
670+
grep -v patch.description <trailers &&
671+
echo
672+
} >expect &&
673+
test_cmp expect actual
674+
'
675+
676+
test_expect_success '%(contents:trailers:only) and %(contents:trailers:unfold) work together' '
677+
git for-each-ref --format="%(contents:trailers:only,unfold)" refs/heads/master >actual &&
678+
git for-each-ref --format="%(contents:trailers:unfold,only)" refs/heads/master >reverse &&
679+
test_cmp actual reverse &&
680+
{
681+
grep -v patch.description <trailers | unfold &&
682+
echo
683+
} >expect &&
684+
test_cmp expect actual
685+
'
686+
658687
test_expect_success '%(trailers) rejects unknown trailers arguments' '
659688
# error message cannot be checked under i18n
660689
cat >expect <<-EOF &&
@@ -664,6 +693,15 @@ test_expect_success '%(trailers) rejects unknown trailers arguments' '
664693
test_i18ncmp expect actual
665694
'
666695

696+
test_expect_success '%(contents:trailers) rejects unknown trailers arguments' '
697+
# error message cannot be checked under i18n
698+
cat >expect <<-EOF &&
699+
fatal: unknown %(trailers) argument: unsupported
700+
EOF
701+
test_must_fail git for-each-ref --format="%(contents:trailers:unsupported)" 2>actual &&
702+
test_i18ncmp expect actual
703+
'
704+
667705
test_expect_success 'basic atom: head contents:trailers' '
668706
git for-each-ref --format="%(contents:trailers)" refs/heads/master >actual &&
669707
sanitize_pgp <actual >actual.clean &&

0 commit comments

Comments
 (0)