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 <linusa@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Arver committed Mar 15, 2024 at 06:55 UTC 65b4ad82b81e1a1f4afbb7f4974384d7db479c0a
3 files changed +30 -19
t/t4205-log-pretty-formats.sh
+6 -6
@@ -675,7 +675,7 @@ test_expect_success '%(trailers:only=no,only=true) shows only "key: value" trail
675 test_cmp expect actual
676 '
677
678 -test_expect_success '%(trailers:unfold) unfolds trailers' '
678 +test_expect_failure '%(trailers:unfold) unfolds trailers' '
679 git log --no-walk --pretty="%(trailers:unfold)" >actual &&
680 {
681 unfold <trailers &&
@@ -737,7 +737,7 @@ test_expect_success '%(trailers:key=foo,unfold) properly unfolds' '
737 test_cmp expect actual
738 '
739
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' '
741 git log --no-walk --pretty="format:%(trailers:key=Acked-by,only=no)" >actual &&
742 {
743 echo "Acked-by: A U Thor <author@example.com>" &&
@@ -752,7 +752,7 @@ test_expect_success '%(trailers:key) without value is error' '
752 test_cmp expect actual
753 '
754
755 -test_expect_success '%(trailers:keyonly) shows only keys' '
755 +test_expect_failure '%(trailers:keyonly) shows only keys' '
756 git log --no-walk --pretty="format:%(trailers:keyonly)" >actual &&
757 test_write_lines \
758 "Signed-off-by" \
@@ -774,7 +774,7 @@ test_expect_success '%(trailers:key=foo,valueonly) shows only value' '
774 test_cmp expect actual
775 '
776
777 -test_expect_success '%(trailers:valueonly) shows only values' '
777 +test_expect_failure '%(trailers:valueonly) shows only values' '
778 git log --no-walk --pretty="format:%(trailers:valueonly)" >actual &&
779 test_write_lines \
780 "A U Thor <author@example.com>" \
@@ -813,7 +813,7 @@ test_expect_success 'pretty format %(trailers:separator=X,unfold) changes separa
813 test_cmp expect actual
814 '
815
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' '
817 git log --no-walk --pretty=format:"X%(trailers:key_value_separator=%x00)X" >actual &&
818 (
819 printf "XSigned-off-by\0A U Thor <author@example.com>\n" &&
@@ -824,7 +824,7 @@ test_expect_success 'pretty format %(trailers:key_value_separator) changes key-v
824 test_cmp expect actual
825 '
826
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' '
828 git log --no-walk --pretty=format:"X%(trailers:key_value_separator=%x00,unfold)X" >actual &&
829 (
830 printf "XSigned-off-by\0A U Thor <author@example.com>\n" &&
t/t6300-for-each-ref.sh
+14 -2
@@ -1446,7 +1446,19 @@ test_trailer_option () {
1446 '
1447 }
1448
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' \
1462 'trailers:unfold' <<-EOF
1463 $(unfold <trailers)
1464
@@ -1530,7 +1542,7 @@ test_trailer_option '%(trailers:key=foo,unfold) properly unfolds' \
1542
1543 EOF
1544
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' \
1546 'trailers:key=Signed-off-by,only=no' <<-EOF
1547 Signed-off-by: A U Thor <author@example.com>
1548 $(grep patch.description <trailers)
trailer.c
+10 -11
@@ -1085,21 +1085,21 @@ void trailer_info_release(struct trailer_info *info)
1085 }
1086
1087 static void format_trailer_info(const struct process_trailer_options *opts,
1088 - const struct trailer_info *info,
1088 + struct list_head *trailers,
1089 struct strbuf *out)
1090 {
1091 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;
1094
1098 - if (separator_pos >= 1) {
1095 + list_for_each(pos, trailers) {
1096 + item = list_entry(pos, struct trailer_item, list);
1097 + if (item->token) {
1098 struct strbuf tok = STRBUF_INIT;
1099 struct strbuf val = STRBUF_INIT;
1100 + strbuf_addstr(&tok, item->token);
1101 + strbuf_addstr(&val, item->value);
1102
1102 - parse_trailer(&tok, &val, NULL, trailer, separator_pos);
1103 if (!opts->filter || opts->filter(&tok, opts->filter_data)) {
1104 if (opts->unfold)
1105 unfold_value(&val);
@@ -1126,13 +1126,12 @@ static void format_trailer_info(const struct process_trailer_options *opts,
1126 if (opts->separator && out->len != origlen) {
1127 strbuf_addbuf(out, opts->separator);
1128 }
1129 - strbuf_addstr(out, trailer);
1129 + strbuf_addstr(out, item->value);
1130 if (opts->separator) {
1131 strbuf_rtrim(out);
1132 }
1133 }
1134 }
1135 -
1135 }
1136
1137 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,
1150 strbuf_add(out, msg + info.trailer_block_start,
1151 info.trailer_block_end - info.trailer_block_start);
1152 } else
1154 - format_trailer_info(opts, &info, out);
1153 + format_trailer_info(opts, &trailer_objects, out);
1154
1155 free_trailers(&trailer_objects);
1156 trailer_info_release(&info);