Merge branch 'dw/shell-basename-dashdash-before-stripping-leading-dash-from-login'
[git/mingw/j6t.git] / trailer.c
blob623adeb02d0e06a83d7b17a9b605ff8c6475a0bd
1 #include "cache.h"
2 #include "string-list.h"
3 #include "run-command.h"
4 #include "string-list.h"
5 #include "commit.h"
6 #include "trailer.h"
7 /*
8 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
9 */
11 enum action_where { WHERE_END, WHERE_AFTER, WHERE_BEFORE, WHERE_START };
12 enum action_if_exists { EXISTS_ADD_IF_DIFFERENT_NEIGHBOR, EXISTS_ADD_IF_DIFFERENT,
13 EXISTS_ADD, EXISTS_REPLACE, EXISTS_DO_NOTHING };
14 enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };
16 struct conf_info {
17 char *name;
18 char *key;
19 char *command;
20 enum action_where where;
21 enum action_if_exists if_exists;
22 enum action_if_missing if_missing;
25 static struct conf_info default_conf_info;
27 struct trailer_item {
28 struct trailer_item *previous;
29 struct trailer_item *next;
30 const char *token;
31 const char *value;
32 struct conf_info conf;
35 static struct trailer_item *first_conf_item;
37 static char *separators = ":";
39 #define TRAILER_ARG_STRING "$ARG"
41 static int after_or_end(enum action_where where)
43 return (where == WHERE_AFTER) || (where == WHERE_END);
47 * Return the length of the string not including any final
48 * punctuation. E.g., the input "Signed-off-by:" would return
49 * 13, stripping the trailing punctuation but retaining
50 * internal punctuation.
52 static size_t token_len_without_separator(const char *token, size_t len)
54 while (len > 0 && !isalnum(token[len - 1]))
55 len--;
56 return len;
59 static int same_token(struct trailer_item *a, struct trailer_item *b)
61 size_t a_len = token_len_without_separator(a->token, strlen(a->token));
62 size_t b_len = token_len_without_separator(b->token, strlen(b->token));
63 size_t min_len = (a_len > b_len) ? b_len : a_len;
65 return !strncasecmp(a->token, b->token, min_len);
68 static int same_value(struct trailer_item *a, struct trailer_item *b)
70 return !strcasecmp(a->value, b->value);
73 static int same_trailer(struct trailer_item *a, struct trailer_item *b)
75 return same_token(a, b) && same_value(a, b);
78 static inline int contains_only_spaces(const char *str)
80 const char *s = str;
81 while (*s && isspace(*s))
82 s++;
83 return !*s;
86 static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b)
88 const char *ptr = strstr(sb->buf, a);
89 if (ptr)
90 strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));
93 static void free_trailer_item(struct trailer_item *item)
95 free(item->conf.name);
96 free(item->conf.key);
97 free(item->conf.command);
98 free((char *)item->token);
99 free((char *)item->value);
100 free(item);
103 static char last_non_space_char(const char *s)
105 int i;
106 for (i = strlen(s) - 1; i >= 0; i--)
107 if (!isspace(s[i]))
108 return s[i];
109 return '\0';
112 static void print_tok_val(const char *tok, const char *val)
114 char c = last_non_space_char(tok);
115 if (!c)
116 return;
117 if (strchr(separators, c))
118 printf("%s%s\n", tok, val);
119 else
120 printf("%s%c %s\n", tok, separators[0], val);
123 static void print_all(struct trailer_item *first, int trim_empty)
125 struct trailer_item *item;
126 for (item = first; item; item = item->next) {
127 if (!trim_empty || strlen(item->value) > 0)
128 print_tok_val(item->token, item->value);
132 static void update_last(struct trailer_item **last)
134 if (*last)
135 while ((*last)->next != NULL)
136 *last = (*last)->next;
139 static void update_first(struct trailer_item **first)
141 if (*first)
142 while ((*first)->previous != NULL)
143 *first = (*first)->previous;
146 static void add_arg_to_input_list(struct trailer_item *on_tok,
147 struct trailer_item *arg_tok,
148 struct trailer_item **first,
149 struct trailer_item **last)
151 if (after_or_end(arg_tok->conf.where)) {
152 arg_tok->next = on_tok->next;
153 on_tok->next = arg_tok;
154 arg_tok->previous = on_tok;
155 if (arg_tok->next)
156 arg_tok->next->previous = arg_tok;
157 update_last(last);
158 } else {
159 arg_tok->previous = on_tok->previous;
160 on_tok->previous = arg_tok;
161 arg_tok->next = on_tok;
162 if (arg_tok->previous)
163 arg_tok->previous->next = arg_tok;
164 update_first(first);
168 static int check_if_different(struct trailer_item *in_tok,
169 struct trailer_item *arg_tok,
170 int check_all)
172 enum action_where where = arg_tok->conf.where;
173 do {
174 if (!in_tok)
175 return 1;
176 if (same_trailer(in_tok, arg_tok))
177 return 0;
179 * if we want to add a trailer after another one,
180 * we have to check those before this one
182 in_tok = after_or_end(where) ? in_tok->previous : in_tok->next;
183 } while (check_all);
184 return 1;
187 static void remove_from_list(struct trailer_item *item,
188 struct trailer_item **first,
189 struct trailer_item **last)
191 struct trailer_item *next = item->next;
192 struct trailer_item *previous = item->previous;
194 if (next) {
195 item->next->previous = previous;
196 item->next = NULL;
197 } else if (last)
198 *last = previous;
200 if (previous) {
201 item->previous->next = next;
202 item->previous = NULL;
203 } else if (first)
204 *first = next;
207 static struct trailer_item *remove_first(struct trailer_item **first)
209 struct trailer_item *item = *first;
210 *first = item->next;
211 if (item->next) {
212 item->next->previous = NULL;
213 item->next = NULL;
215 return item;
218 static int read_from_command(struct child_process *cp, struct strbuf *buf)
220 if (run_command(cp))
221 return error("running trailer command '%s' failed", cp->argv[0]);
222 if (strbuf_read(buf, cp->out, 1024) < 1)
223 return error("reading from trailer command '%s' failed", cp->argv[0]);
224 strbuf_trim(buf);
225 return 0;
228 static const char *apply_command(const char *command, const char *arg)
230 struct strbuf cmd = STRBUF_INIT;
231 struct strbuf buf = STRBUF_INIT;
232 struct child_process cp = CHILD_PROCESS_INIT;
233 const char *argv[] = {NULL, NULL};
234 const char *result;
236 strbuf_addstr(&cmd, command);
237 if (arg)
238 strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
240 argv[0] = cmd.buf;
241 cp.argv = argv;
242 cp.env = local_repo_env;
243 cp.no_stdin = 1;
244 cp.out = -1;
245 cp.use_shell = 1;
247 if (read_from_command(&cp, &buf)) {
248 strbuf_release(&buf);
249 result = xstrdup("");
250 } else
251 result = strbuf_detach(&buf, NULL);
253 strbuf_release(&cmd);
254 return result;
257 static void apply_item_command(struct trailer_item *in_tok, struct trailer_item *arg_tok)
259 if (arg_tok->conf.command) {
260 const char *arg;
261 if (arg_tok->value && arg_tok->value[0]) {
262 arg = arg_tok->value;
263 } else {
264 if (in_tok && in_tok->value)
265 arg = xstrdup(in_tok->value);
266 else
267 arg = xstrdup("");
269 arg_tok->value = apply_command(arg_tok->conf.command, arg);
270 free((char *)arg);
274 static void apply_arg_if_exists(struct trailer_item *in_tok,
275 struct trailer_item *arg_tok,
276 struct trailer_item *on_tok,
277 struct trailer_item **in_tok_first,
278 struct trailer_item **in_tok_last)
280 switch (arg_tok->conf.if_exists) {
281 case EXISTS_DO_NOTHING:
282 free_trailer_item(arg_tok);
283 break;
284 case EXISTS_REPLACE:
285 apply_item_command(in_tok, arg_tok);
286 add_arg_to_input_list(on_tok, arg_tok,
287 in_tok_first, in_tok_last);
288 remove_from_list(in_tok, in_tok_first, in_tok_last);
289 free_trailer_item(in_tok);
290 break;
291 case EXISTS_ADD:
292 apply_item_command(in_tok, arg_tok);
293 add_arg_to_input_list(on_tok, arg_tok,
294 in_tok_first, in_tok_last);
295 break;
296 case EXISTS_ADD_IF_DIFFERENT:
297 apply_item_command(in_tok, arg_tok);
298 if (check_if_different(in_tok, arg_tok, 1))
299 add_arg_to_input_list(on_tok, arg_tok,
300 in_tok_first, in_tok_last);
301 else
302 free_trailer_item(arg_tok);
303 break;
304 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR:
305 apply_item_command(in_tok, arg_tok);
306 if (check_if_different(on_tok, arg_tok, 0))
307 add_arg_to_input_list(on_tok, arg_tok,
308 in_tok_first, in_tok_last);
309 else
310 free_trailer_item(arg_tok);
311 break;
315 static void apply_arg_if_missing(struct trailer_item **in_tok_first,
316 struct trailer_item **in_tok_last,
317 struct trailer_item *arg_tok)
319 struct trailer_item **in_tok;
320 enum action_where where;
322 switch (arg_tok->conf.if_missing) {
323 case MISSING_DO_NOTHING:
324 free_trailer_item(arg_tok);
325 break;
326 case MISSING_ADD:
327 where = arg_tok->conf.where;
328 in_tok = after_or_end(where) ? in_tok_last : in_tok_first;
329 apply_item_command(NULL, arg_tok);
330 if (*in_tok) {
331 add_arg_to_input_list(*in_tok, arg_tok,
332 in_tok_first, in_tok_last);
333 } else {
334 *in_tok_first = arg_tok;
335 *in_tok_last = arg_tok;
337 break;
341 static int find_same_and_apply_arg(struct trailer_item **in_tok_first,
342 struct trailer_item **in_tok_last,
343 struct trailer_item *arg_tok)
345 struct trailer_item *in_tok;
346 struct trailer_item *on_tok;
347 struct trailer_item *following_tok;
349 enum action_where where = arg_tok->conf.where;
350 int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
351 int backwards = after_or_end(where);
352 struct trailer_item *start_tok = backwards ? *in_tok_last : *in_tok_first;
354 for (in_tok = start_tok; in_tok; in_tok = following_tok) {
355 following_tok = backwards ? in_tok->previous : in_tok->next;
356 if (!same_token(in_tok, arg_tok))
357 continue;
358 on_tok = middle ? in_tok : start_tok;
359 apply_arg_if_exists(in_tok, arg_tok, on_tok,
360 in_tok_first, in_tok_last);
361 return 1;
363 return 0;
366 static void process_trailers_lists(struct trailer_item **in_tok_first,
367 struct trailer_item **in_tok_last,
368 struct trailer_item **arg_tok_first)
370 struct trailer_item *arg_tok;
371 struct trailer_item *next_arg;
373 if (!*arg_tok_first)
374 return;
376 for (arg_tok = *arg_tok_first; arg_tok; arg_tok = next_arg) {
377 int applied = 0;
379 next_arg = arg_tok->next;
380 remove_from_list(arg_tok, arg_tok_first, NULL);
382 applied = find_same_and_apply_arg(in_tok_first,
383 in_tok_last,
384 arg_tok);
386 if (!applied)
387 apply_arg_if_missing(in_tok_first,
388 in_tok_last,
389 arg_tok);
393 static int set_where(struct conf_info *item, const char *value)
395 if (!strcasecmp("after", value))
396 item->where = WHERE_AFTER;
397 else if (!strcasecmp("before", value))
398 item->where = WHERE_BEFORE;
399 else if (!strcasecmp("end", value))
400 item->where = WHERE_END;
401 else if (!strcasecmp("start", value))
402 item->where = WHERE_START;
403 else
404 return -1;
405 return 0;
408 static int set_if_exists(struct conf_info *item, const char *value)
410 if (!strcasecmp("addIfDifferent", value))
411 item->if_exists = EXISTS_ADD_IF_DIFFERENT;
412 else if (!strcasecmp("addIfDifferentNeighbor", value))
413 item->if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
414 else if (!strcasecmp("add", value))
415 item->if_exists = EXISTS_ADD;
416 else if (!strcasecmp("replace", value))
417 item->if_exists = EXISTS_REPLACE;
418 else if (!strcasecmp("doNothing", value))
419 item->if_exists = EXISTS_DO_NOTHING;
420 else
421 return -1;
422 return 0;
425 static int set_if_missing(struct conf_info *item, const char *value)
427 if (!strcasecmp("doNothing", value))
428 item->if_missing = MISSING_DO_NOTHING;
429 else if (!strcasecmp("add", value))
430 item->if_missing = MISSING_ADD;
431 else
432 return -1;
433 return 0;
436 static void duplicate_conf(struct conf_info *dst, struct conf_info *src)
438 *dst = *src;
439 if (src->name)
440 dst->name = xstrdup(src->name);
441 if (src->key)
442 dst->key = xstrdup(src->key);
443 if (src->command)
444 dst->command = xstrdup(src->command);
447 static struct trailer_item *get_conf_item(const char *name)
449 struct trailer_item *item;
450 struct trailer_item *previous;
452 /* Look up item with same name */
453 for (previous = NULL, item = first_conf_item;
454 item;
455 previous = item, item = item->next) {
456 if (!strcasecmp(item->conf.name, name))
457 return item;
460 /* Item does not already exists, create it */
461 item = xcalloc(sizeof(struct trailer_item), 1);
462 duplicate_conf(&item->conf, &default_conf_info);
463 item->conf.name = xstrdup(name);
465 if (!previous)
466 first_conf_item = item;
467 else {
468 previous->next = item;
469 item->previous = previous;
472 return item;
475 enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_WHERE,
476 TRAILER_IF_EXISTS, TRAILER_IF_MISSING };
478 static struct {
479 const char *name;
480 enum trailer_info_type type;
481 } trailer_config_items[] = {
482 { "key", TRAILER_KEY },
483 { "command", TRAILER_COMMAND },
484 { "where", TRAILER_WHERE },
485 { "ifexists", TRAILER_IF_EXISTS },
486 { "ifmissing", TRAILER_IF_MISSING }
489 static int git_trailer_default_config(const char *conf_key, const char *value, void *cb)
491 const char *trailer_item, *variable_name;
493 if (!skip_prefix(conf_key, "trailer.", &trailer_item))
494 return 0;
496 variable_name = strrchr(trailer_item, '.');
497 if (!variable_name) {
498 if (!strcmp(trailer_item, "where")) {
499 if (set_where(&default_conf_info, value) < 0)
500 warning(_("unknown value '%s' for key '%s'"),
501 value, conf_key);
502 } else if (!strcmp(trailer_item, "ifexists")) {
503 if (set_if_exists(&default_conf_info, value) < 0)
504 warning(_("unknown value '%s' for key '%s'"),
505 value, conf_key);
506 } else if (!strcmp(trailer_item, "ifmissing")) {
507 if (set_if_missing(&default_conf_info, value) < 0)
508 warning(_("unknown value '%s' for key '%s'"),
509 value, conf_key);
510 } else if (!strcmp(trailer_item, "separators")) {
511 separators = xstrdup(value);
514 return 0;
517 static int git_trailer_config(const char *conf_key, const char *value, void *cb)
519 const char *trailer_item, *variable_name;
520 struct trailer_item *item;
521 struct conf_info *conf;
522 char *name = NULL;
523 enum trailer_info_type type;
524 int i;
526 if (!skip_prefix(conf_key, "trailer.", &trailer_item))
527 return 0;
529 variable_name = strrchr(trailer_item, '.');
530 if (!variable_name)
531 return 0;
533 variable_name++;
534 for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) {
535 if (strcmp(trailer_config_items[i].name, variable_name))
536 continue;
537 name = xstrndup(trailer_item, variable_name - trailer_item - 1);
538 type = trailer_config_items[i].type;
539 break;
542 if (!name)
543 return 0;
545 item = get_conf_item(name);
546 conf = &item->conf;
547 free(name);
549 switch (type) {
550 case TRAILER_KEY:
551 if (conf->key)
552 warning(_("more than one %s"), conf_key);
553 conf->key = xstrdup(value);
554 break;
555 case TRAILER_COMMAND:
556 if (conf->command)
557 warning(_("more than one %s"), conf_key);
558 conf->command = xstrdup(value);
559 break;
560 case TRAILER_WHERE:
561 if (set_where(conf, value))
562 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
563 break;
564 case TRAILER_IF_EXISTS:
565 if (set_if_exists(conf, value))
566 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
567 break;
568 case TRAILER_IF_MISSING:
569 if (set_if_missing(conf, value))
570 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
571 break;
572 default:
573 die("internal bug in trailer.c");
575 return 0;
578 static int parse_trailer(struct strbuf *tok, struct strbuf *val, const char *trailer)
580 size_t len;
581 struct strbuf seps = STRBUF_INIT;
582 strbuf_addstr(&seps, separators);
583 strbuf_addch(&seps, '=');
584 len = strcspn(trailer, seps.buf);
585 strbuf_release(&seps);
586 if (len == 0) {
587 int l = strlen(trailer);
588 while (l > 0 && isspace(trailer[l - 1]))
589 l--;
590 return error(_("empty trailer token in trailer '%.*s'"), l, trailer);
592 if (len < strlen(trailer)) {
593 strbuf_add(tok, trailer, len);
594 strbuf_trim(tok);
595 strbuf_addstr(val, trailer + len + 1);
596 strbuf_trim(val);
597 } else {
598 strbuf_addstr(tok, trailer);
599 strbuf_trim(tok);
601 return 0;
604 static const char *token_from_item(struct trailer_item *item, char *tok)
606 if (item->conf.key)
607 return item->conf.key;
608 if (tok)
609 return tok;
610 return item->conf.name;
613 static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,
614 char *tok, char *val)
616 struct trailer_item *new = xcalloc(sizeof(*new), 1);
617 new->value = val ? val : xstrdup("");
619 if (conf_item) {
620 duplicate_conf(&new->conf, &conf_item->conf);
621 new->token = xstrdup(token_from_item(conf_item, tok));
622 free(tok);
623 } else {
624 duplicate_conf(&new->conf, &default_conf_info);
625 new->token = tok;
628 return new;
631 static int token_matches_item(const char *tok, struct trailer_item *item, int tok_len)
633 if (!strncasecmp(tok, item->conf.name, tok_len))
634 return 1;
635 return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0;
638 static struct trailer_item *create_trailer_item(const char *string)
640 struct strbuf tok = STRBUF_INIT;
641 struct strbuf val = STRBUF_INIT;
642 struct trailer_item *item;
643 int tok_len;
645 if (parse_trailer(&tok, &val, string))
646 return NULL;
648 tok_len = token_len_without_separator(tok.buf, tok.len);
650 /* Lookup if the token matches something in the config */
651 for (item = first_conf_item; item; item = item->next) {
652 if (token_matches_item(tok.buf, item, tok_len))
653 return new_trailer_item(item,
654 strbuf_detach(&tok, NULL),
655 strbuf_detach(&val, NULL));
658 return new_trailer_item(NULL,
659 strbuf_detach(&tok, NULL),
660 strbuf_detach(&val, NULL));
663 static void add_trailer_item(struct trailer_item **first,
664 struct trailer_item **last,
665 struct trailer_item *new)
667 if (!new)
668 return;
669 if (!*last) {
670 *first = new;
671 *last = new;
672 } else {
673 (*last)->next = new;
674 new->previous = *last;
675 *last = new;
679 static struct trailer_item *process_command_line_args(struct string_list *trailers)
681 struct trailer_item *arg_tok_first = NULL;
682 struct trailer_item *arg_tok_last = NULL;
683 struct string_list_item *tr;
684 struct trailer_item *item;
686 /* Add a trailer item for each configured trailer with a command */
687 for (item = first_conf_item; item; item = item->next) {
688 if (item->conf.command) {
689 struct trailer_item *new = new_trailer_item(item, NULL, NULL);
690 add_trailer_item(&arg_tok_first, &arg_tok_last, new);
694 /* Add a trailer item for each trailer on the command line */
695 for_each_string_list_item(tr, trailers) {
696 struct trailer_item *new = create_trailer_item(tr->string);
697 add_trailer_item(&arg_tok_first, &arg_tok_last, new);
700 return arg_tok_first;
703 static struct strbuf **read_input_file(const char *file)
705 struct strbuf **lines;
706 struct strbuf sb = STRBUF_INIT;
708 if (file) {
709 if (strbuf_read_file(&sb, file, 0) < 0)
710 die_errno(_("could not read input file '%s'"), file);
711 } else {
712 if (strbuf_read(&sb, fileno(stdin), 0) < 0)
713 die_errno(_("could not read from stdin"));
716 lines = strbuf_split(&sb, '\n');
718 strbuf_release(&sb);
720 return lines;
724 * Return the (0 based) index of the start of the patch or the line
725 * count if there is no patch in the message.
727 static int find_patch_start(struct strbuf **lines, int count)
729 int i;
731 /* Get the start of the patch part if any */
732 for (i = 0; i < count; i++) {
733 if (starts_with(lines[i]->buf, "---"))
734 return i;
737 return count;
741 * Return the (0 based) index of the first trailer line or count if
742 * there are no trailers. Trailers are searched only in the lines from
743 * index (count - 1) down to index 0.
745 static int find_trailer_start(struct strbuf **lines, int count)
747 int start, only_spaces = 1;
750 * Get the start of the trailers by looking starting from the end
751 * for a line with only spaces before lines with one separator.
753 for (start = count - 1; start >= 0; start--) {
754 if (lines[start]->buf[0] == comment_line_char)
755 continue;
756 if (contains_only_spaces(lines[start]->buf)) {
757 if (only_spaces)
758 continue;
759 return start + 1;
761 if (strcspn(lines[start]->buf, separators) < lines[start]->len) {
762 if (only_spaces)
763 only_spaces = 0;
764 continue;
766 return count;
769 return only_spaces ? count : 0;
772 /* Get the index of the end of the trailers */
773 static int find_trailer_end(struct strbuf **lines, int patch_start)
775 struct strbuf sb = STRBUF_INIT;
776 int i, ignore_bytes;
778 for (i = 0; i < patch_start; i++)
779 strbuf_addbuf(&sb, lines[i]);
780 ignore_bytes = ignore_non_trailer(&sb);
781 strbuf_release(&sb);
782 for (i = patch_start - 1; i >= 0 && ignore_bytes > 0; i--)
783 ignore_bytes -= lines[i]->len;
785 return i + 1;
788 static int has_blank_line_before(struct strbuf **lines, int start)
790 for (;start >= 0; start--) {
791 if (lines[start]->buf[0] == comment_line_char)
792 continue;
793 return contains_only_spaces(lines[start]->buf);
795 return 0;
798 static void print_lines(struct strbuf **lines, int start, int end)
800 int i;
801 for (i = start; lines[i] && i < end; i++)
802 printf("%s", lines[i]->buf);
805 static int process_input_file(struct strbuf **lines,
806 struct trailer_item **in_tok_first,
807 struct trailer_item **in_tok_last)
809 int count = 0;
810 int patch_start, trailer_start, trailer_end, i;
812 /* Get the line count */
813 while (lines[count])
814 count++;
816 patch_start = find_patch_start(lines, count);
817 trailer_end = find_trailer_end(lines, patch_start);
818 trailer_start = find_trailer_start(lines, trailer_end);
820 /* Print lines before the trailers as is */
821 print_lines(lines, 0, trailer_start);
823 if (!has_blank_line_before(lines, trailer_start - 1))
824 printf("\n");
826 /* Parse trailer lines */
827 for (i = trailer_start; i < trailer_end; i++) {
828 if (lines[i]->buf[0] != comment_line_char) {
829 struct trailer_item *new = create_trailer_item(lines[i]->buf);
830 add_trailer_item(in_tok_first, in_tok_last, new);
834 return trailer_end;
837 static void free_all(struct trailer_item **first)
839 while (*first) {
840 struct trailer_item *item = remove_first(first);
841 free_trailer_item(item);
845 void process_trailers(const char *file, int trim_empty, struct string_list *trailers)
847 struct trailer_item *in_tok_first = NULL;
848 struct trailer_item *in_tok_last = NULL;
849 struct trailer_item *arg_tok_first;
850 struct strbuf **lines;
851 int trailer_end;
853 /* Default config must be setup first */
854 git_config(git_trailer_default_config, NULL);
855 git_config(git_trailer_config, NULL);
857 lines = read_input_file(file);
859 /* Print the lines before the trailers */
860 trailer_end = process_input_file(lines, &in_tok_first, &in_tok_last);
862 arg_tok_first = process_command_line_args(trailers);
864 process_trailers_lists(&in_tok_first, &in_tok_last, &arg_tok_first);
866 print_all(in_tok_first, trim_empty);
868 free_all(&in_tok_first);
870 /* Print the lines after the trailers as is */
871 print_lines(lines, trailer_end, INT_MAX);
873 strbuf_list_free(lines);