line-range: plug leaking find functions
[git/gitster.git] / trailer.c
blob2bcb9ba8f7b44bb957b129a014821899fe47436d
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "environment.h"
4 #include "gettext.h"
5 #include "string-list.h"
6 #include "run-command.h"
7 #include "commit.h"
8 #include "trailer.h"
9 #include "list.h"
11 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
14 struct trailer_info {
16 * True if there is a blank line before the location pointed to by
17 * trailer_block_start.
19 int blank_line_before_trailer;
22 * Offsets to the trailer block start and end positions in the input
23 * string. If no trailer block is found, these are both set to the
24 * "true" end of the input (find_end_of_log_message()).
26 size_t trailer_block_start, trailer_block_end;
29 * Array of trailers found.
31 char **trailers;
32 size_t trailer_nr;
35 struct conf_info {
36 char *name;
37 char *key;
38 char *command;
39 char *cmd;
40 enum trailer_where where;
41 enum trailer_if_exists if_exists;
42 enum trailer_if_missing if_missing;
45 static struct conf_info default_conf_info;
47 struct trailer_item {
48 struct list_head list;
50 * If this is not a trailer line, the line is stored in value
51 * (excluding the terminating newline) and token is NULL.
53 char *token;
54 char *value;
57 struct arg_item {
58 struct list_head list;
59 char *token;
60 char *value;
61 struct conf_info conf;
64 static LIST_HEAD(conf_head);
66 static char *separators = ":";
68 static int configured;
70 #define TRAILER_ARG_STRING "$ARG"
72 static const char *git_generated_prefixes[] = {
73 "Signed-off-by: ",
74 "(cherry picked from commit ",
75 NULL
78 /* Iterate over the elements of the list. */
79 #define list_for_each_dir(pos, head, is_reverse) \
80 for (pos = is_reverse ? (head)->prev : (head)->next; \
81 pos != (head); \
82 pos = is_reverse ? pos->prev : pos->next)
84 static int after_or_end(enum trailer_where where)
86 return (where == WHERE_AFTER) || (where == WHERE_END);
90 * Return the length of the string not including any final
91 * punctuation. E.g., the input "Signed-off-by:" would return
92 * 13, stripping the trailing punctuation but retaining
93 * internal punctuation.
95 static size_t token_len_without_separator(const char *token, size_t len)
97 while (len > 0 && !isalnum(token[len - 1]))
98 len--;
99 return len;
102 static int same_token(struct trailer_item *a, struct arg_item *b)
104 size_t a_len, b_len, min_len;
106 if (!a->token)
107 return 0;
109 a_len = token_len_without_separator(a->token, strlen(a->token));
110 b_len = token_len_without_separator(b->token, strlen(b->token));
111 min_len = (a_len > b_len) ? b_len : a_len;
113 return !strncasecmp(a->token, b->token, min_len);
116 static int same_value(struct trailer_item *a, struct arg_item *b)
118 return !strcasecmp(a->value, b->value);
121 static int same_trailer(struct trailer_item *a, struct arg_item *b)
123 return same_token(a, b) && same_value(a, b);
126 static inline int is_blank_line(const char *str)
128 const char *s = str;
129 while (*s && *s != '\n' && isspace(*s))
130 s++;
131 return !*s || *s == '\n';
134 static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b)
136 const char *ptr = strstr(sb->buf, a);
137 if (ptr)
138 strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));
141 static void free_trailer_item(struct trailer_item *item)
143 free(item->token);
144 free(item->value);
145 free(item);
148 static void free_arg_item(struct arg_item *item)
150 free(item->conf.name);
151 free(item->conf.key);
152 free(item->conf.command);
153 free(item->conf.cmd);
154 free(item->token);
155 free(item->value);
156 free(item);
159 static char last_non_space_char(const char *s)
161 int i;
162 for (i = strlen(s) - 1; i >= 0; i--)
163 if (!isspace(s[i]))
164 return s[i];
165 return '\0';
168 static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok)
170 struct trailer_item *new_item = xcalloc(1, sizeof(*new_item));
171 new_item->token = arg_tok->token;
172 new_item->value = arg_tok->value;
173 arg_tok->token = arg_tok->value = NULL;
174 free_arg_item(arg_tok);
175 return new_item;
178 static void add_arg_to_input_list(struct trailer_item *on_tok,
179 struct arg_item *arg_tok)
181 int aoe = after_or_end(arg_tok->conf.where);
182 struct trailer_item *to_add = trailer_from_arg(arg_tok);
183 if (aoe)
184 list_add(&to_add->list, &on_tok->list);
185 else
186 list_add_tail(&to_add->list, &on_tok->list);
189 static int check_if_different(struct trailer_item *in_tok,
190 struct arg_item *arg_tok,
191 int check_all,
192 struct list_head *head)
194 enum trailer_where where = arg_tok->conf.where;
195 struct list_head *next_head;
196 do {
197 if (same_trailer(in_tok, arg_tok))
198 return 0;
200 * if we want to add a trailer after another one,
201 * we have to check those before this one
203 next_head = after_or_end(where) ? in_tok->list.prev
204 : in_tok->list.next;
205 if (next_head == head)
206 break;
207 in_tok = list_entry(next_head, struct trailer_item, list);
208 } while (check_all);
209 return 1;
212 static char *apply_command(struct conf_info *conf, const char *arg)
214 struct strbuf cmd = STRBUF_INIT;
215 struct strbuf buf = STRBUF_INIT;
216 struct child_process cp = CHILD_PROCESS_INIT;
217 char *result;
219 if (conf->cmd) {
220 strbuf_addstr(&cmd, conf->cmd);
221 strvec_push(&cp.args, cmd.buf);
222 if (arg)
223 strvec_push(&cp.args, arg);
224 } else if (conf->command) {
225 strbuf_addstr(&cmd, conf->command);
226 if (arg)
227 strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
228 strvec_push(&cp.args, cmd.buf);
230 strvec_pushv(&cp.env, (const char **)local_repo_env);
231 cp.no_stdin = 1;
232 cp.use_shell = 1;
234 if (capture_command(&cp, &buf, 1024)) {
235 error(_("running trailer command '%s' failed"), cmd.buf);
236 strbuf_release(&buf);
237 result = xstrdup("");
238 } else {
239 strbuf_trim(&buf);
240 result = strbuf_detach(&buf, NULL);
243 strbuf_release(&cmd);
244 return result;
247 static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok)
249 if (arg_tok->conf.command || arg_tok->conf.cmd) {
250 const char *arg;
251 if (arg_tok->value && arg_tok->value[0]) {
252 arg = arg_tok->value;
253 } else {
254 if (in_tok && in_tok->value)
255 arg = xstrdup(in_tok->value);
256 else
257 arg = xstrdup("");
259 arg_tok->value = apply_command(&arg_tok->conf, arg);
260 free((char *)arg);
264 static void apply_arg_if_exists(struct trailer_item *in_tok,
265 struct arg_item *arg_tok,
266 struct trailer_item *on_tok,
267 struct list_head *head)
269 switch (arg_tok->conf.if_exists) {
270 case EXISTS_DO_NOTHING:
271 free_arg_item(arg_tok);
272 break;
273 case EXISTS_REPLACE:
274 apply_item_command(in_tok, arg_tok);
275 add_arg_to_input_list(on_tok, arg_tok);
276 list_del(&in_tok->list);
277 free_trailer_item(in_tok);
278 break;
279 case EXISTS_ADD:
280 apply_item_command(in_tok, arg_tok);
281 add_arg_to_input_list(on_tok, arg_tok);
282 break;
283 case EXISTS_ADD_IF_DIFFERENT:
284 apply_item_command(in_tok, arg_tok);
285 if (check_if_different(in_tok, arg_tok, 1, head))
286 add_arg_to_input_list(on_tok, arg_tok);
287 else
288 free_arg_item(arg_tok);
289 break;
290 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR:
291 apply_item_command(in_tok, arg_tok);
292 if (check_if_different(on_tok, arg_tok, 0, head))
293 add_arg_to_input_list(on_tok, arg_tok);
294 else
295 free_arg_item(arg_tok);
296 break;
297 default:
298 BUG("trailer.c: unhandled value %d",
299 arg_tok->conf.if_exists);
303 static void apply_arg_if_missing(struct list_head *head,
304 struct arg_item *arg_tok)
306 enum trailer_where where;
307 struct trailer_item *to_add;
309 switch (arg_tok->conf.if_missing) {
310 case MISSING_DO_NOTHING:
311 free_arg_item(arg_tok);
312 break;
313 case MISSING_ADD:
314 where = arg_tok->conf.where;
315 apply_item_command(NULL, arg_tok);
316 to_add = trailer_from_arg(arg_tok);
317 if (after_or_end(where))
318 list_add_tail(&to_add->list, head);
319 else
320 list_add(&to_add->list, head);
321 break;
322 default:
323 BUG("trailer.c: unhandled value %d",
324 arg_tok->conf.if_missing);
328 static int find_same_and_apply_arg(struct list_head *head,
329 struct arg_item *arg_tok)
331 struct list_head *pos;
332 struct trailer_item *in_tok;
333 struct trailer_item *on_tok;
335 enum trailer_where where = arg_tok->conf.where;
336 int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
337 int backwards = after_or_end(where);
338 struct trailer_item *start_tok;
340 if (list_empty(head))
341 return 0;
343 start_tok = list_entry(backwards ? head->prev : head->next,
344 struct trailer_item,
345 list);
347 list_for_each_dir(pos, head, backwards) {
348 in_tok = list_entry(pos, struct trailer_item, list);
349 if (!same_token(in_tok, arg_tok))
350 continue;
351 on_tok = middle ? in_tok : start_tok;
352 apply_arg_if_exists(in_tok, arg_tok, on_tok, head);
353 return 1;
355 return 0;
358 void process_trailers_lists(struct list_head *head,
359 struct list_head *arg_head)
361 struct list_head *pos, *p;
362 struct arg_item *arg_tok;
364 list_for_each_safe(pos, p, arg_head) {
365 int applied = 0;
366 arg_tok = list_entry(pos, struct arg_item, list);
368 list_del(pos);
370 applied = find_same_and_apply_arg(head, arg_tok);
372 if (!applied)
373 apply_arg_if_missing(head, arg_tok);
377 int trailer_set_where(enum trailer_where *item, const char *value)
379 if (!value)
380 *item = WHERE_DEFAULT;
381 else if (!strcasecmp("after", value))
382 *item = WHERE_AFTER;
383 else if (!strcasecmp("before", value))
384 *item = WHERE_BEFORE;
385 else if (!strcasecmp("end", value))
386 *item = WHERE_END;
387 else if (!strcasecmp("start", value))
388 *item = WHERE_START;
389 else
390 return -1;
391 return 0;
394 int trailer_set_if_exists(enum trailer_if_exists *item, const char *value)
396 if (!value)
397 *item = EXISTS_DEFAULT;
398 else if (!strcasecmp("addIfDifferent", value))
399 *item = EXISTS_ADD_IF_DIFFERENT;
400 else if (!strcasecmp("addIfDifferentNeighbor", value))
401 *item = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
402 else if (!strcasecmp("add", value))
403 *item = EXISTS_ADD;
404 else if (!strcasecmp("replace", value))
405 *item = EXISTS_REPLACE;
406 else if (!strcasecmp("doNothing", value))
407 *item = EXISTS_DO_NOTHING;
408 else
409 return -1;
410 return 0;
413 int trailer_set_if_missing(enum trailer_if_missing *item, const char *value)
415 if (!value)
416 *item = MISSING_DEFAULT;
417 else if (!strcasecmp("doNothing", value))
418 *item = MISSING_DO_NOTHING;
419 else if (!strcasecmp("add", value))
420 *item = MISSING_ADD;
421 else
422 return -1;
423 return 0;
426 static void duplicate_conf(struct conf_info *dst, const struct conf_info *src)
428 *dst = *src;
429 dst->name = xstrdup_or_null(src->name);
430 dst->key = xstrdup_or_null(src->key);
431 dst->command = xstrdup_or_null(src->command);
432 dst->cmd = xstrdup_or_null(src->cmd);
435 static struct arg_item *get_conf_item(const char *name)
437 struct list_head *pos;
438 struct arg_item *item;
440 /* Look up item with same name */
441 list_for_each(pos, &conf_head) {
442 item = list_entry(pos, struct arg_item, list);
443 if (!strcasecmp(item->conf.name, name))
444 return item;
447 /* Item does not already exists, create it */
448 CALLOC_ARRAY(item, 1);
449 duplicate_conf(&item->conf, &default_conf_info);
450 item->conf.name = xstrdup(name);
452 list_add_tail(&item->list, &conf_head);
454 return item;
457 enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_CMD,
458 TRAILER_WHERE, TRAILER_IF_EXISTS, TRAILER_IF_MISSING };
460 static struct {
461 const char *name;
462 enum trailer_info_type type;
463 } trailer_config_items[] = {
464 { "key", TRAILER_KEY },
465 { "command", TRAILER_COMMAND },
466 { "cmd", TRAILER_CMD },
467 { "where", TRAILER_WHERE },
468 { "ifexists", TRAILER_IF_EXISTS },
469 { "ifmissing", TRAILER_IF_MISSING }
472 static int git_trailer_default_config(const char *conf_key, const char *value,
473 const struct config_context *ctx UNUSED,
474 void *cb UNUSED)
476 const char *trailer_item, *variable_name;
478 if (!skip_prefix(conf_key, "trailer.", &trailer_item))
479 return 0;
481 variable_name = strrchr(trailer_item, '.');
482 if (!variable_name) {
483 if (!strcmp(trailer_item, "where")) {
484 if (trailer_set_where(&default_conf_info.where,
485 value) < 0)
486 warning(_("unknown value '%s' for key '%s'"),
487 value, conf_key);
488 } else if (!strcmp(trailer_item, "ifexists")) {
489 if (trailer_set_if_exists(&default_conf_info.if_exists,
490 value) < 0)
491 warning(_("unknown value '%s' for key '%s'"),
492 value, conf_key);
493 } else if (!strcmp(trailer_item, "ifmissing")) {
494 if (trailer_set_if_missing(&default_conf_info.if_missing,
495 value) < 0)
496 warning(_("unknown value '%s' for key '%s'"),
497 value, conf_key);
498 } else if (!strcmp(trailer_item, "separators")) {
499 if (!value)
500 return config_error_nonbool(conf_key);
501 separators = xstrdup(value);
504 return 0;
507 static int git_trailer_config(const char *conf_key, const char *value,
508 const struct config_context *ctx UNUSED,
509 void *cb UNUSED)
511 const char *trailer_item, *variable_name;
512 struct arg_item *item;
513 struct conf_info *conf;
514 char *name = NULL;
515 enum trailer_info_type type;
516 int i;
518 if (!skip_prefix(conf_key, "trailer.", &trailer_item))
519 return 0;
521 variable_name = strrchr(trailer_item, '.');
522 if (!variable_name)
523 return 0;
525 variable_name++;
526 for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) {
527 if (strcmp(trailer_config_items[i].name, variable_name))
528 continue;
529 name = xstrndup(trailer_item, variable_name - trailer_item - 1);
530 type = trailer_config_items[i].type;
531 break;
534 if (!name)
535 return 0;
537 item = get_conf_item(name);
538 conf = &item->conf;
539 free(name);
541 switch (type) {
542 case TRAILER_KEY:
543 if (conf->key)
544 warning(_("more than one %s"), conf_key);
545 if (!value)
546 return config_error_nonbool(conf_key);
547 conf->key = xstrdup(value);
548 break;
549 case TRAILER_COMMAND:
550 if (conf->command)
551 warning(_("more than one %s"), conf_key);
552 if (!value)
553 return config_error_nonbool(conf_key);
554 conf->command = xstrdup(value);
555 break;
556 case TRAILER_CMD:
557 if (conf->cmd)
558 warning(_("more than one %s"), conf_key);
559 if (!value)
560 return config_error_nonbool(conf_key);
561 conf->cmd = xstrdup(value);
562 break;
563 case TRAILER_WHERE:
564 if (trailer_set_where(&conf->where, value))
565 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
566 break;
567 case TRAILER_IF_EXISTS:
568 if (trailer_set_if_exists(&conf->if_exists, value))
569 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
570 break;
571 case TRAILER_IF_MISSING:
572 if (trailer_set_if_missing(&conf->if_missing, value))
573 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
574 break;
575 default:
576 BUG("trailer.c: unhandled type %d", type);
578 return 0;
581 void trailer_config_init(void)
583 if (configured)
584 return;
586 /* Default config must be setup first */
587 default_conf_info.where = WHERE_END;
588 default_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
589 default_conf_info.if_missing = MISSING_ADD;
590 git_config(git_trailer_default_config, NULL);
591 git_config(git_trailer_config, NULL);
592 configured = 1;
595 static const char *token_from_item(struct arg_item *item, char *tok)
597 if (item->conf.key)
598 return item->conf.key;
599 if (tok)
600 return tok;
601 return item->conf.name;
604 static int token_matches_item(const char *tok, struct arg_item *item, size_t tok_len)
606 if (!strncasecmp(tok, item->conf.name, tok_len))
607 return 1;
608 return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0;
612 * If the given line is of the form
613 * "<token><optional whitespace><separator>..." or "<separator>...", return the
614 * location of the separator. Otherwise, return -1. The optional whitespace
615 * is allowed there primarily to allow things like "Bug #43" where <token> is
616 * "Bug" and <separator> is "#".
618 * The separator-starts-line case (in which this function returns 0) is
619 * distinguished from the non-well-formed-line case (in which this function
620 * returns -1) because some callers of this function need such a distinction.
622 static ssize_t find_separator(const char *line, const char *separators)
624 int whitespace_found = 0;
625 const char *c;
626 for (c = line; *c; c++) {
627 if (strchr(separators, *c))
628 return c - line;
629 if (!whitespace_found && (isalnum(*c) || *c == '-'))
630 continue;
631 if (c != line && (*c == ' ' || *c == '\t')) {
632 whitespace_found = 1;
633 continue;
635 break;
637 return -1;
641 * Obtain the token, value, and conf from the given trailer.
643 * separator_pos must not be 0, since the token cannot be an empty string.
645 * If separator_pos is -1, interpret the whole trailer as a token.
647 static void parse_trailer(struct strbuf *tok, struct strbuf *val,
648 const struct conf_info **conf, const char *trailer,
649 ssize_t separator_pos)
651 struct arg_item *item;
652 size_t tok_len;
653 struct list_head *pos;
655 if (separator_pos != -1) {
656 strbuf_add(tok, trailer, separator_pos);
657 strbuf_trim(tok);
658 strbuf_addstr(val, trailer + separator_pos + 1);
659 strbuf_trim(val);
660 } else {
661 strbuf_addstr(tok, trailer);
662 strbuf_trim(tok);
665 /* Lookup if the token matches something in the config */
666 tok_len = token_len_without_separator(tok->buf, tok->len);
667 if (conf)
668 *conf = &default_conf_info;
669 list_for_each(pos, &conf_head) {
670 item = list_entry(pos, struct arg_item, list);
671 if (token_matches_item(tok->buf, item, tok_len)) {
672 char *tok_buf = strbuf_detach(tok, NULL);
673 if (conf)
674 *conf = &item->conf;
675 strbuf_addstr(tok, token_from_item(item, tok_buf));
676 free(tok_buf);
677 break;
682 static struct trailer_item *add_trailer_item(struct list_head *head, char *tok,
683 char *val)
685 struct trailer_item *new_item = xcalloc(1, sizeof(*new_item));
686 new_item->token = tok;
687 new_item->value = val;
688 list_add_tail(&new_item->list, head);
689 return new_item;
692 static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
693 const struct conf_info *conf,
694 const struct new_trailer_item *new_trailer_item)
696 struct arg_item *new_item = xcalloc(1, sizeof(*new_item));
697 new_item->token = tok;
698 new_item->value = val;
699 duplicate_conf(&new_item->conf, conf);
700 if (new_trailer_item) {
701 if (new_trailer_item->where != WHERE_DEFAULT)
702 new_item->conf.where = new_trailer_item->where;
703 if (new_trailer_item->if_exists != EXISTS_DEFAULT)
704 new_item->conf.if_exists = new_trailer_item->if_exists;
705 if (new_trailer_item->if_missing != MISSING_DEFAULT)
706 new_item->conf.if_missing = new_trailer_item->if_missing;
708 list_add_tail(&new_item->list, arg_head);
711 void parse_trailers_from_config(struct list_head *config_head)
713 struct arg_item *item;
714 struct list_head *pos;
716 /* Add an arg item for each configured trailer with a command */
717 list_for_each(pos, &conf_head) {
718 item = list_entry(pos, struct arg_item, list);
719 if (item->conf.command)
720 add_arg_item(config_head,
721 xstrdup(token_from_item(item, NULL)),
722 xstrdup(""),
723 &item->conf, NULL);
727 void parse_trailers_from_command_line_args(struct list_head *arg_head,
728 struct list_head *new_trailer_head)
730 struct strbuf tok = STRBUF_INIT;
731 struct strbuf val = STRBUF_INIT;
732 const struct conf_info *conf;
733 struct list_head *pos;
736 * In command-line arguments, '=' is accepted (in addition to the
737 * separators that are defined).
739 char *cl_separators = xstrfmt("=%s", separators);
741 /* Add an arg item for each trailer on the command line */
742 list_for_each(pos, new_trailer_head) {
743 struct new_trailer_item *tr =
744 list_entry(pos, struct new_trailer_item, list);
745 ssize_t separator_pos = find_separator(tr->text, cl_separators);
747 if (separator_pos == 0) {
748 struct strbuf sb = STRBUF_INIT;
749 strbuf_addstr(&sb, tr->text);
750 strbuf_trim(&sb);
751 error(_("empty trailer token in trailer '%.*s'"),
752 (int) sb.len, sb.buf);
753 strbuf_release(&sb);
754 } else {
755 parse_trailer(&tok, &val, &conf, tr->text,
756 separator_pos);
757 add_arg_item(arg_head,
758 strbuf_detach(&tok, NULL),
759 strbuf_detach(&val, NULL),
760 conf, tr);
764 free(cl_separators);
767 static const char *next_line(const char *str)
769 const char *nl = strchrnul(str, '\n');
770 return nl + !!*nl;
774 * Return the position of the start of the last line. If len is 0, return -1.
776 static ssize_t last_line(const char *buf, size_t len)
778 ssize_t i;
779 if (len == 0)
780 return -1;
781 if (len == 1)
782 return 0;
784 * Skip the last character (in addition to the null terminator),
785 * because if the last character is a newline, it is considered as part
786 * of the last line anyway.
788 i = len - 2;
790 for (; i >= 0; i--) {
791 if (buf[i] == '\n')
792 return i + 1;
794 return 0;
798 * Find the end of the log message as an offset from the start of the input
799 * (where callers of this function are interested in looking for a trailers
800 * block in the same input). We have to consider two categories of content that
801 * can come at the end of the input which we want to ignore (because they don't
802 * belong in the log message):
804 * (1) the "patch part" which begins with a "---" divider and has patch
805 * information (like the output of git-format-patch), and
807 * (2) any trailing comment lines, blank lines like in the output of "git
808 * commit -v", or stuff below the "cut" (scissor) line.
810 * As a formula, the situation looks like this:
812 * INPUT = LOG MESSAGE + IGNORED
814 * where IGNORED can be either of the two categories described above. It may be
815 * that there is nothing to ignore. Now it may be the case that the LOG MESSAGE
816 * contains a trailer block, but that's not the concern of this function.
818 static size_t find_end_of_log_message(const char *input, int no_divider)
820 size_t end;
821 const char *s;
823 /* Assume the naive end of the input is already what we want. */
824 end = strlen(input);
826 /* Optionally skip over any patch part ("---" line and below). */
827 if (!no_divider) {
828 for (s = input; *s; s = next_line(s)) {
829 const char *v;
831 if (skip_prefix(s, "---", &v) && isspace(*v)) {
832 end = s - input;
833 break;
838 /* Skip over other ignorable bits. */
839 return end - ignored_log_message_bytes(input, end);
843 * Return the position of the first trailer line or len if there are no
844 * trailers.
846 static size_t find_trailer_block_start(const char *buf, size_t len)
848 const char *s;
849 ssize_t end_of_title, l;
850 int only_spaces = 1;
851 int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0;
853 * Number of possible continuation lines encountered. This will be
854 * reset to 0 if we encounter a trailer (since those lines are to be
855 * considered continuations of that trailer), and added to
856 * non_trailer_lines if we encounter a non-trailer (since those lines
857 * are to be considered non-trailers).
859 int possible_continuation_lines = 0;
861 /* The first paragraph is the title and cannot be trailers */
862 for (s = buf; s < buf + len; s = next_line(s)) {
863 if (starts_with_mem(s, buf + len - s, comment_line_str))
864 continue;
865 if (is_blank_line(s))
866 break;
868 end_of_title = s - buf;
871 * Get the start of the trailers by looking starting from the end for a
872 * blank line before a set of non-blank lines that (i) are all
873 * trailers, or (ii) contains at least one Git-generated trailer and
874 * consists of at least 25% trailers.
876 for (l = last_line(buf, len);
877 l >= end_of_title;
878 l = last_line(buf, l)) {
879 const char *bol = buf + l;
880 const char **p;
881 ssize_t separator_pos;
883 if (starts_with_mem(bol, buf + len - bol, comment_line_str)) {
884 non_trailer_lines += possible_continuation_lines;
885 possible_continuation_lines = 0;
886 continue;
888 if (is_blank_line(bol)) {
889 if (only_spaces)
890 continue;
891 non_trailer_lines += possible_continuation_lines;
892 if (recognized_prefix &&
893 trailer_lines * 3 >= non_trailer_lines)
894 return next_line(bol) - buf;
895 else if (trailer_lines && !non_trailer_lines)
896 return next_line(bol) - buf;
897 return len;
899 only_spaces = 0;
901 for (p = git_generated_prefixes; *p; p++) {
902 if (starts_with(bol, *p)) {
903 trailer_lines++;
904 possible_continuation_lines = 0;
905 recognized_prefix = 1;
906 goto continue_outer_loop;
910 separator_pos = find_separator(bol, separators);
911 if (separator_pos >= 1 && !isspace(bol[0])) {
912 struct list_head *pos;
914 trailer_lines++;
915 possible_continuation_lines = 0;
916 if (recognized_prefix)
917 continue;
918 list_for_each(pos, &conf_head) {
919 struct arg_item *item;
920 item = list_entry(pos, struct arg_item, list);
921 if (token_matches_item(bol, item,
922 separator_pos)) {
923 recognized_prefix = 1;
924 break;
927 } else if (isspace(bol[0]))
928 possible_continuation_lines++;
929 else {
930 non_trailer_lines++;
931 non_trailer_lines += possible_continuation_lines;
932 possible_continuation_lines = 0;
934 continue_outer_loop:
938 return len;
941 static int ends_with_blank_line(const char *buf, size_t len)
943 ssize_t ll = last_line(buf, len);
944 if (ll < 0)
945 return 0;
946 return is_blank_line(buf + ll);
949 static void unfold_value(struct strbuf *val)
951 struct strbuf out = STRBUF_INIT;
952 size_t i;
954 strbuf_grow(&out, val->len);
955 i = 0;
956 while (i < val->len) {
957 char c = val->buf[i++];
958 if (c == '\n') {
959 /* Collapse continuation down to a single space. */
960 while (i < val->len && isspace(val->buf[i]))
961 i++;
962 strbuf_addch(&out, ' ');
963 } else {
964 strbuf_addch(&out, c);
968 /* Empty lines may have left us with whitespace cruft at the edges */
969 strbuf_trim(&out);
971 /* output goes back to val as if we modified it in-place */
972 strbuf_swap(&out, val);
973 strbuf_release(&out);
976 static struct trailer_info *trailer_info_new(void)
978 struct trailer_info *info = xcalloc(1, sizeof(*info));
979 return info;
982 static struct trailer_info *trailer_info_get(const struct process_trailer_options *opts,
983 const char *str)
985 struct trailer_info *info = trailer_info_new();
986 size_t end_of_log_message = 0, trailer_block_start = 0;
987 struct strbuf **trailer_lines, **ptr;
988 char **trailer_strings = NULL;
989 size_t nr = 0, alloc = 0;
990 char **last = NULL;
992 trailer_config_init();
994 end_of_log_message = find_end_of_log_message(str, opts->no_divider);
995 trailer_block_start = find_trailer_block_start(str, end_of_log_message);
997 trailer_lines = strbuf_split_buf(str + trailer_block_start,
998 end_of_log_message - trailer_block_start,
999 '\n',
1001 for (ptr = trailer_lines; *ptr; ptr++) {
1002 if (last && isspace((*ptr)->buf[0])) {
1003 struct strbuf sb = STRBUF_INIT;
1004 strbuf_attach(&sb, *last, strlen(*last), strlen(*last));
1005 strbuf_addbuf(&sb, *ptr);
1006 *last = strbuf_detach(&sb, NULL);
1007 continue;
1009 ALLOC_GROW(trailer_strings, nr + 1, alloc);
1010 trailer_strings[nr] = strbuf_detach(*ptr, NULL);
1011 last = find_separator(trailer_strings[nr], separators) >= 1
1012 ? &trailer_strings[nr]
1013 : NULL;
1014 nr++;
1016 strbuf_list_free(trailer_lines);
1018 info->blank_line_before_trailer = ends_with_blank_line(str,
1019 trailer_block_start);
1020 info->trailer_block_start = trailer_block_start;
1021 info->trailer_block_end = end_of_log_message;
1022 info->trailers = trailer_strings;
1023 info->trailer_nr = nr;
1025 return info;
1029 * Parse trailers in "str", populating the trailer info and "trailer_objects"
1030 * linked list structure.
1032 struct trailer_info *parse_trailers(const struct process_trailer_options *opts,
1033 const char *str,
1034 struct list_head *trailer_objects)
1036 struct trailer_info *info;
1037 struct strbuf tok = STRBUF_INIT;
1038 struct strbuf val = STRBUF_INIT;
1039 size_t i;
1041 info = trailer_info_get(opts, str);
1043 for (i = 0; i < info->trailer_nr; i++) {
1044 int separator_pos;
1045 char *trailer = info->trailers[i];
1046 if (starts_with(trailer, comment_line_str))
1047 continue;
1048 separator_pos = find_separator(trailer, separators);
1049 if (separator_pos >= 1) {
1050 parse_trailer(&tok, &val, NULL, trailer,
1051 separator_pos);
1052 if (opts->unfold)
1053 unfold_value(&val);
1054 add_trailer_item(trailer_objects,
1055 strbuf_detach(&tok, NULL),
1056 strbuf_detach(&val, NULL));
1057 } else if (!opts->only_trailers) {
1058 strbuf_addstr(&val, trailer);
1059 strbuf_strip_suffix(&val, "\n");
1060 add_trailer_item(trailer_objects,
1061 NULL,
1062 strbuf_detach(&val, NULL));
1066 return info;
1069 void free_trailers(struct list_head *trailers)
1071 struct list_head *pos, *p;
1072 list_for_each_safe(pos, p, trailers) {
1073 list_del(pos);
1074 free_trailer_item(list_entry(pos, struct trailer_item, list));
1078 size_t trailer_block_start(struct trailer_info *info)
1080 return info->trailer_block_start;
1083 size_t trailer_block_end(struct trailer_info *info)
1085 return info->trailer_block_end;
1088 int blank_line_before_trailer_block(struct trailer_info *info)
1090 return info->blank_line_before_trailer;
1093 void trailer_info_release(struct trailer_info *info)
1095 size_t i;
1096 for (i = 0; i < info->trailer_nr; i++)
1097 free(info->trailers[i]);
1098 free(info->trailers);
1099 free(info);
1102 void format_trailers(const struct process_trailer_options *opts,
1103 struct list_head *trailers,
1104 struct strbuf *out)
1106 size_t origlen = out->len;
1107 struct list_head *pos;
1108 struct trailer_item *item;
1110 list_for_each(pos, trailers) {
1111 item = list_entry(pos, struct trailer_item, list);
1112 if (item->token) {
1113 struct strbuf tok = STRBUF_INIT;
1114 struct strbuf val = STRBUF_INIT;
1115 strbuf_addstr(&tok, item->token);
1116 strbuf_addstr(&val, item->value);
1119 * Skip key/value pairs where the value was empty. This
1120 * can happen from trailers specified without a
1121 * separator, like `--trailer "Reviewed-by"` (no
1122 * corresponding value).
1124 if (opts->trim_empty && !strlen(item->value))
1125 continue;
1127 if (!opts->filter || opts->filter(&tok, opts->filter_data)) {
1128 if (opts->separator && out->len != origlen)
1129 strbuf_addbuf(out, opts->separator);
1130 if (!opts->value_only)
1131 strbuf_addbuf(out, &tok);
1132 if (!opts->key_only && !opts->value_only) {
1133 if (opts->key_value_separator)
1134 strbuf_addbuf(out, opts->key_value_separator);
1135 else {
1136 char c = last_non_space_char(tok.buf);
1137 if (c && !strchr(separators, c))
1138 strbuf_addf(out, "%c ", separators[0]);
1141 if (!opts->key_only)
1142 strbuf_addbuf(out, &val);
1143 if (!opts->separator)
1144 strbuf_addch(out, '\n');
1146 strbuf_release(&tok);
1147 strbuf_release(&val);
1149 } else if (!opts->only_trailers) {
1150 if (opts->separator && out->len != origlen) {
1151 strbuf_addbuf(out, opts->separator);
1153 strbuf_addstr(out, item->value);
1154 if (opts->separator)
1155 strbuf_rtrim(out);
1156 else
1157 strbuf_addch(out, '\n');
1162 void format_trailers_from_commit(const struct process_trailer_options *opts,
1163 const char *msg,
1164 struct strbuf *out)
1166 LIST_HEAD(trailer_objects);
1167 struct trailer_info *info = parse_trailers(opts, msg, &trailer_objects);
1169 /* If we want the whole block untouched, we can take the fast path. */
1170 if (!opts->only_trailers && !opts->unfold && !opts->filter &&
1171 !opts->separator && !opts->key_only && !opts->value_only &&
1172 !opts->key_value_separator) {
1173 strbuf_add(out, msg + info->trailer_block_start,
1174 info->trailer_block_end - info->trailer_block_start);
1175 } else
1176 format_trailers(opts, &trailer_objects, out);
1178 free_trailers(&trailer_objects);
1179 trailer_info_release(info);
1182 void trailer_iterator_init(struct trailer_iterator *iter, const char *msg)
1184 struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
1185 strbuf_init(&iter->key, 0);
1186 strbuf_init(&iter->val, 0);
1187 opts.no_divider = 1;
1188 iter->internal.info = trailer_info_get(&opts, msg);
1189 iter->internal.cur = 0;
1192 int trailer_iterator_advance(struct trailer_iterator *iter)
1194 if (iter->internal.cur < iter->internal.info->trailer_nr) {
1195 char *line = iter->internal.info->trailers[iter->internal.cur++];
1196 int separator_pos = find_separator(line, separators);
1198 iter->raw = line;
1199 strbuf_reset(&iter->key);
1200 strbuf_reset(&iter->val);
1201 parse_trailer(&iter->key, &iter->val, NULL,
1202 line, separator_pos);
1203 /* Always unfold values during iteration. */
1204 unfold_value(&iter->val);
1205 return 1;
1207 return 0;
1210 void trailer_iterator_release(struct trailer_iterator *iter)
1212 trailer_info_release(iter->internal.info);
1213 strbuf_release(&iter->val);
1214 strbuf_release(&iter->key);
1217 int amend_file_with_trailers(const char *path, const struct strvec *trailer_args)
1219 struct child_process run_trailer = CHILD_PROCESS_INIT;
1221 run_trailer.git_cmd = 1;
1222 strvec_pushl(&run_trailer.args, "interpret-trailers",
1223 "--in-place", "--no-divider",
1224 path, NULL);
1225 strvec_pushv(&run_trailer.args, trailer_args->v);
1226 return run_command(&run_trailer);