trailer: teach iterator about non-trailer lines

Previously the iterator did not iterate over non-trailer lines. This was somewhat unfortunate, because trailer blocks could have non-trailer lines in them since 146245063e (trailer: allow non-trailers in trailer block, 2016-10-21), which was before the iterator was created in f0939a0eb1 (trailer: add interface for iterating over commit trailers, 2020-09-27). So if trailer API users wanted to iterate over all lines in a trailer block (including non-trailer lines), they could not use the iterator and were forced to use the lower-level trailer_info struct directly (which provides a raw string array that includes all lines in the trailer block). Change the iterator's behavior so that we also iterate over non-trailer lines, instead of skipping over them. The new "raw" member of the iterator allows API users to access previously inaccessible non-trailer lines. Reword the variable "trailer" to just "line" because this variable can now hold both trailer lines _and_ non-trailer lines. The new "raw" member is important because anyone currently not using the iterator is using trailer_info's raw string array directly to access lines to check what the combined key + value looks like. If we didn't provide a "raw" member here, iterator users would have to re-construct the unparsed line by concatenating the key and value back together again Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Arver committed May 2, 2024 at 04:54 UTC 3be65e6ee2f585a0aad0363c8ce7d966a6f8c2b3
3 files changed +23 -12
t/unit-tests/t-trailer.c
+11 -5
@@ -1,7 +1,7 @@
1 #include "test-lib.h"
2 #include "trailer.h"
3
4 -static void t_trailer_iterator(const char *msg, size_t num_expected_trailers)
4 +static void t_trailer_iterator(const char *msg, size_t num_expected)
5 {
6 struct trailer_iterator iter;
7 size_t i = 0;
@@ -11,7 +11,7 @@ static void t_trailer_iterator(const char *msg, size_t num_expected_trailers)
11 i++;
12 trailer_iterator_release(&iter);
13
14 - check_uint(i, ==, num_expected_trailers);
14 + check_uint(i, ==, num_expected);
15 }
16
17 static void run_t_trailer_iterator(void)
@@ -19,7 +19,7 @@ static void run_t_trailer_iterator(void)
19 static struct test_cases {
20 const char *name;
21 const char *msg;
22 - size_t num_expected_trailers;
22 + size_t num_expected;
23 } tc[] = {
24 {
25 "empty input",
@@ -119,7 +119,13 @@ static void run_t_trailer_iterator(void)
119 "not a trailer line\n"
120 "not a trailer line\n"
121 "Signed-off-by: x\n",
122 - 1
122 + /*
123 + * Even though there is only really 1 real "trailer"
124 + * (Signed-off-by), we still have 4 trailer objects
125 + * because we still want to iterate through the entire
126 + * block.
127 + */
128 + 4
129 },
130 {
131 "with non-trailer lines (one too many) in trailer block",
@@ -162,7 +168,7 @@ static void run_t_trailer_iterator(void)
168
169 for (int i = 0; i < sizeof(tc) / sizeof(tc[0]); i++) {
170 TEST(t_trailer_iterator(tc[i].msg,
165 - tc[i].num_expected_trailers),
171 + tc[i].num_expected),
172 "%s", tc[i].name);
173 }
174 }
trailer.c
+5 -7
@@ -1146,17 +1146,15 @@ void trailer_iterator_init(struct trailer_iterator *iter, const char *msg)
1146
1147 int trailer_iterator_advance(struct trailer_iterator *iter)
1148 {
1149 - while (iter->internal.cur < iter->internal.info.trailer_nr) {
1150 - char *trailer = iter->internal.info.trailers[iter->internal.cur++];
1151 - int separator_pos = find_separator(trailer, separators);
1152 -
1153 - if (separator_pos < 1)
1154 - continue; /* not a real trailer */
1149 + if (iter->internal.cur < iter->internal.info.trailer_nr) {
1150 + char *line = iter->internal.info.trailers[iter->internal.cur++];
1151 + int separator_pos = find_separator(line, separators);
1152
1153 + iter->raw = line;
1154 strbuf_reset(&iter->key);
1155 strbuf_reset(&iter->val);
1156 parse_trailer(&iter->key, &iter->val, NULL,
1159 - trailer, separator_pos);
1157 + line, separator_pos);
1158 /* Always unfold values during iteration. */
1159 unfold_value(&iter->val);
1160 return 1;
trailer.h
+7
@@ -125,6 +125,13 @@ void format_trailers_from_commit(const struct process_trailer_options *,
125 * trailer_iterator_release(&iter);
126 */
127 struct trailer_iterator {
128 + /*
129 + * Raw line (e.g., "foo: bar baz") before being parsed as a trailer
130 + * key/val pair as part of a trailer block (as the "key" and "val"
131 + * fields below). If a line fails to parse as a trailer, then the "key"
132 + * will be the entire line and "val" will be the empty string.
133 + */
134 + const char *raw;
135 struct strbuf key;
136 struct strbuf val;
137