3 #include "string-list.h"
4 #include "run-command.h"
10 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
18 enum trailer_where where;
19 enum trailer_if_exists if_exists;
20 enum trailer_if_missing if_missing;
23 static struct conf_info default_conf_info;
26 struct list_head list;
28 * If this is not a trailer line, the line is stored in value
29 * (excluding the terminating newline) and token is NULL.
36 struct list_head list;
39 struct conf_info conf;
42 static LIST_HEAD(conf_head);
44 static char *separators = ":";
46 static int configured;
48 #define TRAILER_ARG_STRING "$ARG"
50 static const char *git_generated_prefixes[] = {
52 "(cherry picked from commit ",
56 /* Iterate over the elements of the list. */
57 #define list_for_each_dir(pos, head, is_reverse) \
58 for (pos = is_reverse ? (head)->prev : (head)->next; \
60 pos = is_reverse ? pos->prev : pos->next)
62 static int after_or_end(enum trailer_where where)
64 return (where == WHERE_AFTER) || (where == WHERE_END);
68 * Return the length of the string not including any final
69 * punctuation. E.g., the input "Signed-off-by:" would return
70 * 13, stripping the trailing punctuation but retaining
71 * internal punctuation.
73 static size_t token_len_without_separator(const char *token, size_t len)
75 while (len > 0 && !isalnum(token[len - 1]))
80 static int same_token(struct trailer_item *a, struct arg_item *b)
82 size_t a_len, b_len, min_len;
87 a_len = token_len_without_separator(a->token, strlen(a->token));
88 b_len = token_len_without_separator(b->token, strlen(b->token));
89 min_len = (a_len > b_len) ? b_len : a_len;
91 return !strncasecmp(a->token, b->token, min_len);
94 static int same_value(struct trailer_item *a, struct arg_item *b)
96 return !strcasecmp(a->value, b->value);
99 static int same_trailer(struct trailer_item *a, struct arg_item *b)
101 return same_token(a, b) && same_value(a, b);
104 static inline int is_blank_line(const char *str)
107 while (*s && *s != '\n' && isspace(*s))
109 return !*s || *s == '\n';
112 static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b)
114 const char *ptr = strstr(sb->buf, a);
116 strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));
119 static void free_trailer_item(struct trailer_item *item)
126 static void free_arg_item(struct arg_item *item)
128 free(item->conf.name);
129 free(item->conf.key);
130 free(item->conf.command);
131 free(item->conf.cmd);
137 static char last_non_space_char(const char *s)
140 for (i = strlen(s) - 1; i >= 0; i--)
146 static void print_tok_val(FILE *outfile, const char *tok, const char *val)
151 fprintf(outfile, "%s\n", val);
155 c = last_non_space_char(tok);
158 if (strchr(separators, c))
159 fprintf(outfile, "%s%s\n", tok, val);
161 fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
164 static void print_all(FILE *outfile, struct list_head *head,
165 const struct process_trailer_options *opts)
167 struct list_head *pos;
168 struct trailer_item *item;
169 list_for_each(pos, head) {
170 item = list_entry(pos, struct trailer_item, list);
171 if ((!opts->trim_empty || strlen(item->value) > 0) &&
172 (!opts->only_trailers || item->token))
173 print_tok_val(outfile, item->token, item->value);
177 static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok)
179 struct trailer_item *new_item = xcalloc(1, sizeof(*new_item));
180 new_item->token = arg_tok->token;
181 new_item->value = arg_tok->value;
182 arg_tok->token = arg_tok->value = NULL;
183 free_arg_item(arg_tok);
187 static void add_arg_to_input_list(struct trailer_item *on_tok,
188 struct arg_item *arg_tok)
190 int aoe = after_or_end(arg_tok->conf.where);
191 struct trailer_item *to_add = trailer_from_arg(arg_tok);
193 list_add(&to_add->list, &on_tok->list);
195 list_add_tail(&to_add->list, &on_tok->list);
198 static int check_if_different(struct trailer_item *in_tok,
199 struct arg_item *arg_tok,
201 struct list_head *head)
203 enum trailer_where where = arg_tok->conf.where;
204 struct list_head *next_head;
206 if (same_trailer(in_tok, arg_tok))
209 * if we want to add a trailer after another one,
210 * we have to check those before this one
212 next_head = after_or_end(where) ? in_tok->list.prev
214 if (next_head == head)
216 in_tok = list_entry(next_head, struct trailer_item, list);
221 static char *apply_command(struct conf_info *conf, const char *arg)
223 struct strbuf cmd = STRBUF_INIT;
224 struct strbuf buf = STRBUF_INIT;
225 struct child_process cp = CHILD_PROCESS_INIT;
229 strbuf_addstr(&cmd, conf->cmd);
230 strvec_push(&cp.args, cmd.buf);
232 strvec_push(&cp.args, arg);
233 } else if (conf->command) {
234 strbuf_addstr(&cmd, conf->command);
236 strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
237 strvec_push(&cp.args, cmd.buf);
239 cp.env = local_repo_env;
243 if (capture_command(&cp, &buf, 1024)) {
244 error(_("running trailer command '%s' failed"), cmd.buf);
245 strbuf_release(&buf);
246 result = xstrdup("");
249 result = strbuf_detach(&buf, NULL);
252 strbuf_release(&cmd);
256 static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok)
258 if (arg_tok->conf.command || arg_tok->conf.cmd) {
260 if (arg_tok->value && arg_tok->value[0]) {
261 arg = arg_tok->value;
263 if (in_tok && in_tok->value)
264 arg = xstrdup(in_tok->value);
268 arg_tok->value = apply_command(&arg_tok->conf, arg);
273 static void apply_arg_if_exists(struct trailer_item *in_tok,
274 struct arg_item *arg_tok,
275 struct trailer_item *on_tok,
276 struct list_head *head)
278 switch (arg_tok->conf.if_exists) {
279 case EXISTS_DO_NOTHING:
280 free_arg_item(arg_tok);
283 apply_item_command(in_tok, arg_tok);
284 add_arg_to_input_list(on_tok, arg_tok);
285 list_del(&in_tok->list);
286 free_trailer_item(in_tok);
289 apply_item_command(in_tok, arg_tok);
290 add_arg_to_input_list(on_tok, arg_tok);
292 case EXISTS_ADD_IF_DIFFERENT:
293 apply_item_command(in_tok, arg_tok);
294 if (check_if_different(in_tok, arg_tok, 1, head))
295 add_arg_to_input_list(on_tok, arg_tok);
297 free_arg_item(arg_tok);
299 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR:
300 apply_item_command(in_tok, arg_tok);
301 if (check_if_different(on_tok, arg_tok, 0, head))
302 add_arg_to_input_list(on_tok, arg_tok);
304 free_arg_item(arg_tok);
307 BUG("trailer.c: unhandled value %d",
308 arg_tok->conf.if_exists);
312 static void apply_arg_if_missing(struct list_head *head,
313 struct arg_item *arg_tok)
315 enum trailer_where where;
316 struct trailer_item *to_add;
318 switch (arg_tok->conf.if_missing) {
319 case MISSING_DO_NOTHING:
320 free_arg_item(arg_tok);
323 where = arg_tok->conf.where;
324 apply_item_command(NULL, arg_tok);
325 to_add = trailer_from_arg(arg_tok);
326 if (after_or_end(where))
327 list_add_tail(&to_add->list, head);
329 list_add(&to_add->list, head);
332 BUG("trailer.c: unhandled value %d",
333 arg_tok->conf.if_missing);
337 static int find_same_and_apply_arg(struct list_head *head,
338 struct arg_item *arg_tok)
340 struct list_head *pos;
341 struct trailer_item *in_tok;
342 struct trailer_item *on_tok;
344 enum trailer_where where = arg_tok->conf.where;
345 int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
346 int backwards = after_or_end(where);
347 struct trailer_item *start_tok;
349 if (list_empty(head))
352 start_tok = list_entry(backwards ? head->prev : head->next,
356 list_for_each_dir(pos, head, backwards) {
357 in_tok = list_entry(pos, struct trailer_item, list);
358 if (!same_token(in_tok, arg_tok))
360 on_tok = middle ? in_tok : start_tok;
361 apply_arg_if_exists(in_tok, arg_tok, on_tok, head);
367 static void process_trailers_lists(struct list_head *head,
368 struct list_head *arg_head)
370 struct list_head *pos, *p;
371 struct arg_item *arg_tok;
373 list_for_each_safe(pos, p, arg_head) {
375 arg_tok = list_entry(pos, struct arg_item, list);
379 applied = find_same_and_apply_arg(head, arg_tok);
382 apply_arg_if_missing(head, arg_tok);
386 int trailer_set_where(enum trailer_where *item, const char *value)
389 *item = WHERE_DEFAULT;
390 else if (!strcasecmp("after", value))
392 else if (!strcasecmp("before", value))
393 *item = WHERE_BEFORE;
394 else if (!strcasecmp("end", value))
396 else if (!strcasecmp("start", value))
403 int trailer_set_if_exists(enum trailer_if_exists *item, const char *value)
406 *item = EXISTS_DEFAULT;
407 else if (!strcasecmp("addIfDifferent", value))
408 *item = EXISTS_ADD_IF_DIFFERENT;
409 else if (!strcasecmp("addIfDifferentNeighbor", value))
410 *item = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
411 else if (!strcasecmp("add", value))
413 else if (!strcasecmp("replace", value))
414 *item = EXISTS_REPLACE;
415 else if (!strcasecmp("doNothing", value))
416 *item = EXISTS_DO_NOTHING;
422 int trailer_set_if_missing(enum trailer_if_missing *item, const char *value)
425 *item = MISSING_DEFAULT;
426 else if (!strcasecmp("doNothing", value))
427 *item = MISSING_DO_NOTHING;
428 else if (!strcasecmp("add", value))
435 static void duplicate_conf(struct conf_info *dst, const struct conf_info *src)
438 dst->name = xstrdup_or_null(src->name);
439 dst->key = xstrdup_or_null(src->key);
440 dst->command = xstrdup_or_null(src->command);
441 dst->cmd = xstrdup_or_null(src->cmd);
444 static struct arg_item *get_conf_item(const char *name)
446 struct list_head *pos;
447 struct arg_item *item;
449 /* Look up item with same name */
450 list_for_each(pos, &conf_head) {
451 item = list_entry(pos, struct arg_item, list);
452 if (!strcasecmp(item->conf.name, name))
456 /* Item does not already exists, create it */
457 CALLOC_ARRAY(item, 1);
458 duplicate_conf(&item->conf, &default_conf_info);
459 item->conf.name = xstrdup(name);
461 list_add_tail(&item->list, &conf_head);
466 enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_CMD,
467 TRAILER_WHERE, TRAILER_IF_EXISTS, TRAILER_IF_MISSING };
471 enum trailer_info_type type;
472 } trailer_config_items[] = {
473 { "key", TRAILER_KEY },
474 { "command", TRAILER_COMMAND },
475 { "cmd", TRAILER_CMD },
476 { "where", TRAILER_WHERE },
477 { "ifexists", TRAILER_IF_EXISTS },
478 { "ifmissing", TRAILER_IF_MISSING }
481 static int git_trailer_default_config(const char *conf_key, const char *value, void *cb)
483 const char *trailer_item, *variable_name;
485 if (!skip_prefix(conf_key, "trailer.", &trailer_item))
488 variable_name = strrchr(trailer_item, '.');
489 if (!variable_name) {
490 if (!strcmp(trailer_item, "where")) {
491 if (trailer_set_where(&default_conf_info.where,
493 warning(_("unknown value '%s' for key '%s'"),
495 } else if (!strcmp(trailer_item, "ifexists")) {
496 if (trailer_set_if_exists(&default_conf_info.if_exists,
498 warning(_("unknown value '%s' for key '%s'"),
500 } else if (!strcmp(trailer_item, "ifmissing")) {
501 if (trailer_set_if_missing(&default_conf_info.if_missing,
503 warning(_("unknown value '%s' for key '%s'"),
505 } else if (!strcmp(trailer_item, "separators")) {
506 separators = xstrdup(value);
512 static int git_trailer_config(const char *conf_key, const char *value, void *cb)
514 const char *trailer_item, *variable_name;
515 struct arg_item *item;
516 struct conf_info *conf;
518 enum trailer_info_type type;
521 if (!skip_prefix(conf_key, "trailer.", &trailer_item))
524 variable_name = strrchr(trailer_item, '.');
529 for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) {
530 if (strcmp(trailer_config_items[i].name, variable_name))
532 name = xstrndup(trailer_item, variable_name - trailer_item - 1);
533 type = trailer_config_items[i].type;
540 item = get_conf_item(name);
547 warning(_("more than one %s"), conf_key);
548 conf->key = xstrdup(value);
550 case TRAILER_COMMAND:
552 warning(_("more than one %s"), conf_key);
553 conf->command = xstrdup(value);
557 warning(_("more than one %s"), conf_key);
558 conf->cmd = xstrdup(value);
561 if (trailer_set_where(&conf->where, value))
562 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
564 case TRAILER_IF_EXISTS:
565 if (trailer_set_if_exists(&conf->if_exists, value))
566 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
568 case TRAILER_IF_MISSING:
569 if (trailer_set_if_missing(&conf->if_missing, value))
570 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
573 BUG("trailer.c: unhandled type %d", type);
578 static void ensure_configured(void)
583 /* Default config must be setup first */
584 default_conf_info.where = WHERE_END;
585 default_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
586 default_conf_info.if_missing = MISSING_ADD;
587 git_config(git_trailer_default_config, NULL);
588 git_config(git_trailer_config, NULL);
592 static const char *token_from_item(struct arg_item *item, char *tok)
595 return item->conf.key;
598 return item->conf.name;
601 static int token_matches_item(const char *tok, struct arg_item *item, size_t tok_len)
603 if (!strncasecmp(tok, item->conf.name, tok_len))
605 return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0;
609 * If the given line is of the form
610 * "<token><optional whitespace><separator>..." or "<separator>...", return the
611 * location of the separator. Otherwise, return -1. The optional whitespace
612 * is allowed there primarily to allow things like "Bug #43" where <token> is
613 * "Bug" and <separator> is "#".
615 * The separator-starts-line case (in which this function returns 0) is
616 * distinguished from the non-well-formed-line case (in which this function
617 * returns -1) because some callers of this function need such a distinction.
619 static ssize_t find_separator(const char *line, const char *separators)
621 int whitespace_found = 0;
623 for (c = line; *c; c++) {
624 if (strchr(separators, *c))
626 if (!whitespace_found && (isalnum(*c) || *c == '-'))
628 if (c != line && (*c == ' ' || *c == '\t')) {
629 whitespace_found = 1;
638 * Obtain the token, value, and conf from the given trailer.
640 * separator_pos must not be 0, since the token cannot be an empty string.
642 * If separator_pos is -1, interpret the whole trailer as a token.
644 static void parse_trailer(struct strbuf *tok, struct strbuf *val,
645 const struct conf_info **conf, const char *trailer,
646 ssize_t separator_pos)
648 struct arg_item *item;
650 struct list_head *pos;
652 if (separator_pos != -1) {
653 strbuf_add(tok, trailer, separator_pos);
655 strbuf_addstr(val, trailer + separator_pos + 1);
658 strbuf_addstr(tok, trailer);
662 /* Lookup if the token matches something in the config */
663 tok_len = token_len_without_separator(tok->buf, tok->len);
665 *conf = &default_conf_info;
666 list_for_each(pos, &conf_head) {
667 item = list_entry(pos, struct arg_item, list);
668 if (token_matches_item(tok->buf, item, tok_len)) {
669 char *tok_buf = strbuf_detach(tok, NULL);
672 strbuf_addstr(tok, token_from_item(item, tok_buf));
679 static struct trailer_item *add_trailer_item(struct list_head *head, char *tok,
682 struct trailer_item *new_item = xcalloc(1, sizeof(*new_item));
683 new_item->token = tok;
684 new_item->value = val;
685 list_add_tail(&new_item->list, head);
689 static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
690 const struct conf_info *conf,
691 const struct new_trailer_item *new_trailer_item)
693 struct arg_item *new_item = xcalloc(1, sizeof(*new_item));
694 new_item->token = tok;
695 new_item->value = val;
696 duplicate_conf(&new_item->conf, conf);
697 if (new_trailer_item) {
698 if (new_trailer_item->where != WHERE_DEFAULT)
699 new_item->conf.where = new_trailer_item->where;
700 if (new_trailer_item->if_exists != EXISTS_DEFAULT)
701 new_item->conf.if_exists = new_trailer_item->if_exists;
702 if (new_trailer_item->if_missing != MISSING_DEFAULT)
703 new_item->conf.if_missing = new_trailer_item->if_missing;
705 list_add_tail(&new_item->list, arg_head);
708 static void process_command_line_args(struct list_head *arg_head,
709 struct list_head *new_trailer_head)
711 struct arg_item *item;
712 struct strbuf tok = STRBUF_INIT;
713 struct strbuf val = STRBUF_INIT;
714 const struct conf_info *conf;
715 struct list_head *pos;
718 * In command-line arguments, '=' is accepted (in addition to the
719 * separators that are defined).
721 char *cl_separators = xstrfmt("=%s", separators);
723 /* Add an arg item for each configured trailer with a command */
724 list_for_each(pos, &conf_head) {
725 item = list_entry(pos, struct arg_item, list);
726 if (item->conf.command)
727 add_arg_item(arg_head,
728 xstrdup(token_from_item(item, NULL)),
733 /* Add an arg item for each trailer on the command line */
734 list_for_each(pos, new_trailer_head) {
735 struct new_trailer_item *tr =
736 list_entry(pos, struct new_trailer_item, list);
737 ssize_t separator_pos = find_separator(tr->text, cl_separators);
739 if (separator_pos == 0) {
740 struct strbuf sb = STRBUF_INIT;
741 strbuf_addstr(&sb, tr->text);
743 error(_("empty trailer token in trailer '%.*s'"),
744 (int) sb.len, sb.buf);
747 parse_trailer(&tok, &val, &conf, tr->text,
749 add_arg_item(arg_head,
750 strbuf_detach(&tok, NULL),
751 strbuf_detach(&val, NULL),
759 static void read_input_file(struct strbuf *sb, const char *file)
762 if (strbuf_read_file(sb, file, 0) < 0)
763 die_errno(_("could not read input file '%s'"), file);
765 if (strbuf_read(sb, fileno(stdin), 0) < 0)
766 die_errno(_("could not read from stdin"));
770 static const char *next_line(const char *str)
772 const char *nl = strchrnul(str, '\n');
777 * Return the position of the start of the last line. If len is 0, return -1.
779 static ssize_t last_line(const char *buf, size_t len)
787 * Skip the last character (in addition to the null terminator),
788 * because if the last character is a newline, it is considered as part
789 * of the last line anyway.
793 for (; i >= 0; i--) {
801 * Return the position of the start of the patch or the length of str if there
802 * is no patch in the message.
804 static size_t find_patch_start(const char *str)
808 for (s = str; *s; s = next_line(s)) {
811 if (skip_prefix(s, "---", &v) && isspace(*v))
819 * Return the position of the first trailer line or len if there are no
822 static size_t find_trailer_start(const char *buf, size_t len)
825 ssize_t end_of_title, l;
827 int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0;
829 * Number of possible continuation lines encountered. This will be
830 * reset to 0 if we encounter a trailer (since those lines are to be
831 * considered continuations of that trailer), and added to
832 * non_trailer_lines if we encounter a non-trailer (since those lines
833 * are to be considered non-trailers).
835 int possible_continuation_lines = 0;
837 /* The first paragraph is the title and cannot be trailers */
838 for (s = buf; s < buf + len; s = next_line(s)) {
839 if (s[0] == comment_line_char)
841 if (is_blank_line(s))
844 end_of_title = s - buf;
847 * Get the start of the trailers by looking starting from the end for a
848 * blank line before a set of non-blank lines that (i) are all
849 * trailers, or (ii) contains at least one Git-generated trailer and
850 * consists of at least 25% trailers.
852 for (l = last_line(buf, len);
854 l = last_line(buf, l)) {
855 const char *bol = buf + l;
857 ssize_t separator_pos;
859 if (bol[0] == comment_line_char) {
860 non_trailer_lines += possible_continuation_lines;
861 possible_continuation_lines = 0;
864 if (is_blank_line(bol)) {
867 non_trailer_lines += possible_continuation_lines;
868 if (recognized_prefix &&
869 trailer_lines * 3 >= non_trailer_lines)
870 return next_line(bol) - buf;
871 else if (trailer_lines && !non_trailer_lines)
872 return next_line(bol) - buf;
877 for (p = git_generated_prefixes; *p; p++) {
878 if (starts_with(bol, *p)) {
880 possible_continuation_lines = 0;
881 recognized_prefix = 1;
882 goto continue_outer_loop;
886 separator_pos = find_separator(bol, separators);
887 if (separator_pos >= 1 && !isspace(bol[0])) {
888 struct list_head *pos;
891 possible_continuation_lines = 0;
892 if (recognized_prefix)
894 list_for_each(pos, &conf_head) {
895 struct arg_item *item;
896 item = list_entry(pos, struct arg_item, list);
897 if (token_matches_item(bol, item,
899 recognized_prefix = 1;
903 } else if (isspace(bol[0]))
904 possible_continuation_lines++;
907 non_trailer_lines += possible_continuation_lines;
908 possible_continuation_lines = 0;
917 /* Return the position of the end of the trailers. */
918 static size_t find_trailer_end(const char *buf, size_t len)
920 return len - ignore_non_trailer(buf, len);
923 static int ends_with_blank_line(const char *buf, size_t len)
925 ssize_t ll = last_line(buf, len);
928 return is_blank_line(buf + ll);
931 static void unfold_value(struct strbuf *val)
933 struct strbuf out = STRBUF_INIT;
936 strbuf_grow(&out, val->len);
938 while (i < val->len) {
939 char c = val->buf[i++];
941 /* Collapse continuation down to a single space. */
942 while (i < val->len && isspace(val->buf[i]))
944 strbuf_addch(&out, ' ');
946 strbuf_addch(&out, c);
950 /* Empty lines may have left us with whitespace cruft at the edges */
953 /* output goes back to val as if we modified it in-place */
954 strbuf_swap(&out, val);
955 strbuf_release(&out);
958 static size_t process_input_file(FILE *outfile,
960 struct list_head *head,
961 const struct process_trailer_options *opts)
963 struct trailer_info info;
964 struct strbuf tok = STRBUF_INIT;
965 struct strbuf val = STRBUF_INIT;
968 trailer_info_get(&info, str, opts);
970 /* Print lines before the trailers as is */
971 if (!opts->only_trailers)
972 fwrite(str, 1, info.trailer_start - str, outfile);
974 if (!opts->only_trailers && !info.blank_line_before_trailer)
975 fprintf(outfile, "\n");
977 for (i = 0; i < info.trailer_nr; i++) {
979 char *trailer = info.trailers[i];
980 if (trailer[0] == comment_line_char)
982 separator_pos = find_separator(trailer, separators);
983 if (separator_pos >= 1) {
984 parse_trailer(&tok, &val, NULL, trailer,
988 add_trailer_item(head,
989 strbuf_detach(&tok, NULL),
990 strbuf_detach(&val, NULL));
991 } else if (!opts->only_trailers) {
992 strbuf_addstr(&val, trailer);
993 strbuf_strip_suffix(&val, "\n");
994 add_trailer_item(head,
996 strbuf_detach(&val, NULL));
1000 trailer_info_release(&info);
1002 return info.trailer_end - str;
1005 static void free_all(struct list_head *head)
1007 struct list_head *pos, *p;
1008 list_for_each_safe(pos, p, head) {
1010 free_trailer_item(list_entry(pos, struct trailer_item, list));
1014 static struct tempfile *trailers_tempfile;
1016 static FILE *create_in_place_tempfile(const char *file)
1019 struct strbuf filename_template = STRBUF_INIT;
1023 if (stat(file, &st))
1024 die_errno(_("could not stat %s"), file);
1025 if (!S_ISREG(st.st_mode))
1026 die(_("file %s is not a regular file"), file);
1027 if (!(st.st_mode & S_IWUSR))
1028 die(_("file %s is not writable by user"), file);
1030 /* Create temporary file in the same directory as the original */
1031 tail = strrchr(file, '/');
1033 strbuf_add(&filename_template, file, tail - file + 1);
1034 strbuf_addstr(&filename_template, "git-interpret-trailers-XXXXXX");
1036 trailers_tempfile = xmks_tempfile_m(filename_template.buf, st.st_mode);
1037 strbuf_release(&filename_template);
1038 outfile = fdopen_tempfile(trailers_tempfile, "w");
1040 die_errno(_("could not open temporary file"));
1045 void process_trailers(const char *file,
1046 const struct process_trailer_options *opts,
1047 struct list_head *new_trailer_head)
1050 struct strbuf sb = STRBUF_INIT;
1052 FILE *outfile = stdout;
1054 ensure_configured();
1056 read_input_file(&sb, file);
1059 outfile = create_in_place_tempfile(file);
1061 /* Print the lines before the trailers */
1062 trailer_end = process_input_file(outfile, sb.buf, &head, opts);
1064 if (!opts->only_input) {
1065 LIST_HEAD(arg_head);
1066 process_command_line_args(&arg_head, new_trailer_head);
1067 process_trailers_lists(&head, &arg_head);
1070 print_all(outfile, &head, opts);
1074 /* Print the lines after the trailers as is */
1075 if (!opts->only_trailers)
1076 fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
1079 if (rename_tempfile(&trailers_tempfile, file))
1080 die_errno(_("could not rename temporary file to %s"), file);
1082 strbuf_release(&sb);
1085 void trailer_info_get(struct trailer_info *info, const char *str,
1086 const struct process_trailer_options *opts)
1088 int patch_start, trailer_end, trailer_start;
1089 struct strbuf **trailer_lines, **ptr;
1090 char **trailer_strings = NULL;
1091 size_t nr = 0, alloc = 0;
1094 ensure_configured();
1096 if (opts->no_divider)
1097 patch_start = strlen(str);
1099 patch_start = find_patch_start(str);
1101 trailer_end = find_trailer_end(str, patch_start);
1102 trailer_start = find_trailer_start(str, trailer_end);
1104 trailer_lines = strbuf_split_buf(str + trailer_start,
1105 trailer_end - trailer_start,
1108 for (ptr = trailer_lines; *ptr; ptr++) {
1109 if (last && isspace((*ptr)->buf[0])) {
1110 struct strbuf sb = STRBUF_INIT;
1111 strbuf_attach(&sb, *last, strlen(*last), strlen(*last));
1112 strbuf_addbuf(&sb, *ptr);
1113 *last = strbuf_detach(&sb, NULL);
1116 ALLOC_GROW(trailer_strings, nr + 1, alloc);
1117 trailer_strings[nr] = strbuf_detach(*ptr, NULL);
1118 last = find_separator(trailer_strings[nr], separators) >= 1
1119 ? &trailer_strings[nr]
1123 strbuf_list_free(trailer_lines);
1125 info->blank_line_before_trailer = ends_with_blank_line(str,
1127 info->trailer_start = str + trailer_start;
1128 info->trailer_end = str + trailer_end;
1129 info->trailers = trailer_strings;
1130 info->trailer_nr = nr;
1133 void trailer_info_release(struct trailer_info *info)
1136 for (i = 0; i < info->trailer_nr; i++)
1137 free(info->trailers[i]);
1138 free(info->trailers);
1141 static void format_trailer_info(struct strbuf *out,
1142 const struct trailer_info *info,
1143 const struct process_trailer_options *opts)
1145 size_t origlen = out->len;
1148 /* If we want the whole block untouched, we can take the fast path. */
1149 if (!opts->only_trailers && !opts->unfold && !opts->filter &&
1150 !opts->separator && !opts->key_only && !opts->value_only &&
1151 !opts->key_value_separator) {
1152 strbuf_add(out, info->trailer_start,
1153 info->trailer_end - info->trailer_start);
1157 for (i = 0; i < info->trailer_nr; i++) {
1158 char *trailer = info->trailers[i];
1159 ssize_t separator_pos = find_separator(trailer, separators);
1161 if (separator_pos >= 1) {
1162 struct strbuf tok = STRBUF_INIT;
1163 struct strbuf val = STRBUF_INIT;
1165 parse_trailer(&tok, &val, NULL, trailer, separator_pos);
1166 if (!opts->filter || opts->filter(&tok, opts->filter_data)) {
1170 if (opts->separator && out->len != origlen)
1171 strbuf_addbuf(out, opts->separator);
1172 if (!opts->value_only)
1173 strbuf_addbuf(out, &tok);
1174 if (!opts->key_only && !opts->value_only) {
1175 if (opts->key_value_separator)
1176 strbuf_addbuf(out, opts->key_value_separator);
1178 strbuf_addstr(out, ": ");
1180 if (!opts->key_only)
1181 strbuf_addbuf(out, &val);
1182 if (!opts->separator)
1183 strbuf_addch(out, '\n');
1185 strbuf_release(&tok);
1186 strbuf_release(&val);
1188 } else if (!opts->only_trailers) {
1189 if (opts->separator && out->len != origlen) {
1190 strbuf_addbuf(out, opts->separator);
1192 strbuf_addstr(out, trailer);
1193 if (opts->separator) {
1201 void format_trailers_from_commit(struct strbuf *out, const char *msg,
1202 const struct process_trailer_options *opts)
1204 struct trailer_info info;
1206 trailer_info_get(&info, msg, opts);
1207 format_trailer_info(out, &info, opts);
1208 trailer_info_release(&info);
1211 void trailer_iterator_init(struct trailer_iterator *iter, const char *msg)
1213 struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
1214 strbuf_init(&iter->key, 0);
1215 strbuf_init(&iter->val, 0);
1216 opts.no_divider = 1;
1217 trailer_info_get(&iter->info, msg, &opts);
1221 int trailer_iterator_advance(struct trailer_iterator *iter)
1223 while (iter->cur < iter->info.trailer_nr) {
1224 char *trailer = iter->info.trailers[iter->cur++];
1225 int separator_pos = find_separator(trailer, separators);
1227 if (separator_pos < 1)
1228 continue; /* not a real trailer */
1230 strbuf_reset(&iter->key);
1231 strbuf_reset(&iter->val);
1232 parse_trailer(&iter->key, &iter->val, NULL,
1233 trailer, separator_pos);
1234 unfold_value(&iter->val);
1240 void trailer_iterator_release(struct trailer_iterator *iter)
1242 trailer_info_release(&iter->info);
1243 strbuf_release(&iter->val);
1244 strbuf_release(&iter->key);