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