Skip to content

Commit 65b4ad8

Browse files
Linus Arvergitster
authored andcommitted
format_trailer_info(): use trailer_item objects
This is another preparatory refactor to unify the trailer formatters. Make format_trailer_info() operate on trailer_item objects, not the raw string array. We will continue to make improvements, culminating in the renaming of format_trailer_info() to format_trailers(), at which point the unification of these formatters will be complete. In order to avoid breaking t4205 and t6300, flip *_success to *_failure in the affected test cases. Add a temporary "test_trailer_option_expect_failure" wrapper which we will use along with "test_expect_failure" in the next commit to avoid breaking tests. When the dust settles with the refactors a few more commits later, we will drop the use of *_failure to make the tests truly pass again. When the preparatory refactors are complete, we'll be able to drop the use of *_failure that we introduce here. Signed-off-by: Linus Arver <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4f9b731 commit 65b4ad8

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

t/t4205-log-pretty-formats.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ test_expect_success '%(trailers:only=no,only=true) shows only "key: value" trail
675675
test_cmp expect actual
676676
'
677677

678-
test_expect_success '%(trailers:unfold) unfolds trailers' '
678+
test_expect_failure '%(trailers:unfold) unfolds trailers' '
679679
git log --no-walk --pretty="%(trailers:unfold)" >actual &&
680680
{
681681
unfold <trailers &&
@@ -737,7 +737,7 @@ test_expect_success '%(trailers:key=foo,unfold) properly unfolds' '
737737
test_cmp expect actual
738738
'
739739

740-
test_expect_success 'pretty format %(trailers:key=foo,only=no) also includes nontrailer lines' '
740+
test_expect_failure 'pretty format %(trailers:key=foo,only=no) also includes nontrailer lines' '
741741
git log --no-walk --pretty="format:%(trailers:key=Acked-by,only=no)" >actual &&
742742
{
743743
echo "Acked-by: A U Thor <[email protected]>" &&
@@ -752,7 +752,7 @@ test_expect_success '%(trailers:key) without value is error' '
752752
test_cmp expect actual
753753
'
754754

755-
test_expect_success '%(trailers:keyonly) shows only keys' '
755+
test_expect_failure '%(trailers:keyonly) shows only keys' '
756756
git log --no-walk --pretty="format:%(trailers:keyonly)" >actual &&
757757
test_write_lines \
758758
"Signed-off-by" \
@@ -774,7 +774,7 @@ test_expect_success '%(trailers:key=foo,valueonly) shows only value' '
774774
test_cmp expect actual
775775
'
776776

777-
test_expect_success '%(trailers:valueonly) shows only values' '
777+
test_expect_failure '%(trailers:valueonly) shows only values' '
778778
git log --no-walk --pretty="format:%(trailers:valueonly)" >actual &&
779779
test_write_lines \
780780
"A U Thor <[email protected]>" \
@@ -813,7 +813,7 @@ test_expect_success 'pretty format %(trailers:separator=X,unfold) changes separa
813813
test_cmp expect actual
814814
'
815815

816-
test_expect_success 'pretty format %(trailers:key_value_separator) changes key-value separator' '
816+
test_expect_failure 'pretty format %(trailers:key_value_separator) changes key-value separator' '
817817
git log --no-walk --pretty=format:"X%(trailers:key_value_separator=%x00)X" >actual &&
818818
(
819819
printf "XSigned-off-by\0A U Thor <[email protected]>\n" &&
@@ -824,7 +824,7 @@ test_expect_success 'pretty format %(trailers:key_value_separator) changes key-v
824824
test_cmp expect actual
825825
'
826826

827-
test_expect_success 'pretty format %(trailers:key_value_separator,unfold) changes key-value separator' '
827+
test_expect_failure 'pretty format %(trailers:key_value_separator,unfold) changes key-value separator' '
828828
git log --no-walk --pretty=format:"X%(trailers:key_value_separator=%x00,unfold)X" >actual &&
829829
(
830830
printf "XSigned-off-by\0A U Thor <[email protected]>\n" &&

t/t6300-for-each-ref.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,19 @@ test_trailer_option () {
14461446
'
14471447
}
14481448

1449-
test_trailer_option '%(trailers:unfold) unfolds trailers' \
1449+
# Just like test_trailer_option, but expect failure instead of success.
1450+
test_trailer_option_expect_failure () {
1451+
title=$1 option=$2
1452+
cat >expect
1453+
test_expect_failure "$title" '
1454+
git for-each-ref --format="%($option)" refs/heads/main >actual &&
1455+
test_cmp expect actual &&
1456+
git for-each-ref --format="%(contents:$option)" refs/heads/main >actual &&
1457+
test_cmp expect actual
1458+
'
1459+
}
1460+
1461+
test_trailer_option_expect_failure '%(trailers:unfold) unfolds trailers' \
14501462
'trailers:unfold' <<-EOF
14511463
$(unfold <trailers)
14521464
@@ -1530,7 +1542,7 @@ test_trailer_option '%(trailers:key=foo,unfold) properly unfolds' \
15301542
15311543
EOF
15321544

1533-
test_trailer_option '%(trailers:key=foo,only=no) also includes nontrailer lines' \
1545+
test_trailer_option_expect_failure '%(trailers:key=foo,only=no) also includes nontrailer lines' \
15341546
'trailers:key=Signed-off-by,only=no' <<-EOF
15351547
Signed-off-by: A U Thor <[email protected]>
15361548
$(grep patch.description <trailers)

trailer.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,21 +1085,21 @@ void trailer_info_release(struct trailer_info *info)
10851085
}
10861086

10871087
static void format_trailer_info(const struct process_trailer_options *opts,
1088-
const struct trailer_info *info,
1088+
struct list_head *trailers,
10891089
struct strbuf *out)
10901090
{
10911091
size_t origlen = out->len;
1092-
size_t i;
1093-
1094-
for (i = 0; i < info->trailer_nr; i++) {
1095-
char *trailer = info->trailers[i];
1096-
ssize_t separator_pos = find_separator(trailer, separators);
1092+
struct list_head *pos;
1093+
struct trailer_item *item;
10971094

1098-
if (separator_pos >= 1) {
1095+
list_for_each(pos, trailers) {
1096+
item = list_entry(pos, struct trailer_item, list);
1097+
if (item->token) {
10991098
struct strbuf tok = STRBUF_INIT;
11001099
struct strbuf val = STRBUF_INIT;
1100+
strbuf_addstr(&tok, item->token);
1101+
strbuf_addstr(&val, item->value);
11011102

1102-
parse_trailer(&tok, &val, NULL, trailer, separator_pos);
11031103
if (!opts->filter || opts->filter(&tok, opts->filter_data)) {
11041104
if (opts->unfold)
11051105
unfold_value(&val);
@@ -1126,13 +1126,12 @@ static void format_trailer_info(const struct process_trailer_options *opts,
11261126
if (opts->separator && out->len != origlen) {
11271127
strbuf_addbuf(out, opts->separator);
11281128
}
1129-
strbuf_addstr(out, trailer);
1129+
strbuf_addstr(out, item->value);
11301130
if (opts->separator) {
11311131
strbuf_rtrim(out);
11321132
}
11331133
}
11341134
}
1135-
11361135
}
11371136

11381137
void format_trailers_from_commit(const struct process_trailer_options *opts,
@@ -1151,7 +1150,7 @@ void format_trailers_from_commit(const struct process_trailer_options *opts,
11511150
strbuf_add(out, msg + info.trailer_block_start,
11521151
info.trailer_block_end - info.trailer_block_start);
11531152
} else
1154-
format_trailer_info(opts, &info, out);
1153+
format_trailer_info(opts, &trailer_objects, out);
11551154

11561155
free_trailers(&trailer_objects);
11571156
trailer_info_release(&info);

0 commit comments

Comments
 (0)