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