strbuf: strbuf_read_file() should return ssize_t
[git] / trailer.c
1 #include "cache.h"
2 #include "string-list.h"
3 #include "run-command.h"
4 #include "commit.h"
5 #include "trailer.h"
6 /*
7  * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
8  */
9
10 enum action_where { WHERE_END, WHERE_AFTER, WHERE_BEFORE, WHERE_START };
11 enum action_if_exists { EXISTS_ADD_IF_DIFFERENT_NEIGHBOR, EXISTS_ADD_IF_DIFFERENT,
12                         EXISTS_ADD, EXISTS_REPLACE, EXISTS_DO_NOTHING };
13 enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };
14
15 struct conf_info {
16         char *name;
17         char *key;
18         char *command;
19         enum action_where where;
20         enum action_if_exists if_exists;
21         enum action_if_missing if_missing;
22 };
23
24 static struct conf_info default_conf_info;
25
26 struct trailer_item {
27         struct trailer_item *previous;
28         struct trailer_item *next;
29         const char *token;
30         const char *value;
31         struct conf_info conf;
32 };
33
34 static struct trailer_item *first_conf_item;
35
36 static char *separators = ":";
37
38 #define TRAILER_ARG_STRING "$ARG"
39
40 static int after_or_end(enum action_where where)
41 {
42         return (where == WHERE_AFTER) || (where == WHERE_END);
43 }
44
45 /*
46  * Return the length of the string not including any final
47  * punctuation. E.g., the input "Signed-off-by:" would return
48  * 13, stripping the trailing punctuation but retaining
49  * internal punctuation.
50  */
51 static size_t token_len_without_separator(const char *token, size_t len)
52 {
53         while (len > 0 && !isalnum(token[len - 1]))
54                 len--;
55         return len;
56 }
57
58 static int same_token(struct trailer_item *a, struct trailer_item *b)
59 {
60         size_t a_len = token_len_without_separator(a->token, strlen(a->token));
61         size_t b_len = token_len_without_separator(b->token, strlen(b->token));
62         size_t min_len = (a_len > b_len) ? b_len : a_len;
63
64         return !strncasecmp(a->token, b->token, min_len);
65 }
66
67 static int same_value(struct trailer_item *a, struct trailer_item *b)
68 {
69         return !strcasecmp(a->value, b->value);
70 }
71
72 static int same_trailer(struct trailer_item *a, struct trailer_item *b)
73 {
74         return same_token(a, b) && same_value(a, b);
75 }
76
77 static inline int contains_only_spaces(const char *str)
78 {
79         const char *s = str;
80         while (*s && isspace(*s))
81                 s++;
82         return !*s;
83 }
84
85 static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b)
86 {
87         const char *ptr = strstr(sb->buf, a);
88         if (ptr)
89                 strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));
90 }
91
92 static void free_trailer_item(struct trailer_item *item)
93 {
94         free(item->conf.name);
95         free(item->conf.key);
96         free(item->conf.command);
97         free((char *)item->token);
98         free((char *)item->value);
99         free(item);
100 }
101
102 static char last_non_space_char(const char *s)
103 {
104         int i;
105         for (i = strlen(s) - 1; i >= 0; i--)
106                 if (!isspace(s[i]))
107                         return s[i];
108         return '\0';
109 }
110
111 static void print_tok_val(const char *tok, const char *val)
112 {
113         char c = last_non_space_char(tok);
114         if (!c)
115                 return;
116         if (strchr(separators, c))
117                 printf("%s%s\n", tok, val);
118         else
119                 printf("%s%c %s\n", tok, separators[0], val);
120 }
121
122 static void print_all(struct trailer_item *first, int trim_empty)
123 {
124         struct trailer_item *item;
125         for (item = first; item; item = item->next) {
126                 if (!trim_empty || strlen(item->value) > 0)
127                         print_tok_val(item->token, item->value);
128         }
129 }
130
131 static void update_last(struct trailer_item **last)
132 {
133         if (*last)
134                 while ((*last)->next != NULL)
135                         *last = (*last)->next;
136 }
137
138 static void update_first(struct trailer_item **first)
139 {
140         if (*first)
141                 while ((*first)->previous != NULL)
142                         *first = (*first)->previous;
143 }
144
145 static void add_arg_to_input_list(struct trailer_item *on_tok,
146                                   struct trailer_item *arg_tok,
147                                   struct trailer_item **first,
148                                   struct trailer_item **last)
149 {
150         if (after_or_end(arg_tok->conf.where)) {
151                 arg_tok->next = on_tok->next;
152                 on_tok->next = arg_tok;
153                 arg_tok->previous = on_tok;
154                 if (arg_tok->next)
155                         arg_tok->next->previous = arg_tok;
156                 update_last(last);
157         } else {
158                 arg_tok->previous = on_tok->previous;
159                 on_tok->previous = arg_tok;
160                 arg_tok->next = on_tok;
161                 if (arg_tok->previous)
162                         arg_tok->previous->next = arg_tok;
163                 update_first(first);
164         }
165 }
166
167 static int check_if_different(struct trailer_item *in_tok,
168                               struct trailer_item *arg_tok,
169                               int check_all)
170 {
171         enum action_where where = arg_tok->conf.where;
172         do {
173                 if (!in_tok)
174                         return 1;
175                 if (same_trailer(in_tok, arg_tok))
176                         return 0;
177                 /*
178                  * if we want to add a trailer after another one,
179                  * we have to check those before this one
180                  */
181                 in_tok = after_or_end(where) ? in_tok->previous : in_tok->next;
182         } while (check_all);
183         return 1;
184 }
185
186 static void remove_from_list(struct trailer_item *item,
187                              struct trailer_item **first,
188                              struct trailer_item **last)
189 {
190         struct trailer_item *next = item->next;
191         struct trailer_item *previous = item->previous;
192
193         if (next) {
194                 item->next->previous = previous;
195                 item->next = NULL;
196         } else if (last)
197                 *last = previous;
198
199         if (previous) {
200                 item->previous->next = next;
201                 item->previous = NULL;
202         } else if (first)
203                 *first = next;
204 }
205
206 static struct trailer_item *remove_first(struct trailer_item **first)
207 {
208         struct trailer_item *item = *first;
209         *first = item->next;
210         if (item->next) {
211                 item->next->previous = NULL;
212                 item->next = NULL;
213         }
214         return item;
215 }
216
217 static const char *apply_command(const char *command, const char *arg)
218 {
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};
223         const char *result;
224
225         strbuf_addstr(&cmd, command);
226         if (arg)
227                 strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
228
229         argv[0] = cmd.buf;
230         cp.argv = argv;
231         cp.env = local_repo_env;
232         cp.no_stdin = 1;
233         cp.use_shell = 1;
234
235         if (capture_command(&cp, &buf, 1024)) {
236                 error("running trailer command '%s' failed", cmd.buf);
237                 strbuf_release(&buf);
238                 result = xstrdup("");
239         } else {
240                 strbuf_trim(&buf);
241                 result = strbuf_detach(&buf, NULL);
242         }
243
244         strbuf_release(&cmd);
245         return result;
246 }
247
248 static void apply_item_command(struct trailer_item *in_tok, struct trailer_item *arg_tok)
249 {
250         if (arg_tok->conf.command) {
251                 const char *arg;
252                 if (arg_tok->value && arg_tok->value[0]) {
253                         arg = arg_tok->value;
254                 } else {
255                         if (in_tok && in_tok->value)
256                                 arg = xstrdup(in_tok->value);
257                         else
258                                 arg = xstrdup("");
259                 }
260                 arg_tok->value = apply_command(arg_tok->conf.command, arg);
261                 free((char *)arg);
262         }
263 }
264
265 static void apply_arg_if_exists(struct trailer_item *in_tok,
266                                 struct trailer_item *arg_tok,
267                                 struct trailer_item *on_tok,
268                                 struct trailer_item **in_tok_first,
269                                 struct trailer_item **in_tok_last)
270 {
271         switch (arg_tok->conf.if_exists) {
272         case EXISTS_DO_NOTHING:
273                 free_trailer_item(arg_tok);
274                 break;
275         case EXISTS_REPLACE:
276                 apply_item_command(in_tok, arg_tok);
277                 add_arg_to_input_list(on_tok, arg_tok,
278                                       in_tok_first, in_tok_last);
279                 remove_from_list(in_tok, in_tok_first, in_tok_last);
280                 free_trailer_item(in_tok);
281                 break;
282         case EXISTS_ADD:
283                 apply_item_command(in_tok, arg_tok);
284                 add_arg_to_input_list(on_tok, arg_tok,
285                                       in_tok_first, in_tok_last);
286                 break;
287         case EXISTS_ADD_IF_DIFFERENT:
288                 apply_item_command(in_tok, arg_tok);
289                 if (check_if_different(in_tok, arg_tok, 1))
290                         add_arg_to_input_list(on_tok, arg_tok,
291                                               in_tok_first, in_tok_last);
292                 else
293                         free_trailer_item(arg_tok);
294                 break;
295         case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR:
296                 apply_item_command(in_tok, arg_tok);
297                 if (check_if_different(on_tok, arg_tok, 0))
298                         add_arg_to_input_list(on_tok, arg_tok,
299                                               in_tok_first, in_tok_last);
300                 else
301                         free_trailer_item(arg_tok);
302                 break;
303         }
304 }
305
306 static void apply_arg_if_missing(struct trailer_item **in_tok_first,
307                                  struct trailer_item **in_tok_last,
308                                  struct trailer_item *arg_tok)
309 {
310         struct trailer_item **in_tok;
311         enum action_where where;
312
313         switch (arg_tok->conf.if_missing) {
314         case MISSING_DO_NOTHING:
315                 free_trailer_item(arg_tok);
316                 break;
317         case MISSING_ADD:
318                 where = arg_tok->conf.where;
319                 in_tok = after_or_end(where) ? in_tok_last : in_tok_first;
320                 apply_item_command(NULL, arg_tok);
321                 if (*in_tok) {
322                         add_arg_to_input_list(*in_tok, arg_tok,
323                                               in_tok_first, in_tok_last);
324                 } else {
325                         *in_tok_first = arg_tok;
326                         *in_tok_last = arg_tok;
327                 }
328                 break;
329         }
330 }
331
332 static int find_same_and_apply_arg(struct trailer_item **in_tok_first,
333                                    struct trailer_item **in_tok_last,
334                                    struct trailer_item *arg_tok)
335 {
336         struct trailer_item *in_tok;
337         struct trailer_item *on_tok;
338         struct trailer_item *following_tok;
339
340         enum action_where where = arg_tok->conf.where;
341         int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
342         int backwards = after_or_end(where);
343         struct trailer_item *start_tok = backwards ? *in_tok_last : *in_tok_first;
344
345         for (in_tok = start_tok; in_tok; in_tok = following_tok) {
346                 following_tok = backwards ? in_tok->previous : in_tok->next;
347                 if (!same_token(in_tok, arg_tok))
348                         continue;
349                 on_tok = middle ? in_tok : start_tok;
350                 apply_arg_if_exists(in_tok, arg_tok, on_tok,
351                                     in_tok_first, in_tok_last);
352                 return 1;
353         }
354         return 0;
355 }
356
357 static void process_trailers_lists(struct trailer_item **in_tok_first,
358                                    struct trailer_item **in_tok_last,
359                                    struct trailer_item **arg_tok_first)
360 {
361         struct trailer_item *arg_tok;
362         struct trailer_item *next_arg;
363
364         if (!*arg_tok_first)
365                 return;
366
367         for (arg_tok = *arg_tok_first; arg_tok; arg_tok = next_arg) {
368                 int applied = 0;
369
370                 next_arg = arg_tok->next;
371                 remove_from_list(arg_tok, arg_tok_first, NULL);
372
373                 applied = find_same_and_apply_arg(in_tok_first,
374                                                   in_tok_last,
375                                                   arg_tok);
376
377                 if (!applied)
378                         apply_arg_if_missing(in_tok_first,
379                                              in_tok_last,
380                                              arg_tok);
381         }
382 }
383
384 static int set_where(struct conf_info *item, const char *value)
385 {
386         if (!strcasecmp("after", value))
387                 item->where = WHERE_AFTER;
388         else if (!strcasecmp("before", value))
389                 item->where = WHERE_BEFORE;
390         else if (!strcasecmp("end", value))
391                 item->where = WHERE_END;
392         else if (!strcasecmp("start", value))
393                 item->where = WHERE_START;
394         else
395                 return -1;
396         return 0;
397 }
398
399 static int set_if_exists(struct conf_info *item, const char *value)
400 {
401         if (!strcasecmp("addIfDifferent", value))
402                 item->if_exists = EXISTS_ADD_IF_DIFFERENT;
403         else if (!strcasecmp("addIfDifferentNeighbor", value))
404                 item->if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
405         else if (!strcasecmp("add", value))
406                 item->if_exists = EXISTS_ADD;
407         else if (!strcasecmp("replace", value))
408                 item->if_exists = EXISTS_REPLACE;
409         else if (!strcasecmp("doNothing", value))
410                 item->if_exists = EXISTS_DO_NOTHING;
411         else
412                 return -1;
413         return 0;
414 }
415
416 static int set_if_missing(struct conf_info *item, const char *value)
417 {
418         if (!strcasecmp("doNothing", value))
419                 item->if_missing = MISSING_DO_NOTHING;
420         else if (!strcasecmp("add", value))
421                 item->if_missing = MISSING_ADD;
422         else
423                 return -1;
424         return 0;
425 }
426
427 static void duplicate_conf(struct conf_info *dst, struct conf_info *src)
428 {
429         *dst = *src;
430         if (src->name)
431                 dst->name = xstrdup(src->name);
432         if (src->key)
433                 dst->key = xstrdup(src->key);
434         if (src->command)
435                 dst->command = xstrdup(src->command);
436 }
437
438 static struct trailer_item *get_conf_item(const char *name)
439 {
440         struct trailer_item *item;
441         struct trailer_item *previous;
442
443         /* Look up item with same name */
444         for (previous = NULL, item = first_conf_item;
445              item;
446              previous = item, item = item->next) {
447                 if (!strcasecmp(item->conf.name, name))
448                         return item;
449         }
450
451         /* Item does not already exists, create it */
452         item = xcalloc(sizeof(struct trailer_item), 1);
453         duplicate_conf(&item->conf, &default_conf_info);
454         item->conf.name = xstrdup(name);
455
456         if (!previous)
457                 first_conf_item = item;
458         else {
459                 previous->next = item;
460                 item->previous = previous;
461         }
462
463         return item;
464 }
465
466 enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_WHERE,
467                          TRAILER_IF_EXISTS, TRAILER_IF_MISSING };
468
469 static struct {
470         const char *name;
471         enum trailer_info_type type;
472 } trailer_config_items[] = {
473         { "key", TRAILER_KEY },
474         { "command", TRAILER_COMMAND },
475         { "where", TRAILER_WHERE },
476         { "ifexists", TRAILER_IF_EXISTS },
477         { "ifmissing", TRAILER_IF_MISSING }
478 };
479
480 static int git_trailer_default_config(const char *conf_key, const char *value, void *cb)
481 {
482         const char *trailer_item, *variable_name;
483
484         if (!skip_prefix(conf_key, "trailer.", &trailer_item))
485                 return 0;
486
487         variable_name = strrchr(trailer_item, '.');
488         if (!variable_name) {
489                 if (!strcmp(trailer_item, "where")) {
490                         if (set_where(&default_conf_info, value) < 0)
491                                 warning(_("unknown value '%s' for key '%s'"),
492                                         value, conf_key);
493                 } else if (!strcmp(trailer_item, "ifexists")) {
494                         if (set_if_exists(&default_conf_info, value) < 0)
495                                 warning(_("unknown value '%s' for key '%s'"),
496                                         value, conf_key);
497                 } else if (!strcmp(trailer_item, "ifmissing")) {
498                         if (set_if_missing(&default_conf_info, value) < 0)
499                                 warning(_("unknown value '%s' for key '%s'"),
500                                         value, conf_key);
501                 } else if (!strcmp(trailer_item, "separators")) {
502                         separators = xstrdup(value);
503                 }
504         }
505         return 0;
506 }
507
508 static int git_trailer_config(const char *conf_key, const char *value, void *cb)
509 {
510         const char *trailer_item, *variable_name;
511         struct trailer_item *item;
512         struct conf_info *conf;
513         char *name = NULL;
514         enum trailer_info_type type;
515         int i;
516
517         if (!skip_prefix(conf_key, "trailer.", &trailer_item))
518                 return 0;
519
520         variable_name = strrchr(trailer_item, '.');
521         if (!variable_name)
522                 return 0;
523
524         variable_name++;
525         for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) {
526                 if (strcmp(trailer_config_items[i].name, variable_name))
527                         continue;
528                 name = xstrndup(trailer_item,  variable_name - trailer_item - 1);
529                 type = trailer_config_items[i].type;
530                 break;
531         }
532
533         if (!name)
534                 return 0;
535
536         item = get_conf_item(name);
537         conf = &item->conf;
538         free(name);
539
540         switch (type) {
541         case TRAILER_KEY:
542                 if (conf->key)
543                         warning(_("more than one %s"), conf_key);
544                 conf->key = xstrdup(value);
545                 break;
546         case TRAILER_COMMAND:
547                 if (conf->command)
548                         warning(_("more than one %s"), conf_key);
549                 conf->command = xstrdup(value);
550                 break;
551         case TRAILER_WHERE:
552                 if (set_where(conf, value))
553                         warning(_("unknown value '%s' for key '%s'"), value, conf_key);
554                 break;
555         case TRAILER_IF_EXISTS:
556                 if (set_if_exists(conf, value))
557                         warning(_("unknown value '%s' for key '%s'"), value, conf_key);
558                 break;
559         case TRAILER_IF_MISSING:
560                 if (set_if_missing(conf, value))
561                         warning(_("unknown value '%s' for key '%s'"), value, conf_key);
562                 break;
563         default:
564                 die("internal bug in trailer.c");
565         }
566         return 0;
567 }
568
569 static int parse_trailer(struct strbuf *tok, struct strbuf *val, const char *trailer)
570 {
571         size_t len;
572         struct strbuf seps = STRBUF_INIT;
573         strbuf_addstr(&seps, separators);
574         strbuf_addch(&seps, '=');
575         len = strcspn(trailer, seps.buf);
576         strbuf_release(&seps);
577         if (len == 0) {
578                 int l = strlen(trailer);
579                 while (l > 0 && isspace(trailer[l - 1]))
580                         l--;
581                 return error(_("empty trailer token in trailer '%.*s'"), l, trailer);
582         }
583         if (len < strlen(trailer)) {
584                 strbuf_add(tok, trailer, len);
585                 strbuf_trim(tok);
586                 strbuf_addstr(val, trailer + len + 1);
587                 strbuf_trim(val);
588         } else {
589                 strbuf_addstr(tok, trailer);
590                 strbuf_trim(tok);
591         }
592         return 0;
593 }
594
595 static const char *token_from_item(struct trailer_item *item, char *tok)
596 {
597         if (item->conf.key)
598                 return item->conf.key;
599         if (tok)
600                 return tok;
601         return item->conf.name;
602 }
603
604 static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,
605                                              char *tok, char *val)
606 {
607         struct trailer_item *new = xcalloc(sizeof(*new), 1);
608         new->value = val ? val : xstrdup("");
609
610         if (conf_item) {
611                 duplicate_conf(&new->conf, &conf_item->conf);
612                 new->token = xstrdup(token_from_item(conf_item, tok));
613                 free(tok);
614         } else {
615                 duplicate_conf(&new->conf, &default_conf_info);
616                 new->token = tok;
617         }
618
619         return new;
620 }
621
622 static int token_matches_item(const char *tok, struct trailer_item *item, int tok_len)
623 {
624         if (!strncasecmp(tok, item->conf.name, tok_len))
625                 return 1;
626         return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0;
627 }
628
629 static struct trailer_item *create_trailer_item(const char *string)
630 {
631         struct strbuf tok = STRBUF_INIT;
632         struct strbuf val = STRBUF_INIT;
633         struct trailer_item *item;
634         int tok_len;
635
636         if (parse_trailer(&tok, &val, string))
637                 return NULL;
638
639         tok_len = token_len_without_separator(tok.buf, tok.len);
640
641         /* Lookup if the token matches something in the config */
642         for (item = first_conf_item; item; item = item->next) {
643                 if (token_matches_item(tok.buf, item, tok_len))
644                         return new_trailer_item(item,
645                                                 strbuf_detach(&tok, NULL),
646                                                 strbuf_detach(&val, NULL));
647         }
648
649         return new_trailer_item(NULL,
650                                 strbuf_detach(&tok, NULL),
651                                 strbuf_detach(&val, NULL));
652 }
653
654 static void add_trailer_item(struct trailer_item **first,
655                              struct trailer_item **last,
656                              struct trailer_item *new)
657 {
658         if (!new)
659                 return;
660         if (!*last) {
661                 *first = new;
662                 *last = new;
663         } else {
664                 (*last)->next = new;
665                 new->previous = *last;
666                 *last = new;
667         }
668 }
669
670 static struct trailer_item *process_command_line_args(struct string_list *trailers)
671 {
672         struct trailer_item *arg_tok_first = NULL;
673         struct trailer_item *arg_tok_last = NULL;
674         struct string_list_item *tr;
675         struct trailer_item *item;
676
677         /* Add a trailer item for each configured trailer with a command */
678         for (item = first_conf_item; item; item = item->next) {
679                 if (item->conf.command) {
680                         struct trailer_item *new = new_trailer_item(item, NULL, NULL);
681                         add_trailer_item(&arg_tok_first, &arg_tok_last, new);
682                 }
683         }
684
685         /* Add a trailer item for each trailer on the command line */
686         for_each_string_list_item(tr, trailers) {
687                 struct trailer_item *new = create_trailer_item(tr->string);
688                 add_trailer_item(&arg_tok_first, &arg_tok_last, new);
689         }
690
691         return arg_tok_first;
692 }
693
694 static struct strbuf **read_input_file(const char *file)
695 {
696         struct strbuf **lines;
697         struct strbuf sb = STRBUF_INIT;
698
699         if (file) {
700                 if (strbuf_read_file(&sb, file, 0) < 0)
701                         die_errno(_("could not read input file '%s'"), file);
702         } else {
703                 if (strbuf_read(&sb, fileno(stdin), 0) < 0)
704                         die_errno(_("could not read from stdin"));
705         }
706
707         lines = strbuf_split(&sb, '\n');
708
709         strbuf_release(&sb);
710
711         return lines;
712 }
713
714 /*
715  * Return the (0 based) index of the start of the patch or the line
716  * count if there is no patch in the message.
717  */
718 static int find_patch_start(struct strbuf **lines, int count)
719 {
720         int i;
721
722         /* Get the start of the patch part if any */
723         for (i = 0; i < count; i++) {
724                 if (starts_with(lines[i]->buf, "---"))
725                         return i;
726         }
727
728         return count;
729 }
730
731 /*
732  * Return the (0 based) index of the first trailer line or count if
733  * there are no trailers. Trailers are searched only in the lines from
734  * index (count - 1) down to index 0.
735  */
736 static int find_trailer_start(struct strbuf **lines, int count)
737 {
738         int start, only_spaces = 1;
739
740         /*
741          * Get the start of the trailers by looking starting from the end
742          * for a line with only spaces before lines with one separator.
743          */
744         for (start = count - 1; start >= 0; start--) {
745                 if (lines[start]->buf[0] == comment_line_char)
746                         continue;
747                 if (contains_only_spaces(lines[start]->buf)) {
748                         if (only_spaces)
749                                 continue;
750                         return start + 1;
751                 }
752                 if (strcspn(lines[start]->buf, separators) < lines[start]->len) {
753                         if (only_spaces)
754                                 only_spaces = 0;
755                         continue;
756                 }
757                 return count;
758         }
759
760         return only_spaces ? count : 0;
761 }
762
763 /* Get the index of the end of the trailers */
764 static int find_trailer_end(struct strbuf **lines, int patch_start)
765 {
766         struct strbuf sb = STRBUF_INIT;
767         int i, ignore_bytes;
768
769         for (i = 0; i < patch_start; i++)
770                 strbuf_addbuf(&sb, lines[i]);
771         ignore_bytes = ignore_non_trailer(&sb);
772         strbuf_release(&sb);
773         for (i = patch_start - 1; i >= 0 && ignore_bytes > 0; i--)
774                 ignore_bytes -= lines[i]->len;
775
776         return i + 1;
777 }
778
779 static int has_blank_line_before(struct strbuf **lines, int start)
780 {
781         for (;start >= 0; start--) {
782                 if (lines[start]->buf[0] == comment_line_char)
783                         continue;
784                 return contains_only_spaces(lines[start]->buf);
785         }
786         return 0;
787 }
788
789 static void print_lines(struct strbuf **lines, int start, int end)
790 {
791         int i;
792         for (i = start; lines[i] && i < end; i++)
793                 printf("%s", lines[i]->buf);
794 }
795
796 static int process_input_file(struct strbuf **lines,
797                               struct trailer_item **in_tok_first,
798                               struct trailer_item **in_tok_last)
799 {
800         int count = 0;
801         int patch_start, trailer_start, trailer_end, i;
802
803         /* Get the line count */
804         while (lines[count])
805                 count++;
806
807         patch_start = find_patch_start(lines, count);
808         trailer_end = find_trailer_end(lines, patch_start);
809         trailer_start = find_trailer_start(lines, trailer_end);
810
811         /* Print lines before the trailers as is */
812         print_lines(lines, 0, trailer_start);
813
814         if (!has_blank_line_before(lines, trailer_start - 1))
815                 printf("\n");
816
817         /* Parse trailer lines */
818         for (i = trailer_start; i < trailer_end; i++) {
819                 if (lines[i]->buf[0] != comment_line_char) {
820                         struct trailer_item *new = create_trailer_item(lines[i]->buf);
821                         add_trailer_item(in_tok_first, in_tok_last, new);
822                 }
823         }
824
825         return trailer_end;
826 }
827
828 static void free_all(struct trailer_item **first)
829 {
830         while (*first) {
831                 struct trailer_item *item = remove_first(first);
832                 free_trailer_item(item);
833         }
834 }
835
836 void process_trailers(const char *file, int trim_empty, struct string_list *trailers)
837 {
838         struct trailer_item *in_tok_first = NULL;
839         struct trailer_item *in_tok_last = NULL;
840         struct trailer_item *arg_tok_first;
841         struct strbuf **lines;
842         int trailer_end;
843
844         /* Default config must be setup first */
845         git_config(git_trailer_default_config, NULL);
846         git_config(git_trailer_config, NULL);
847
848         lines = read_input_file(file);
849
850         /* Print the lines before the trailers */
851         trailer_end = process_input_file(lines, &in_tok_first, &in_tok_last);
852
853         arg_tok_first = process_command_line_args(trailers);
854
855         process_trailers_lists(&in_tok_first, &in_tok_last, &arg_tok_first);
856
857         print_all(in_tok_first, trim_empty);
858
859         free_all(&in_tok_first);
860
861         /* Print the lines after the trailers as is */
862         print_lines(lines, trailer_end, INT_MAX);
863
864         strbuf_list_free(lines);
865 }