2 #include "string-list.h"
3 #include "run-command.h"
4 #include "string-list.h"
7 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
10 enum action_where
{ WHERE_END
, WHERE_AFTER
, WHERE_BEFORE
, WHERE_START
};
11 enum action_if_exists
{ EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
, EXISTS_ADD_IF_DIFFERENT
,
12 EXISTS_ADD
, EXISTS_REPLACE
, EXISTS_DO_NOTHING
};
13 enum action_if_missing
{ MISSING_ADD
, MISSING_DO_NOTHING
};
19 enum action_where where
;
20 enum action_if_exists if_exists
;
21 enum action_if_missing if_missing
;
24 static struct conf_info default_conf_info
;
27 struct trailer_item
*previous
;
28 struct trailer_item
*next
;
31 struct conf_info conf
;
34 static struct trailer_item
*first_conf_item
;
36 static char *separators
= ":";
38 #define TRAILER_ARG_STRING "$ARG"
40 static int after_or_end(enum action_where where
)
42 return (where
== WHERE_AFTER
) || (where
== WHERE_END
);
46 * Return the length of the string not including any final
47 * punctuation. E.g., the input "Signed-off-by:" would return
48 * 13, stripping the trailing punctuation but retaining
49 * internal punctuation.
51 static size_t token_len_without_separator(const char *token
, size_t len
)
53 while (len
> 0 && !isalnum(token
[len
- 1]))
58 static int same_token(struct trailer_item
*a
, struct trailer_item
*b
)
60 size_t a_len
= token_len_without_separator(a
->token
, strlen(a
->token
));
61 size_t b_len
= token_len_without_separator(b
->token
, strlen(b
->token
));
62 size_t min_len
= (a_len
> b_len
) ? b_len
: a_len
;
64 return !strncasecmp(a
->token
, b
->token
, min_len
);
67 static int same_value(struct trailer_item
*a
, struct trailer_item
*b
)
69 return !strcasecmp(a
->value
, b
->value
);
72 static int same_trailer(struct trailer_item
*a
, struct trailer_item
*b
)
74 return same_token(a
, b
) && same_value(a
, b
);
77 static inline int contains_only_spaces(const char *str
)
80 while (*s
&& isspace(*s
))
85 static inline void strbuf_replace(struct strbuf
*sb
, const char *a
, const char *b
)
87 const char *ptr
= strstr(sb
->buf
, a
);
89 strbuf_splice(sb
, ptr
- sb
->buf
, strlen(a
), b
, strlen(b
));
92 static void free_trailer_item(struct trailer_item
*item
)
94 free(item
->conf
.name
);
96 free(item
->conf
.command
);
97 free((char *)item
->token
);
98 free((char *)item
->value
);
102 static char last_non_space_char(const char *s
)
105 for (i
= strlen(s
) - 1; i
>= 0; i
--)
111 static void print_tok_val(const char *tok
, const char *val
)
113 char c
= last_non_space_char(tok
);
116 if (strchr(separators
, c
))
117 printf("%s%s\n", tok
, val
);
119 printf("%s%c %s\n", tok
, separators
[0], val
);
122 static void print_all(struct trailer_item
*first
, int trim_empty
)
124 struct trailer_item
*item
;
125 for (item
= first
; item
; item
= item
->next
) {
126 if (!trim_empty
|| strlen(item
->value
) > 0)
127 print_tok_val(item
->token
, item
->value
);
131 static void update_last(struct trailer_item
**last
)
134 while ((*last
)->next
!= NULL
)
135 *last
= (*last
)->next
;
138 static void update_first(struct trailer_item
**first
)
141 while ((*first
)->previous
!= NULL
)
142 *first
= (*first
)->previous
;
145 static void add_arg_to_input_list(struct trailer_item
*on_tok
,
146 struct trailer_item
*arg_tok
,
147 struct trailer_item
**first
,
148 struct trailer_item
**last
)
150 if (after_or_end(arg_tok
->conf
.where
)) {
151 arg_tok
->next
= on_tok
->next
;
152 on_tok
->next
= arg_tok
;
153 arg_tok
->previous
= on_tok
;
155 arg_tok
->next
->previous
= arg_tok
;
158 arg_tok
->previous
= on_tok
->previous
;
159 on_tok
->previous
= arg_tok
;
160 arg_tok
->next
= on_tok
;
161 if (arg_tok
->previous
)
162 arg_tok
->previous
->next
= arg_tok
;
167 static int check_if_different(struct trailer_item
*in_tok
,
168 struct trailer_item
*arg_tok
,
171 enum action_where where
= arg_tok
->conf
.where
;
175 if (same_trailer(in_tok
, arg_tok
))
178 * if we want to add a trailer after another one,
179 * we have to check those before this one
181 in_tok
= after_or_end(where
) ? in_tok
->previous
: in_tok
->next
;
186 static void remove_from_list(struct trailer_item
*item
,
187 struct trailer_item
**first
,
188 struct trailer_item
**last
)
190 struct trailer_item
*next
= item
->next
;
191 struct trailer_item
*previous
= item
->previous
;
194 item
->next
->previous
= previous
;
200 item
->previous
->next
= next
;
201 item
->previous
= NULL
;
206 static struct trailer_item
*remove_first(struct trailer_item
**first
)
208 struct trailer_item
*item
= *first
;
211 item
->next
->previous
= NULL
;
217 static int read_from_command(struct child_process
*cp
, struct strbuf
*buf
)
220 return error("running trailer command '%s' failed", cp
->argv
[0]);
221 if (strbuf_read(buf
, cp
->out
, 1024) < 1)
222 return error("reading from trailer command '%s' failed", cp
->argv
[0]);
227 static const char *apply_command(const char *command
, const char *arg
)
229 struct strbuf cmd
= STRBUF_INIT
;
230 struct strbuf buf
= STRBUF_INIT
;
231 struct child_process cp
;
232 const char *argv
[] = {NULL
, NULL
};
235 strbuf_addstr(&cmd
, command
);
237 strbuf_replace(&cmd
, TRAILER_ARG_STRING
, arg
);
240 memset(&cp
, 0, sizeof(cp
));
242 cp
.env
= local_repo_env
;
247 if (read_from_command(&cp
, &buf
)) {
248 strbuf_release(&buf
);
249 result
= xstrdup("");
251 result
= strbuf_detach(&buf
, NULL
);
253 strbuf_release(&cmd
);
257 static void apply_item_command(struct trailer_item
*in_tok
, struct trailer_item
*arg_tok
)
259 if (arg_tok
->conf
.command
) {
261 if (arg_tok
->value
&& arg_tok
->value
[0]) {
262 arg
= arg_tok
->value
;
264 if (in_tok
&& in_tok
->value
)
265 arg
= xstrdup(in_tok
->value
);
269 arg_tok
->value
= apply_command(arg_tok
->conf
.command
, arg
);
274 static void apply_arg_if_exists(struct trailer_item
*in_tok
,
275 struct trailer_item
*arg_tok
,
276 struct trailer_item
*on_tok
,
277 struct trailer_item
**in_tok_first
,
278 struct trailer_item
**in_tok_last
)
280 switch (arg_tok
->conf
.if_exists
) {
281 case EXISTS_DO_NOTHING
:
282 free_trailer_item(arg_tok
);
285 apply_item_command(in_tok
, arg_tok
);
286 add_arg_to_input_list(on_tok
, arg_tok
,
287 in_tok_first
, in_tok_last
);
288 remove_from_list(in_tok
, in_tok_first
, in_tok_last
);
289 free_trailer_item(in_tok
);
292 apply_item_command(in_tok
, arg_tok
);
293 add_arg_to_input_list(on_tok
, arg_tok
,
294 in_tok_first
, in_tok_last
);
296 case EXISTS_ADD_IF_DIFFERENT
:
297 apply_item_command(in_tok
, arg_tok
);
298 if (check_if_different(in_tok
, arg_tok
, 1))
299 add_arg_to_input_list(on_tok
, arg_tok
,
300 in_tok_first
, in_tok_last
);
302 free_trailer_item(arg_tok
);
304 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
:
305 apply_item_command(in_tok
, arg_tok
);
306 if (check_if_different(on_tok
, arg_tok
, 0))
307 add_arg_to_input_list(on_tok
, arg_tok
,
308 in_tok_first
, in_tok_last
);
310 free_trailer_item(arg_tok
);
315 static void apply_arg_if_missing(struct trailer_item
**in_tok_first
,
316 struct trailer_item
**in_tok_last
,
317 struct trailer_item
*arg_tok
)
319 struct trailer_item
**in_tok
;
320 enum action_where where
;
322 switch (arg_tok
->conf
.if_missing
) {
323 case MISSING_DO_NOTHING
:
324 free_trailer_item(arg_tok
);
327 where
= arg_tok
->conf
.where
;
328 in_tok
= after_or_end(where
) ? in_tok_last
: in_tok_first
;
329 apply_item_command(NULL
, arg_tok
);
331 add_arg_to_input_list(*in_tok
, arg_tok
,
332 in_tok_first
, in_tok_last
);
334 *in_tok_first
= arg_tok
;
335 *in_tok_last
= arg_tok
;
341 static int find_same_and_apply_arg(struct trailer_item
**in_tok_first
,
342 struct trailer_item
**in_tok_last
,
343 struct trailer_item
*arg_tok
)
345 struct trailer_item
*in_tok
;
346 struct trailer_item
*on_tok
;
347 struct trailer_item
*following_tok
;
349 enum action_where where
= arg_tok
->conf
.where
;
350 int middle
= (where
== WHERE_AFTER
) || (where
== WHERE_BEFORE
);
351 int backwards
= after_or_end(where
);
352 struct trailer_item
*start_tok
= backwards
? *in_tok_last
: *in_tok_first
;
354 for (in_tok
= start_tok
; in_tok
; in_tok
= following_tok
) {
355 following_tok
= backwards
? in_tok
->previous
: in_tok
->next
;
356 if (!same_token(in_tok
, arg_tok
))
358 on_tok
= middle
? in_tok
: start_tok
;
359 apply_arg_if_exists(in_tok
, arg_tok
, on_tok
,
360 in_tok_first
, in_tok_last
);
366 static void process_trailers_lists(struct trailer_item
**in_tok_first
,
367 struct trailer_item
**in_tok_last
,
368 struct trailer_item
**arg_tok_first
)
370 struct trailer_item
*arg_tok
;
371 struct trailer_item
*next_arg
;
376 for (arg_tok
= *arg_tok_first
; arg_tok
; arg_tok
= next_arg
) {
379 next_arg
= arg_tok
->next
;
380 remove_from_list(arg_tok
, arg_tok_first
, NULL
);
382 applied
= find_same_and_apply_arg(in_tok_first
,
387 apply_arg_if_missing(in_tok_first
,
393 static int set_where(struct conf_info
*item
, const char *value
)
395 if (!strcasecmp("after", value
))
396 item
->where
= WHERE_AFTER
;
397 else if (!strcasecmp("before", value
))
398 item
->where
= WHERE_BEFORE
;
399 else if (!strcasecmp("end", value
))
400 item
->where
= WHERE_END
;
401 else if (!strcasecmp("start", value
))
402 item
->where
= WHERE_START
;
408 static int set_if_exists(struct conf_info
*item
, const char *value
)
410 if (!strcasecmp("addIfDifferent", value
))
411 item
->if_exists
= EXISTS_ADD_IF_DIFFERENT
;
412 else if (!strcasecmp("addIfDifferentNeighbor", value
))
413 item
->if_exists
= EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
;
414 else if (!strcasecmp("add", value
))
415 item
->if_exists
= EXISTS_ADD
;
416 else if (!strcasecmp("replace", value
))
417 item
->if_exists
= EXISTS_REPLACE
;
418 else if (!strcasecmp("doNothing", value
))
419 item
->if_exists
= EXISTS_DO_NOTHING
;
425 static int set_if_missing(struct conf_info
*item
, const char *value
)
427 if (!strcasecmp("doNothing", value
))
428 item
->if_missing
= MISSING_DO_NOTHING
;
429 else if (!strcasecmp("add", value
))
430 item
->if_missing
= MISSING_ADD
;
436 static void duplicate_conf(struct conf_info
*dst
, struct conf_info
*src
)
440 dst
->name
= xstrdup(src
->name
);
442 dst
->key
= xstrdup(src
->key
);
444 dst
->command
= xstrdup(src
->command
);
447 static struct trailer_item
*get_conf_item(const char *name
)
449 struct trailer_item
*item
;
450 struct trailer_item
*previous
;
452 /* Look up item with same name */
453 for (previous
= NULL
, item
= first_conf_item
;
455 previous
= item
, item
= item
->next
) {
456 if (!strcasecmp(item
->conf
.name
, name
))
460 /* Item does not already exists, create it */
461 item
= xcalloc(sizeof(struct trailer_item
), 1);
462 duplicate_conf(&item
->conf
, &default_conf_info
);
463 item
->conf
.name
= xstrdup(name
);
466 first_conf_item
= item
;
468 previous
->next
= item
;
469 item
->previous
= previous
;
475 enum trailer_info_type
{ TRAILER_KEY
, TRAILER_COMMAND
, TRAILER_WHERE
,
476 TRAILER_IF_EXISTS
, TRAILER_IF_MISSING
};
480 enum trailer_info_type type
;
481 } trailer_config_items
[] = {
482 { "key", TRAILER_KEY
},
483 { "command", TRAILER_COMMAND
},
484 { "where", TRAILER_WHERE
},
485 { "ifexists", TRAILER_IF_EXISTS
},
486 { "ifmissing", TRAILER_IF_MISSING
}
489 static int git_trailer_default_config(const char *conf_key
, const char *value
, void *cb
)
491 const char *trailer_item
, *variable_name
;
493 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
496 variable_name
= strrchr(trailer_item
, '.');
497 if (!variable_name
) {
498 if (!strcmp(trailer_item
, "where")) {
499 if (set_where(&default_conf_info
, value
) < 0)
500 warning(_("unknown value '%s' for key '%s'"),
502 } else if (!strcmp(trailer_item
, "ifexists")) {
503 if (set_if_exists(&default_conf_info
, value
) < 0)
504 warning(_("unknown value '%s' for key '%s'"),
506 } else if (!strcmp(trailer_item
, "ifmissing")) {
507 if (set_if_missing(&default_conf_info
, value
) < 0)
508 warning(_("unknown value '%s' for key '%s'"),
510 } else if (!strcmp(trailer_item
, "separators")) {
511 separators
= xstrdup(value
);
517 static int git_trailer_config(const char *conf_key
, const char *value
, void *cb
)
519 const char *trailer_item
, *variable_name
;
520 struct trailer_item
*item
;
521 struct conf_info
*conf
;
523 enum trailer_info_type type
;
526 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
529 variable_name
= strrchr(trailer_item
, '.');
534 for (i
= 0; i
< ARRAY_SIZE(trailer_config_items
); i
++) {
535 if (strcmp(trailer_config_items
[i
].name
, variable_name
))
537 name
= xstrndup(trailer_item
, variable_name
- trailer_item
- 1);
538 type
= trailer_config_items
[i
].type
;
545 item
= get_conf_item(name
);
552 warning(_("more than one %s"), conf_key
);
553 conf
->key
= xstrdup(value
);
555 case TRAILER_COMMAND
:
557 warning(_("more than one %s"), conf_key
);
558 conf
->command
= xstrdup(value
);
561 if (set_where(conf
, value
))
562 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
564 case TRAILER_IF_EXISTS
:
565 if (set_if_exists(conf
, value
))
566 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
568 case TRAILER_IF_MISSING
:
569 if (set_if_missing(conf
, value
))
570 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
573 die("internal bug in trailer.c");
578 static int parse_trailer(struct strbuf
*tok
, struct strbuf
*val
, const char *trailer
)
581 struct strbuf seps
= STRBUF_INIT
;
582 strbuf_addstr(&seps
, separators
);
583 strbuf_addch(&seps
, '=');
584 len
= strcspn(trailer
, seps
.buf
);
585 strbuf_release(&seps
);
587 return error(_("empty trailer token in trailer '%s'"), trailer
);
588 if (len
< strlen(trailer
)) {
589 strbuf_add(tok
, trailer
, len
);
591 strbuf_addstr(val
, trailer
+ len
+ 1);
594 strbuf_addstr(tok
, trailer
);
600 static const char *token_from_item(struct trailer_item
*item
, char *tok
)
603 return item
->conf
.key
;
606 return item
->conf
.name
;
609 static struct trailer_item
*new_trailer_item(struct trailer_item
*conf_item
,
610 char *tok
, char *val
)
612 struct trailer_item
*new = xcalloc(sizeof(*new), 1);
613 new->value
= val
? val
: xstrdup("");
616 duplicate_conf(&new->conf
, &conf_item
->conf
);
617 new->token
= xstrdup(token_from_item(conf_item
, tok
));
620 duplicate_conf(&new->conf
, &default_conf_info
);
627 static int token_matches_item(const char *tok
, struct trailer_item
*item
, int tok_len
)
629 if (!strncasecmp(tok
, item
->conf
.name
, tok_len
))
631 return item
->conf
.key
? !strncasecmp(tok
, item
->conf
.key
, tok_len
) : 0;
634 static struct trailer_item
*create_trailer_item(const char *string
)
636 struct strbuf tok
= STRBUF_INIT
;
637 struct strbuf val
= STRBUF_INIT
;
638 struct trailer_item
*item
;
641 if (parse_trailer(&tok
, &val
, string
))
644 tok_len
= token_len_without_separator(tok
.buf
, tok
.len
);
646 /* Lookup if the token matches something in the config */
647 for (item
= first_conf_item
; item
; item
= item
->next
) {
648 if (token_matches_item(tok
.buf
, item
, tok_len
))
649 return new_trailer_item(item
,
650 strbuf_detach(&tok
, NULL
),
651 strbuf_detach(&val
, NULL
));
654 return new_trailer_item(NULL
,
655 strbuf_detach(&tok
, NULL
),
656 strbuf_detach(&val
, NULL
));
659 static void add_trailer_item(struct trailer_item
**first
,
660 struct trailer_item
**last
,
661 struct trailer_item
*new)
670 new->previous
= *last
;
675 static struct trailer_item
*process_command_line_args(struct string_list
*trailers
)
677 struct trailer_item
*arg_tok_first
= NULL
;
678 struct trailer_item
*arg_tok_last
= NULL
;
679 struct string_list_item
*tr
;
680 struct trailer_item
*item
;
682 /* Add a trailer item for each configured trailer with a command */
683 for (item
= first_conf_item
; item
; item
= item
->next
) {
684 if (item
->conf
.command
) {
685 struct trailer_item
*new = new_trailer_item(item
, NULL
, NULL
);
686 add_trailer_item(&arg_tok_first
, &arg_tok_last
, new);
690 /* Add a trailer item for each trailer on the command line */
691 for_each_string_list_item(tr
, trailers
) {
692 struct trailer_item
*new = create_trailer_item(tr
->string
);
693 add_trailer_item(&arg_tok_first
, &arg_tok_last
, new);
696 return arg_tok_first
;
699 static struct strbuf
**read_input_file(const char *file
)
701 struct strbuf
**lines
;
702 struct strbuf sb
= STRBUF_INIT
;
705 if (strbuf_read_file(&sb
, file
, 0) < 0)
706 die_errno(_("could not read input file '%s'"), file
);
708 if (strbuf_read(&sb
, fileno(stdin
), 0) < 0)
709 die_errno(_("could not read from stdin"));
712 lines
= strbuf_split(&sb
, '\n');
720 * Return the (0 based) index of the start of the patch or the line
721 * count if there is no patch in the message.
723 static int find_patch_start(struct strbuf
**lines
, int count
)
727 /* Get the start of the patch part if any */
728 for (i
= 0; i
< count
; i
++) {
729 if (starts_with(lines
[i
]->buf
, "---"))
737 * Return the (0 based) index of the first trailer line or count if
738 * there are no trailers. Trailers are searched only in the lines from
739 * index (count - 1) down to index 0.
741 static int find_trailer_start(struct strbuf
**lines
, int count
)
743 int start
, only_spaces
= 1;
746 * Get the start of the trailers by looking starting from the end
747 * for a line with only spaces before lines with one separator.
749 for (start
= count
- 1; start
>= 0; start
--) {
750 if (lines
[start
]->buf
[0] == comment_line_char
)
752 if (contains_only_spaces(lines
[start
]->buf
)) {
757 if (strcspn(lines
[start
]->buf
, separators
) < lines
[start
]->len
) {
765 return only_spaces
? count
: 0;
768 static int has_blank_line_before(struct strbuf
**lines
, int start
)
770 for (;start
>= 0; start
--) {
771 if (lines
[start
]->buf
[0] == comment_line_char
)
773 return contains_only_spaces(lines
[start
]->buf
);
778 static void print_lines(struct strbuf
**lines
, int start
, int end
)
781 for (i
= start
; lines
[i
] && i
< end
; i
++)
782 printf("%s", lines
[i
]->buf
);
785 static int process_input_file(struct strbuf
**lines
,
786 struct trailer_item
**in_tok_first
,
787 struct trailer_item
**in_tok_last
)
790 int patch_start
, trailer_start
, i
;
792 /* Get the line count */
796 patch_start
= find_patch_start(lines
, count
);
797 trailer_start
= find_trailer_start(lines
, patch_start
);
799 /* Print lines before the trailers as is */
800 print_lines(lines
, 0, trailer_start
);
802 if (!has_blank_line_before(lines
, trailer_start
- 1))
805 /* Parse trailer lines */
806 for (i
= trailer_start
; i
< patch_start
; i
++) {
807 struct trailer_item
*new = create_trailer_item(lines
[i
]->buf
);
808 add_trailer_item(in_tok_first
, in_tok_last
, new);
814 static void free_all(struct trailer_item
**first
)
817 struct trailer_item
*item
= remove_first(first
);
818 free_trailer_item(item
);
822 void process_trailers(const char *file
, int trim_empty
, struct string_list
*trailers
)
824 struct trailer_item
*in_tok_first
= NULL
;
825 struct trailer_item
*in_tok_last
= NULL
;
826 struct trailer_item
*arg_tok_first
;
827 struct strbuf
**lines
;
830 /* Default config must be setup first */
831 git_config(git_trailer_default_config
, NULL
);
832 git_config(git_trailer_config
, NULL
);
834 lines
= read_input_file(file
);
836 /* Print the lines before the trailers */
837 patch_start
= process_input_file(lines
, &in_tok_first
, &in_tok_last
);
839 arg_tok_first
= process_command_line_args(trailers
);
841 process_trailers_lists(&in_tok_first
, &in_tok_last
, &arg_tok_first
);
843 print_all(in_tok_first
, trim_empty
);
845 free_all(&in_tok_first
);
847 /* Print the lines after the trailers as is */
848 print_lines(lines
, patch_start
, INT_MAX
);
850 strbuf_list_free(lines
);