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