2 #include "string-list.h"
3 #include "run-command.h"
8 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
11 enum action_where { WHERE_END, WHERE_AFTER, WHERE_BEFORE, WHERE_START };
12 enum action_if_exists { EXISTS_ADD_IF_DIFFERENT_NEIGHBOR, EXISTS_ADD_IF_DIFFERENT,
13 EXISTS_ADD, EXISTS_REPLACE, EXISTS_DO_NOTHING };
14 enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING };
20 enum action_where where;
21 enum action_if_exists if_exists;
22 enum action_if_missing if_missing;
25 static struct conf_info default_conf_info;
28 struct trailer_item *previous;
29 struct trailer_item *next;
32 struct conf_info conf;
35 static struct trailer_item *first_conf_item;
37 static char *separators = ":";
39 #define TRAILER_ARG_STRING "$ARG"
41 static int after_or_end(enum action_where where)
43 return (where == WHERE_AFTER) || (where == WHERE_END);
47 * Return the length of the string not including any final
48 * punctuation. E.g., the input "Signed-off-by:" would return
49 * 13, stripping the trailing punctuation but retaining
50 * internal punctuation.
52 static size_t token_len_without_separator(const char *token, size_t len)
54 while (len > 0 && !isalnum(token[len - 1]))
59 static int same_token(struct trailer_item *a, struct trailer_item *b)
61 size_t a_len = token_len_without_separator(a->token, strlen(a->token));
62 size_t b_len = token_len_without_separator(b->token, strlen(b->token));
63 size_t min_len = (a_len > b_len) ? b_len : a_len;
65 return !strncasecmp(a->token, b->token, min_len);
68 static int same_value(struct trailer_item *a, struct trailer_item *b)
70 return !strcasecmp(a->value, b->value);
73 static int same_trailer(struct trailer_item *a, struct trailer_item *b)
75 return same_token(a, b) && same_value(a, b);
78 static inline int contains_only_spaces(const char *str)
81 while (*s && isspace(*s))
86 static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b)
88 const char *ptr = strstr(sb->buf, a);
90 strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b));
93 static void free_trailer_item(struct trailer_item *item)
95 free(item->conf.name);
97 free(item->conf.command);
98 free((char *)item->token);
99 free((char *)item->value);
103 static char last_non_space_char(const char *s)
106 for (i = strlen(s) - 1; i >= 0; i--)
112 static void print_tok_val(FILE *outfile, const char *tok, const char *val)
114 char c = last_non_space_char(tok);
117 if (strchr(separators, c))
118 fprintf(outfile, "%s%s\n", tok, val);
120 fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
123 static void print_all(FILE *outfile, struct trailer_item *first, int trim_empty)
125 struct trailer_item *item;
126 for (item = first; item; item = item->next) {
127 if (!trim_empty || strlen(item->value) > 0)
128 print_tok_val(outfile, item->token, item->value);
132 static void update_last(struct trailer_item **last)
135 while ((*last)->next != NULL)
136 *last = (*last)->next;
139 static void update_first(struct trailer_item **first)
142 while ((*first)->previous != NULL)
143 *first = (*first)->previous;
146 static void add_arg_to_input_list(struct trailer_item *on_tok,
147 struct trailer_item *arg_tok,
148 struct trailer_item **first,
149 struct trailer_item **last)
151 if (after_or_end(arg_tok->conf.where)) {
152 arg_tok->next = on_tok->next;
153 on_tok->next = arg_tok;
154 arg_tok->previous = on_tok;
156 arg_tok->next->previous = arg_tok;
159 arg_tok->previous = on_tok->previous;
160 on_tok->previous = arg_tok;
161 arg_tok->next = on_tok;
162 if (arg_tok->previous)
163 arg_tok->previous->next = arg_tok;
168 static int check_if_different(struct trailer_item *in_tok,
169 struct trailer_item *arg_tok,
172 enum action_where where = arg_tok->conf.where;
176 if (same_trailer(in_tok, arg_tok))
179 * if we want to add a trailer after another one,
180 * we have to check those before this one
182 in_tok = after_or_end(where) ? in_tok->previous : in_tok->next;
187 static void remove_from_list(struct trailer_item *item,
188 struct trailer_item **first,
189 struct trailer_item **last)
191 struct trailer_item *next = item->next;
192 struct trailer_item *previous = item->previous;
195 item->next->previous = previous;
201 item->previous->next = next;
202 item->previous = NULL;
207 static struct trailer_item *remove_first(struct trailer_item **first)
209 struct trailer_item *item = *first;
212 item->next->previous = NULL;
218 static const char *apply_command(const char *command, const char *arg)
220 struct strbuf cmd = STRBUF_INIT;
221 struct strbuf buf = STRBUF_INIT;
222 struct child_process cp = CHILD_PROCESS_INIT;
223 const char *argv[] = {NULL, NULL};
226 strbuf_addstr(&cmd, command);
228 strbuf_replace(&cmd, TRAILER_ARG_STRING, arg);
232 cp.env = local_repo_env;
236 if (capture_command(&cp, &buf, 1024)) {
237 error(_("running trailer command '%s' failed"), cmd.buf);
238 strbuf_release(&buf);
239 result = xstrdup("");
242 result = strbuf_detach(&buf, NULL);
245 strbuf_release(&cmd);
249 static void apply_item_command(struct trailer_item *in_tok, struct trailer_item *arg_tok)
251 if (arg_tok->conf.command) {
253 if (arg_tok->value && arg_tok->value[0]) {
254 arg = arg_tok->value;
256 if (in_tok && in_tok->value)
257 arg = xstrdup(in_tok->value);
261 arg_tok->value = apply_command(arg_tok->conf.command, arg);
266 static void apply_arg_if_exists(struct trailer_item *in_tok,
267 struct trailer_item *arg_tok,
268 struct trailer_item *on_tok,
269 struct trailer_item **in_tok_first,
270 struct trailer_item **in_tok_last)
272 switch (arg_tok->conf.if_exists) {
273 case EXISTS_DO_NOTHING:
274 free_trailer_item(arg_tok);
277 apply_item_command(in_tok, arg_tok);
278 add_arg_to_input_list(on_tok, arg_tok,
279 in_tok_first, in_tok_last);
280 remove_from_list(in_tok, in_tok_first, in_tok_last);
281 free_trailer_item(in_tok);
284 apply_item_command(in_tok, arg_tok);
285 add_arg_to_input_list(on_tok, arg_tok,
286 in_tok_first, in_tok_last);
288 case EXISTS_ADD_IF_DIFFERENT:
289 apply_item_command(in_tok, arg_tok);
290 if (check_if_different(in_tok, arg_tok, 1))
291 add_arg_to_input_list(on_tok, arg_tok,
292 in_tok_first, in_tok_last);
294 free_trailer_item(arg_tok);
296 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR:
297 apply_item_command(in_tok, arg_tok);
298 if (check_if_different(on_tok, arg_tok, 0))
299 add_arg_to_input_list(on_tok, arg_tok,
300 in_tok_first, in_tok_last);
302 free_trailer_item(arg_tok);
307 static void apply_arg_if_missing(struct trailer_item **in_tok_first,
308 struct trailer_item **in_tok_last,
309 struct trailer_item *arg_tok)
311 struct trailer_item **in_tok;
312 enum action_where where;
314 switch (arg_tok->conf.if_missing) {
315 case MISSING_DO_NOTHING:
316 free_trailer_item(arg_tok);
319 where = arg_tok->conf.where;
320 in_tok = after_or_end(where) ? in_tok_last : in_tok_first;
321 apply_item_command(NULL, arg_tok);
323 add_arg_to_input_list(*in_tok, arg_tok,
324 in_tok_first, in_tok_last);
326 *in_tok_first = arg_tok;
327 *in_tok_last = arg_tok;
333 static int find_same_and_apply_arg(struct trailer_item **in_tok_first,
334 struct trailer_item **in_tok_last,
335 struct trailer_item *arg_tok)
337 struct trailer_item *in_tok;
338 struct trailer_item *on_tok;
339 struct trailer_item *following_tok;
341 enum action_where where = arg_tok->conf.where;
342 int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE);
343 int backwards = after_or_end(where);
344 struct trailer_item *start_tok = backwards ? *in_tok_last : *in_tok_first;
346 for (in_tok = start_tok; in_tok; in_tok = following_tok) {
347 following_tok = backwards ? in_tok->previous : in_tok->next;
348 if (!same_token(in_tok, arg_tok))
350 on_tok = middle ? in_tok : start_tok;
351 apply_arg_if_exists(in_tok, arg_tok, on_tok,
352 in_tok_first, in_tok_last);
358 static void process_trailers_lists(struct trailer_item **in_tok_first,
359 struct trailer_item **in_tok_last,
360 struct trailer_item **arg_tok_first)
362 struct trailer_item *arg_tok;
363 struct trailer_item *next_arg;
368 for (arg_tok = *arg_tok_first; arg_tok; arg_tok = next_arg) {
371 next_arg = arg_tok->next;
372 remove_from_list(arg_tok, arg_tok_first, NULL);
374 applied = find_same_and_apply_arg(in_tok_first,
379 apply_arg_if_missing(in_tok_first,
385 static int set_where(struct conf_info *item, const char *value)
387 if (!strcasecmp("after", value))
388 item->where = WHERE_AFTER;
389 else if (!strcasecmp("before", value))
390 item->where = WHERE_BEFORE;
391 else if (!strcasecmp("end", value))
392 item->where = WHERE_END;
393 else if (!strcasecmp("start", value))
394 item->where = WHERE_START;
400 static int set_if_exists(struct conf_info *item, const char *value)
402 if (!strcasecmp("addIfDifferent", value))
403 item->if_exists = EXISTS_ADD_IF_DIFFERENT;
404 else if (!strcasecmp("addIfDifferentNeighbor", value))
405 item->if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR;
406 else if (!strcasecmp("add", value))
407 item->if_exists = EXISTS_ADD;
408 else if (!strcasecmp("replace", value))
409 item->if_exists = EXISTS_REPLACE;
410 else if (!strcasecmp("doNothing", value))
411 item->if_exists = EXISTS_DO_NOTHING;
417 static int set_if_missing(struct conf_info *item, const char *value)
419 if (!strcasecmp("doNothing", value))
420 item->if_missing = MISSING_DO_NOTHING;
421 else if (!strcasecmp("add", value))
422 item->if_missing = MISSING_ADD;
428 static void duplicate_conf(struct conf_info *dst, struct conf_info *src)
432 dst->name = xstrdup(src->name);
434 dst->key = xstrdup(src->key);
436 dst->command = xstrdup(src->command);
439 static struct trailer_item *get_conf_item(const char *name)
441 struct trailer_item *item;
442 struct trailer_item *previous;
444 /* Look up item with same name */
445 for (previous = NULL, item = first_conf_item;
447 previous = item, item = item->next) {
448 if (!strcasecmp(item->conf.name, name))
452 /* Item does not already exists, create it */
453 item = xcalloc(sizeof(struct trailer_item), 1);
454 duplicate_conf(&item->conf, &default_conf_info);
455 item->conf.name = xstrdup(name);
458 first_conf_item = item;
460 previous->next = item;
461 item->previous = previous;
467 enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_WHERE,
468 TRAILER_IF_EXISTS, TRAILER_IF_MISSING };
472 enum trailer_info_type type;
473 } trailer_config_items[] = {
474 { "key", TRAILER_KEY },
475 { "command", TRAILER_COMMAND },
476 { "where", TRAILER_WHERE },
477 { "ifexists", TRAILER_IF_EXISTS },
478 { "ifmissing", TRAILER_IF_MISSING }
481 static int git_trailer_default_config(const char *conf_key, const char *value, void *cb)
483 const char *trailer_item, *variable_name;
485 if (!skip_prefix(conf_key, "trailer.", &trailer_item))
488 variable_name = strrchr(trailer_item, '.');
489 if (!variable_name) {
490 if (!strcmp(trailer_item, "where")) {
491 if (set_where(&default_conf_info, value) < 0)
492 warning(_("unknown value '%s' for key '%s'"),
494 } else if (!strcmp(trailer_item, "ifexists")) {
495 if (set_if_exists(&default_conf_info, value) < 0)
496 warning(_("unknown value '%s' for key '%s'"),
498 } else if (!strcmp(trailer_item, "ifmissing")) {
499 if (set_if_missing(&default_conf_info, value) < 0)
500 warning(_("unknown value '%s' for key '%s'"),
502 } else if (!strcmp(trailer_item, "separators")) {
503 separators = xstrdup(value);
509 static int git_trailer_config(const char *conf_key, const char *value, void *cb)
511 const char *trailer_item, *variable_name;
512 struct trailer_item *item;
513 struct conf_info *conf;
515 enum trailer_info_type type;
518 if (!skip_prefix(conf_key, "trailer.", &trailer_item))
521 variable_name = strrchr(trailer_item, '.');
526 for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) {
527 if (strcmp(trailer_config_items[i].name, variable_name))
529 name = xstrndup(trailer_item, variable_name - trailer_item - 1);
530 type = trailer_config_items[i].type;
537 item = get_conf_item(name);
544 warning(_("more than one %s"), conf_key);
545 conf->key = xstrdup(value);
547 case TRAILER_COMMAND:
549 warning(_("more than one %s"), conf_key);
550 conf->command = xstrdup(value);
553 if (set_where(conf, value))
554 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
556 case TRAILER_IF_EXISTS:
557 if (set_if_exists(conf, value))
558 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
560 case TRAILER_IF_MISSING:
561 if (set_if_missing(conf, value))
562 warning(_("unknown value '%s' for key '%s'"), value, conf_key);
565 die("internal bug in trailer.c");
570 static int parse_trailer(struct strbuf *tok, struct strbuf *val, const char *trailer)
573 struct strbuf seps = STRBUF_INIT;
574 strbuf_addstr(&seps, separators);
575 strbuf_addch(&seps, '=');
576 len = strcspn(trailer, seps.buf);
577 strbuf_release(&seps);
579 int l = strlen(trailer);
580 while (l > 0 && isspace(trailer[l - 1]))
582 return error(_("empty trailer token in trailer '%.*s'"), l, trailer);
584 if (len < strlen(trailer)) {
585 strbuf_add(tok, trailer, len);
587 strbuf_addstr(val, trailer + len + 1);
590 strbuf_addstr(tok, trailer);
596 static const char *token_from_item(struct trailer_item *item, char *tok)
599 return item->conf.key;
602 return item->conf.name;
605 static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,
606 char *tok, char *val)
608 struct trailer_item *new = xcalloc(sizeof(*new), 1);
609 new->value = val ? val : xstrdup("");
612 duplicate_conf(&new->conf, &conf_item->conf);
613 new->token = xstrdup(token_from_item(conf_item, tok));
616 duplicate_conf(&new->conf, &default_conf_info);
623 static int token_matches_item(const char *tok, struct trailer_item *item, int tok_len)
625 if (!strncasecmp(tok, item->conf.name, tok_len))
627 return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0;
630 static struct trailer_item *create_trailer_item(const char *string)
632 struct strbuf tok = STRBUF_INIT;
633 struct strbuf val = STRBUF_INIT;
634 struct trailer_item *item;
637 if (parse_trailer(&tok, &val, string))
640 tok_len = token_len_without_separator(tok.buf, tok.len);
642 /* Lookup if the token matches something in the config */
643 for (item = first_conf_item; item; item = item->next) {
644 if (token_matches_item(tok.buf, item, tok_len))
645 return new_trailer_item(item,
646 strbuf_detach(&tok, NULL),
647 strbuf_detach(&val, NULL));
650 return new_trailer_item(NULL,
651 strbuf_detach(&tok, NULL),
652 strbuf_detach(&val, NULL));
655 static void add_trailer_item(struct trailer_item **first,
656 struct trailer_item **last,
657 struct trailer_item *new)
666 new->previous = *last;
671 static struct trailer_item *process_command_line_args(struct string_list *trailers)
673 struct trailer_item *arg_tok_first = NULL;
674 struct trailer_item *arg_tok_last = NULL;
675 struct string_list_item *tr;
676 struct trailer_item *item;
678 /* Add a trailer item for each configured trailer with a command */
679 for (item = first_conf_item; item; item = item->next) {
680 if (item->conf.command) {
681 struct trailer_item *new = new_trailer_item(item, NULL, NULL);
682 add_trailer_item(&arg_tok_first, &arg_tok_last, new);
686 /* Add a trailer item for each trailer on the command line */
687 for_each_string_list_item(tr, trailers) {
688 struct trailer_item *new = create_trailer_item(tr->string);
689 add_trailer_item(&arg_tok_first, &arg_tok_last, new);
692 return arg_tok_first;
695 static struct strbuf **read_input_file(const char *file)
697 struct strbuf **lines;
698 struct strbuf sb = STRBUF_INIT;
701 if (strbuf_read_file(&sb, file, 0) < 0)
702 die_errno(_("could not read input file '%s'"), file);
704 if (strbuf_read(&sb, fileno(stdin), 0) < 0)
705 die_errno(_("could not read from stdin"));
708 lines = strbuf_split(&sb, '\n');
716 * Return the (0 based) index of the start of the patch or the line
717 * count if there is no patch in the message.
719 static int find_patch_start(struct strbuf **lines, int count)
723 /* Get the start of the patch part if any */
724 for (i = 0; i < count; i++) {
725 if (starts_with(lines[i]->buf, "---"))
733 * Return the (0 based) index of the first trailer line or count if
734 * there are no trailers. Trailers are searched only in the lines from
735 * index (count - 1) down to index 0.
737 static int find_trailer_start(struct strbuf **lines, int count)
739 int start, end_of_title, only_spaces = 1;
741 /* The first paragraph is the title and cannot be trailers */
742 for (start = 0; start < count; start++) {
743 if (lines[start]->buf[0] == comment_line_char)
745 if (contains_only_spaces(lines[start]->buf))
748 end_of_title = start;
751 * Get the start of the trailers by looking starting from the end
752 * for a line with only spaces before lines with one separator.
754 for (start = count - 1; start >= end_of_title; start--) {
755 if (lines[start]->buf[0] == comment_line_char)
757 if (contains_only_spaces(lines[start]->buf)) {
762 if (strcspn(lines[start]->buf, separators) < lines[start]->len) {
770 return only_spaces ? count : 0;
773 /* Get the index of the end of the trailers */
774 static int find_trailer_end(struct strbuf **lines, int patch_start)
776 struct strbuf sb = STRBUF_INIT;
779 for (i = 0; i < patch_start; i++)
780 strbuf_addbuf(&sb, lines[i]);
781 ignore_bytes = ignore_non_trailer(&sb);
783 for (i = patch_start - 1; i >= 0 && ignore_bytes > 0; i--)
784 ignore_bytes -= lines[i]->len;
789 static int has_blank_line_before(struct strbuf **lines, int start)
791 for (;start >= 0; start--) {
792 if (lines[start]->buf[0] == comment_line_char)
794 return contains_only_spaces(lines[start]->buf);
799 static void print_lines(FILE *outfile, struct strbuf **lines, int start, int end)
802 for (i = start; lines[i] && i < end; i++)
803 fprintf(outfile, "%s", lines[i]->buf);
806 static int process_input_file(FILE *outfile,
807 struct strbuf **lines,
808 struct trailer_item **in_tok_first,
809 struct trailer_item **in_tok_last)
812 int patch_start, trailer_start, trailer_end, i;
814 /* Get the line count */
818 patch_start = find_patch_start(lines, count);
819 trailer_end = find_trailer_end(lines, patch_start);
820 trailer_start = find_trailer_start(lines, trailer_end);
822 /* Print lines before the trailers as is */
823 print_lines(outfile, lines, 0, trailer_start);
825 if (!has_blank_line_before(lines, trailer_start - 1))
826 fprintf(outfile, "\n");
828 /* Parse trailer lines */
829 for (i = trailer_start; i < trailer_end; i++) {
830 if (lines[i]->buf[0] != comment_line_char) {
831 struct trailer_item *new = create_trailer_item(lines[i]->buf);
832 add_trailer_item(in_tok_first, in_tok_last, new);
839 static void free_all(struct trailer_item **first)
842 struct trailer_item *item = remove_first(first);
843 free_trailer_item(item);
847 static struct tempfile trailers_tempfile;
849 static FILE *create_in_place_tempfile(const char *file)
852 struct strbuf template = STRBUF_INIT;
857 die_errno(_("could not stat %s"), file);
858 if (!S_ISREG(st.st_mode))
859 die(_("file %s is not a regular file"), file);
860 if (!(st.st_mode & S_IWUSR))
861 die(_("file %s is not writable by user"), file);
863 /* Create temporary file in the same directory as the original */
864 tail = strrchr(file, '/');
866 strbuf_add(&template, file, tail - file + 1);
867 strbuf_addstr(&template, "git-interpret-trailers-XXXXXX");
869 xmks_tempfile_m(&trailers_tempfile, template.buf, st.st_mode);
870 strbuf_release(&template);
871 outfile = fdopen_tempfile(&trailers_tempfile, "w");
873 die_errno(_("could not open temporary file"));
878 void process_trailers(const char *file, int in_place, int trim_empty, struct string_list *trailers)
880 struct trailer_item *in_tok_first = NULL;
881 struct trailer_item *in_tok_last = NULL;
882 struct trailer_item *arg_tok_first;
883 struct strbuf **lines;
885 FILE *outfile = stdout;
887 /* Default config must be setup first */
888 git_config(git_trailer_default_config, NULL);
889 git_config(git_trailer_config, NULL);
891 lines = read_input_file(file);
894 outfile = create_in_place_tempfile(file);
896 /* Print the lines before the trailers */
897 trailer_end = process_input_file(outfile, lines, &in_tok_first, &in_tok_last);
899 arg_tok_first = process_command_line_args(trailers);
901 process_trailers_lists(&in_tok_first, &in_tok_last, &arg_tok_first);
903 print_all(outfile, in_tok_first, trim_empty);
905 free_all(&in_tok_first);
907 /* Print the lines after the trailers as is */
908 print_lines(outfile, lines, trailer_end, INT_MAX);
911 if (rename_tempfile(&trailers_tempfile, file))
912 die_errno(_("could not rename temporary file to %s"), file);
914 strbuf_list_free(lines);