]> git.ipfire.org Git - thirdparty/git.git/blame - trailer.c
submodule: handle NULL value when parsing submodule.*.branch
[thirdparty/git.git] / trailer.c
CommitLineData
98750588 1#include "git-compat-util.h"
b2141fc1 2#include "config.h"
7ee24e18 3#include "environment.h"
f394e093 4#include "gettext.h"
f0a90b4e 5#include "string-list.h"
85039fb6 6#include "run-command.h"
61cfef4c 7#include "commit.h"
e1f89863 8#include "tempfile.h"
b1d78d77 9#include "trailer.h"
8966a394 10#include "list.h"
9385b5d7
CC
11/*
12 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
13 */
14
9385b5d7
CC
15struct conf_info {
16 char *name;
17 char *key;
18 char *command;
c364b7ef 19 char *cmd;
52fc319d
PB
20 enum trailer_where where;
21 enum trailer_if_exists if_exists;
22 enum trailer_if_missing if_missing;
9385b5d7
CC
23};
24
25static struct conf_info default_conf_info;
26
27struct trailer_item {
8966a394 28 struct list_head list;
14624506
JT
29 /*
30 * If this is not a trailer line, the line is stored in value
31 * (excluding the terminating newline) and token is NULL.
32 */
d65fd424
JT
33 char *token;
34 char *value;
cc71b0de
JT
35};
36
37struct arg_item {
38 struct list_head list;
39 char *token;
40 char *value;
9385b5d7
CC
41 struct conf_info conf;
42};
43
8966a394 44static LIST_HEAD(conf_head);
9385b5d7
CC
45
46static char *separators = ":";
47
e8c352c3
JT
48static int configured;
49
85039fb6
CC
50#define TRAILER_ARG_STRING "$ARG"
51
14624506
JT
52static const char *git_generated_prefixes[] = {
53 "Signed-off-by: ",
54 "(cherry picked from commit ",
55 NULL
56};
57
8966a394
JT
58/* Iterate over the elements of the list. */
59#define list_for_each_dir(pos, head, is_reverse) \
60 for (pos = is_reverse ? (head)->prev : (head)->next; \
61 pos != (head); \
62 pos = is_reverse ? pos->prev : pos->next)
63
52fc319d 64static int after_or_end(enum trailer_where where)
9385b5d7
CC
65{
66 return (where == WHERE_AFTER) || (where == WHERE_END);
67}
68
69/*
70 * Return the length of the string not including any final
71 * punctuation. E.g., the input "Signed-off-by:" would return
72 * 13, stripping the trailing punctuation but retaining
73 * internal punctuation.
74 */
75static size_t token_len_without_separator(const char *token, size_t len)
76{
77 while (len > 0 && !isalnum(token[len - 1]))
78 len--;
79 return len;
80}
81
cc71b0de 82static int same_token(struct trailer_item *a, struct arg_item *b)
9385b5d7 83{
14624506
JT
84 size_t a_len, b_len, min_len;
85
86 if (!a->token)
87 return 0;
88
89 a_len = token_len_without_separator(a->token, strlen(a->token));
90 b_len = token_len_without_separator(b->token, strlen(b->token));
91 min_len = (a_len > b_len) ? b_len : a_len;
9385b5d7
CC
92
93 return !strncasecmp(a->token, b->token, min_len);
94}
95
cc71b0de 96static int same_value(struct trailer_item *a, struct arg_item *b)
9385b5d7
CC
97{
98 return !strcasecmp(a->value, b->value);
99}
100
cc71b0de 101static int same_trailer(struct trailer_item *a, struct arg_item *b)
9385b5d7
CC
102{
103 return same_token(a, b) && same_value(a, b);
104}
4103818d 105
022349c3 106static inline int is_blank_line(const char *str)
2013d850
CC
107{
108 const char *s = str;
022349c3 109 while (*s && *s != '\n' && isspace(*s))
2013d850 110 s++;
022349c3 111 return !*s || *s == '\n';
2013d850
CC
112}
113
85039fb6
CC
114static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b)
115{
116 const char *ptr = strstr(sb->buf, a);
117 if (ptr)
118 strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));
119}
120
4103818d 121static void free_trailer_item(struct trailer_item *item)
cc71b0de
JT
122{
123 free(item->token);
124 free(item->value);
125 free(item);
126}
127
128static void free_arg_item(struct arg_item *item)
4103818d
CC
129{
130 free(item->conf.name);
131 free(item->conf.key);
132 free(item->conf.command);
c364b7ef 133 free(item->conf.cmd);
d65fd424
JT
134 free(item->token);
135 free(item->value);
4103818d
CC
136 free(item);
137}
138
b1d78d77
CC
139static char last_non_space_char(const char *s)
140{
141 int i;
142 for (i = strlen(s) - 1; i >= 0; i--)
143 if (!isspace(s[i]))
144 return s[i];
145 return '\0';
146}
147
d0d2344a 148static void print_tok_val(FILE *outfile, const char *tok, const char *val)
b1d78d77 149{
14624506
JT
150 char c;
151
152 if (!tok) {
153 fprintf(outfile, "%s\n", val);
154 return;
155 }
156
157 c = last_non_space_char(tok);
b1d78d77
CC
158 if (!c)
159 return;
160 if (strchr(separators, c))
d0d2344a 161 fprintf(outfile, "%s%s\n", tok, val);
b1d78d77 162 else
d0d2344a 163 fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
b1d78d77
CC
164}
165
56c493ed
JK
166static void print_all(FILE *outfile, struct list_head *head,
167 const struct process_trailer_options *opts)
b1d78d77 168{
8966a394 169 struct list_head *pos;
b1d78d77 170 struct trailer_item *item;
8966a394
JT
171 list_for_each(pos, head) {
172 item = list_entry(pos, struct trailer_item, list);
56c493ed
JK
173 if ((!opts->trim_empty || strlen(item->value) > 0) &&
174 (!opts->only_trailers || item->token))
d0d2344a 175 print_tok_val(outfile, item->token, item->value);
b1d78d77
CC
176 }
177}
178
cc71b0de 179static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok)
4103818d 180{
241b5d3e 181 struct trailer_item *new_item = xcalloc(1, sizeof(*new_item));
17651326
BW
182 new_item->token = arg_tok->token;
183 new_item->value = arg_tok->value;
cc71b0de
JT
184 arg_tok->token = arg_tok->value = NULL;
185 free_arg_item(arg_tok);
17651326 186 return new_item;
4103818d
CC
187}
188
189static void add_arg_to_input_list(struct trailer_item *on_tok,
cc71b0de 190 struct arg_item *arg_tok)
8966a394 191{
cc71b0de
JT
192 int aoe = after_or_end(arg_tok->conf.where);
193 struct trailer_item *to_add = trailer_from_arg(arg_tok);
194 if (aoe)
195 list_add(&to_add->list, &on_tok->list);
8966a394 196 else
cc71b0de 197 list_add_tail(&to_add->list, &on_tok->list);
4103818d
CC
198}
199
200static int check_if_different(struct trailer_item *in_tok,
cc71b0de 201 struct arg_item *arg_tok,
8966a394
JT
202 int check_all,
203 struct list_head *head)
4103818d 204{
52fc319d 205 enum trailer_where where = arg_tok->conf.where;
8966a394 206 struct list_head *next_head;
4103818d 207 do {
4103818d
CC
208 if (same_trailer(in_tok, arg_tok))
209 return 0;
210 /*
211 * if we want to add a trailer after another one,
212 * we have to check those before this one
213 */
8966a394
JT
214 next_head = after_or_end(where) ? in_tok->list.prev
215 : in_tok->list.next;
216 if (next_head == head)
217 break;
218 in_tok = list_entry(next_head, struct trailer_item, list);
4103818d
CC
219 } while (check_all);
220 return 1;
221}
222
c364b7ef 223static char *apply_command(struct conf_info *conf, const char *arg)
85039fb6
CC
224{
225 struct strbuf cmd = STRBUF_INIT;
226 struct strbuf buf = STRBUF_INIT;
b226293b 227 struct child_process cp = CHILD_PROCESS_INIT;
d65fd424 228 char *result;
85039fb6 229
c364b7ef
ZH
230 if (conf->cmd) {
231 strbuf_addstr(&cmd, conf->cmd);
232 strvec_push(&cp.args, cmd.buf);
233 if (arg)
234 strvec_push(&cp.args, arg);
235 } else if (conf->command) {
236 strbuf_addstr(&cmd, conf->command);
237 if (arg)
238 strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
239 strvec_push(&cp.args, cmd.buf);
240 }
29fda24d 241 strvec_pushv(&cp.env, (const char **)local_repo_env);
85039fb6 242 cp.no_stdin = 1;
85039fb6
CC
243 cp.use_shell = 1;
244
c5eadcaa 245 if (capture_command(&cp, &buf, 1024)) {
13ad56f8 246 error(_("running trailer command '%s' failed"), cmd.buf);
85039fb6
CC
247 strbuf_release(&buf);
248 result = xstrdup("");
c5eadcaa
JK
249 } else {
250 strbuf_trim(&buf);
85039fb6 251 result = strbuf_detach(&buf, NULL);
c5eadcaa 252 }
85039fb6
CC
253
254 strbuf_release(&cmd);
255 return result;
256}
257
cc71b0de 258static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok)
85039fb6 259{
c364b7ef 260 if (arg_tok->conf.command || arg_tok->conf.cmd) {
85039fb6
CC
261 const char *arg;
262 if (arg_tok->value && arg_tok->value[0]) {
263 arg = arg_tok->value;
264 } else {
265 if (in_tok && in_tok->value)
266 arg = xstrdup(in_tok->value);
267 else
268 arg = xstrdup("");
269 }
c364b7ef 270 arg_tok->value = apply_command(&arg_tok->conf, arg);
85039fb6
CC
271 free((char *)arg);
272 }
273}
274
4103818d 275static void apply_arg_if_exists(struct trailer_item *in_tok,
cc71b0de 276 struct arg_item *arg_tok,
4103818d 277 struct trailer_item *on_tok,
8966a394 278 struct list_head *head)
4103818d
CC
279{
280 switch (arg_tok->conf.if_exists) {
281 case EXISTS_DO_NOTHING:
cc71b0de 282 free_arg_item(arg_tok);
4103818d
CC
283 break;
284 case EXISTS_REPLACE:
85039fb6 285 apply_item_command(in_tok, arg_tok);
8966a394
JT
286 add_arg_to_input_list(on_tok, arg_tok);
287 list_del(&in_tok->list);
4103818d
CC
288 free_trailer_item(in_tok);
289 break;
290 case EXISTS_ADD:
85039fb6 291 apply_item_command(in_tok, arg_tok);
8966a394 292 add_arg_to_input_list(on_tok, arg_tok);
4103818d
CC
293 break;
294 case EXISTS_ADD_IF_DIFFERENT:
85039fb6 295 apply_item_command(in_tok, arg_tok);
8966a394
JT
296 if (check_if_different(in_tok, arg_tok, 1, head))
297 add_arg_to_input_list(on_tok, arg_tok);
4103818d 298 else
cc71b0de 299 free_arg_item(arg_tok);
4103818d
CC
300 break;
301 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR:
85039fb6 302 apply_item_command(in_tok, arg_tok);
8966a394
JT
303 if (check_if_different(on_tok, arg_tok, 0, head))
304 add_arg_to_input_list(on_tok, arg_tok);
4103818d 305 else
cc71b0de 306 free_arg_item(arg_tok);
4103818d 307 break;
0ea5292e 308 default:
033abf97 309 BUG("trailer.c: unhandled value %d",
0ea5292e 310 arg_tok->conf.if_exists);
4103818d
CC
311 }
312}
313
8966a394 314static void apply_arg_if_missing(struct list_head *head,
cc71b0de 315 struct arg_item *arg_tok)
4103818d 316{
52fc319d 317 enum trailer_where where;
cc71b0de 318 struct trailer_item *to_add;
4103818d
CC
319
320 switch (arg_tok->conf.if_missing) {
321 case MISSING_DO_NOTHING:
cc71b0de 322 free_arg_item(arg_tok);
4103818d
CC
323 break;
324 case MISSING_ADD:
325 where = arg_tok->conf.where;
85039fb6 326 apply_item_command(NULL, arg_tok);
cc71b0de 327 to_add = trailer_from_arg(arg_tok);
8966a394 328 if (after_or_end(where))
cc71b0de 329 list_add_tail(&to_add->list, head);
8966a394 330 else
cc71b0de 331 list_add(&to_add->list, head);
0ea5292e
PB
332 break;
333 default:
033abf97 334 BUG("trailer.c: unhandled value %d",
0ea5292e 335 arg_tok->conf.if_missing);
4103818d
CC
336 }
337}
338
8966a394 339static int find_same_and_apply_arg(struct list_head *head,
cc71b0de 340 struct arg_item *arg_tok)
4103818d 341{
8966a394 342 struct list_head *pos;
4103818d
CC
343 struct trailer_item *in_tok;
344 struct trailer_item *on_tok;
4103818d 345
52fc319d 346 enum trailer_where where = arg_tok->conf.where;
4103818d
CC
347 int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
348 int backwards = after_or_end(where);
8966a394 349 struct trailer_item *start_tok;
4103818d 350
8966a394
JT
351 if (list_empty(head))
352 return 0;
4103818d 353
8966a394
JT
354 start_tok = list_entry(backwards ? head->prev : head->next,
355 struct trailer_item,
356 list);
357
358 list_for_each_dir(pos, head, backwards) {
359 in_tok = list_entry(pos, struct trailer_item, list);
4103818d
CC
360 if (!same_token(in_tok, arg_tok))
361 continue;
362 on_tok = middle ? in_tok : start_tok;
8966a394 363 apply_arg_if_exists(in_tok, arg_tok, on_tok, head);
4103818d
CC
364 return 1;
365 }
366 return 0;
367}
368
8966a394
JT
369static void process_trailers_lists(struct list_head *head,
370 struct list_head *arg_head)
4103818d 371{
8966a394 372 struct list_head *pos, *p;
cc71b0de 373 struct arg_item *arg_tok;
4103818d 374
8966a394 375 list_for_each_safe(pos, p, arg_head) {
4103818d 376 int applied = 0;
cc71b0de 377 arg_tok = list_entry(pos, struct arg_item, list);
4103818d 378
8966a394 379 list_del(pos);
4103818d 380
8966a394 381 applied = find_same_and_apply_arg(head, arg_tok);
4103818d
CC
382
383 if (!applied)
8966a394 384 apply_arg_if_missing(head, arg_tok);
4103818d
CC
385 }
386}
46a0613f 387
52fc319d 388int trailer_set_where(enum trailer_where *item, const char *value)
46a0613f 389{
0ea5292e
PB
390 if (!value)
391 *item = WHERE_DEFAULT;
392 else if (!strcasecmp("after", value))
52fc319d 393 *item = WHERE_AFTER;
46a0613f 394 else if (!strcasecmp("before", value))
52fc319d 395 *item = WHERE_BEFORE;
46a0613f 396 else if (!strcasecmp("end", value))
52fc319d 397 *item = WHERE_END;
46a0613f 398 else if (!strcasecmp("start", value))
52fc319d 399 *item = WHERE_START;
46a0613f
CC
400 else
401 return -1;
402 return 0;
403}
404
52fc319d 405int trailer_set_if_exists(enum trailer_if_exists *item, const char *value)
46a0613f 406{
0ea5292e
PB
407 if (!value)
408 *item = EXISTS_DEFAULT;
409 else if (!strcasecmp("addIfDifferent", value))
52fc319d 410 *item = EXISTS_ADD_IF_DIFFERENT;
46a0613f 411 else if (!strcasecmp("addIfDifferentNeighbor", value))
52fc319d 412 *item = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
46a0613f 413 else if (!strcasecmp("add", value))
52fc319d 414 *item = EXISTS_ADD;
46a0613f 415 else if (!strcasecmp("replace", value))
52fc319d 416 *item = EXISTS_REPLACE;
46a0613f 417 else if (!strcasecmp("doNothing", value))
52fc319d 418 *item = EXISTS_DO_NOTHING;
46a0613f
CC
419 else
420 return -1;
421 return 0;
422}
423
52fc319d 424int trailer_set_if_missing(enum trailer_if_missing *item, const char *value)
46a0613f 425{
0ea5292e
PB
426 if (!value)
427 *item = MISSING_DEFAULT;
428 else if (!strcasecmp("doNothing", value))
52fc319d 429 *item = MISSING_DO_NOTHING;
46a0613f 430 else if (!strcasecmp("add", value))
52fc319d 431 *item = MISSING_ADD;
46a0613f
CC
432 else
433 return -1;
434 return 0;
435}
436
d65fd424 437static void duplicate_conf(struct conf_info *dst, const struct conf_info *src)
46a0613f
CC
438{
439 *dst = *src;
13092a91
JH
440 dst->name = xstrdup_or_null(src->name);
441 dst->key = xstrdup_or_null(src->key);
442 dst->command = xstrdup_or_null(src->command);
c364b7ef 443 dst->cmd = xstrdup_or_null(src->cmd);
46a0613f
CC
444}
445
cc71b0de 446static struct arg_item *get_conf_item(const char *name)
46a0613f 447{
8966a394 448 struct list_head *pos;
cc71b0de 449 struct arg_item *item;
46a0613f
CC
450
451 /* Look up item with same name */
8966a394 452 list_for_each(pos, &conf_head) {
cc71b0de 453 item = list_entry(pos, struct arg_item, list);
46a0613f
CC
454 if (!strcasecmp(item->conf.name, name))
455 return item;
456 }
457
458 /* Item does not already exists, create it */
ca56dadb 459 CALLOC_ARRAY(item, 1);
46a0613f
CC
460 duplicate_conf(&item->conf, &default_conf_info);
461 item->conf.name = xstrdup(name);
462
8966a394 463 list_add_tail(&item->list, &conf_head);
46a0613f
CC
464
465 return item;
466}
467
c364b7ef
ZH
468enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_CMD,
469 TRAILER_WHERE, TRAILER_IF_EXISTS, TRAILER_IF_MISSING };
46a0613f
CC
470
471static struct {
472 const char *name;
473 enum trailer_info_type type;
474} trailer_config_items[] = {
475 { "key", TRAILER_KEY },
476 { "command", TRAILER_COMMAND },
c364b7ef 477 { "cmd", TRAILER_CMD },
46a0613f
CC
478 { "where", TRAILER_WHERE },
479 { "ifexists", TRAILER_IF_EXISTS },
480 { "ifmissing", TRAILER_IF_MISSING }
481};
482
783a86c1 483static int git_trailer_default_config(const char *conf_key, const char *value,
a4e7e317 484 const struct config_context *ctx UNUSED,
5cf88fd8 485 void *cb UNUSED)
46a0613f
CC
486{
487 const char *trailer_item, *variable_name;
488
489 if (!skip_prefix(conf_key, "trailer.", &trailer_item))
490 return 0;
491
492 variable_name = strrchr(trailer_item, '.');
493 if (!variable_name) {
494 if (!strcmp(trailer_item, "where")) {
52fc319d
PB
495 if (trailer_set_where(&default_conf_info.where,
496 value) < 0)
46a0613f
CC
497 warning(_("unknown value '%s' for key '%s'"),
498 value, conf_key);
499 } else if (!strcmp(trailer_item, "ifexists")) {
52fc319d
PB
500 if (trailer_set_if_exists(&default_conf_info.if_exists,
501 value) < 0)
46a0613f
CC
502 warning(_("unknown value '%s' for key '%s'"),
503 value, conf_key);
504 } else if (!strcmp(trailer_item, "ifmissing")) {
52fc319d
PB
505 if (trailer_set_if_missing(&default_conf_info.if_missing,
506 value) < 0)
46a0613f
CC
507 warning(_("unknown value '%s' for key '%s'"),
508 value, conf_key);
509 } else if (!strcmp(trailer_item, "separators")) {
ba176db5
JK
510 if (!value)
511 return config_error_nonbool(conf_key);
46a0613f
CC
512 separators = xstrdup(value);
513 }
514 }
515 return 0;
516}
517
783a86c1 518static int git_trailer_config(const char *conf_key, const char *value,
a4e7e317 519 const struct config_context *ctx UNUSED,
5cf88fd8 520 void *cb UNUSED)
46a0613f
CC
521{
522 const char *trailer_item, *variable_name;
cc71b0de 523 struct arg_item *item;
46a0613f
CC
524 struct conf_info *conf;
525 char *name = NULL;
526 enum trailer_info_type type;
527 int i;
528
529 if (!skip_prefix(conf_key, "trailer.", &trailer_item))
530 return 0;
531
532 variable_name = strrchr(trailer_item, '.');
533 if (!variable_name)
534 return 0;
535
536 variable_name++;
537 for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) {
538 if (strcmp(trailer_config_items[i].name, variable_name))
539 continue;
540 name = xstrndup(trailer_item, variable_name - trailer_item - 1);
541 type = trailer_config_items[i].type;
542 break;
543 }
544
545 if (!name)
546 return 0;
547
548 item = get_conf_item(name);
549 conf = &item->conf;
550 free(name);
551
552 switch (type) {
553 case TRAILER_KEY:
554 if (conf->key)
555 warning(_("more than one %s"), conf_key);
556 conf->key = xstrdup(value);
557 break;
558 case TRAILER_COMMAND:
559 if (conf->command)
560 warning(_("more than one %s"), conf_key);
561 conf->command = xstrdup(value);
562 break;
c364b7ef
ZH
563 case TRAILER_CMD:
564 if (conf->cmd)
565 warning(_("more than one %s"), conf_key);
566 conf->cmd = xstrdup(value);
567 break;
46a0613f 568 case TRAILER_WHERE:
52fc319d 569 if (trailer_set_where(&conf->where, value))
46a0613f
CC
570 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
571 break;
572 case TRAILER_IF_EXISTS:
52fc319d 573 if (trailer_set_if_exists(&conf->if_exists, value))
46a0613f
CC
574 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
575 break;
576 case TRAILER_IF_MISSING:
52fc319d 577 if (trailer_set_if_missing(&conf->if_missing, value))
46a0613f
CC
578 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
579 break;
580 default:
033abf97 581 BUG("trailer.c: unhandled type %d", type);
46a0613f
CC
582 }
583 return 0;
584}
f0a90b4e 585
e8c352c3
JT
586static void ensure_configured(void)
587{
588 if (configured)
589 return;
590
591 /* Default config must be setup first */
52fc319d
PB
592 default_conf_info.where = WHERE_END;
593 default_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
594 default_conf_info.if_missing = MISSING_ADD;
e8c352c3
JT
595 git_config(git_trailer_default_config, NULL);
596 git_config(git_trailer_config, NULL);
597 configured = 1;
598}
599
cc71b0de 600static const char *token_from_item(struct arg_item *item, char *tok)
f0a90b4e
CC
601{
602 if (item->conf.key)
603 return item->conf.key;
604 if (tok)
605 return tok;
606 return item->conf.name;
607}
608
0d2db00e 609static int token_matches_item(const char *tok, struct arg_item *item, size_t tok_len)
f0a90b4e
CC
610{
611 if (!strncasecmp(tok, item->conf.name, tok_len))
612 return 1;
613 return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0;
614}
615
fdbf4510 616/*
e4319562
JT
617 * If the given line is of the form
618 * "<token><optional whitespace><separator>..." or "<separator>...", return the
619 * location of the separator. Otherwise, return -1. The optional whitespace
620 * is allowed there primarily to allow things like "Bug #43" where <token> is
621 * "Bug" and <separator> is "#".
622 *
623 * The separator-starts-line case (in which this function returns 0) is
624 * distinguished from the non-well-formed-line case (in which this function
625 * returns -1) because some callers of this function need such a distinction.
fdbf4510 626 */
0d2db00e 627static ssize_t find_separator(const char *line, const char *separators)
f0a90b4e 628{
e4319562
JT
629 int whitespace_found = 0;
630 const char *c;
631 for (c = line; *c; c++) {
632 if (strchr(separators, *c))
633 return c - line;
634 if (!whitespace_found && (isalnum(*c) || *c == '-'))
635 continue;
636 if (c != line && (*c == ' ' || *c == '\t')) {
637 whitespace_found = 1;
638 continue;
639 }
640 break;
641 }
642 return -1;
fdbf4510 643}
f0a90b4e 644
fdbf4510
JT
645/*
646 * Obtain the token, value, and conf from the given trailer.
647 *
648 * separator_pos must not be 0, since the token cannot be an empty string.
649 *
650 * If separator_pos is -1, interpret the whole trailer as a token.
651 */
652static void parse_trailer(struct strbuf *tok, struct strbuf *val,
653 const struct conf_info **conf, const char *trailer,
0d2db00e 654 ssize_t separator_pos)
f0a90b4e 655{
cc71b0de 656 struct arg_item *item;
0d2db00e 657 size_t tok_len;
63ab3f34 658 struct list_head *pos;
f0a90b4e 659
fdbf4510
JT
660 if (separator_pos != -1) {
661 strbuf_add(tok, trailer, separator_pos);
f0a90b4e 662 strbuf_trim(tok);
fdbf4510 663 strbuf_addstr(val, trailer + separator_pos + 1);
f0a90b4e
CC
664 strbuf_trim(val);
665 } else {
666 strbuf_addstr(tok, trailer);
667 strbuf_trim(tok);
668 }
f0a90b4e
CC
669
670 /* Lookup if the token matches something in the config */
63ab3f34 671 tok_len = token_len_without_separator(tok->buf, tok->len);
cc71b0de
JT
672 if (conf)
673 *conf = &default_conf_info;
8966a394 674 list_for_each(pos, &conf_head) {
cc71b0de 675 item = list_entry(pos, struct arg_item, list);
63ab3f34
JT
676 if (token_matches_item(tok->buf, item, tok_len)) {
677 char *tok_buf = strbuf_detach(tok, NULL);
cc71b0de
JT
678 if (conf)
679 *conf = &item->conf;
63ab3f34
JT
680 strbuf_addstr(tok, token_from_item(item, tok_buf));
681 free(tok_buf);
682 break;
683 }
f0a90b4e 684 }
f0a90b4e 685}
f0a90b4e 686
60ef86a1
JT
687static struct trailer_item *add_trailer_item(struct list_head *head, char *tok,
688 char *val)
f0a90b4e 689{
241b5d3e 690 struct trailer_item *new_item = xcalloc(1, sizeof(*new_item));
17651326
BW
691 new_item->token = tok;
692 new_item->value = val;
693 list_add_tail(&new_item->list, head);
694 return new_item;
f0a90b4e
CC
695}
696
cc71b0de 697static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
0ea5292e
PB
698 const struct conf_info *conf,
699 const struct new_trailer_item *new_trailer_item)
f0a90b4e 700{
241b5d3e 701 struct arg_item *new_item = xcalloc(1, sizeof(*new_item));
17651326
BW
702 new_item->token = tok;
703 new_item->value = val;
704 duplicate_conf(&new_item->conf, conf);
0ea5292e
PB
705 if (new_trailer_item) {
706 if (new_trailer_item->where != WHERE_DEFAULT)
17651326 707 new_item->conf.where = new_trailer_item->where;
0ea5292e 708 if (new_trailer_item->if_exists != EXISTS_DEFAULT)
17651326 709 new_item->conf.if_exists = new_trailer_item->if_exists;
0ea5292e 710 if (new_trailer_item->if_missing != MISSING_DEFAULT)
17651326 711 new_item->conf.if_missing = new_trailer_item->if_missing;
0ea5292e 712 }
17651326 713 list_add_tail(&new_item->list, arg_head);
f0a90b4e
CC
714}
715
94430d03 716static void parse_trailers_from_config(struct list_head *config_head)
f0a90b4e 717{
cc71b0de 718 struct arg_item *item;
8966a394 719 struct list_head *pos;
85039fb6 720
cc71b0de 721 /* Add an arg item for each configured trailer with a command */
8966a394 722 list_for_each(pos, &conf_head) {
cc71b0de 723 item = list_entry(pos, struct arg_item, list);
63ab3f34 724 if (item->conf.command)
94430d03 725 add_arg_item(config_head,
cc71b0de
JT
726 xstrdup(token_from_item(item, NULL)),
727 xstrdup(""),
0ea5292e 728 &item->conf, NULL);
85039fb6 729 }
94430d03
LA
730}
731
732static void parse_trailers_from_command_line_args(struct list_head *arg_head,
733 struct list_head *new_trailer_head)
734{
735 struct strbuf tok = STRBUF_INIT;
736 struct strbuf val = STRBUF_INIT;
737 const struct conf_info *conf;
738 struct list_head *pos;
739
740 /*
741 * In command-line arguments, '=' is accepted (in addition to the
742 * separators that are defined).
743 */
744 char *cl_separators = xstrfmt("=%s", separators);
f0a90b4e 745
cc71b0de 746 /* Add an arg item for each trailer on the command line */
51166b87
PB
747 list_for_each(pos, new_trailer_head) {
748 struct new_trailer_item *tr =
749 list_entry(pos, struct new_trailer_item, list);
0d2db00e 750 ssize_t separator_pos = find_separator(tr->text, cl_separators);
51166b87 751
fdbf4510
JT
752 if (separator_pos == 0) {
753 struct strbuf sb = STRBUF_INIT;
51166b87 754 strbuf_addstr(&sb, tr->text);
fdbf4510
JT
755 strbuf_trim(&sb);
756 error(_("empty trailer token in trailer '%.*s'"),
757 (int) sb.len, sb.buf);
758 strbuf_release(&sb);
759 } else {
51166b87 760 parse_trailer(&tok, &val, &conf, tr->text,
fdbf4510 761 separator_pos);
cc71b0de
JT
762 add_arg_item(arg_head,
763 strbuf_detach(&tok, NULL),
764 strbuf_detach(&val, NULL),
0ea5292e 765 conf, tr);
fdbf4510 766 }
f0a90b4e
CC
767 }
768
fdbf4510 769 free(cl_separators);
f0a90b4e 770}
2013d850 771
022349c3 772static void read_input_file(struct strbuf *sb, const char *file)
2013d850 773{
2013d850 774 if (file) {
022349c3 775 if (strbuf_read_file(sb, file, 0) < 0)
2013d850
CC
776 die_errno(_("could not read input file '%s'"), file);
777 } else {
022349c3 778 if (strbuf_read(sb, fileno(stdin), 0) < 0)
2013d850
CC
779 die_errno(_("could not read from stdin"));
780 }
022349c3 781}
2013d850 782
022349c3
JT
783static const char *next_line(const char *str)
784{
785 const char *nl = strchrnul(str, '\n');
786 return nl + !!*nl;
787}
2013d850 788
022349c3
JT
789/*
790 * Return the position of the start of the last line. If len is 0, return -1.
791 */
0d2db00e 792static ssize_t last_line(const char *buf, size_t len)
022349c3 793{
0d2db00e 794 ssize_t i;
022349c3
JT
795 if (len == 0)
796 return -1;
797 if (len == 1)
798 return 0;
799 /*
800 * Skip the last character (in addition to the null terminator),
801 * because if the last character is a newline, it is considered as part
802 * of the last line anyway.
803 */
804 i = len - 2;
2013d850 805
022349c3
JT
806 for (; i >= 0; i--) {
807 if (buf[i] == '\n')
808 return i + 1;
809 }
810 return 0;
2013d850
CC
811}
812
813/*
022349c3
JT
814 * Return the position of the start of the patch or the length of str if there
815 * is no patch in the message.
2013d850 816 */
0d2db00e 817static size_t find_patch_start(const char *str)
2013d850 818{
022349c3 819 const char *s;
2013d850 820
022349c3 821 for (s = str; *s; s = next_line(s)) {
c188668e
JK
822 const char *v;
823
824 if (skip_prefix(s, "---", &v) && isspace(*v))
022349c3 825 return s - str;
2013d850
CC
826 }
827
022349c3 828 return s - str;
2013d850
CC
829}
830
831/*
022349c3
JT
832 * Return the position of the first trailer line or len if there are no
833 * trailers.
2013d850 834 */
0d2db00e 835static size_t find_trailer_start(const char *buf, size_t len)
2013d850 836{
022349c3 837 const char *s;
0d2db00e
JK
838 ssize_t end_of_title, l;
839 int only_spaces = 1;
14624506 840 int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0;
60ef86a1
JT
841 /*
842 * Number of possible continuation lines encountered. This will be
843 * reset to 0 if we encounter a trailer (since those lines are to be
844 * considered continuations of that trailer), and added to
845 * non_trailer_lines if we encounter a non-trailer (since those lines
846 * are to be considered non-trailers).
847 */
848 int possible_continuation_lines = 0;
5c99995d
CC
849
850 /* The first paragraph is the title and cannot be trailers */
022349c3
JT
851 for (s = buf; s < buf + len; s = next_line(s)) {
852 if (s[0] == comment_line_char)
5c99995d 853 continue;
022349c3 854 if (is_blank_line(s))
5c99995d
CC
855 break;
856 }
022349c3 857 end_of_title = s - buf;
2013d850
CC
858
859 /*
14624506
JT
860 * Get the start of the trailers by looking starting from the end for a
861 * blank line before a set of non-blank lines that (i) are all
862 * trailers, or (ii) contains at least one Git-generated trailer and
863 * consists of at least 25% trailers.
2013d850 864 */
022349c3
JT
865 for (l = last_line(buf, len);
866 l >= end_of_title;
867 l = last_line(buf, l)) {
868 const char *bol = buf + l;
14624506 869 const char **p;
0d2db00e 870 ssize_t separator_pos;
14624506 871
022349c3 872 if (bol[0] == comment_line_char) {
60ef86a1
JT
873 non_trailer_lines += possible_continuation_lines;
874 possible_continuation_lines = 0;
2013d850 875 continue;
60ef86a1 876 }
022349c3 877 if (is_blank_line(bol)) {
2013d850
CC
878 if (only_spaces)
879 continue;
60ef86a1 880 non_trailer_lines += possible_continuation_lines;
14624506
JT
881 if (recognized_prefix &&
882 trailer_lines * 3 >= non_trailer_lines)
022349c3
JT
883 return next_line(bol) - buf;
884 else if (trailer_lines && !non_trailer_lines)
885 return next_line(bol) - buf;
886 return len;
2013d850 887 }
14624506
JT
888 only_spaces = 0;
889
890 for (p = git_generated_prefixes; *p; p++) {
022349c3 891 if (starts_with(bol, *p)) {
14624506 892 trailer_lines++;
60ef86a1 893 possible_continuation_lines = 0;
14624506
JT
894 recognized_prefix = 1;
895 goto continue_outer_loop;
896 }
2013d850 897 }
14624506 898
022349c3
JT
899 separator_pos = find_separator(bol, separators);
900 if (separator_pos >= 1 && !isspace(bol[0])) {
14624506
JT
901 struct list_head *pos;
902
903 trailer_lines++;
60ef86a1 904 possible_continuation_lines = 0;
14624506
JT
905 if (recognized_prefix)
906 continue;
907 list_for_each(pos, &conf_head) {
908 struct arg_item *item;
909 item = list_entry(pos, struct arg_item, list);
022349c3 910 if (token_matches_item(bol, item,
14624506
JT
911 separator_pos)) {
912 recognized_prefix = 1;
913 break;
914 }
915 }
022349c3 916 } else if (isspace(bol[0]))
60ef86a1
JT
917 possible_continuation_lines++;
918 else {
14624506 919 non_trailer_lines++;
60ef86a1
JT
920 non_trailer_lines += possible_continuation_lines;
921 possible_continuation_lines = 0;
2013d850 922 }
14624506
JT
923continue_outer_loop:
924 ;
2013d850
CC
925 }
926
022349c3 927 return len;
61cfef4c
CC
928}
929
022349c3 930/* Return the position of the end of the trailers. */
0d2db00e 931static size_t find_trailer_end(const char *buf, size_t len)
2013d850 932{
022349c3 933 return len - ignore_non_trailer(buf, len);
2013d850
CC
934}
935
022349c3 936static int ends_with_blank_line(const char *buf, size_t len)
2013d850 937{
0d2db00e 938 ssize_t ll = last_line(buf, len);
022349c3
JT
939 if (ll < 0)
940 return 0;
941 return is_blank_line(buf + ll);
2013d850
CC
942}
943
00002396
JK
944static void unfold_value(struct strbuf *val)
945{
946 struct strbuf out = STRBUF_INIT;
947 size_t i;
948
949 strbuf_grow(&out, val->len);
950 i = 0;
951 while (i < val->len) {
952 char c = val->buf[i++];
953 if (c == '\n') {
954 /* Collapse continuation down to a single space. */
955 while (i < val->len && isspace(val->buf[i]))
956 i++;
957 strbuf_addch(&out, ' ');
958 } else {
959 strbuf_addch(&out, c);
960 }
961 }
962
963 /* Empty lines may have left us with whitespace cruft at the edges */
964 strbuf_trim(&out);
965
966 /* output goes back to val as if we modified it in-place */
967 strbuf_swap(&out, val);
968 strbuf_release(&out);
969}
970
c2a8edf9
LA
971/*
972 * Parse trailers in "str", populating the trailer info and "head"
973 * linked list structure.
974 */
975static void parse_trailers(struct trailer_info *info,
976 const char *str,
977 struct list_head *head,
978 const struct process_trailer_options *opts)
2013d850 979{
63ab3f34
JT
980 struct strbuf tok = STRBUF_INIT;
981 struct strbuf val = STRBUF_INIT;
a3b636e2 982 size_t i;
2013d850 983
c2a8edf9 984 trailer_info_get(info, str, opts);
2013d850 985
c2a8edf9 986 for (i = 0; i < info->trailer_nr; i++) {
fdbf4510 987 int separator_pos;
c2a8edf9 988 char *trailer = info->trailers[i];
e8c352c3 989 if (trailer[0] == comment_line_char)
60ef86a1 990 continue;
e8c352c3 991 separator_pos = find_separator(trailer, separators);
fdbf4510 992 if (separator_pos >= 1) {
e8c352c3 993 parse_trailer(&tok, &val, NULL, trailer,
fdbf4510 994 separator_pos);
00002396
JK
995 if (opts->unfold)
996 unfold_value(&val);
e8c352c3
JT
997 add_trailer_item(head,
998 strbuf_detach(&tok, NULL),
999 strbuf_detach(&val, NULL));
56c493ed 1000 } else if (!opts->only_trailers) {
e8c352c3 1001 strbuf_addstr(&val, trailer);
14624506
JT
1002 strbuf_strip_suffix(&val, "\n");
1003 add_trailer_item(head,
1004 NULL,
1005 strbuf_detach(&val, NULL));
2887103b 1006 }
2013d850 1007 }
2013d850 1008}
b1d78d77 1009
8966a394 1010static void free_all(struct list_head *head)
b1d78d77 1011{
8966a394
JT
1012 struct list_head *pos, *p;
1013 list_for_each_safe(pos, p, head) {
1014 list_del(pos);
1015 free_trailer_item(list_entry(pos, struct trailer_item, list));
b1d78d77
CC
1016 }
1017}
1018
076aa2cb 1019static struct tempfile *trailers_tempfile;
e1f89863
TK
1020
1021static FILE *create_in_place_tempfile(const char *file)
1022{
1023 struct stat st;
31c2c7a0 1024 struct strbuf filename_template = STRBUF_INIT;
e1f89863
TK
1025 const char *tail;
1026 FILE *outfile;
1027
1028 if (stat(file, &st))
1029 die_errno(_("could not stat %s"), file);
1030 if (!S_ISREG(st.st_mode))
1031 die(_("file %s is not a regular file"), file);
1032 if (!(st.st_mode & S_IWUSR))
1033 die(_("file %s is not writable by user"), file);
1034
1035 /* Create temporary file in the same directory as the original */
1036 tail = strrchr(file, '/');
afe8a907 1037 if (tail)
31c2c7a0
BW
1038 strbuf_add(&filename_template, file, tail - file + 1);
1039 strbuf_addstr(&filename_template, "git-interpret-trailers-XXXXXX");
e1f89863 1040
31c2c7a0
BW
1041 trailers_tempfile = xmks_tempfile_m(filename_template.buf, st.st_mode);
1042 strbuf_release(&filename_template);
076aa2cb 1043 outfile = fdopen_tempfile(trailers_tempfile, "w");
e1f89863
TK
1044 if (!outfile)
1045 die_errno(_("could not open temporary file"));
1046
1047 return outfile;
1048}
1049
8abc8980
JK
1050void process_trailers(const char *file,
1051 const struct process_trailer_options *opts,
51166b87 1052 struct list_head *new_trailer_head)
b1d78d77 1053{
8966a394 1054 LIST_HEAD(head);
022349c3 1055 struct strbuf sb = STRBUF_INIT;
c2a8edf9 1056 struct trailer_info info;
0d2db00e 1057 size_t trailer_end;
d0d2344a 1058 FILE *outfile = stdout;
b1d78d77 1059
e8c352c3 1060 ensure_configured();
b1d78d77 1061
022349c3 1062 read_input_file(&sb, file);
b1d78d77 1063
8abc8980 1064 if (opts->in_place)
e1f89863
TK
1065 outfile = create_in_place_tempfile(file);
1066
c2a8edf9
LA
1067 parse_trailers(&info, sb.buf, &head, opts);
1068 trailer_end = info.trailer_end - sb.buf;
1069
b1d78d77 1070 /* Print the lines before the trailers */
c2a8edf9
LA
1071 if (!opts->only_trailers)
1072 fwrite(sb.buf, 1, info.trailer_start - sb.buf, outfile);
1073
1074 if (!opts->only_trailers && !info.blank_line_before_trailer)
1075 fprintf(outfile, "\n");
1076
b1d78d77 1077
fdbdb64f 1078 if (!opts->only_input) {
94430d03 1079 LIST_HEAD(config_head);
fdbdb64f 1080 LIST_HEAD(arg_head);
94430d03
LA
1081 parse_trailers_from_config(&config_head);
1082 parse_trailers_from_command_line_args(&arg_head, new_trailer_head);
1083 list_splice(&config_head, &arg_head);
fdbdb64f
JK
1084 process_trailers_lists(&head, &arg_head);
1085 }
b1d78d77 1086
56c493ed 1087 print_all(outfile, &head, opts);
b1d78d77 1088
8966a394 1089 free_all(&head);
c2a8edf9 1090 trailer_info_release(&info);
b1d78d77
CC
1091
1092 /* Print the lines after the trailers as is */
56c493ed
JK
1093 if (!opts->only_trailers)
1094 fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
b1d78d77 1095
8abc8980 1096 if (opts->in_place)
e1f89863
TK
1097 if (rename_tempfile(&trailers_tempfile, file))
1098 die_errno(_("could not rename temporary file to %s"), file);
1099
022349c3 1100 strbuf_release(&sb);
b1d78d77 1101}
e8c352c3 1102
00a21f5c
JK
1103void trailer_info_get(struct trailer_info *info, const char *str,
1104 const struct process_trailer_options *opts)
e8c352c3
JT
1105{
1106 int patch_start, trailer_end, trailer_start;
1107 struct strbuf **trailer_lines, **ptr;
1108 char **trailer_strings = NULL;
1109 size_t nr = 0, alloc = 0;
1110 char **last = NULL;
1111
1112 ensure_configured();
1113
1688c9a4
JK
1114 if (opts->no_divider)
1115 patch_start = strlen(str);
1116 else
1117 patch_start = find_patch_start(str);
1118
e8c352c3
JT
1119 trailer_end = find_trailer_end(str, patch_start);
1120 trailer_start = find_trailer_start(str, trailer_end);
1121
1122 trailer_lines = strbuf_split_buf(str + trailer_start,
1123 trailer_end - trailer_start,
1124 '\n',
1125 0);
1126 for (ptr = trailer_lines; *ptr; ptr++) {
1127 if (last && isspace((*ptr)->buf[0])) {
1128 struct strbuf sb = STRBUF_INIT;
1129 strbuf_attach(&sb, *last, strlen(*last), strlen(*last));
1130 strbuf_addbuf(&sb, *ptr);
1131 *last = strbuf_detach(&sb, NULL);
1132 continue;
1133 }
1134 ALLOC_GROW(trailer_strings, nr + 1, alloc);
1135 trailer_strings[nr] = strbuf_detach(*ptr, NULL);
1136 last = find_separator(trailer_strings[nr], separators) >= 1
1137 ? &trailer_strings[nr]
1138 : NULL;
1139 nr++;
1140 }
1141 strbuf_list_free(trailer_lines);
1142
1143 info->blank_line_before_trailer = ends_with_blank_line(str,
1144 trailer_start);
1145 info->trailer_start = str + trailer_start;
1146 info->trailer_end = str + trailer_end;
1147 info->trailers = trailer_strings;
1148 info->trailer_nr = nr;
1149}
1150
1151void trailer_info_release(struct trailer_info *info)
1152{
a3b636e2 1153 size_t i;
e8c352c3
JT
1154 for (i = 0; i < info->trailer_nr; i++)
1155 free(info->trailers[i]);
1156 free(info->trailers);
1157}
a388b10f
JK
1158
1159static void format_trailer_info(struct strbuf *out,
1160 const struct trailer_info *info,
1161 const struct process_trailer_options *opts)
1162{
0b691d86 1163 size_t origlen = out->len;
a3b636e2 1164 size_t i;
58311c66
JK
1165
1166 /* If we want the whole block untouched, we can take the fast path. */
8b966a05 1167 if (!opts->only_trailers && !opts->unfold && !opts->filter &&
058761f1
ÆAB
1168 !opts->separator && !opts->key_only && !opts->value_only &&
1169 !opts->key_value_separator) {
58311c66
JK
1170 strbuf_add(out, info->trailer_start,
1171 info->trailer_end - info->trailer_start);
1172 return;
1173 }
1174
1175 for (i = 0; i < info->trailer_nr; i++) {
1176 char *trailer = info->trailers[i];
0d2db00e 1177 ssize_t separator_pos = find_separator(trailer, separators);
58311c66
JK
1178
1179 if (separator_pos >= 1) {
1180 struct strbuf tok = STRBUF_INIT;
1181 struct strbuf val = STRBUF_INIT;
1182
1183 parse_trailer(&tok, &val, NULL, trailer, separator_pos);
250bea0c
AW
1184 if (!opts->filter || opts->filter(&tok, opts->filter_data)) {
1185 if (opts->unfold)
1186 unfold_value(&val);
0b691d86
AW
1187
1188 if (opts->separator && out->len != origlen)
1189 strbuf_addbuf(out, opts->separator);
d9b936db 1190 if (!opts->value_only)
9d87d5ae 1191 strbuf_addbuf(out, &tok);
058761f1
ÆAB
1192 if (!opts->key_only && !opts->value_only) {
1193 if (opts->key_value_separator)
1194 strbuf_addbuf(out, opts->key_value_separator);
1195 else
1196 strbuf_addstr(out, ": ");
1197 }
9d87d5ae
ÆAB
1198 if (!opts->key_only)
1199 strbuf_addbuf(out, &val);
0b691d86
AW
1200 if (!opts->separator)
1201 strbuf_addch(out, '\n');
250bea0c 1202 }
58311c66
JK
1203 strbuf_release(&tok);
1204 strbuf_release(&val);
1205
1206 } else if (!opts->only_trailers) {
0b691d86
AW
1207 if (opts->separator && out->len != origlen) {
1208 strbuf_addbuf(out, opts->separator);
1209 }
58311c66 1210 strbuf_addstr(out, trailer);
0b691d86
AW
1211 if (opts->separator) {
1212 strbuf_rtrim(out);
1213 }
58311c66
JK
1214 }
1215 }
1216
a388b10f
JK
1217}
1218
1219void format_trailers_from_commit(struct strbuf *out, const char *msg,
1220 const struct process_trailer_options *opts)
1221{
1222 struct trailer_info info;
1223
00a21f5c 1224 trailer_info_get(&info, msg, opts);
a388b10f
JK
1225 format_trailer_info(out, &info, opts);
1226 trailer_info_release(&info);
1227}
f0939a0e
JK
1228
1229void trailer_iterator_init(struct trailer_iterator *iter, const char *msg)
1230{
1231 struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
1232 strbuf_init(&iter->key, 0);
1233 strbuf_init(&iter->val, 0);
1234 opts.no_divider = 1;
13211ae2
LA
1235 trailer_info_get(&iter->internal.info, msg, &opts);
1236 iter->internal.cur = 0;
f0939a0e
JK
1237}
1238
1239int trailer_iterator_advance(struct trailer_iterator *iter)
1240{
13211ae2
LA
1241 while (iter->internal.cur < iter->internal.info.trailer_nr) {
1242 char *trailer = iter->internal.info.trailers[iter->internal.cur++];
f0939a0e
JK
1243 int separator_pos = find_separator(trailer, separators);
1244
1245 if (separator_pos < 1)
1246 continue; /* not a real trailer */
1247
1248 strbuf_reset(&iter->key);
1249 strbuf_reset(&iter->val);
1250 parse_trailer(&iter->key, &iter->val, NULL,
1251 trailer, separator_pos);
1252 unfold_value(&iter->val);
1253 return 1;
1254 }
1255 return 0;
1256}
1257
1258void trailer_iterator_release(struct trailer_iterator *iter)
1259{
13211ae2 1260 trailer_info_release(&iter->internal.info);
f0939a0e
JK
1261 strbuf_release(&iter->val);
1262 strbuf_release(&iter->key);
1263}