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