2 #include "string-list.h"
3 #include "run-command.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 const char *apply_command(const char *command
, const char *arg
)
219 struct strbuf cmd
= STRBUF_INIT
;
220 struct strbuf buf
= STRBUF_INIT
;
221 struct child_process cp
= CHILD_PROCESS_INIT
;
222 const char *argv
[] = {NULL
, NULL
};
225 strbuf_addstr(&cmd
, command
);
227 strbuf_replace(&cmd
, TRAILER_ARG_STRING
, arg
);
231 cp
.env
= local_repo_env
;
235 if (capture_command(&cp
, &buf
, 1024)) {
236 error("running trailer command '%s' failed", cmd
.buf
);
237 strbuf_release(&buf
);
238 result
= xstrdup("");
241 result
= strbuf_detach(&buf
, NULL
);
244 strbuf_release(&cmd
);
248 static void apply_item_command(struct trailer_item
*in_tok
, struct trailer_item
*arg_tok
)
250 if (arg_tok
->conf
.command
) {
252 if (arg_tok
->value
&& arg_tok
->value
[0]) {
253 arg
= arg_tok
->value
;
255 if (in_tok
&& in_tok
->value
)
256 arg
= xstrdup(in_tok
->value
);
260 arg_tok
->value
= apply_command(arg_tok
->conf
.command
, arg
);
265 static void apply_arg_if_exists(struct trailer_item
*in_tok
,
266 struct trailer_item
*arg_tok
,
267 struct trailer_item
*on_tok
,
268 struct trailer_item
**in_tok_first
,
269 struct trailer_item
**in_tok_last
)
271 switch (arg_tok
->conf
.if_exists
) {
272 case EXISTS_DO_NOTHING
:
273 free_trailer_item(arg_tok
);
276 apply_item_command(in_tok
, arg_tok
);
277 add_arg_to_input_list(on_tok
, arg_tok
,
278 in_tok_first
, in_tok_last
);
279 remove_from_list(in_tok
, in_tok_first
, in_tok_last
);
280 free_trailer_item(in_tok
);
283 apply_item_command(in_tok
, arg_tok
);
284 add_arg_to_input_list(on_tok
, arg_tok
,
285 in_tok_first
, in_tok_last
);
287 case EXISTS_ADD_IF_DIFFERENT
:
288 apply_item_command(in_tok
, arg_tok
);
289 if (check_if_different(in_tok
, arg_tok
, 1))
290 add_arg_to_input_list(on_tok
, arg_tok
,
291 in_tok_first
, in_tok_last
);
293 free_trailer_item(arg_tok
);
295 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
:
296 apply_item_command(in_tok
, arg_tok
);
297 if (check_if_different(on_tok
, arg_tok
, 0))
298 add_arg_to_input_list(on_tok
, arg_tok
,
299 in_tok_first
, in_tok_last
);
301 free_trailer_item(arg_tok
);
306 static void apply_arg_if_missing(struct trailer_item
**in_tok_first
,
307 struct trailer_item
**in_tok_last
,
308 struct trailer_item
*arg_tok
)
310 struct trailer_item
**in_tok
;
311 enum action_where where
;
313 switch (arg_tok
->conf
.if_missing
) {
314 case MISSING_DO_NOTHING
:
315 free_trailer_item(arg_tok
);
318 where
= arg_tok
->conf
.where
;
319 in_tok
= after_or_end(where
) ? in_tok_last
: in_tok_first
;
320 apply_item_command(NULL
, arg_tok
);
322 add_arg_to_input_list(*in_tok
, arg_tok
,
323 in_tok_first
, in_tok_last
);
325 *in_tok_first
= arg_tok
;
326 *in_tok_last
= arg_tok
;
332 static int find_same_and_apply_arg(struct trailer_item
**in_tok_first
,
333 struct trailer_item
**in_tok_last
,
334 struct trailer_item
*arg_tok
)
336 struct trailer_item
*in_tok
;
337 struct trailer_item
*on_tok
;
338 struct trailer_item
*following_tok
;
340 enum action_where where
= arg_tok
->conf
.where
;
341 int middle
= (where
== WHERE_AFTER
) || (where
== WHERE_BEFORE
);
342 int backwards
= after_or_end(where
);
343 struct trailer_item
*start_tok
= backwards
? *in_tok_last
: *in_tok_first
;
345 for (in_tok
= start_tok
; in_tok
; in_tok
= following_tok
) {
346 following_tok
= backwards
? in_tok
->previous
: in_tok
->next
;
347 if (!same_token(in_tok
, arg_tok
))
349 on_tok
= middle
? in_tok
: start_tok
;
350 apply_arg_if_exists(in_tok
, arg_tok
, on_tok
,
351 in_tok_first
, in_tok_last
);
357 static void process_trailers_lists(struct trailer_item
**in_tok_first
,
358 struct trailer_item
**in_tok_last
,
359 struct trailer_item
**arg_tok_first
)
361 struct trailer_item
*arg_tok
;
362 struct trailer_item
*next_arg
;
367 for (arg_tok
= *arg_tok_first
; arg_tok
; arg_tok
= next_arg
) {
370 next_arg
= arg_tok
->next
;
371 remove_from_list(arg_tok
, arg_tok_first
, NULL
);
373 applied
= find_same_and_apply_arg(in_tok_first
,
378 apply_arg_if_missing(in_tok_first
,
384 static int set_where(struct conf_info
*item
, const char *value
)
386 if (!strcasecmp("after", value
))
387 item
->where
= WHERE_AFTER
;
388 else if (!strcasecmp("before", value
))
389 item
->where
= WHERE_BEFORE
;
390 else if (!strcasecmp("end", value
))
391 item
->where
= WHERE_END
;
392 else if (!strcasecmp("start", value
))
393 item
->where
= WHERE_START
;
399 static int set_if_exists(struct conf_info
*item
, const char *value
)
401 if (!strcasecmp("addIfDifferent", value
))
402 item
->if_exists
= EXISTS_ADD_IF_DIFFERENT
;
403 else if (!strcasecmp("addIfDifferentNeighbor", value
))
404 item
->if_exists
= EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
;
405 else if (!strcasecmp("add", value
))
406 item
->if_exists
= EXISTS_ADD
;
407 else if (!strcasecmp("replace", value
))
408 item
->if_exists
= EXISTS_REPLACE
;
409 else if (!strcasecmp("doNothing", value
))
410 item
->if_exists
= EXISTS_DO_NOTHING
;
416 static int set_if_missing(struct conf_info
*item
, const char *value
)
418 if (!strcasecmp("doNothing", value
))
419 item
->if_missing
= MISSING_DO_NOTHING
;
420 else if (!strcasecmp("add", value
))
421 item
->if_missing
= MISSING_ADD
;
427 static void duplicate_conf(struct conf_info
*dst
, struct conf_info
*src
)
431 dst
->name
= xstrdup(src
->name
);
433 dst
->key
= xstrdup(src
->key
);
435 dst
->command
= xstrdup(src
->command
);
438 static struct trailer_item
*get_conf_item(const char *name
)
440 struct trailer_item
*item
;
441 struct trailer_item
*previous
;
443 /* Look up item with same name */
444 for (previous
= NULL
, item
= first_conf_item
;
446 previous
= item
, item
= item
->next
) {
447 if (!strcasecmp(item
->conf
.name
, name
))
451 /* Item does not already exists, create it */
452 item
= xcalloc(sizeof(struct trailer_item
), 1);
453 duplicate_conf(&item
->conf
, &default_conf_info
);
454 item
->conf
.name
= xstrdup(name
);
457 first_conf_item
= item
;
459 previous
->next
= item
;
460 item
->previous
= previous
;
466 enum trailer_info_type
{ TRAILER_KEY
, TRAILER_COMMAND
, TRAILER_WHERE
,
467 TRAILER_IF_EXISTS
, TRAILER_IF_MISSING
};
471 enum trailer_info_type type
;
472 } trailer_config_items
[] = {
473 { "key", TRAILER_KEY
},
474 { "command", TRAILER_COMMAND
},
475 { "where", TRAILER_WHERE
},
476 { "ifexists", TRAILER_IF_EXISTS
},
477 { "ifmissing", TRAILER_IF_MISSING
}
480 static int git_trailer_default_config(const char *conf_key
, const char *value
, void *cb
)
482 const char *trailer_item
, *variable_name
;
484 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
487 variable_name
= strrchr(trailer_item
, '.');
488 if (!variable_name
) {
489 if (!strcmp(trailer_item
, "where")) {
490 if (set_where(&default_conf_info
, value
) < 0)
491 warning(_("unknown value '%s' for key '%s'"),
493 } else if (!strcmp(trailer_item
, "ifexists")) {
494 if (set_if_exists(&default_conf_info
, value
) < 0)
495 warning(_("unknown value '%s' for key '%s'"),
497 } else if (!strcmp(trailer_item
, "ifmissing")) {
498 if (set_if_missing(&default_conf_info
, value
) < 0)
499 warning(_("unknown value '%s' for key '%s'"),
501 } else if (!strcmp(trailer_item
, "separators")) {
502 separators
= xstrdup(value
);
508 static int git_trailer_config(const char *conf_key
, const char *value
, void *cb
)
510 const char *trailer_item
, *variable_name
;
511 struct trailer_item
*item
;
512 struct conf_info
*conf
;
514 enum trailer_info_type type
;
517 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
520 variable_name
= strrchr(trailer_item
, '.');
525 for (i
= 0; i
< ARRAY_SIZE(trailer_config_items
); i
++) {
526 if (strcmp(trailer_config_items
[i
].name
, variable_name
))
528 name
= xstrndup(trailer_item
, variable_name
- trailer_item
- 1);
529 type
= trailer_config_items
[i
].type
;
536 item
= get_conf_item(name
);
543 warning(_("more than one %s"), conf_key
);
544 conf
->key
= xstrdup(value
);
546 case TRAILER_COMMAND
:
548 warning(_("more than one %s"), conf_key
);
549 conf
->command
= xstrdup(value
);
552 if (set_where(conf
, value
))
553 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
555 case TRAILER_IF_EXISTS
:
556 if (set_if_exists(conf
, value
))
557 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
559 case TRAILER_IF_MISSING
:
560 if (set_if_missing(conf
, value
))
561 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
564 die("internal bug in trailer.c");
569 static int parse_trailer(struct strbuf
*tok
, struct strbuf
*val
, const char *trailer
)
572 struct strbuf seps
= STRBUF_INIT
;
573 strbuf_addstr(&seps
, separators
);
574 strbuf_addch(&seps
, '=');
575 len
= strcspn(trailer
, seps
.buf
);
576 strbuf_release(&seps
);
578 int l
= strlen(trailer
);
579 while (l
> 0 && isspace(trailer
[l
- 1]))
581 return error(_("empty trailer token in trailer '%.*s'"), l
, trailer
);
583 if (len
< strlen(trailer
)) {
584 strbuf_add(tok
, trailer
, len
);
586 strbuf_addstr(val
, trailer
+ len
+ 1);
589 strbuf_addstr(tok
, trailer
);
595 static const char *token_from_item(struct trailer_item
*item
, char *tok
)
598 return item
->conf
.key
;
601 return item
->conf
.name
;
604 static struct trailer_item
*new_trailer_item(struct trailer_item
*conf_item
,
605 char *tok
, char *val
)
607 struct trailer_item
*new = xcalloc(sizeof(*new), 1);
608 new->value
= val
? val
: xstrdup("");
611 duplicate_conf(&new->conf
, &conf_item
->conf
);
612 new->token
= xstrdup(token_from_item(conf_item
, tok
));
615 duplicate_conf(&new->conf
, &default_conf_info
);
622 static int token_matches_item(const char *tok
, struct trailer_item
*item
, int tok_len
)
624 if (!strncasecmp(tok
, item
->conf
.name
, tok_len
))
626 return item
->conf
.key
? !strncasecmp(tok
, item
->conf
.key
, tok_len
) : 0;
629 static struct trailer_item
*create_trailer_item(const char *string
)
631 struct strbuf tok
= STRBUF_INIT
;
632 struct strbuf val
= STRBUF_INIT
;
633 struct trailer_item
*item
;
636 if (parse_trailer(&tok
, &val
, string
))
639 tok_len
= token_len_without_separator(tok
.buf
, tok
.len
);
641 /* Lookup if the token matches something in the config */
642 for (item
= first_conf_item
; item
; item
= item
->next
) {
643 if (token_matches_item(tok
.buf
, item
, tok_len
))
644 return new_trailer_item(item
,
645 strbuf_detach(&tok
, NULL
),
646 strbuf_detach(&val
, NULL
));
649 return new_trailer_item(NULL
,
650 strbuf_detach(&tok
, NULL
),
651 strbuf_detach(&val
, NULL
));
654 static void add_trailer_item(struct trailer_item
**first
,
655 struct trailer_item
**last
,
656 struct trailer_item
*new)
665 new->previous
= *last
;
670 static struct trailer_item
*process_command_line_args(struct string_list
*trailers
)
672 struct trailer_item
*arg_tok_first
= NULL
;
673 struct trailer_item
*arg_tok_last
= NULL
;
674 struct string_list_item
*tr
;
675 struct trailer_item
*item
;
677 /* Add a trailer item for each configured trailer with a command */
678 for (item
= first_conf_item
; item
; item
= item
->next
) {
679 if (item
->conf
.command
) {
680 struct trailer_item
*new = new_trailer_item(item
, NULL
, NULL
);
681 add_trailer_item(&arg_tok_first
, &arg_tok_last
, new);
685 /* Add a trailer item for each trailer on the command line */
686 for_each_string_list_item(tr
, trailers
) {
687 struct trailer_item
*new = create_trailer_item(tr
->string
);
688 add_trailer_item(&arg_tok_first
, &arg_tok_last
, new);
691 return arg_tok_first
;
694 static struct strbuf
**read_input_file(const char *file
)
696 struct strbuf
**lines
;
697 struct strbuf sb
= STRBUF_INIT
;
700 if (strbuf_read_file(&sb
, file
, 0) < 0)
701 die_errno(_("could not read input file '%s'"), file
);
703 if (strbuf_read(&sb
, fileno(stdin
), 0) < 0)
704 die_errno(_("could not read from stdin"));
707 lines
= strbuf_split(&sb
, '\n');
715 * Return the (0 based) index of the start of the patch or the line
716 * count if there is no patch in the message.
718 static int find_patch_start(struct strbuf
**lines
, int count
)
722 /* Get the start of the patch part if any */
723 for (i
= 0; i
< count
; i
++) {
724 if (starts_with(lines
[i
]->buf
, "---"))
732 * Return the (0 based) index of the first trailer line or count if
733 * there are no trailers. Trailers are searched only in the lines from
734 * index (count - 1) down to index 0.
736 static int find_trailer_start(struct strbuf
**lines
, int count
)
738 int start
, only_spaces
= 1;
741 * Get the start of the trailers by looking starting from the end
742 * for a line with only spaces before lines with one separator.
743 * The first line must not be analyzed as the others as it
744 * should be either the message title or a blank line.
746 for (start
= count
- 1; start
>= 1; start
--) {
747 if (lines
[start
]->buf
[0] == comment_line_char
)
749 if (contains_only_spaces(lines
[start
]->buf
)) {
754 if (strcspn(lines
[start
]->buf
, separators
) < lines
[start
]->len
) {
762 return only_spaces
? count
: 0;
765 /* Get the index of the end of the trailers */
766 static int find_trailer_end(struct strbuf
**lines
, int patch_start
)
768 struct strbuf sb
= STRBUF_INIT
;
771 for (i
= 0; i
< patch_start
; i
++)
772 strbuf_addbuf(&sb
, lines
[i
]);
773 ignore_bytes
= ignore_non_trailer(&sb
);
775 for (i
= patch_start
- 1; i
>= 0 && ignore_bytes
> 0; i
--)
776 ignore_bytes
-= lines
[i
]->len
;
781 static int has_blank_line_before(struct strbuf
**lines
, int start
)
783 for (;start
>= 0; start
--) {
784 if (lines
[start
]->buf
[0] == comment_line_char
)
786 return contains_only_spaces(lines
[start
]->buf
);
791 static void print_lines(struct strbuf
**lines
, int start
, int end
)
794 for (i
= start
; lines
[i
] && i
< end
; i
++)
795 printf("%s", lines
[i
]->buf
);
798 static int process_input_file(struct strbuf
**lines
,
799 struct trailer_item
**in_tok_first
,
800 struct trailer_item
**in_tok_last
)
803 int patch_start
, trailer_start
, trailer_end
, i
;
805 /* Get the line count */
809 patch_start
= find_patch_start(lines
, count
);
810 trailer_end
= find_trailer_end(lines
, patch_start
);
811 trailer_start
= find_trailer_start(lines
, trailer_end
);
813 /* Print lines before the trailers as is */
814 print_lines(lines
, 0, trailer_start
);
816 if (!has_blank_line_before(lines
, trailer_start
- 1))
819 /* Parse trailer lines */
820 for (i
= trailer_start
; i
< trailer_end
; i
++) {
821 if (lines
[i
]->buf
[0] != comment_line_char
) {
822 struct trailer_item
*new = create_trailer_item(lines
[i
]->buf
);
823 add_trailer_item(in_tok_first
, in_tok_last
, new);
830 static void free_all(struct trailer_item
**first
)
833 struct trailer_item
*item
= remove_first(first
);
834 free_trailer_item(item
);
838 void process_trailers(const char *file
, int trim_empty
, struct string_list
*trailers
)
840 struct trailer_item
*in_tok_first
= NULL
;
841 struct trailer_item
*in_tok_last
= NULL
;
842 struct trailer_item
*arg_tok_first
;
843 struct strbuf
**lines
;
846 /* Default config must be setup first */
847 git_config(git_trailer_default_config
, NULL
);
848 git_config(git_trailer_config
, NULL
);
850 lines
= read_input_file(file
);
852 /* Print the lines before the trailers */
853 trailer_end
= process_input_file(lines
, &in_tok_first
, &in_tok_last
);
855 arg_tok_first
= process_command_line_args(trailers
);
857 process_trailers_lists(&in_tok_first
, &in_tok_last
, &arg_tok_first
);
859 print_all(in_tok_first
, trim_empty
);
861 free_all(&in_tok_first
);
863 /* Print the lines after the trailers as is */
864 print_lines(lines
, trailer_end
, INT_MAX
);
866 strbuf_list_free(lines
);