Merge branch 'pb/doc-external-diff-env'
[git] / trailer.c
1 #include "cache.h"
2 #include "config.h"
3 #include "string-list.h"
4 #include "run-command.h"
5 #include "commit.h"
6 #include "tempfile.h"
7 #include "trailer.h"
8 #include "list.h"
9 /*
10  * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
11  */
12
13 struct conf_info {
14         char *name;
15         char *key;
16         char *command;
17         enum trailer_where where;
18         enum trailer_if_exists if_exists;
19         enum trailer_if_missing if_missing;
20 };
21
22 static struct conf_info default_conf_info;
23
24 struct trailer_item {
25         struct list_head list;
26         /*
27          * If this is not a trailer line, the line is stored in value
28          * (excluding the terminating newline) and token is NULL.
29          */
30         char *token;
31         char *value;
32 };
33
34 struct arg_item {
35         struct list_head list;
36         char *token;
37         char *value;
38         struct conf_info conf;
39 };
40
41 static LIST_HEAD(conf_head);
42
43 static char *separators = ":";
44
45 static int configured;
46
47 #define TRAILER_ARG_STRING "$ARG"
48
49 static const char *git_generated_prefixes[] = {
50         "Signed-off-by: ",
51         "(cherry picked from commit ",
52         NULL
53 };
54
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; \
58                 pos != (head); \
59                 pos = is_reverse ? pos->prev : pos->next)
60
61 static int after_or_end(enum trailer_where where)
62 {
63         return (where == WHERE_AFTER) || (where == WHERE_END);
64 }
65
66 /*
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.
71  */
72 static size_t token_len_without_separator(const char *token, size_t len)
73 {
74         while (len > 0 && !isalnum(token[len - 1]))
75                 len--;
76         return len;
77 }
78
79 static int same_token(struct trailer_item *a, struct arg_item *b)
80 {
81         size_t a_len, b_len, min_len;
82
83         if (!a->token)
84                 return 0;
85
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;
89
90         return !strncasecmp(a->token, b->token, min_len);
91 }
92
93 static int same_value(struct trailer_item *a, struct arg_item *b)
94 {
95         return !strcasecmp(a->value, b->value);
96 }
97
98 static int same_trailer(struct trailer_item *a, struct arg_item *b)
99 {
100         return same_token(a, b) && same_value(a, b);
101 }
102
103 static inline int is_blank_line(const char *str)
104 {
105         const char *s = str;
106         while (*s && *s != '\n' && isspace(*s))
107                 s++;
108         return !*s || *s == '\n';
109 }
110
111 static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b)
112 {
113         const char *ptr = strstr(sb->buf, a);
114         if (ptr)
115                 strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));
116 }
117
118 static void free_trailer_item(struct trailer_item *item)
119 {
120         free(item->token);
121         free(item->value);
122         free(item);
123 }
124
125 static void free_arg_item(struct arg_item *item)
126 {
127         free(item->conf.name);
128         free(item->conf.key);
129         free(item->conf.command);
130         free(item->token);
131         free(item->value);
132         free(item);
133 }
134
135 static char last_non_space_char(const char *s)
136 {
137         int i;
138         for (i = strlen(s) - 1; i >= 0; i--)
139                 if (!isspace(s[i]))
140                         return s[i];
141         return '\0';
142 }
143
144 static void print_tok_val(FILE *outfile, const char *tok, const char *val)
145 {
146         char c;
147
148         if (!tok) {
149                 fprintf(outfile, "%s\n", val);
150                 return;
151         }
152
153         c = last_non_space_char(tok);
154         if (!c)
155                 return;
156         if (strchr(separators, c))
157                 fprintf(outfile, "%s%s\n", tok, val);
158         else
159                 fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
160 }
161
162 static void print_all(FILE *outfile, struct list_head *head,
163                       const struct process_trailer_options *opts)
164 {
165         struct list_head *pos;
166         struct trailer_item *item;
167         list_for_each(pos, head) {
168                 item = list_entry(pos, struct trailer_item, list);
169                 if ((!opts->trim_empty || strlen(item->value) > 0) &&
170                     (!opts->only_trailers || item->token))
171                         print_tok_val(outfile, item->token, item->value);
172         }
173 }
174
175 static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok)
176 {
177         struct trailer_item *new_item = xcalloc(sizeof(*new_item), 1);
178         new_item->token = arg_tok->token;
179         new_item->value = arg_tok->value;
180         arg_tok->token = arg_tok->value = NULL;
181         free_arg_item(arg_tok);
182         return new_item;
183 }
184
185 static void add_arg_to_input_list(struct trailer_item *on_tok,
186                                   struct arg_item *arg_tok)
187 {
188         int aoe = after_or_end(arg_tok->conf.where);
189         struct trailer_item *to_add = trailer_from_arg(arg_tok);
190         if (aoe)
191                 list_add(&to_add->list, &on_tok->list);
192         else
193                 list_add_tail(&to_add->list, &on_tok->list);
194 }
195
196 static int check_if_different(struct trailer_item *in_tok,
197                               struct arg_item *arg_tok,
198                               int check_all,
199                               struct list_head *head)
200 {
201         enum trailer_where where = arg_tok->conf.where;
202         struct list_head *next_head;
203         do {
204                 if (same_trailer(in_tok, arg_tok))
205                         return 0;
206                 /*
207                  * if we want to add a trailer after another one,
208                  * we have to check those before this one
209                  */
210                 next_head = after_or_end(where) ? in_tok->list.prev
211                                                 : in_tok->list.next;
212                 if (next_head == head)
213                         break;
214                 in_tok = list_entry(next_head, struct trailer_item, list);
215         } while (check_all);
216         return 1;
217 }
218
219 static char *apply_command(const char *command, const char *arg)
220 {
221         struct strbuf cmd = STRBUF_INIT;
222         struct strbuf buf = STRBUF_INIT;
223         struct child_process cp = CHILD_PROCESS_INIT;
224         char *result;
225
226         strbuf_addstr(&cmd, command);
227         if (arg)
228                 strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
229
230         strvec_push(&cp.args, cmd.buf);
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 arg_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 arg_item *arg_tok,
267                                 struct trailer_item *on_tok,
268                                 struct list_head *head)
269 {
270         switch (arg_tok->conf.if_exists) {
271         case EXISTS_DO_NOTHING:
272                 free_arg_item(arg_tok);
273                 break;
274         case EXISTS_REPLACE:
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);
279                 break;
280         case EXISTS_ADD:
281                 apply_item_command(in_tok, arg_tok);
282                 add_arg_to_input_list(on_tok, arg_tok);
283                 break;
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);
288                 else
289                         free_arg_item(arg_tok);
290                 break;
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);
295                 else
296                         free_arg_item(arg_tok);
297                 break;
298         default:
299                 BUG("trailer.c: unhandled value %d",
300                     arg_tok->conf.if_exists);
301         }
302 }
303
304 static void apply_arg_if_missing(struct list_head *head,
305                                  struct arg_item *arg_tok)
306 {
307         enum trailer_where where;
308         struct trailer_item *to_add;
309
310         switch (arg_tok->conf.if_missing) {
311         case MISSING_DO_NOTHING:
312                 free_arg_item(arg_tok);
313                 break;
314         case MISSING_ADD:
315                 where = arg_tok->conf.where;
316                 apply_item_command(NULL, arg_tok);
317                 to_add = trailer_from_arg(arg_tok);
318                 if (after_or_end(where))
319                         list_add_tail(&to_add->list, head);
320                 else
321                         list_add(&to_add->list, head);
322                 break;
323         default:
324                 BUG("trailer.c: unhandled value %d",
325                     arg_tok->conf.if_missing);
326         }
327 }
328
329 static int find_same_and_apply_arg(struct list_head *head,
330                                    struct arg_item *arg_tok)
331 {
332         struct list_head *pos;
333         struct trailer_item *in_tok;
334         struct trailer_item *on_tok;
335
336         enum trailer_where where = arg_tok->conf.where;
337         int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
338         int backwards = after_or_end(where);
339         struct trailer_item *start_tok;
340
341         if (list_empty(head))
342                 return 0;
343
344         start_tok = list_entry(backwards ? head->prev : head->next,
345                                struct trailer_item,
346                                list);
347
348         list_for_each_dir(pos, head, backwards) {
349                 in_tok = list_entry(pos, struct trailer_item, list);
350                 if (!same_token(in_tok, arg_tok))
351                         continue;
352                 on_tok = middle ? in_tok : start_tok;
353                 apply_arg_if_exists(in_tok, arg_tok, on_tok, head);
354                 return 1;
355         }
356         return 0;
357 }
358
359 static void process_trailers_lists(struct list_head *head,
360                                    struct list_head *arg_head)
361 {
362         struct list_head *pos, *p;
363         struct arg_item *arg_tok;
364
365         list_for_each_safe(pos, p, arg_head) {
366                 int applied = 0;
367                 arg_tok = list_entry(pos, struct arg_item, list);
368
369                 list_del(pos);
370
371                 applied = find_same_and_apply_arg(head, arg_tok);
372
373                 if (!applied)
374                         apply_arg_if_missing(head, arg_tok);
375         }
376 }
377
378 int trailer_set_where(enum trailer_where *item, const char *value)
379 {
380         if (!value)
381                 *item = WHERE_DEFAULT;
382         else if (!strcasecmp("after", value))
383                 *item = WHERE_AFTER;
384         else if (!strcasecmp("before", value))
385                 *item = WHERE_BEFORE;
386         else if (!strcasecmp("end", value))
387                 *item = WHERE_END;
388         else if (!strcasecmp("start", value))
389                 *item = WHERE_START;
390         else
391                 return -1;
392         return 0;
393 }
394
395 int trailer_set_if_exists(enum trailer_if_exists *item, const char *value)
396 {
397         if (!value)
398                 *item = EXISTS_DEFAULT;
399         else if (!strcasecmp("addIfDifferent", value))
400                 *item = EXISTS_ADD_IF_DIFFERENT;
401         else if (!strcasecmp("addIfDifferentNeighbor", value))
402                 *item = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
403         else if (!strcasecmp("add", value))
404                 *item = EXISTS_ADD;
405         else if (!strcasecmp("replace", value))
406                 *item = EXISTS_REPLACE;
407         else if (!strcasecmp("doNothing", value))
408                 *item = EXISTS_DO_NOTHING;
409         else
410                 return -1;
411         return 0;
412 }
413
414 int trailer_set_if_missing(enum trailer_if_missing *item, const char *value)
415 {
416         if (!value)
417                 *item = MISSING_DEFAULT;
418         else if (!strcasecmp("doNothing", value))
419                 *item = MISSING_DO_NOTHING;
420         else if (!strcasecmp("add", value))
421                 *item = MISSING_ADD;
422         else
423                 return -1;
424         return 0;
425 }
426
427 static void duplicate_conf(struct conf_info *dst, const struct conf_info *src)
428 {
429         *dst = *src;
430         dst->name = xstrdup_or_null(src->name);
431         dst->key = xstrdup_or_null(src->key);
432         dst->command = xstrdup_or_null(src->command);
433 }
434
435 static struct arg_item *get_conf_item(const char *name)
436 {
437         struct list_head *pos;
438         struct arg_item *item;
439
440         /* Look up item with same name */
441         list_for_each(pos, &conf_head) {
442                 item = list_entry(pos, struct arg_item, list);
443                 if (!strcasecmp(item->conf.name, name))
444                         return item;
445         }
446
447         /* Item does not already exists, create it */
448         item = xcalloc(sizeof(*item), 1);
449         duplicate_conf(&item->conf, &default_conf_info);
450         item->conf.name = xstrdup(name);
451
452         list_add_tail(&item->list, &conf_head);
453
454         return item;
455 }
456
457 enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_WHERE,
458                          TRAILER_IF_EXISTS, TRAILER_IF_MISSING };
459
460 static struct {
461         const char *name;
462         enum trailer_info_type type;
463 } trailer_config_items[] = {
464         { "key", TRAILER_KEY },
465         { "command", TRAILER_COMMAND },
466         { "where", TRAILER_WHERE },
467         { "ifexists", TRAILER_IF_EXISTS },
468         { "ifmissing", TRAILER_IF_MISSING }
469 };
470
471 static int git_trailer_default_config(const char *conf_key, const char *value, void *cb)
472 {
473         const char *trailer_item, *variable_name;
474
475         if (!skip_prefix(conf_key, "trailer.", &trailer_item))
476                 return 0;
477
478         variable_name = strrchr(trailer_item, '.');
479         if (!variable_name) {
480                 if (!strcmp(trailer_item, "where")) {
481                         if (trailer_set_where(&default_conf_info.where,
482                                               value) < 0)
483                                 warning(_("unknown value '%s' for key '%s'"),
484                                         value, conf_key);
485                 } else if (!strcmp(trailer_item, "ifexists")) {
486                         if (trailer_set_if_exists(&default_conf_info.if_exists,
487                                                   value) < 0)
488                                 warning(_("unknown value '%s' for key '%s'"),
489                                         value, conf_key);
490                 } else if (!strcmp(trailer_item, "ifmissing")) {
491                         if (trailer_set_if_missing(&default_conf_info.if_missing,
492                                                    value) < 0)
493                                 warning(_("unknown value '%s' for key '%s'"),
494                                         value, conf_key);
495                 } else if (!strcmp(trailer_item, "separators")) {
496                         separators = xstrdup(value);
497                 }
498         }
499         return 0;
500 }
501
502 static int git_trailer_config(const char *conf_key, const char *value, void *cb)
503 {
504         const char *trailer_item, *variable_name;
505         struct arg_item *item;
506         struct conf_info *conf;
507         char *name = NULL;
508         enum trailer_info_type type;
509         int i;
510
511         if (!skip_prefix(conf_key, "trailer.", &trailer_item))
512                 return 0;
513
514         variable_name = strrchr(trailer_item, '.');
515         if (!variable_name)
516                 return 0;
517
518         variable_name++;
519         for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) {
520                 if (strcmp(trailer_config_items[i].name, variable_name))
521                         continue;
522                 name = xstrndup(trailer_item,  variable_name - trailer_item - 1);
523                 type = trailer_config_items[i].type;
524                 break;
525         }
526
527         if (!name)
528                 return 0;
529
530         item = get_conf_item(name);
531         conf = &item->conf;
532         free(name);
533
534         switch (type) {
535         case TRAILER_KEY:
536                 if (conf->key)
537                         warning(_("more than one %s"), conf_key);
538                 conf->key = xstrdup(value);
539                 break;
540         case TRAILER_COMMAND:
541                 if (conf->command)
542                         warning(_("more than one %s"), conf_key);
543                 conf->command = xstrdup(value);
544                 break;
545         case TRAILER_WHERE:
546                 if (trailer_set_where(&conf->where, value))
547                         warning(_("unknown value '%s' for key '%s'"), value, conf_key);
548                 break;
549         case TRAILER_IF_EXISTS:
550                 if (trailer_set_if_exists(&conf->if_exists, value))
551                         warning(_("unknown value '%s' for key '%s'"), value, conf_key);
552                 break;
553         case TRAILER_IF_MISSING:
554                 if (trailer_set_if_missing(&conf->if_missing, value))
555                         warning(_("unknown value '%s' for key '%s'"), value, conf_key);
556                 break;
557         default:
558                 BUG("trailer.c: unhandled type %d", type);
559         }
560         return 0;
561 }
562
563 static void ensure_configured(void)
564 {
565         if (configured)
566                 return;
567
568         /* Default config must be setup first */
569         default_conf_info.where = WHERE_END;
570         default_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
571         default_conf_info.if_missing = MISSING_ADD;
572         git_config(git_trailer_default_config, NULL);
573         git_config(git_trailer_config, NULL);
574         configured = 1;
575 }
576
577 static const char *token_from_item(struct arg_item *item, char *tok)
578 {
579         if (item->conf.key)
580                 return item->conf.key;
581         if (tok)
582                 return tok;
583         return item->conf.name;
584 }
585
586 static int token_matches_item(const char *tok, struct arg_item *item, size_t tok_len)
587 {
588         if (!strncasecmp(tok, item->conf.name, tok_len))
589                 return 1;
590         return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0;
591 }
592
593 /*
594  * If the given line is of the form
595  * "<token><optional whitespace><separator>..." or "<separator>...", return the
596  * location of the separator. Otherwise, return -1.  The optional whitespace
597  * is allowed there primarily to allow things like "Bug #43" where <token> is
598  * "Bug" and <separator> is "#".
599  *
600  * The separator-starts-line case (in which this function returns 0) is
601  * distinguished from the non-well-formed-line case (in which this function
602  * returns -1) because some callers of this function need such a distinction.
603  */
604 static ssize_t find_separator(const char *line, const char *separators)
605 {
606         int whitespace_found = 0;
607         const char *c;
608         for (c = line; *c; c++) {
609                 if (strchr(separators, *c))
610                         return c - line;
611                 if (!whitespace_found && (isalnum(*c) || *c == '-'))
612                         continue;
613                 if (c != line && (*c == ' ' || *c == '\t')) {
614                         whitespace_found = 1;
615                         continue;
616                 }
617                 break;
618         }
619         return -1;
620 }
621
622 /*
623  * Obtain the token, value, and conf from the given trailer.
624  *
625  * separator_pos must not be 0, since the token cannot be an empty string.
626  *
627  * If separator_pos is -1, interpret the whole trailer as a token.
628  */
629 static void parse_trailer(struct strbuf *tok, struct strbuf *val,
630                          const struct conf_info **conf, const char *trailer,
631                          ssize_t separator_pos)
632 {
633         struct arg_item *item;
634         size_t tok_len;
635         struct list_head *pos;
636
637         if (separator_pos != -1) {
638                 strbuf_add(tok, trailer, separator_pos);
639                 strbuf_trim(tok);
640                 strbuf_addstr(val, trailer + separator_pos + 1);
641                 strbuf_trim(val);
642         } else {
643                 strbuf_addstr(tok, trailer);
644                 strbuf_trim(tok);
645         }
646
647         /* Lookup if the token matches something in the config */
648         tok_len = token_len_without_separator(tok->buf, tok->len);
649         if (conf)
650                 *conf = &default_conf_info;
651         list_for_each(pos, &conf_head) {
652                 item = list_entry(pos, struct arg_item, list);
653                 if (token_matches_item(tok->buf, item, tok_len)) {
654                         char *tok_buf = strbuf_detach(tok, NULL);
655                         if (conf)
656                                 *conf = &item->conf;
657                         strbuf_addstr(tok, token_from_item(item, tok_buf));
658                         free(tok_buf);
659                         break;
660                 }
661         }
662 }
663
664 static struct trailer_item *add_trailer_item(struct list_head *head, char *tok,
665                                              char *val)
666 {
667         struct trailer_item *new_item = xcalloc(sizeof(*new_item), 1);
668         new_item->token = tok;
669         new_item->value = val;
670         list_add_tail(&new_item->list, head);
671         return new_item;
672 }
673
674 static void add_arg_item(struct list_head *arg_head, char *tok, char *val,
675                          const struct conf_info *conf,
676                          const struct new_trailer_item *new_trailer_item)
677 {
678         struct arg_item *new_item = xcalloc(sizeof(*new_item), 1);
679         new_item->token = tok;
680         new_item->value = val;
681         duplicate_conf(&new_item->conf, conf);
682         if (new_trailer_item) {
683                 if (new_trailer_item->where != WHERE_DEFAULT)
684                         new_item->conf.where = new_trailer_item->where;
685                 if (new_trailer_item->if_exists != EXISTS_DEFAULT)
686                         new_item->conf.if_exists = new_trailer_item->if_exists;
687                 if (new_trailer_item->if_missing != MISSING_DEFAULT)
688                         new_item->conf.if_missing = new_trailer_item->if_missing;
689         }
690         list_add_tail(&new_item->list, arg_head);
691 }
692
693 static void process_command_line_args(struct list_head *arg_head,
694                                       struct list_head *new_trailer_head)
695 {
696         struct arg_item *item;
697         struct strbuf tok = STRBUF_INIT;
698         struct strbuf val = STRBUF_INIT;
699         const struct conf_info *conf;
700         struct list_head *pos;
701
702         /*
703          * In command-line arguments, '=' is accepted (in addition to the
704          * separators that are defined).
705          */
706         char *cl_separators = xstrfmt("=%s", separators);
707
708         /* Add an arg item for each configured trailer with a command */
709         list_for_each(pos, &conf_head) {
710                 item = list_entry(pos, struct arg_item, list);
711                 if (item->conf.command)
712                         add_arg_item(arg_head,
713                                      xstrdup(token_from_item(item, NULL)),
714                                      xstrdup(""),
715                                      &item->conf, NULL);
716         }
717
718         /* Add an arg item for each trailer on the command line */
719         list_for_each(pos, new_trailer_head) {
720                 struct new_trailer_item *tr =
721                         list_entry(pos, struct new_trailer_item, list);
722                 ssize_t separator_pos = find_separator(tr->text, cl_separators);
723
724                 if (separator_pos == 0) {
725                         struct strbuf sb = STRBUF_INIT;
726                         strbuf_addstr(&sb, tr->text);
727                         strbuf_trim(&sb);
728                         error(_("empty trailer token in trailer '%.*s'"),
729                               (int) sb.len, sb.buf);
730                         strbuf_release(&sb);
731                 } else {
732                         parse_trailer(&tok, &val, &conf, tr->text,
733                                       separator_pos);
734                         add_arg_item(arg_head,
735                                      strbuf_detach(&tok, NULL),
736                                      strbuf_detach(&val, NULL),
737                                      conf, tr);
738                 }
739         }
740
741         free(cl_separators);
742 }
743
744 static void read_input_file(struct strbuf *sb, const char *file)
745 {
746         if (file) {
747                 if (strbuf_read_file(sb, file, 0) < 0)
748                         die_errno(_("could not read input file '%s'"), file);
749         } else {
750                 if (strbuf_read(sb, fileno(stdin), 0) < 0)
751                         die_errno(_("could not read from stdin"));
752         }
753 }
754
755 static const char *next_line(const char *str)
756 {
757         const char *nl = strchrnul(str, '\n');
758         return nl + !!*nl;
759 }
760
761 /*
762  * Return the position of the start of the last line. If len is 0, return -1.
763  */
764 static ssize_t last_line(const char *buf, size_t len)
765 {
766         ssize_t i;
767         if (len == 0)
768                 return -1;
769         if (len == 1)
770                 return 0;
771         /*
772          * Skip the last character (in addition to the null terminator),
773          * because if the last character is a newline, it is considered as part
774          * of the last line anyway.
775          */
776         i = len - 2;
777
778         for (; i >= 0; i--) {
779                 if (buf[i] == '\n')
780                         return i + 1;
781         }
782         return 0;
783 }
784
785 /*
786  * Return the position of the start of the patch or the length of str if there
787  * is no patch in the message.
788  */
789 static size_t find_patch_start(const char *str)
790 {
791         const char *s;
792
793         for (s = str; *s; s = next_line(s)) {
794                 const char *v;
795
796                 if (skip_prefix(s, "---", &v) && isspace(*v))
797                         return s - str;
798         }
799
800         return s - str;
801 }
802
803 /*
804  * Return the position of the first trailer line or len if there are no
805  * trailers.
806  */
807 static size_t find_trailer_start(const char *buf, size_t len)
808 {
809         const char *s;
810         ssize_t end_of_title, l;
811         int only_spaces = 1;
812         int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0;
813         /*
814          * Number of possible continuation lines encountered. This will be
815          * reset to 0 if we encounter a trailer (since those lines are to be
816          * considered continuations of that trailer), and added to
817          * non_trailer_lines if we encounter a non-trailer (since those lines
818          * are to be considered non-trailers).
819          */
820         int possible_continuation_lines = 0;
821
822         /* The first paragraph is the title and cannot be trailers */
823         for (s = buf; s < buf + len; s = next_line(s)) {
824                 if (s[0] == comment_line_char)
825                         continue;
826                 if (is_blank_line(s))
827                         break;
828         }
829         end_of_title = s - buf;
830
831         /*
832          * Get the start of the trailers by looking starting from the end for a
833          * blank line before a set of non-blank lines that (i) are all
834          * trailers, or (ii) contains at least one Git-generated trailer and
835          * consists of at least 25% trailers.
836          */
837         for (l = last_line(buf, len);
838              l >= end_of_title;
839              l = last_line(buf, l)) {
840                 const char *bol = buf + l;
841                 const char **p;
842                 ssize_t separator_pos;
843
844                 if (bol[0] == comment_line_char) {
845                         non_trailer_lines += possible_continuation_lines;
846                         possible_continuation_lines = 0;
847                         continue;
848                 }
849                 if (is_blank_line(bol)) {
850                         if (only_spaces)
851                                 continue;
852                         non_trailer_lines += possible_continuation_lines;
853                         if (recognized_prefix &&
854                             trailer_lines * 3 >= non_trailer_lines)
855                                 return next_line(bol) - buf;
856                         else if (trailer_lines && !non_trailer_lines)
857                                 return next_line(bol) - buf;
858                         return len;
859                 }
860                 only_spaces = 0;
861
862                 for (p = git_generated_prefixes; *p; p++) {
863                         if (starts_with(bol, *p)) {
864                                 trailer_lines++;
865                                 possible_continuation_lines = 0;
866                                 recognized_prefix = 1;
867                                 goto continue_outer_loop;
868                         }
869                 }
870
871                 separator_pos = find_separator(bol, separators);
872                 if (separator_pos >= 1 && !isspace(bol[0])) {
873                         struct list_head *pos;
874
875                         trailer_lines++;
876                         possible_continuation_lines = 0;
877                         if (recognized_prefix)
878                                 continue;
879                         list_for_each(pos, &conf_head) {
880                                 struct arg_item *item;
881                                 item = list_entry(pos, struct arg_item, list);
882                                 if (token_matches_item(bol, item,
883                                                        separator_pos)) {
884                                         recognized_prefix = 1;
885                                         break;
886                                 }
887                         }
888                 } else if (isspace(bol[0]))
889                         possible_continuation_lines++;
890                 else {
891                         non_trailer_lines++;
892                         non_trailer_lines += possible_continuation_lines;
893                         possible_continuation_lines = 0;
894                 }
895 continue_outer_loop:
896                 ;
897         }
898
899         return len;
900 }
901
902 /* Return the position of the end of the trailers. */
903 static size_t find_trailer_end(const char *buf, size_t len)
904 {
905         return len - ignore_non_trailer(buf, len);
906 }
907
908 static int ends_with_blank_line(const char *buf, size_t len)
909 {
910         ssize_t ll = last_line(buf, len);
911         if (ll < 0)
912                 return 0;
913         return is_blank_line(buf + ll);
914 }
915
916 static void unfold_value(struct strbuf *val)
917 {
918         struct strbuf out = STRBUF_INIT;
919         size_t i;
920
921         strbuf_grow(&out, val->len);
922         i = 0;
923         while (i < val->len) {
924                 char c = val->buf[i++];
925                 if (c == '\n') {
926                         /* Collapse continuation down to a single space. */
927                         while (i < val->len && isspace(val->buf[i]))
928                                 i++;
929                         strbuf_addch(&out, ' ');
930                 } else {
931                         strbuf_addch(&out, c);
932                 }
933         }
934
935         /* Empty lines may have left us with whitespace cruft at the edges */
936         strbuf_trim(&out);
937
938         /* output goes back to val as if we modified it in-place */
939         strbuf_swap(&out, val);
940         strbuf_release(&out);
941 }
942
943 static size_t process_input_file(FILE *outfile,
944                                  const char *str,
945                                  struct list_head *head,
946                                  const struct process_trailer_options *opts)
947 {
948         struct trailer_info info;
949         struct strbuf tok = STRBUF_INIT;
950         struct strbuf val = STRBUF_INIT;
951         size_t i;
952
953         trailer_info_get(&info, str, opts);
954
955         /* Print lines before the trailers as is */
956         if (!opts->only_trailers)
957                 fwrite(str, 1, info.trailer_start - str, outfile);
958
959         if (!opts->only_trailers && !info.blank_line_before_trailer)
960                 fprintf(outfile, "\n");
961
962         for (i = 0; i < info.trailer_nr; i++) {
963                 int separator_pos;
964                 char *trailer = info.trailers[i];
965                 if (trailer[0] == comment_line_char)
966                         continue;
967                 separator_pos = find_separator(trailer, separators);
968                 if (separator_pos >= 1) {
969                         parse_trailer(&tok, &val, NULL, trailer,
970                                       separator_pos);
971                         if (opts->unfold)
972                                 unfold_value(&val);
973                         add_trailer_item(head,
974                                          strbuf_detach(&tok, NULL),
975                                          strbuf_detach(&val, NULL));
976                 } else if (!opts->only_trailers) {
977                         strbuf_addstr(&val, trailer);
978                         strbuf_strip_suffix(&val, "\n");
979                         add_trailer_item(head,
980                                          NULL,
981                                          strbuf_detach(&val, NULL));
982                 }
983         }
984
985         trailer_info_release(&info);
986
987         return info.trailer_end - str;
988 }
989
990 static void free_all(struct list_head *head)
991 {
992         struct list_head *pos, *p;
993         list_for_each_safe(pos, p, head) {
994                 list_del(pos);
995                 free_trailer_item(list_entry(pos, struct trailer_item, list));
996         }
997 }
998
999 static struct tempfile *trailers_tempfile;
1000
1001 static FILE *create_in_place_tempfile(const char *file)
1002 {
1003         struct stat st;
1004         struct strbuf filename_template = STRBUF_INIT;
1005         const char *tail;
1006         FILE *outfile;
1007
1008         if (stat(file, &st))
1009                 die_errno(_("could not stat %s"), file);
1010         if (!S_ISREG(st.st_mode))
1011                 die(_("file %s is not a regular file"), file);
1012         if (!(st.st_mode & S_IWUSR))
1013                 die(_("file %s is not writable by user"), file);
1014
1015         /* Create temporary file in the same directory as the original */
1016         tail = strrchr(file, '/');
1017         if (tail != NULL)
1018                 strbuf_add(&filename_template, file, tail - file + 1);
1019         strbuf_addstr(&filename_template, "git-interpret-trailers-XXXXXX");
1020
1021         trailers_tempfile = xmks_tempfile_m(filename_template.buf, st.st_mode);
1022         strbuf_release(&filename_template);
1023         outfile = fdopen_tempfile(trailers_tempfile, "w");
1024         if (!outfile)
1025                 die_errno(_("could not open temporary file"));
1026
1027         return outfile;
1028 }
1029
1030 void process_trailers(const char *file,
1031                       const struct process_trailer_options *opts,
1032                       struct list_head *new_trailer_head)
1033 {
1034         LIST_HEAD(head);
1035         struct strbuf sb = STRBUF_INIT;
1036         size_t trailer_end;
1037         FILE *outfile = stdout;
1038
1039         ensure_configured();
1040
1041         read_input_file(&sb, file);
1042
1043         if (opts->in_place)
1044                 outfile = create_in_place_tempfile(file);
1045
1046         /* Print the lines before the trailers */
1047         trailer_end = process_input_file(outfile, sb.buf, &head, opts);
1048
1049         if (!opts->only_input) {
1050                 LIST_HEAD(arg_head);
1051                 process_command_line_args(&arg_head, new_trailer_head);
1052                 process_trailers_lists(&head, &arg_head);
1053         }
1054
1055         print_all(outfile, &head, opts);
1056
1057         free_all(&head);
1058
1059         /* Print the lines after the trailers as is */
1060         if (!opts->only_trailers)
1061                 fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
1062
1063         if (opts->in_place)
1064                 if (rename_tempfile(&trailers_tempfile, file))
1065                         die_errno(_("could not rename temporary file to %s"), file);
1066
1067         strbuf_release(&sb);
1068 }
1069
1070 void trailer_info_get(struct trailer_info *info, const char *str,
1071                       const struct process_trailer_options *opts)
1072 {
1073         int patch_start, trailer_end, trailer_start;
1074         struct strbuf **trailer_lines, **ptr;
1075         char **trailer_strings = NULL;
1076         size_t nr = 0, alloc = 0;
1077         char **last = NULL;
1078
1079         ensure_configured();
1080
1081         if (opts->no_divider)
1082                 patch_start = strlen(str);
1083         else
1084                 patch_start = find_patch_start(str);
1085
1086         trailer_end = find_trailer_end(str, patch_start);
1087         trailer_start = find_trailer_start(str, trailer_end);
1088
1089         trailer_lines = strbuf_split_buf(str + trailer_start,
1090                                          trailer_end - trailer_start,
1091                                          '\n',
1092                                          0);
1093         for (ptr = trailer_lines; *ptr; ptr++) {
1094                 if (last && isspace((*ptr)->buf[0])) {
1095                         struct strbuf sb = STRBUF_INIT;
1096                         strbuf_attach(&sb, *last, strlen(*last), strlen(*last));
1097                         strbuf_addbuf(&sb, *ptr);
1098                         *last = strbuf_detach(&sb, NULL);
1099                         continue;
1100                 }
1101                 ALLOC_GROW(trailer_strings, nr + 1, alloc);
1102                 trailer_strings[nr] = strbuf_detach(*ptr, NULL);
1103                 last = find_separator(trailer_strings[nr], separators) >= 1
1104                         ? &trailer_strings[nr]
1105                         : NULL;
1106                 nr++;
1107         }
1108         strbuf_list_free(trailer_lines);
1109
1110         info->blank_line_before_trailer = ends_with_blank_line(str,
1111                                                                trailer_start);
1112         info->trailer_start = str + trailer_start;
1113         info->trailer_end = str + trailer_end;
1114         info->trailers = trailer_strings;
1115         info->trailer_nr = nr;
1116 }
1117
1118 void trailer_info_release(struct trailer_info *info)
1119 {
1120         size_t i;
1121         for (i = 0; i < info->trailer_nr; i++)
1122                 free(info->trailers[i]);
1123         free(info->trailers);
1124 }
1125
1126 static void format_trailer_info(struct strbuf *out,
1127                                 const struct trailer_info *info,
1128                                 const struct process_trailer_options *opts)
1129 {
1130         size_t origlen = out->len;
1131         size_t i;
1132
1133         /* If we want the whole block untouched, we can take the fast path. */
1134         if (!opts->only_trailers && !opts->unfold && !opts->filter && !opts->separator) {
1135                 strbuf_add(out, info->trailer_start,
1136                            info->trailer_end - info->trailer_start);
1137                 return;
1138         }
1139
1140         for (i = 0; i < info->trailer_nr; i++) {
1141                 char *trailer = info->trailers[i];
1142                 ssize_t separator_pos = find_separator(trailer, separators);
1143
1144                 if (separator_pos >= 1) {
1145                         struct strbuf tok = STRBUF_INIT;
1146                         struct strbuf val = STRBUF_INIT;
1147
1148                         parse_trailer(&tok, &val, NULL, trailer, separator_pos);
1149                         if (!opts->filter || opts->filter(&tok, opts->filter_data)) {
1150                                 if (opts->unfold)
1151                                         unfold_value(&val);
1152
1153                                 if (opts->separator && out->len != origlen)
1154                                         strbuf_addbuf(out, opts->separator);
1155                                 if (!opts->value_only)
1156                                         strbuf_addf(out, "%s: ", tok.buf);
1157                                 strbuf_addbuf(out, &val);
1158                                 if (!opts->separator)
1159                                         strbuf_addch(out, '\n');
1160                         }
1161                         strbuf_release(&tok);
1162                         strbuf_release(&val);
1163
1164                 } else if (!opts->only_trailers) {
1165                         if (opts->separator && out->len != origlen) {
1166                                 strbuf_addbuf(out, opts->separator);
1167                         }
1168                         strbuf_addstr(out, trailer);
1169                         if (opts->separator) {
1170                                 strbuf_rtrim(out);
1171                         }
1172                 }
1173         }
1174
1175 }
1176
1177 void format_trailers_from_commit(struct strbuf *out, const char *msg,
1178                                  const struct process_trailer_options *opts)
1179 {
1180         struct trailer_info info;
1181
1182         trailer_info_get(&info, msg, opts);
1183         format_trailer_info(out, &info, opts);
1184         trailer_info_release(&info);
1185 }