3 #include "string-list.h"
4 #include "run-command.h"
10 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
17 enum trailer_where where;
18 enum trailer_if_exists if_exists;
19 enum trailer_if_missing if_missing;
22 static struct conf_info default_conf_info;
25 struct list_head list;
27 * If this is not a trailer line, the line is stored in value
28 * (excluding the terminating newline) and token is NULL.
35 struct list_head list;
38 struct conf_info conf;
41 static LIST_HEAD(conf_head);
43 static char *separators = ":";
45 static int configured;
47 #define TRAILER_ARG_STRING "$ARG"
49 static const char *git_generated_prefixes[] = {
51 "(cherry picked from commit ",
55 /* Iterate over the elements of the list. */
56 #define list_for_each_dir(pos, head, is_reverse) \
57 for (pos = is_reverse ? (head)->prev : (head)->next; \
59 pos = is_reverse ? pos->prev : pos->next)
61 static int after_or_end(enum trailer_where where)
63 return (where == WHERE_AFTER) || (where == WHERE_END);
67 * Return the length of the string not including any final
68 * punctuation. E.g., the input "Signed-off-by:" would return
69 * 13, stripping the trailing punctuation but retaining
70 * internal punctuation.
72 static size_t token_len_without_separator(const char *token, size_t len)
74 while (len > 0 && !isalnum(token[len - 1]))
79 static int same_token(struct trailer_item *a, struct arg_item *b)
81 size_t a_len, b_len, min_len;
86 a_len = token_len_without_separator(a->token, strlen(a->token));
87 b_len = token_len_without_separator(b->token, strlen(b->token));
88 min_len = (a_len > b_len) ? b_len : a_len;
90 return !strncasecmp(a->token, b->token, min_len);
93 static int same_value(struct trailer_item *a, struct arg_item *b)
95 return !strcasecmp(a->value, b->value);
98 static int same_trailer(struct trailer_item *a, struct arg_item *b)
100 return same_token(a, b) && same_value(a, b);
103 static inline int is_blank_line(const char *str)
106 while (*s && *s != '\n' && isspace(*s))
108 return !*s || *s == '\n';
111 static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b)
113 const char *ptr = strstr(sb->buf, a);
115 strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));
118 static void free_trailer_item(struct trailer_item *item)
125 static void free_arg_item(struct arg_item *item)
127 free(item->conf.name);
128 free(item->conf.key);
129 free(item->conf.command);
135 static char last_non_space_char(const char *s)
138 for (i = strlen(s) - 1; i >= 0; i--)
144 static void print_tok_val(FILE *outfile, const char *tok, const char *val)
149 fprintf(outfile, "%s\n", val);
153 c = last_non_space_char(tok);
156 if (strchr(separators, c))
157 fprintf(outfile, "%s%s\n", tok, val);
159 fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
162 static void print_all(FILE *outfile, struct list_head *head, int trim_empty)
164 struct list_head *pos;
165 struct trailer_item *item;
166 list_for_each(pos, head) {
167 item = list_entry(pos, struct trailer_item, list);
168 if (!trim_empty || strlen(item->value) > 0)
169 print_tok_val(outfile, item->token, item->value);
173 static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok)
175 struct trailer_item *new = xcalloc(sizeof(*new), 1);
176 new->token = arg_tok->token;
177 new->value = arg_tok->value;
178 arg_tok->token = arg_tok->value = NULL;
179 free_arg_item(arg_tok);
183 static void add_arg_to_input_list(struct trailer_item *on_tok,
184 struct arg_item *arg_tok)
186 int aoe = after_or_end(arg_tok->conf.where);
187 struct trailer_item *to_add = trailer_from_arg(arg_tok);
189 list_add(&to_add->list, &on_tok->list);
191 list_add_tail(&to_add->list, &on_tok->list);
194 static int check_if_different(struct trailer_item *in_tok,
195 struct arg_item *arg_tok,
197 struct list_head *head)
199 enum trailer_where where = arg_tok->conf.where;
200 struct list_head *next_head;
202 if (same_trailer(in_tok, arg_tok))
205 * if we want to add a trailer after another one,
206 * we have to check those before this one
208 next_head = after_or_end(where) ? in_tok->list.prev
210 if (next_head == head)
212 in_tok = list_entry(next_head, struct trailer_item, list);
217 static char *apply_command(const char *command, const char *arg)
219 struct strbuf cmd = STRBUF_INIT;
220 struct strbuf buf = STRBUF_INIT;
221 struct child_process cp = CHILD_PROCESS_INIT;
222 const char *argv[] = {NULL, NULL};
225 strbuf_addstr(&cmd, command);
227 strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
231 cp.env = local_repo_env;
235 if (capture_command(&cp, &buf, 1024)) {
236 error(_("running trailer command '%s' failed"), cmd.buf);
237 strbuf_release(&buf);
238 result = xstrdup("");
241 result = strbuf_detach(&buf, NULL);
244 strbuf_release(&cmd);
248 static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok)
250 if (arg_tok->conf.command) {
252 if (arg_tok->value && arg_tok->value[0]) {
253 arg = arg_tok->value;
255 if (in_tok && in_tok->value)
256 arg = xstrdup(in_tok->value);
260 arg_tok->value = apply_command(arg_tok->conf.command, arg);
265 static void apply_arg_if_exists(struct trailer_item *in_tok,
266 struct arg_item *arg_tok,
267 struct trailer_item *on_tok,
268 struct list_head *head)
270 switch (arg_tok->conf.if_exists) {
271 case EXISTS_DO_NOTHING:
272 free_arg_item(arg_tok);
275 apply_item_command(in_tok, arg_tok);
276 add_arg_to_input_list(on_tok, arg_tok);
277 list_del(&in_tok->list);
278 free_trailer_item(in_tok);
281 apply_item_command(in_tok, arg_tok);
282 add_arg_to_input_list(on_tok, arg_tok);
284 case EXISTS_ADD_IF_DIFFERENT:
285 apply_item_command(in_tok, arg_tok);
286 if (check_if_different(in_tok, arg_tok, 1, head))
287 add_arg_to_input_list(on_tok, arg_tok);
289 free_arg_item(arg_tok);
291 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR:
292 apply_item_command(in_tok, arg_tok);
293 if (check_if_different(on_tok, arg_tok, 0, head))
294 add_arg_to_input_list(on_tok, arg_tok);
296 free_arg_item(arg_tok);
301 static void apply_arg_if_missing(struct list_head *head,
302 struct arg_item *arg_tok)
304 enum trailer_where where;
305 struct trailer_item *to_add;
307 switch (arg_tok->conf.if_missing) {
308 case MISSING_DO_NOTHING:
309 free_arg_item(arg_tok);
312 where = arg_tok->conf.where;
313 apply_item_command(NULL, arg_tok);
314 to_add = trailer_from_arg(arg_tok);
315 if (after_or_end(where))
316 list_add_tail(&to_add->list, head);
318 list_add(&to_add->list, head);
322 static int find_same_and_apply_arg(struct list_head *head,
323 struct arg_item *arg_tok)
325 struct list_head *pos;
326 struct trailer_item *in_tok;
327 struct trailer_item *on_tok;
329 enum trailer_where where = arg_tok->conf.where;
330 int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
331 int backwards = after_or_end(where);
332 struct trailer_item *start_tok;
334 if (list_empty(head))
337 start_tok = list_entry(backwards ? head->prev : head->next,
341 list_for_each_dir(pos, head, backwards) {
342 in_tok = list_entry(pos, struct trailer_item, list);
343 if (!same_token(in_tok, arg_tok))
345 on_tok = middle ? in_tok : start_tok;
346 apply_arg_if_exists(in_tok, arg_tok, on_tok, head);
352 static void process_trailers_lists(struct list_head *head,
353 struct list_head *arg_head)
355 struct list_head *pos, *p;
356 struct arg_item *arg_tok;
358 list_for_each_safe(pos, p, arg_head) {
360 arg_tok = list_entry(pos, struct arg_item, list);
364 applied = find_same_and_apply_arg(head, arg_tok);
367 apply_arg_if_missing(head, arg_tok);
371 int trailer_set_where(enum trailer_where *item, const char *value)
373 if (!strcasecmp("after", value))
375 else if (!strcasecmp("before", value))
376 *item = WHERE_BEFORE;
377 else if (!strcasecmp("end", value))
379 else if (!strcasecmp("start", value))
386 int trailer_set_if_exists(enum trailer_if_exists *item, const char *value)
388 if (!strcasecmp("addIfDifferent", value))
389 *item = EXISTS_ADD_IF_DIFFERENT;
390 else if (!strcasecmp("addIfDifferentNeighbor", value))
391 *item = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
392 else if (!strcasecmp("add", value))
394 else if (!strcasecmp("replace", value))
395 *item = EXISTS_REPLACE;
396 else if (!strcasecmp("doNothing", value))
397 *item = EXISTS_DO_NOTHING;
403 int trailer_set_if_missing(enum trailer_if_missing *item, const char *value)
405 if (!strcasecmp("doNothing", value))
406 *item = MISSING_DO_NOTHING;
407 else if (!strcasecmp("add", value))
414 static void duplicate_conf(struct conf_info *dst, const struct conf_info *src)
417 dst->name = xstrdup_or_null(src->name);
418 dst->key = xstrdup_or_null(src->key);
419 dst->command = xstrdup_or_null(src->command);
422 static struct arg_item *get_conf_item(const char *name)
424 struct list_head *pos;
425 struct arg_item *item;
427 /* Look up item with same name */
428 list_for_each(pos, &conf_head) {
429 item = list_entry(pos, struct arg_item, list);
430 if (!strcasecmp(item->conf.name, name))
434 /* Item does not already exists, create it */
435 item = xcalloc(sizeof(*item), 1);
436 duplicate_conf(&item->conf, &default_conf_info);
437 item->conf.name = xstrdup(name);
439 list_add_tail(&item->list, &conf_head);
444 enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_WHERE,
445 TRAILER_IF_EXISTS, TRAILER_IF_MISSING };
449 enum trailer_info_type type;
450 } trailer_config_items[] = {
451 { "key", TRAILER_KEY },
452 { "command", TRAILER_COMMAND },
453 { "where", TRAILER_WHERE },
454 { "ifexists", TRAILER_IF_EXISTS },
455 { "ifmissing", TRAILER_IF_MISSING }
458 static int git_trailer_default_config(const char *conf_key, const char *value, void *cb)
460 const char *trailer_item, *variable_name;
462 if (!skip_prefix(conf_key, "trailer.", &trailer_item))
465 variable_name = strrchr(trailer_item, '.');
466 if (!variable_name) {
467 if (!strcmp(trailer_item, "where")) {
468 if (trailer_set_where(&default_conf_info.where,
470 warning(_("unknown value '%s' for key '%s'"),
472 } else if (!strcmp(trailer_item, "ifexists")) {
473 if (trailer_set_if_exists(&default_conf_info.if_exists,
475 warning(_("unknown value '%s' for key '%s'"),
477 } else if (!strcmp(trailer_item, "ifmissing")) {
478 if (trailer_set_if_missing(&default_conf_info.if_missing,
480 warning(_("unknown value '%s' for key '%s'"),
482 } else if (!strcmp(trailer_item, "separators")) {
483 separators = xstrdup(value);
489 static int git_trailer_config(const char *conf_key, const char *value, void *cb)
491 const char *trailer_item, *variable_name;
492 struct arg_item *item;
493 struct conf_info *conf;
495 enum trailer_info_type type;
498 if (!skip_prefix(conf_key, "trailer.", &trailer_item))
501 variable_name = strrchr(trailer_item, '.');
506 for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) {
507 if (strcmp(trailer_config_items[i].name, variable_name))
509 name = xstrndup(trailer_item, variable_name - trailer_item - 1);
510 type = trailer_config_items[i].type;
517 item = get_conf_item(name);
524 warning(_("more than one %s"), conf_key);
525 conf->key = xstrdup(value);
527 case TRAILER_COMMAND:
529 warning(_("more than one %s"), conf_key);
530 conf->command = xstrdup(value);
533 if (trailer_set_where(&conf->where, value))
534 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
536 case TRAILER_IF_EXISTS:
537 if (trailer_set_if_exists(&conf->if_exists, value))
538 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
540 case TRAILER_IF_MISSING:
541 if (trailer_set_if_missing(&conf->if_missing, value))
542 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
545 die("BUG: trailer.c: unhandled type %d", type);
550 static void ensure_configured(void)
555 /* Default config must be setup first */
556 default_conf_info.where = WHERE_END;
557 default_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
558 default_conf_info.if_missing = MISSING_ADD;
559 git_config(git_trailer_default_config, NULL);
560 git_config(git_trailer_config, NULL);
564 static const char *token_from_item(struct arg_item *item, char *tok)
567 return item->conf.key;
570 return item->conf.name;
573 static int token_matches_item(const char *tok, struct arg_item *item, int tok_len)
575 if (!strncasecmp(tok, item->conf.name, tok_len))
577 return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0;
581 * If the given line is of the form
582 * "<token><optional whitespace><separator>..." or "<separator>...", return the
583 * location of the separator. Otherwise, return -1. The optional whitespace
584 * is allowed there primarily to allow things like "Bug #43" where <token> is
585 * "Bug" and <separator> is "#".
587 * The separator-starts-line case (in which this function returns 0) is
588 * distinguished from the non-well-formed-line case (in which this function
589 * returns -1) because some callers of this function need such a distinction.
591 static int find_separator(const char *line, const char *separators)
593 int whitespace_found = 0;
595 for (c = line; *c; c++) {
596 if (strchr(separators, *c))
598 if (!whitespace_found && (isalnum(*c) || *c == '-'))
600 if (c != line && (*c == ' ' || *c == '\t')) {
601 whitespace_found = 1;
610 * Obtain the token, value, and conf from the given trailer.
612 * separator_pos must not be 0, since the token cannot be an empty string.
614 * If separator_pos is -1, interpret the whole trailer as a token.
616 static void parse_trailer(struct strbuf *tok, struct strbuf *val,
617 const struct conf_info **conf, const char *trailer,
620 struct arg_item *item;
622 struct list_head *pos;
624 if (separator_pos != -1) {
625 strbuf_add(tok, trailer, separator_pos);
627 strbuf_addstr(val, trailer + separator_pos + 1);
630 strbuf_addstr(tok, trailer);
634 /* Lookup if the token matches something in the config */
635 tok_len = token_len_without_separator(tok->buf, tok->len);
637 *conf = &default_conf_info;
638 list_for_each(pos, &conf_head) {
639 item = list_entry(pos, struct arg_item, list);
640 if (token_matches_item(tok->buf, item, tok_len)) {
641 char *tok_buf = strbuf_detach(tok, NULL);
644 strbuf_addstr(tok, token_from_item(item, tok_buf));
651 static struct trailer_item *add_trailer_item(struct list_head *head, char *tok,
654 struct trailer_item *new = xcalloc(sizeof(*new), 1);
657 list_add_tail(&new->list, head);
661 static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
662 const struct conf_info *conf)
664 struct arg_item *new = xcalloc(sizeof(*new), 1);
667 duplicate_conf(&new->conf, conf);
668 list_add_tail(&new->list, arg_head);
671 static void process_command_line_args(struct list_head *arg_head,
672 struct string_list *trailers)
674 struct string_list_item *tr;
675 struct arg_item *item;
676 struct strbuf tok = STRBUF_INIT;
677 struct strbuf val = STRBUF_INIT;
678 const struct conf_info *conf;
679 struct list_head *pos;
682 * In command-line arguments, '=' is accepted (in addition to the
683 * separators that are defined).
685 char *cl_separators = xstrfmt("=%s", separators);
687 /* Add an arg item for each configured trailer with a command */
688 list_for_each(pos, &conf_head) {
689 item = list_entry(pos, struct arg_item, list);
690 if (item->conf.command)
691 add_arg_item(arg_head,
692 xstrdup(token_from_item(item, NULL)),
697 /* Add an arg item for each trailer on the command line */
698 for_each_string_list_item(tr, trailers) {
699 int separator_pos = find_separator(tr->string, cl_separators);
700 if (separator_pos == 0) {
701 struct strbuf sb = STRBUF_INIT;
702 strbuf_addstr(&sb, tr->string);
704 error(_("empty trailer token in trailer '%.*s'"),
705 (int) sb.len, sb.buf);
708 parse_trailer(&tok, &val, &conf, tr->string,
710 add_arg_item(arg_head,
711 strbuf_detach(&tok, NULL),
712 strbuf_detach(&val, NULL),
720 static void read_input_file(struct strbuf *sb, const char *file)
723 if (strbuf_read_file(sb, file, 0) < 0)
724 die_errno(_("could not read input file '%s'"), file);
726 if (strbuf_read(sb, fileno(stdin), 0) < 0)
727 die_errno(_("could not read from stdin"));
731 static const char *next_line(const char *str)
733 const char *nl = strchrnul(str, '\n');
738 * Return the position of the start of the last line. If len is 0, return -1.
740 static int last_line(const char *buf, size_t len)
748 * Skip the last character (in addition to the null terminator),
749 * because if the last character is a newline, it is considered as part
750 * of the last line anyway.
754 for (; i >= 0; i--) {
762 * Return the position of the start of the patch or the length of str if there
763 * is no patch in the message.
765 static int find_patch_start(const char *str)
769 for (s = str; *s; s = next_line(s)) {
770 if (starts_with(s, "---"))
778 * Return the position of the first trailer line or len if there are no
781 static int find_trailer_start(const char *buf, size_t len)
784 int end_of_title, l, only_spaces = 1;
785 int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0;
787 * Number of possible continuation lines encountered. This will be
788 * reset to 0 if we encounter a trailer (since those lines are to be
789 * considered continuations of that trailer), and added to
790 * non_trailer_lines if we encounter a non-trailer (since those lines
791 * are to be considered non-trailers).
793 int possible_continuation_lines = 0;
795 /* The first paragraph is the title and cannot be trailers */
796 for (s = buf; s < buf + len; s = next_line(s)) {
797 if (s[0] == comment_line_char)
799 if (is_blank_line(s))
802 end_of_title = s - buf;
805 * Get the start of the trailers by looking starting from the end for a
806 * blank line before a set of non-blank lines that (i) are all
807 * trailers, or (ii) contains at least one Git-generated trailer and
808 * consists of at least 25% trailers.
810 for (l = last_line(buf, len);
812 l = last_line(buf, l)) {
813 const char *bol = buf + l;
817 if (bol[0] == comment_line_char) {
818 non_trailer_lines += possible_continuation_lines;
819 possible_continuation_lines = 0;
822 if (is_blank_line(bol)) {
825 non_trailer_lines += possible_continuation_lines;
826 if (recognized_prefix &&
827 trailer_lines * 3 >= non_trailer_lines)
828 return next_line(bol) - buf;
829 else if (trailer_lines && !non_trailer_lines)
830 return next_line(bol) - buf;
835 for (p = git_generated_prefixes; *p; p++) {
836 if (starts_with(bol, *p)) {
838 possible_continuation_lines = 0;
839 recognized_prefix = 1;
840 goto continue_outer_loop;
844 separator_pos = find_separator(bol, separators);
845 if (separator_pos >= 1 && !isspace(bol[0])) {
846 struct list_head *pos;
849 possible_continuation_lines = 0;
850 if (recognized_prefix)
852 list_for_each(pos, &conf_head) {
853 struct arg_item *item;
854 item = list_entry(pos, struct arg_item, list);
855 if (token_matches_item(bol, item,
857 recognized_prefix = 1;
861 } else if (isspace(bol[0]))
862 possible_continuation_lines++;
865 non_trailer_lines += possible_continuation_lines;
866 possible_continuation_lines = 0;
875 /* Return the position of the end of the trailers. */
876 static int find_trailer_end(const char *buf, size_t len)
878 return len - ignore_non_trailer(buf, len);
881 static int ends_with_blank_line(const char *buf, size_t len)
883 int ll = last_line(buf, len);
886 return is_blank_line(buf + ll);
889 static int process_input_file(FILE *outfile,
891 struct list_head *head)
893 struct trailer_info info;
894 struct strbuf tok = STRBUF_INIT;
895 struct strbuf val = STRBUF_INIT;
898 trailer_info_get(&info, str);
900 /* Print lines before the trailers as is */
901 fwrite(str, 1, info.trailer_start - str, outfile);
903 if (!info.blank_line_before_trailer)
904 fprintf(outfile, "\n");
906 for (i = 0; i < info.trailer_nr; i++) {
908 char *trailer = info.trailers[i];
909 if (trailer[0] == comment_line_char)
911 separator_pos = find_separator(trailer, separators);
912 if (separator_pos >= 1) {
913 parse_trailer(&tok, &val, NULL, trailer,
915 add_trailer_item(head,
916 strbuf_detach(&tok, NULL),
917 strbuf_detach(&val, NULL));
919 strbuf_addstr(&val, trailer);
920 strbuf_strip_suffix(&val, "\n");
921 add_trailer_item(head,
923 strbuf_detach(&val, NULL));
927 trailer_info_release(&info);
929 return info.trailer_end - str;
932 static void free_all(struct list_head *head)
934 struct list_head *pos, *p;
935 list_for_each_safe(pos, p, head) {
937 free_trailer_item(list_entry(pos, struct trailer_item, list));
941 static struct tempfile trailers_tempfile;
943 static FILE *create_in_place_tempfile(const char *file)
946 struct strbuf template = STRBUF_INIT;
951 die_errno(_("could not stat %s"), file);
952 if (!S_ISREG(st.st_mode))
953 die(_("file %s is not a regular file"), file);
954 if (!(st.st_mode & S_IWUSR))
955 die(_("file %s is not writable by user"), file);
957 /* Create temporary file in the same directory as the original */
958 tail = strrchr(file, '/');
960 strbuf_add(&template, file, tail - file + 1);
961 strbuf_addstr(&template, "git-interpret-trailers-XXXXXX");
963 xmks_tempfile_m(&trailers_tempfile, template.buf, st.st_mode);
964 strbuf_release(&template);
965 outfile = fdopen_tempfile(&trailers_tempfile, "w");
967 die_errno(_("could not open temporary file"));
972 void process_trailers(const char *file, int in_place, int trim_empty, struct string_list *trailers)
976 struct strbuf sb = STRBUF_INIT;
978 FILE *outfile = stdout;
982 read_input_file(&sb, file);
985 outfile = create_in_place_tempfile(file);
987 /* Print the lines before the trailers */
988 trailer_end = process_input_file(outfile, sb.buf, &head);
990 process_command_line_args(&arg_head, trailers);
992 process_trailers_lists(&head, &arg_head);
994 print_all(outfile, &head, trim_empty);
998 /* Print the lines after the trailers as is */
999 fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
1002 if (rename_tempfile(&trailers_tempfile, file))
1003 die_errno(_("could not rename temporary file to %s"), file);
1005 strbuf_release(&sb);
1008 void trailer_info_get(struct trailer_info *info, const char *str)
1010 int patch_start, trailer_end, trailer_start;
1011 struct strbuf **trailer_lines, **ptr;
1012 char **trailer_strings = NULL;
1013 size_t nr = 0, alloc = 0;
1016 ensure_configured();
1018 patch_start = find_patch_start(str);
1019 trailer_end = find_trailer_end(str, patch_start);
1020 trailer_start = find_trailer_start(str, trailer_end);
1022 trailer_lines = strbuf_split_buf(str + trailer_start,
1023 trailer_end - trailer_start,
1026 for (ptr = trailer_lines; *ptr; ptr++) {
1027 if (last && isspace((*ptr)->buf[0])) {
1028 struct strbuf sb = STRBUF_INIT;
1029 strbuf_attach(&sb, *last, strlen(*last), strlen(*last));
1030 strbuf_addbuf(&sb, *ptr);
1031 *last = strbuf_detach(&sb, NULL);
1034 ALLOC_GROW(trailer_strings, nr + 1, alloc);
1035 trailer_strings[nr] = strbuf_detach(*ptr, NULL);
1036 last = find_separator(trailer_strings[nr], separators) >= 1
1037 ? &trailer_strings[nr]
1041 strbuf_list_free(trailer_lines);
1043 info->blank_line_before_trailer = ends_with_blank_line(str,
1045 info->trailer_start = str + trailer_start;
1046 info->trailer_end = str + trailer_end;
1047 info->trailers = trailer_strings;
1048 info->trailer_nr = nr;
1051 void trailer_info_release(struct trailer_info *info)
1054 for (i = 0; i < info->trailer_nr; i++)
1055 free(info->trailers[i]);
1056 free(info->trailers);