1 #include "git-compat-util.h"
3 #include "environment.h"
5 #include "string-list.h"
6 #include "run-command.h"
11 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
16 * True if there is a blank line before the location pointed to by
17 * trailer_block_start.
19 int blank_line_before_trailer
;
22 * Offsets to the trailer block start and end positions in the input
23 * string. If no trailer block is found, these are both set to the
24 * "true" end of the input (find_end_of_log_message()).
26 size_t trailer_block_start
, trailer_block_end
;
29 * Array of trailers found.
40 enum trailer_where where
;
41 enum trailer_if_exists if_exists
;
42 enum trailer_if_missing if_missing
;
45 static struct conf_info default_conf_info
;
48 struct list_head list
;
50 * If this is not a trailer line, the line is stored in value
51 * (excluding the terminating newline) and token is NULL.
58 struct list_head list
;
61 struct conf_info conf
;
64 static LIST_HEAD(conf_head
);
66 static char *separators
= ":";
68 static int configured
;
70 #define TRAILER_ARG_STRING "$ARG"
72 static const char *git_generated_prefixes
[] = {
74 "(cherry picked from commit ",
78 /* Iterate over the elements of the list. */
79 #define list_for_each_dir(pos, head, is_reverse) \
80 for (pos = is_reverse ? (head)->prev : (head)->next; \
82 pos = is_reverse ? pos->prev : pos->next)
84 static int after_or_end(enum trailer_where where
)
86 return (where
== WHERE_AFTER
) || (where
== WHERE_END
);
90 * Return the length of the string not including any final
91 * punctuation. E.g., the input "Signed-off-by:" would return
92 * 13, stripping the trailing punctuation but retaining
93 * internal punctuation.
95 static size_t token_len_without_separator(const char *token
, size_t len
)
97 while (len
> 0 && !isalnum(token
[len
- 1]))
102 static int same_token(struct trailer_item
*a
, struct arg_item
*b
)
104 size_t a_len
, b_len
, min_len
;
109 a_len
= token_len_without_separator(a
->token
, strlen(a
->token
));
110 b_len
= token_len_without_separator(b
->token
, strlen(b
->token
));
111 min_len
= (a_len
> b_len
) ? b_len
: a_len
;
113 return !strncasecmp(a
->token
, b
->token
, min_len
);
116 static int same_value(struct trailer_item
*a
, struct arg_item
*b
)
118 return !strcasecmp(a
->value
, b
->value
);
121 static int same_trailer(struct trailer_item
*a
, struct arg_item
*b
)
123 return same_token(a
, b
) && same_value(a
, b
);
126 static inline int is_blank_line(const char *str
)
129 while (*s
&& *s
!= '\n' && isspace(*s
))
131 return !*s
|| *s
== '\n';
134 static inline void strbuf_replace(struct strbuf
*sb
, const char *a
, const char *b
)
136 const char *ptr
= strstr(sb
->buf
, a
);
138 strbuf_splice(sb
, ptr
- sb
->buf
, strlen(a
), b
, strlen(b
));
141 static void free_trailer_item(struct trailer_item
*item
)
148 static void free_arg_item(struct arg_item
*item
)
150 free(item
->conf
.name
);
151 free(item
->conf
.key
);
152 free(item
->conf
.command
);
153 free(item
->conf
.cmd
);
159 static char last_non_space_char(const char *s
)
162 for (i
= strlen(s
) - 1; i
>= 0; i
--)
168 static struct trailer_item
*trailer_from_arg(struct arg_item
*arg_tok
)
170 struct trailer_item
*new_item
= xcalloc(1, sizeof(*new_item
));
171 new_item
->token
= arg_tok
->token
;
172 new_item
->value
= arg_tok
->value
;
173 arg_tok
->token
= arg_tok
->value
= NULL
;
174 free_arg_item(arg_tok
);
178 static void add_arg_to_input_list(struct trailer_item
*on_tok
,
179 struct arg_item
*arg_tok
)
181 int aoe
= after_or_end(arg_tok
->conf
.where
);
182 struct trailer_item
*to_add
= trailer_from_arg(arg_tok
);
184 list_add(&to_add
->list
, &on_tok
->list
);
186 list_add_tail(&to_add
->list
, &on_tok
->list
);
189 static int check_if_different(struct trailer_item
*in_tok
,
190 struct arg_item
*arg_tok
,
192 struct list_head
*head
)
194 enum trailer_where where
= arg_tok
->conf
.where
;
195 struct list_head
*next_head
;
197 if (same_trailer(in_tok
, arg_tok
))
200 * if we want to add a trailer after another one,
201 * we have to check those before this one
203 next_head
= after_or_end(where
) ? in_tok
->list
.prev
205 if (next_head
== head
)
207 in_tok
= list_entry(next_head
, struct trailer_item
, list
);
212 static char *apply_command(struct conf_info
*conf
, const char *arg
)
214 struct strbuf cmd
= STRBUF_INIT
;
215 struct strbuf buf
= STRBUF_INIT
;
216 struct child_process cp
= CHILD_PROCESS_INIT
;
220 strbuf_addstr(&cmd
, conf
->cmd
);
221 strvec_push(&cp
.args
, cmd
.buf
);
223 strvec_push(&cp
.args
, arg
);
224 } else if (conf
->command
) {
225 strbuf_addstr(&cmd
, conf
->command
);
227 strbuf_replace(&cmd
, TRAILER_ARG_STRING
, arg
);
228 strvec_push(&cp
.args
, cmd
.buf
);
230 strvec_pushv(&cp
.env
, (const char **)local_repo_env
);
234 if (capture_command(&cp
, &buf
, 1024)) {
235 error(_("running trailer command '%s' failed"), cmd
.buf
);
236 strbuf_release(&buf
);
237 result
= xstrdup("");
240 result
= strbuf_detach(&buf
, NULL
);
243 strbuf_release(&cmd
);
247 static void apply_item_command(struct trailer_item
*in_tok
, struct arg_item
*arg_tok
)
249 if (arg_tok
->conf
.command
|| arg_tok
->conf
.cmd
) {
251 if (arg_tok
->value
&& arg_tok
->value
[0]) {
252 arg
= arg_tok
->value
;
254 if (in_tok
&& in_tok
->value
)
255 arg
= xstrdup(in_tok
->value
);
259 arg_tok
->value
= apply_command(&arg_tok
->conf
, arg
);
264 static void apply_arg_if_exists(struct trailer_item
*in_tok
,
265 struct arg_item
*arg_tok
,
266 struct trailer_item
*on_tok
,
267 struct list_head
*head
)
269 switch (arg_tok
->conf
.if_exists
) {
270 case EXISTS_DO_NOTHING
:
271 free_arg_item(arg_tok
);
274 apply_item_command(in_tok
, arg_tok
);
275 add_arg_to_input_list(on_tok
, arg_tok
);
276 list_del(&in_tok
->list
);
277 free_trailer_item(in_tok
);
280 apply_item_command(in_tok
, arg_tok
);
281 add_arg_to_input_list(on_tok
, arg_tok
);
283 case EXISTS_ADD_IF_DIFFERENT
:
284 apply_item_command(in_tok
, arg_tok
);
285 if (check_if_different(in_tok
, arg_tok
, 1, head
))
286 add_arg_to_input_list(on_tok
, arg_tok
);
288 free_arg_item(arg_tok
);
290 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
:
291 apply_item_command(in_tok
, arg_tok
);
292 if (check_if_different(on_tok
, arg_tok
, 0, head
))
293 add_arg_to_input_list(on_tok
, arg_tok
);
295 free_arg_item(arg_tok
);
298 BUG("trailer.c: unhandled value %d",
299 arg_tok
->conf
.if_exists
);
303 static void apply_arg_if_missing(struct list_head
*head
,
304 struct arg_item
*arg_tok
)
306 enum trailer_where where
;
307 struct trailer_item
*to_add
;
309 switch (arg_tok
->conf
.if_missing
) {
310 case MISSING_DO_NOTHING
:
311 free_arg_item(arg_tok
);
314 where
= arg_tok
->conf
.where
;
315 apply_item_command(NULL
, arg_tok
);
316 to_add
= trailer_from_arg(arg_tok
);
317 if (after_or_end(where
))
318 list_add_tail(&to_add
->list
, head
);
320 list_add(&to_add
->list
, head
);
323 BUG("trailer.c: unhandled value %d",
324 arg_tok
->conf
.if_missing
);
328 static int find_same_and_apply_arg(struct list_head
*head
,
329 struct arg_item
*arg_tok
)
331 struct list_head
*pos
;
332 struct trailer_item
*in_tok
;
333 struct trailer_item
*on_tok
;
335 enum trailer_where where
= arg_tok
->conf
.where
;
336 int middle
= (where
== WHERE_AFTER
) || (where
== WHERE_BEFORE
);
337 int backwards
= after_or_end(where
);
338 struct trailer_item
*start_tok
;
340 if (list_empty(head
))
343 start_tok
= list_entry(backwards
? head
->prev
: head
->next
,
347 list_for_each_dir(pos
, head
, backwards
) {
348 in_tok
= list_entry(pos
, struct trailer_item
, list
);
349 if (!same_token(in_tok
, arg_tok
))
351 on_tok
= middle
? in_tok
: start_tok
;
352 apply_arg_if_exists(in_tok
, arg_tok
, on_tok
, head
);
358 void process_trailers_lists(struct list_head
*head
,
359 struct list_head
*arg_head
)
361 struct list_head
*pos
, *p
;
362 struct arg_item
*arg_tok
;
364 list_for_each_safe(pos
, p
, arg_head
) {
366 arg_tok
= list_entry(pos
, struct arg_item
, list
);
370 applied
= find_same_and_apply_arg(head
, arg_tok
);
373 apply_arg_if_missing(head
, arg_tok
);
377 int trailer_set_where(enum trailer_where
*item
, const char *value
)
380 *item
= WHERE_DEFAULT
;
381 else if (!strcasecmp("after", value
))
383 else if (!strcasecmp("before", value
))
384 *item
= WHERE_BEFORE
;
385 else if (!strcasecmp("end", value
))
387 else if (!strcasecmp("start", value
))
394 int trailer_set_if_exists(enum trailer_if_exists
*item
, const char *value
)
397 *item
= EXISTS_DEFAULT
;
398 else if (!strcasecmp("addIfDifferent", value
))
399 *item
= EXISTS_ADD_IF_DIFFERENT
;
400 else if (!strcasecmp("addIfDifferentNeighbor", value
))
401 *item
= EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
;
402 else if (!strcasecmp("add", value
))
404 else if (!strcasecmp("replace", value
))
405 *item
= EXISTS_REPLACE
;
406 else if (!strcasecmp("doNothing", value
))
407 *item
= EXISTS_DO_NOTHING
;
413 int trailer_set_if_missing(enum trailer_if_missing
*item
, const char *value
)
416 *item
= MISSING_DEFAULT
;
417 else if (!strcasecmp("doNothing", value
))
418 *item
= MISSING_DO_NOTHING
;
419 else if (!strcasecmp("add", value
))
426 static void duplicate_conf(struct conf_info
*dst
, const struct conf_info
*src
)
429 dst
->name
= xstrdup_or_null(src
->name
);
430 dst
->key
= xstrdup_or_null(src
->key
);
431 dst
->command
= xstrdup_or_null(src
->command
);
432 dst
->cmd
= xstrdup_or_null(src
->cmd
);
435 static struct arg_item
*get_conf_item(const char *name
)
437 struct list_head
*pos
;
438 struct arg_item
*item
;
440 /* Look up item with same name */
441 list_for_each(pos
, &conf_head
) {
442 item
= list_entry(pos
, struct arg_item
, list
);
443 if (!strcasecmp(item
->conf
.name
, name
))
447 /* Item does not already exists, create it */
448 CALLOC_ARRAY(item
, 1);
449 duplicate_conf(&item
->conf
, &default_conf_info
);
450 item
->conf
.name
= xstrdup(name
);
452 list_add_tail(&item
->list
, &conf_head
);
457 enum trailer_info_type
{ TRAILER_KEY
, TRAILER_COMMAND
, TRAILER_CMD
,
458 TRAILER_WHERE
, TRAILER_IF_EXISTS
, TRAILER_IF_MISSING
};
462 enum trailer_info_type type
;
463 } trailer_config_items
[] = {
464 { "key", TRAILER_KEY
},
465 { "command", TRAILER_COMMAND
},
466 { "cmd", TRAILER_CMD
},
467 { "where", TRAILER_WHERE
},
468 { "ifexists", TRAILER_IF_EXISTS
},
469 { "ifmissing", TRAILER_IF_MISSING
}
472 static int git_trailer_default_config(const char *conf_key
, const char *value
,
473 const struct config_context
*ctx UNUSED
,
476 const char *trailer_item
, *variable_name
;
478 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
481 variable_name
= strrchr(trailer_item
, '.');
482 if (!variable_name
) {
483 if (!strcmp(trailer_item
, "where")) {
484 if (trailer_set_where(&default_conf_info
.where
,
486 warning(_("unknown value '%s' for key '%s'"),
488 } else if (!strcmp(trailer_item
, "ifexists")) {
489 if (trailer_set_if_exists(&default_conf_info
.if_exists
,
491 warning(_("unknown value '%s' for key '%s'"),
493 } else if (!strcmp(trailer_item
, "ifmissing")) {
494 if (trailer_set_if_missing(&default_conf_info
.if_missing
,
496 warning(_("unknown value '%s' for key '%s'"),
498 } else if (!strcmp(trailer_item
, "separators")) {
500 return config_error_nonbool(conf_key
);
501 separators
= xstrdup(value
);
507 static int git_trailer_config(const char *conf_key
, const char *value
,
508 const struct config_context
*ctx UNUSED
,
511 const char *trailer_item
, *variable_name
;
512 struct arg_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
);
546 return config_error_nonbool(conf_key
);
547 conf
->key
= xstrdup(value
);
549 case TRAILER_COMMAND
:
551 warning(_("more than one %s"), conf_key
);
553 return config_error_nonbool(conf_key
);
554 conf
->command
= xstrdup(value
);
558 warning(_("more than one %s"), conf_key
);
560 return config_error_nonbool(conf_key
);
561 conf
->cmd
= xstrdup(value
);
564 if (trailer_set_where(&conf
->where
, value
))
565 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
567 case TRAILER_IF_EXISTS
:
568 if (trailer_set_if_exists(&conf
->if_exists
, value
))
569 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
571 case TRAILER_IF_MISSING
:
572 if (trailer_set_if_missing(&conf
->if_missing
, value
))
573 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
576 BUG("trailer.c: unhandled type %d", type
);
581 void trailer_config_init(void)
586 /* Default config must be setup first */
587 default_conf_info
.where
= WHERE_END
;
588 default_conf_info
.if_exists
= EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
;
589 default_conf_info
.if_missing
= MISSING_ADD
;
590 git_config(git_trailer_default_config
, NULL
);
591 git_config(git_trailer_config
, NULL
);
595 static const char *token_from_item(struct arg_item
*item
, char *tok
)
598 return item
->conf
.key
;
601 return item
->conf
.name
;
604 static int token_matches_item(const char *tok
, struct arg_item
*item
, size_t tok_len
)
606 if (!strncasecmp(tok
, item
->conf
.name
, tok_len
))
608 return item
->conf
.key
? !strncasecmp(tok
, item
->conf
.key
, tok_len
) : 0;
612 * If the given line is of the form
613 * "<token><optional whitespace><separator>..." or "<separator>...", return the
614 * location of the separator. Otherwise, return -1. The optional whitespace
615 * is allowed there primarily to allow things like "Bug #43" where <token> is
616 * "Bug" and <separator> is "#".
618 * The separator-starts-line case (in which this function returns 0) is
619 * distinguished from the non-well-formed-line case (in which this function
620 * returns -1) because some callers of this function need such a distinction.
622 static ssize_t
find_separator(const char *line
, const char *separators
)
624 int whitespace_found
= 0;
626 for (c
= line
; *c
; c
++) {
627 if (strchr(separators
, *c
))
629 if (!whitespace_found
&& (isalnum(*c
) || *c
== '-'))
631 if (c
!= line
&& (*c
== ' ' || *c
== '\t')) {
632 whitespace_found
= 1;
641 * Obtain the token, value, and conf from the given trailer.
643 * separator_pos must not be 0, since the token cannot be an empty string.
645 * If separator_pos is -1, interpret the whole trailer as a token.
647 static void parse_trailer(struct strbuf
*tok
, struct strbuf
*val
,
648 const struct conf_info
**conf
, const char *trailer
,
649 ssize_t separator_pos
)
651 struct arg_item
*item
;
653 struct list_head
*pos
;
655 if (separator_pos
!= -1) {
656 strbuf_add(tok
, trailer
, separator_pos
);
658 strbuf_addstr(val
, trailer
+ separator_pos
+ 1);
661 strbuf_addstr(tok
, trailer
);
665 /* Lookup if the token matches something in the config */
666 tok_len
= token_len_without_separator(tok
->buf
, tok
->len
);
668 *conf
= &default_conf_info
;
669 list_for_each(pos
, &conf_head
) {
670 item
= list_entry(pos
, struct arg_item
, list
);
671 if (token_matches_item(tok
->buf
, item
, tok_len
)) {
672 char *tok_buf
= strbuf_detach(tok
, NULL
);
675 strbuf_addstr(tok
, token_from_item(item
, tok_buf
));
682 static struct trailer_item
*add_trailer_item(struct list_head
*head
, char *tok
,
685 struct trailer_item
*new_item
= xcalloc(1, sizeof(*new_item
));
686 new_item
->token
= tok
;
687 new_item
->value
= val
;
688 list_add_tail(&new_item
->list
, head
);
692 static void add_arg_item(struct list_head
*arg_head
, char *tok
, char *val
,
693 const struct conf_info
*conf
,
694 const struct new_trailer_item
*new_trailer_item
)
696 struct arg_item
*new_item
= xcalloc(1, sizeof(*new_item
));
697 new_item
->token
= tok
;
698 new_item
->value
= val
;
699 duplicate_conf(&new_item
->conf
, conf
);
700 if (new_trailer_item
) {
701 if (new_trailer_item
->where
!= WHERE_DEFAULT
)
702 new_item
->conf
.where
= new_trailer_item
->where
;
703 if (new_trailer_item
->if_exists
!= EXISTS_DEFAULT
)
704 new_item
->conf
.if_exists
= new_trailer_item
->if_exists
;
705 if (new_trailer_item
->if_missing
!= MISSING_DEFAULT
)
706 new_item
->conf
.if_missing
= new_trailer_item
->if_missing
;
708 list_add_tail(&new_item
->list
, arg_head
);
711 void parse_trailers_from_config(struct list_head
*config_head
)
713 struct arg_item
*item
;
714 struct list_head
*pos
;
716 /* Add an arg item for each configured trailer with a command */
717 list_for_each(pos
, &conf_head
) {
718 item
= list_entry(pos
, struct arg_item
, list
);
719 if (item
->conf
.command
)
720 add_arg_item(config_head
,
721 xstrdup(token_from_item(item
, NULL
)),
727 void parse_trailers_from_command_line_args(struct list_head
*arg_head
,
728 struct list_head
*new_trailer_head
)
730 struct strbuf tok
= STRBUF_INIT
;
731 struct strbuf val
= STRBUF_INIT
;
732 const struct conf_info
*conf
;
733 struct list_head
*pos
;
736 * In command-line arguments, '=' is accepted (in addition to the
737 * separators that are defined).
739 char *cl_separators
= xstrfmt("=%s", separators
);
741 /* Add an arg item for each trailer on the command line */
742 list_for_each(pos
, new_trailer_head
) {
743 struct new_trailer_item
*tr
=
744 list_entry(pos
, struct new_trailer_item
, list
);
745 ssize_t separator_pos
= find_separator(tr
->text
, cl_separators
);
747 if (separator_pos
== 0) {
748 struct strbuf sb
= STRBUF_INIT
;
749 strbuf_addstr(&sb
, tr
->text
);
751 error(_("empty trailer token in trailer '%.*s'"),
752 (int) sb
.len
, sb
.buf
);
755 parse_trailer(&tok
, &val
, &conf
, tr
->text
,
757 add_arg_item(arg_head
,
758 strbuf_detach(&tok
, NULL
),
759 strbuf_detach(&val
, NULL
),
767 static const char *next_line(const char *str
)
769 const char *nl
= strchrnul(str
, '\n');
774 * Return the position of the start of the last line. If len is 0, return -1.
776 static ssize_t
last_line(const char *buf
, size_t len
)
784 * Skip the last character (in addition to the null terminator),
785 * because if the last character is a newline, it is considered as part
786 * of the last line anyway.
790 for (; i
>= 0; i
--) {
798 * Find the end of the log message as an offset from the start of the input
799 * (where callers of this function are interested in looking for a trailers
800 * block in the same input). We have to consider two categories of content that
801 * can come at the end of the input which we want to ignore (because they don't
802 * belong in the log message):
804 * (1) the "patch part" which begins with a "---" divider and has patch
805 * information (like the output of git-format-patch), and
807 * (2) any trailing comment lines, blank lines like in the output of "git
808 * commit -v", or stuff below the "cut" (scissor) line.
810 * As a formula, the situation looks like this:
812 * INPUT = LOG MESSAGE + IGNORED
814 * where IGNORED can be either of the two categories described above. It may be
815 * that there is nothing to ignore. Now it may be the case that the LOG MESSAGE
816 * contains a trailer block, but that's not the concern of this function.
818 static size_t find_end_of_log_message(const char *input
, int no_divider
)
823 /* Assume the naive end of the input is already what we want. */
826 /* Optionally skip over any patch part ("---" line and below). */
828 for (s
= input
; *s
; s
= next_line(s
)) {
831 if (skip_prefix(s
, "---", &v
) && isspace(*v
)) {
838 /* Skip over other ignorable bits. */
839 return end
- ignored_log_message_bytes(input
, end
);
843 * Return the position of the first trailer line or len if there are no
846 static size_t find_trailer_block_start(const char *buf
, size_t len
)
849 ssize_t end_of_title
, l
;
851 int recognized_prefix
= 0, trailer_lines
= 0, non_trailer_lines
= 0;
853 * Number of possible continuation lines encountered. This will be
854 * reset to 0 if we encounter a trailer (since those lines are to be
855 * considered continuations of that trailer), and added to
856 * non_trailer_lines if we encounter a non-trailer (since those lines
857 * are to be considered non-trailers).
859 int possible_continuation_lines
= 0;
861 /* The first paragraph is the title and cannot be trailers */
862 for (s
= buf
; s
< buf
+ len
; s
= next_line(s
)) {
863 if (starts_with_mem(s
, buf
+ len
- s
, comment_line_str
))
865 if (is_blank_line(s
))
868 end_of_title
= s
- buf
;
871 * Get the start of the trailers by looking starting from the end for a
872 * blank line before a set of non-blank lines that (i) are all
873 * trailers, or (ii) contains at least one Git-generated trailer and
874 * consists of at least 25% trailers.
876 for (l
= last_line(buf
, len
);
878 l
= last_line(buf
, l
)) {
879 const char *bol
= buf
+ l
;
881 ssize_t separator_pos
;
883 if (starts_with_mem(bol
, buf
+ len
- bol
, comment_line_str
)) {
884 non_trailer_lines
+= possible_continuation_lines
;
885 possible_continuation_lines
= 0;
888 if (is_blank_line(bol
)) {
891 non_trailer_lines
+= possible_continuation_lines
;
892 if (recognized_prefix
&&
893 trailer_lines
* 3 >= non_trailer_lines
)
894 return next_line(bol
) - buf
;
895 else if (trailer_lines
&& !non_trailer_lines
)
896 return next_line(bol
) - buf
;
901 for (p
= git_generated_prefixes
; *p
; p
++) {
902 if (starts_with(bol
, *p
)) {
904 possible_continuation_lines
= 0;
905 recognized_prefix
= 1;
906 goto continue_outer_loop
;
910 separator_pos
= find_separator(bol
, separators
);
911 if (separator_pos
>= 1 && !isspace(bol
[0])) {
912 struct list_head
*pos
;
915 possible_continuation_lines
= 0;
916 if (recognized_prefix
)
918 list_for_each(pos
, &conf_head
) {
919 struct arg_item
*item
;
920 item
= list_entry(pos
, struct arg_item
, list
);
921 if (token_matches_item(bol
, item
,
923 recognized_prefix
= 1;
927 } else if (isspace(bol
[0]))
928 possible_continuation_lines
++;
931 non_trailer_lines
+= possible_continuation_lines
;
932 possible_continuation_lines
= 0;
941 static int ends_with_blank_line(const char *buf
, size_t len
)
943 ssize_t ll
= last_line(buf
, len
);
946 return is_blank_line(buf
+ ll
);
949 static void unfold_value(struct strbuf
*val
)
951 struct strbuf out
= STRBUF_INIT
;
954 strbuf_grow(&out
, val
->len
);
956 while (i
< val
->len
) {
957 char c
= val
->buf
[i
++];
959 /* Collapse continuation down to a single space. */
960 while (i
< val
->len
&& isspace(val
->buf
[i
]))
962 strbuf_addch(&out
, ' ');
964 strbuf_addch(&out
, c
);
968 /* Empty lines may have left us with whitespace cruft at the edges */
971 /* output goes back to val as if we modified it in-place */
972 strbuf_swap(&out
, val
);
973 strbuf_release(&out
);
976 static struct trailer_info
*trailer_info_new(void)
978 struct trailer_info
*info
= xcalloc(1, sizeof(*info
));
982 static struct trailer_info
*trailer_info_get(const struct process_trailer_options
*opts
,
985 struct trailer_info
*info
= trailer_info_new();
986 size_t end_of_log_message
= 0, trailer_block_start
= 0;
987 struct strbuf
**trailer_lines
, **ptr
;
988 char **trailer_strings
= NULL
;
989 size_t nr
= 0, alloc
= 0;
992 trailer_config_init();
994 end_of_log_message
= find_end_of_log_message(str
, opts
->no_divider
);
995 trailer_block_start
= find_trailer_block_start(str
, end_of_log_message
);
997 trailer_lines
= strbuf_split_buf(str
+ trailer_block_start
,
998 end_of_log_message
- trailer_block_start
,
1001 for (ptr
= trailer_lines
; *ptr
; ptr
++) {
1002 if (last
&& isspace((*ptr
)->buf
[0])) {
1003 struct strbuf sb
= STRBUF_INIT
;
1004 strbuf_attach(&sb
, *last
, strlen(*last
), strlen(*last
));
1005 strbuf_addbuf(&sb
, *ptr
);
1006 *last
= strbuf_detach(&sb
, NULL
);
1009 ALLOC_GROW(trailer_strings
, nr
+ 1, alloc
);
1010 trailer_strings
[nr
] = strbuf_detach(*ptr
, NULL
);
1011 last
= find_separator(trailer_strings
[nr
], separators
) >= 1
1012 ? &trailer_strings
[nr
]
1016 strbuf_list_free(trailer_lines
);
1018 info
->blank_line_before_trailer
= ends_with_blank_line(str
,
1019 trailer_block_start
);
1020 info
->trailer_block_start
= trailer_block_start
;
1021 info
->trailer_block_end
= end_of_log_message
;
1022 info
->trailers
= trailer_strings
;
1023 info
->trailer_nr
= nr
;
1029 * Parse trailers in "str", populating the trailer info and "trailer_objects"
1030 * linked list structure.
1032 struct trailer_info
*parse_trailers(const struct process_trailer_options
*opts
,
1034 struct list_head
*trailer_objects
)
1036 struct trailer_info
*info
;
1037 struct strbuf tok
= STRBUF_INIT
;
1038 struct strbuf val
= STRBUF_INIT
;
1041 info
= trailer_info_get(opts
, str
);
1043 for (i
= 0; i
< info
->trailer_nr
; i
++) {
1045 char *trailer
= info
->trailers
[i
];
1046 if (starts_with(trailer
, comment_line_str
))
1048 separator_pos
= find_separator(trailer
, separators
);
1049 if (separator_pos
>= 1) {
1050 parse_trailer(&tok
, &val
, NULL
, trailer
,
1054 add_trailer_item(trailer_objects
,
1055 strbuf_detach(&tok
, NULL
),
1056 strbuf_detach(&val
, NULL
));
1057 } else if (!opts
->only_trailers
) {
1058 strbuf_addstr(&val
, trailer
);
1059 strbuf_strip_suffix(&val
, "\n");
1060 add_trailer_item(trailer_objects
,
1062 strbuf_detach(&val
, NULL
));
1069 void free_trailers(struct list_head
*trailers
)
1071 struct list_head
*pos
, *p
;
1072 list_for_each_safe(pos
, p
, trailers
) {
1074 free_trailer_item(list_entry(pos
, struct trailer_item
, list
));
1078 size_t trailer_block_start(struct trailer_info
*info
)
1080 return info
->trailer_block_start
;
1083 size_t trailer_block_end(struct trailer_info
*info
)
1085 return info
->trailer_block_end
;
1088 int blank_line_before_trailer_block(struct trailer_info
*info
)
1090 return info
->blank_line_before_trailer
;
1093 void trailer_info_release(struct trailer_info
*info
)
1096 for (i
= 0; i
< info
->trailer_nr
; i
++)
1097 free(info
->trailers
[i
]);
1098 free(info
->trailers
);
1102 void format_trailers(const struct process_trailer_options
*opts
,
1103 struct list_head
*trailers
,
1106 size_t origlen
= out
->len
;
1107 struct list_head
*pos
;
1108 struct trailer_item
*item
;
1110 list_for_each(pos
, trailers
) {
1111 item
= list_entry(pos
, struct trailer_item
, list
);
1113 struct strbuf tok
= STRBUF_INIT
;
1114 struct strbuf val
= STRBUF_INIT
;
1115 strbuf_addstr(&tok
, item
->token
);
1116 strbuf_addstr(&val
, item
->value
);
1119 * Skip key/value pairs where the value was empty. This
1120 * can happen from trailers specified without a
1121 * separator, like `--trailer "Reviewed-by"` (no
1122 * corresponding value).
1124 if (opts
->trim_empty
&& !strlen(item
->value
))
1127 if (!opts
->filter
|| opts
->filter(&tok
, opts
->filter_data
)) {
1128 if (opts
->separator
&& out
->len
!= origlen
)
1129 strbuf_addbuf(out
, opts
->separator
);
1130 if (!opts
->value_only
)
1131 strbuf_addbuf(out
, &tok
);
1132 if (!opts
->key_only
&& !opts
->value_only
) {
1133 if (opts
->key_value_separator
)
1134 strbuf_addbuf(out
, opts
->key_value_separator
);
1136 char c
= last_non_space_char(tok
.buf
);
1137 if (c
&& !strchr(separators
, c
))
1138 strbuf_addf(out
, "%c ", separators
[0]);
1141 if (!opts
->key_only
)
1142 strbuf_addbuf(out
, &val
);
1143 if (!opts
->separator
)
1144 strbuf_addch(out
, '\n');
1146 strbuf_release(&tok
);
1147 strbuf_release(&val
);
1149 } else if (!opts
->only_trailers
) {
1150 if (opts
->separator
&& out
->len
!= origlen
) {
1151 strbuf_addbuf(out
, opts
->separator
);
1153 strbuf_addstr(out
, item
->value
);
1154 if (opts
->separator
)
1157 strbuf_addch(out
, '\n');
1162 void format_trailers_from_commit(const struct process_trailer_options
*opts
,
1166 LIST_HEAD(trailer_objects
);
1167 struct trailer_info
*info
= parse_trailers(opts
, msg
, &trailer_objects
);
1169 /* If we want the whole block untouched, we can take the fast path. */
1170 if (!opts
->only_trailers
&& !opts
->unfold
&& !opts
->filter
&&
1171 !opts
->separator
&& !opts
->key_only
&& !opts
->value_only
&&
1172 !opts
->key_value_separator
) {
1173 strbuf_add(out
, msg
+ info
->trailer_block_start
,
1174 info
->trailer_block_end
- info
->trailer_block_start
);
1176 format_trailers(opts
, &trailer_objects
, out
);
1178 free_trailers(&trailer_objects
);
1179 trailer_info_release(info
);
1182 void trailer_iterator_init(struct trailer_iterator
*iter
, const char *msg
)
1184 struct process_trailer_options opts
= PROCESS_TRAILER_OPTIONS_INIT
;
1185 strbuf_init(&iter
->key
, 0);
1186 strbuf_init(&iter
->val
, 0);
1187 opts
.no_divider
= 1;
1188 iter
->internal
.info
= trailer_info_get(&opts
, msg
);
1189 iter
->internal
.cur
= 0;
1192 int trailer_iterator_advance(struct trailer_iterator
*iter
)
1194 if (iter
->internal
.cur
< iter
->internal
.info
->trailer_nr
) {
1195 char *line
= iter
->internal
.info
->trailers
[iter
->internal
.cur
++];
1196 int separator_pos
= find_separator(line
, separators
);
1199 strbuf_reset(&iter
->key
);
1200 strbuf_reset(&iter
->val
);
1201 parse_trailer(&iter
->key
, &iter
->val
, NULL
,
1202 line
, separator_pos
);
1203 /* Always unfold values during iteration. */
1204 unfold_value(&iter
->val
);
1210 void trailer_iterator_release(struct trailer_iterator
*iter
)
1212 trailer_info_release(iter
->internal
.info
);
1213 strbuf_release(&iter
->val
);
1214 strbuf_release(&iter
->key
);
1217 int amend_file_with_trailers(const char *path
, const struct strvec
*trailer_args
)
1219 struct child_process run_trailer
= CHILD_PROCESS_INIT
;
1221 run_trailer
.git_cmd
= 1;
1222 strvec_pushl(&run_trailer
.args
, "interpret-trailers",
1223 "--in-place", "--no-divider",
1225 strvec_pushv(&run_trailer
.args
, trailer_args
->v
);
1226 return run_command(&run_trailer
);