Skip to content

Commit 3be65e6

Browse files
listxgitster
authored andcommitted
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 1462450 (trailer: allow non-trailers in trailer block, 2016-10-21), which was before the iterator was created in f0939a0 (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 <[email protected]>
1 parent 56b0488 commit 3be65e6

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

t/unit-tests/t-trailer.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "test-lib.h"
22
#include "trailer.h"
33

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)
55
{
66
struct trailer_iterator iter;
77
size_t i = 0;
@@ -11,15 +11,15 @@ static void t_trailer_iterator(const char *msg, size_t num_expected_trailers)
1111
i++;
1212
trailer_iterator_release(&iter);
1313

14-
check_uint(i, ==, num_expected_trailers);
14+
check_uint(i, ==, num_expected);
1515
}
1616

1717
static void run_t_trailer_iterator(void)
1818
{
1919
static struct test_cases {
2020
const char *name;
2121
const char *msg;
22-
size_t num_expected_trailers;
22+
size_t num_expected;
2323
} tc[] = {
2424
{
2525
"empty input",
@@ -119,7 +119,13 @@ static void run_t_trailer_iterator(void)
119119
"not a trailer line\n"
120120
"not a trailer line\n"
121121
"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
123129
},
124130
{
125131
"with non-trailer lines (one too many) in trailer block",
@@ -162,7 +168,7 @@ static void run_t_trailer_iterator(void)
162168

163169
for (int i = 0; i < sizeof(tc) / sizeof(tc[0]); i++) {
164170
TEST(t_trailer_iterator(tc[i].msg,
165-
tc[i].num_expected_trailers),
171+
tc[i].num_expected),
166172
"%s", tc[i].name);
167173
}
168174
}

trailer.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,17 +1146,15 @@ void trailer_iterator_init(struct trailer_iterator *iter, const char *msg)
11461146

11471147
int trailer_iterator_advance(struct trailer_iterator *iter)
11481148
{
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);
11551152

1153+
iter->raw = line;
11561154
strbuf_reset(&iter->key);
11571155
strbuf_reset(&iter->val);
11581156
parse_trailer(&iter->key, &iter->val, NULL,
1159-
trailer, separator_pos);
1157+
line, separator_pos);
11601158
/* Always unfold values during iteration. */
11611159
unfold_value(&iter->val);
11621160
return 1;

trailer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ void format_trailers_from_commit(const struct process_trailer_options *,
125125
* trailer_iterator_release(&iter);
126126
*/
127127
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;
128135
struct strbuf key;
129136
struct strbuf val;
130137

0 commit comments

Comments
 (0)