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