1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
5 #include "environment.h"
7 #include "string-list.h"
8 #include "run-command.h"
13 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
18 * True if there is a blank line before the location pointed to by
19 * trailer_block_start.
21 int blank_line_before_trailer
;
24 * Offsets to the trailer block start and end positions in the input
25 * string. If no trailer block is found, these are both set to the
26 * "true" end of the input (find_end_of_log_message()).
28 size_t trailer_block_start
, trailer_block_end
;
31 * Array of trailers found.
42 enum trailer_where where
;
43 enum trailer_if_exists if_exists
;
44 enum trailer_if_missing if_missing
;
47 static struct conf_info default_conf_info
;
50 struct list_head list
;
52 * If this is not a trailer line, the line is stored in value
53 * (excluding the terminating newline) and token is NULL.
60 struct list_head list
;
63 struct conf_info conf
;
66 static LIST_HEAD(conf_head
);
68 static const char *separators
= ":";
70 static int configured
;
72 #define TRAILER_ARG_STRING "$ARG"
74 static const char *git_generated_prefixes
[] = {
76 "(cherry picked from commit ",
80 /* Iterate over the elements of the list. */
81 #define list_for_each_dir(pos, head, is_reverse) \
82 for (pos = is_reverse ? (head)->prev : (head)->next; \
84 pos = is_reverse ? pos->prev : pos->next)
86 static int after_or_end(enum trailer_where where
)
88 return (where
== WHERE_AFTER
) || (where
== WHERE_END
);
92 * Return the length of the string not including any final
93 * punctuation. E.g., the input "Signed-off-by:" would return
94 * 13, stripping the trailing punctuation but retaining
95 * internal punctuation.
97 static size_t token_len_without_separator(const char *token
, size_t len
)
99 while (len
> 0 && !isalnum(token
[len
- 1]))
104 static int same_token(struct trailer_item
*a
, struct arg_item
*b
)
106 size_t a_len
, b_len
, min_len
;
111 a_len
= token_len_without_separator(a
->token
, strlen(a
->token
));
112 b_len
= token_len_without_separator(b
->token
, strlen(b
->token
));
113 min_len
= (a_len
> b_len
) ? b_len
: a_len
;
115 return !strncasecmp(a
->token
, b
->token
, min_len
);
118 static int same_value(struct trailer_item
*a
, struct arg_item
*b
)
120 return !strcasecmp(a
->value
, b
->value
);
123 static int same_trailer(struct trailer_item
*a
, struct arg_item
*b
)
125 return same_token(a
, b
) && same_value(a
, b
);
128 static inline int is_blank_line(const char *str
)
131 while (*s
&& *s
!= '\n' && isspace(*s
))
133 return !*s
|| *s
== '\n';
136 static inline void strbuf_replace(struct strbuf
*sb
, const char *a
, const char *b
)
138 const char *ptr
= strstr(sb
->buf
, a
);
140 strbuf_splice(sb
, ptr
- sb
->buf
, strlen(a
), b
, strlen(b
));
143 static void free_trailer_item(struct trailer_item
*item
)
150 static void free_arg_item(struct arg_item
*item
)
152 free(item
->conf
.name
);
153 free(item
->conf
.key
);
154 free(item
->conf
.command
);
155 free(item
->conf
.cmd
);
161 static char last_non_space_char(const char *s
)
164 for (i
= strlen(s
) - 1; i
>= 0; i
--)
170 static struct trailer_item
*trailer_from_arg(struct arg_item
*arg_tok
)
172 struct trailer_item
*new_item
= xcalloc(1, sizeof(*new_item
));
173 new_item
->token
= arg_tok
->token
;
174 new_item
->value
= arg_tok
->value
;
175 arg_tok
->token
= arg_tok
->value
= NULL
;
176 free_arg_item(arg_tok
);
180 static void add_arg_to_input_list(struct trailer_item
*on_tok
,
181 struct arg_item
*arg_tok
)
183 int aoe
= after_or_end(arg_tok
->conf
.where
);
184 struct trailer_item
*to_add
= trailer_from_arg(arg_tok
);
186 list_add(&to_add
->list
, &on_tok
->list
);
188 list_add_tail(&to_add
->list
, &on_tok
->list
);
191 static int check_if_different(struct trailer_item
*in_tok
,
192 struct arg_item
*arg_tok
,
194 struct list_head
*head
)
196 enum trailer_where where
= arg_tok
->conf
.where
;
197 struct list_head
*next_head
;
199 if (same_trailer(in_tok
, arg_tok
))
202 * if we want to add a trailer after another one,
203 * we have to check those before this one
205 next_head
= after_or_end(where
) ? in_tok
->list
.prev
207 if (next_head
== head
)
209 in_tok
= list_entry(next_head
, struct trailer_item
, list
);
214 static char *apply_command(struct conf_info
*conf
, const char *arg
)
216 struct strbuf cmd
= STRBUF_INIT
;
217 struct strbuf buf
= STRBUF_INIT
;
218 struct child_process cp
= CHILD_PROCESS_INIT
;
222 strbuf_addstr(&cmd
, conf
->cmd
);
223 strvec_push(&cp
.args
, cmd
.buf
);
225 strvec_push(&cp
.args
, arg
);
226 } else if (conf
->command
) {
227 strbuf_addstr(&cmd
, conf
->command
);
229 strbuf_replace(&cmd
, TRAILER_ARG_STRING
, arg
);
230 strvec_push(&cp
.args
, cmd
.buf
);
232 strvec_pushv(&cp
.env
, (const char **)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 arg_item
*arg_tok
)
251 if (arg_tok
->conf
.command
|| arg_tok
->conf
.cmd
) {
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
, arg
);
266 static void apply_arg_if_exists(struct trailer_item
*in_tok
,
267 struct arg_item
*arg_tok
,
268 struct trailer_item
*on_tok
,
269 struct list_head
*head
)
271 switch (arg_tok
->conf
.if_exists
) {
272 case EXISTS_DO_NOTHING
:
273 free_arg_item(arg_tok
);
276 apply_item_command(in_tok
, arg_tok
);
277 add_arg_to_input_list(on_tok
, arg_tok
);
278 list_del(&in_tok
->list
);
279 free_trailer_item(in_tok
);
282 apply_item_command(in_tok
, arg_tok
);
283 add_arg_to_input_list(on_tok
, arg_tok
);
285 case EXISTS_ADD_IF_DIFFERENT
:
286 apply_item_command(in_tok
, arg_tok
);
287 if (check_if_different(in_tok
, arg_tok
, 1, head
))
288 add_arg_to_input_list(on_tok
, arg_tok
);
290 free_arg_item(arg_tok
);
292 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
:
293 apply_item_command(in_tok
, arg_tok
);
294 if (check_if_different(on_tok
, arg_tok
, 0, head
))
295 add_arg_to_input_list(on_tok
, arg_tok
);
297 free_arg_item(arg_tok
);
300 BUG("trailer.c: unhandled value %d",
301 arg_tok
->conf
.if_exists
);
305 static void apply_arg_if_missing(struct list_head
*head
,
306 struct arg_item
*arg_tok
)
308 enum trailer_where where
;
309 struct trailer_item
*to_add
;
311 switch (arg_tok
->conf
.if_missing
) {
312 case MISSING_DO_NOTHING
:
313 free_arg_item(arg_tok
);
316 where
= arg_tok
->conf
.where
;
317 apply_item_command(NULL
, arg_tok
);
318 to_add
= trailer_from_arg(arg_tok
);
319 if (after_or_end(where
))
320 list_add_tail(&to_add
->list
, head
);
322 list_add(&to_add
->list
, head
);
325 BUG("trailer.c: unhandled value %d",
326 arg_tok
->conf
.if_missing
);
330 static int find_same_and_apply_arg(struct list_head
*head
,
331 struct arg_item
*arg_tok
)
333 struct list_head
*pos
;
334 struct trailer_item
*in_tok
;
335 struct trailer_item
*on_tok
;
337 enum trailer_where where
= arg_tok
->conf
.where
;
338 int middle
= (where
== WHERE_AFTER
) || (where
== WHERE_BEFORE
);
339 int backwards
= after_or_end(where
);
340 struct trailer_item
*start_tok
;
342 if (list_empty(head
))
345 start_tok
= list_entry(backwards
? head
->prev
: head
->next
,
349 list_for_each_dir(pos
, head
, backwards
) {
350 in_tok
= list_entry(pos
, struct trailer_item
, list
);
351 if (!same_token(in_tok
, arg_tok
))
353 on_tok
= middle
? in_tok
: start_tok
;
354 apply_arg_if_exists(in_tok
, arg_tok
, on_tok
, head
);
360 void process_trailers_lists(struct list_head
*head
,
361 struct list_head
*arg_head
)
363 struct list_head
*pos
, *p
;
364 struct arg_item
*arg_tok
;
366 list_for_each_safe(pos
, p
, arg_head
) {
368 arg_tok
= list_entry(pos
, struct arg_item
, list
);
372 applied
= find_same_and_apply_arg(head
, arg_tok
);
375 apply_arg_if_missing(head
, arg_tok
);
379 int trailer_set_where(enum trailer_where
*item
, const char *value
)
382 *item
= WHERE_DEFAULT
;
383 else if (!strcasecmp("after", value
))
385 else if (!strcasecmp("before", value
))
386 *item
= WHERE_BEFORE
;
387 else if (!strcasecmp("end", value
))
389 else if (!strcasecmp("start", value
))
396 int trailer_set_if_exists(enum trailer_if_exists
*item
, const char *value
)
399 *item
= EXISTS_DEFAULT
;
400 else if (!strcasecmp("addIfDifferent", value
))
401 *item
= EXISTS_ADD_IF_DIFFERENT
;
402 else if (!strcasecmp("addIfDifferentNeighbor", value
))
403 *item
= EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
;
404 else if (!strcasecmp("add", value
))
406 else if (!strcasecmp("replace", value
))
407 *item
= EXISTS_REPLACE
;
408 else if (!strcasecmp("doNothing", value
))
409 *item
= EXISTS_DO_NOTHING
;
415 int trailer_set_if_missing(enum trailer_if_missing
*item
, const char *value
)
418 *item
= MISSING_DEFAULT
;
419 else if (!strcasecmp("doNothing", value
))
420 *item
= MISSING_DO_NOTHING
;
421 else if (!strcasecmp("add", value
))
428 static void duplicate_conf(struct conf_info
*dst
, const struct conf_info
*src
)
431 dst
->name
= xstrdup_or_null(src
->name
);
432 dst
->key
= xstrdup_or_null(src
->key
);
433 dst
->command
= xstrdup_or_null(src
->command
);
434 dst
->cmd
= xstrdup_or_null(src
->cmd
);
437 static struct arg_item
*get_conf_item(const char *name
)
439 struct list_head
*pos
;
440 struct arg_item
*item
;
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
))
449 /* Item does not already exists, create it */
450 CALLOC_ARRAY(item
, 1);
451 duplicate_conf(&item
->conf
, &default_conf_info
);
452 item
->conf
.name
= xstrdup(name
);
454 list_add_tail(&item
->list
, &conf_head
);
459 enum trailer_info_type
{ TRAILER_KEY
, TRAILER_COMMAND
, TRAILER_CMD
,
460 TRAILER_WHERE
, TRAILER_IF_EXISTS
, TRAILER_IF_MISSING
};
464 enum trailer_info_type type
;
465 } trailer_config_items
[] = {
466 { "key", TRAILER_KEY
},
467 { "command", TRAILER_COMMAND
},
468 { "cmd", TRAILER_CMD
},
469 { "where", TRAILER_WHERE
},
470 { "ifexists", TRAILER_IF_EXISTS
},
471 { "ifmissing", TRAILER_IF_MISSING
}
474 static int git_trailer_default_config(const char *conf_key
, const char *value
,
475 const struct config_context
*ctx UNUSED
,
478 const char *trailer_item
, *variable_name
;
480 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
483 variable_name
= strrchr(trailer_item
, '.');
484 if (!variable_name
) {
485 if (!strcmp(trailer_item
, "where")) {
486 if (trailer_set_where(&default_conf_info
.where
,
488 warning(_("unknown value '%s' for key '%s'"),
490 } else if (!strcmp(trailer_item
, "ifexists")) {
491 if (trailer_set_if_exists(&default_conf_info
.if_exists
,
493 warning(_("unknown value '%s' for key '%s'"),
495 } else if (!strcmp(trailer_item
, "ifmissing")) {
496 if (trailer_set_if_missing(&default_conf_info
.if_missing
,
498 warning(_("unknown value '%s' for key '%s'"),
500 } else if (!strcmp(trailer_item
, "separators")) {
502 return config_error_nonbool(conf_key
);
503 separators
= xstrdup(value
);
509 static int git_trailer_config(const char *conf_key
, const char *value
,
510 const struct config_context
*ctx UNUSED
,
513 const char *trailer_item
, *variable_name
;
514 struct arg_item
*item
;
515 struct conf_info
*conf
;
517 enum trailer_info_type type
;
520 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
523 variable_name
= strrchr(trailer_item
, '.');
528 for (i
= 0; i
< ARRAY_SIZE(trailer_config_items
); i
++) {
529 if (strcmp(trailer_config_items
[i
].name
, variable_name
))
531 name
= xstrndup(trailer_item
, variable_name
- trailer_item
- 1);
532 type
= trailer_config_items
[i
].type
;
539 item
= get_conf_item(name
);
546 warning(_("more than one %s"), conf_key
);
548 return config_error_nonbool(conf_key
);
549 conf
->key
= xstrdup(value
);
551 case TRAILER_COMMAND
:
553 warning(_("more than one %s"), conf_key
);
555 return config_error_nonbool(conf_key
);
556 conf
->command
= xstrdup(value
);
560 warning(_("more than one %s"), conf_key
);
562 return config_error_nonbool(conf_key
);
563 conf
->cmd
= xstrdup(value
);
566 if (trailer_set_where(&conf
->where
, value
))
567 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
569 case TRAILER_IF_EXISTS
:
570 if (trailer_set_if_exists(&conf
->if_exists
, value
))
571 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
573 case TRAILER_IF_MISSING
:
574 if (trailer_set_if_missing(&conf
->if_missing
, value
))
575 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
578 BUG("trailer.c: unhandled type %d", type
);
583 void trailer_config_init(void)
588 /* Default config must be setup first */
589 default_conf_info
.where
= WHERE_END
;
590 default_conf_info
.if_exists
= EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
;
591 default_conf_info
.if_missing
= MISSING_ADD
;
592 git_config(git_trailer_default_config
, NULL
);
593 git_config(git_trailer_config
, NULL
);
597 static const char *token_from_item(struct arg_item
*item
, char *tok
)
600 return item
->conf
.key
;
603 return item
->conf
.name
;
606 static int token_matches_item(const char *tok
, struct arg_item
*item
, size_t tok_len
)
608 if (!strncasecmp(tok
, item
->conf
.name
, tok_len
))
610 return item
->conf
.key
? !strncasecmp(tok
, item
->conf
.key
, tok_len
) : 0;
614 * If the given line is of the form
615 * "<token><optional whitespace><separator>..." or "<separator>...", return the
616 * location of the separator. Otherwise, return -1. The optional whitespace
617 * is allowed there primarily to allow things like "Bug #43" where <token> is
618 * "Bug" and <separator> is "#".
620 * The separator-starts-line case (in which this function returns 0) is
621 * distinguished from the non-well-formed-line case (in which this function
622 * returns -1) because some callers of this function need such a distinction.
624 static ssize_t
find_separator(const char *line
, const char *separators
)
626 int whitespace_found
= 0;
628 for (c
= line
; *c
; c
++) {
629 if (strchr(separators
, *c
))
631 if (!whitespace_found
&& (isalnum(*c
) || *c
== '-'))
633 if (c
!= line
&& (*c
== ' ' || *c
== '\t')) {
634 whitespace_found
= 1;
643 * Obtain the token, value, and conf from the given trailer.
645 * separator_pos must not be 0, since the token cannot be an empty string.
647 * If separator_pos is -1, interpret the whole trailer as a token.
649 static void parse_trailer(struct strbuf
*tok
, struct strbuf
*val
,
650 const struct conf_info
**conf
, const char *trailer
,
651 ssize_t separator_pos
)
653 struct arg_item
*item
;
655 struct list_head
*pos
;
657 if (separator_pos
!= -1) {
658 strbuf_add(tok
, trailer
, separator_pos
);
660 strbuf_addstr(val
, trailer
+ separator_pos
+ 1);
663 strbuf_addstr(tok
, trailer
);
667 /* Lookup if the token matches something in the config */
668 tok_len
= token_len_without_separator(tok
->buf
, tok
->len
);
670 *conf
= &default_conf_info
;
671 list_for_each(pos
, &conf_head
) {
672 item
= list_entry(pos
, struct arg_item
, list
);
673 if (token_matches_item(tok
->buf
, item
, tok_len
)) {
674 char *tok_buf
= strbuf_detach(tok
, NULL
);
677 strbuf_addstr(tok
, token_from_item(item
, tok_buf
));
684 static struct trailer_item
*add_trailer_item(struct list_head
*head
, char *tok
,
687 struct trailer_item
*new_item
= xcalloc(1, sizeof(*new_item
));
688 new_item
->token
= tok
;
689 new_item
->value
= val
;
690 list_add_tail(&new_item
->list
, head
);
694 static void add_arg_item(struct list_head
*arg_head
, char *tok
, char *val
,
695 const struct conf_info
*conf
,
696 const struct new_trailer_item
*new_trailer_item
)
698 struct arg_item
*new_item
= xcalloc(1, sizeof(*new_item
));
699 new_item
->token
= tok
;
700 new_item
->value
= val
;
701 duplicate_conf(&new_item
->conf
, conf
);
702 if (new_trailer_item
) {
703 if (new_trailer_item
->where
!= WHERE_DEFAULT
)
704 new_item
->conf
.where
= new_trailer_item
->where
;
705 if (new_trailer_item
->if_exists
!= EXISTS_DEFAULT
)
706 new_item
->conf
.if_exists
= new_trailer_item
->if_exists
;
707 if (new_trailer_item
->if_missing
!= MISSING_DEFAULT
)
708 new_item
->conf
.if_missing
= new_trailer_item
->if_missing
;
710 list_add_tail(&new_item
->list
, arg_head
);
713 void parse_trailers_from_config(struct list_head
*config_head
)
715 struct arg_item
*item
;
716 struct list_head
*pos
;
718 /* Add an arg item for each configured trailer with a command */
719 list_for_each(pos
, &conf_head
) {
720 item
= list_entry(pos
, struct arg_item
, list
);
721 if (item
->conf
.command
)
722 add_arg_item(config_head
,
723 xstrdup(token_from_item(item
, NULL
)),
729 void parse_trailers_from_command_line_args(struct list_head
*arg_head
,
730 struct list_head
*new_trailer_head
)
732 struct strbuf tok
= STRBUF_INIT
;
733 struct strbuf val
= STRBUF_INIT
;
734 const struct conf_info
*conf
;
735 struct list_head
*pos
;
738 * In command-line arguments, '=' is accepted (in addition to the
739 * separators that are defined).
741 char *cl_separators
= xstrfmt("=%s", separators
);
743 /* Add an arg item for each trailer on the command line */
744 list_for_each(pos
, new_trailer_head
) {
745 struct new_trailer_item
*tr
=
746 list_entry(pos
, struct new_trailer_item
, list
);
747 ssize_t separator_pos
= find_separator(tr
->text
, cl_separators
);
749 if (separator_pos
== 0) {
750 struct strbuf sb
= STRBUF_INIT
;
751 strbuf_addstr(&sb
, tr
->text
);
753 error(_("empty trailer token in trailer '%.*s'"),
754 (int) sb
.len
, sb
.buf
);
757 parse_trailer(&tok
, &val
, &conf
, tr
->text
,
759 add_arg_item(arg_head
,
760 strbuf_detach(&tok
, NULL
),
761 strbuf_detach(&val
, NULL
),
769 static const char *next_line(const char *str
)
771 const char *nl
= strchrnul(str
, '\n');
776 * Return the position of the start of the last line. If len is 0, return -1.
778 static ssize_t
last_line(const char *buf
, size_t len
)
786 * Skip the last character (in addition to the null terminator),
787 * because if the last character is a newline, it is considered as part
788 * of the last line anyway.
792 for (; i
>= 0; i
--) {
800 * Find the end of the log message as an offset from the start of the input
801 * (where callers of this function are interested in looking for a trailers
802 * block in the same input). We have to consider two categories of content that
803 * can come at the end of the input which we want to ignore (because they don't
804 * belong in the log message):
806 * (1) the "patch part" which begins with a "---" divider and has patch
807 * information (like the output of git-format-patch), and
809 * (2) any trailing comment lines, blank lines like in the output of "git
810 * commit -v", or stuff below the "cut" (scissor) line.
812 * As a formula, the situation looks like this:
814 * INPUT = LOG MESSAGE + IGNORED
816 * where IGNORED can be either of the two categories described above. It may be
817 * that there is nothing to ignore. Now it may be the case that the LOG MESSAGE
818 * contains a trailer block, but that's not the concern of this function.
820 static size_t find_end_of_log_message(const char *input
, int no_divider
)
825 /* Assume the naive end of the input is already what we want. */
828 /* Optionally skip over any patch part ("---" line and below). */
830 for (s
= input
; *s
; s
= next_line(s
)) {
833 if (skip_prefix(s
, "---", &v
) && isspace(*v
)) {
840 /* Skip over other ignorable bits. */
841 return end
- ignored_log_message_bytes(input
, end
);
845 * Return the position of the first trailer line or len if there are no
848 static size_t find_trailer_block_start(const char *buf
, size_t len
)
851 ssize_t end_of_title
, l
;
853 int recognized_prefix
= 0, trailer_lines
= 0, non_trailer_lines
= 0;
855 * Number of possible continuation lines encountered. This will be
856 * reset to 0 if we encounter a trailer (since those lines are to be
857 * considered continuations of that trailer), and added to
858 * non_trailer_lines if we encounter a non-trailer (since those lines
859 * are to be considered non-trailers).
861 int possible_continuation_lines
= 0;
863 /* The first paragraph is the title and cannot be trailers */
864 for (s
= buf
; s
< buf
+ len
; s
= next_line(s
)) {
865 if (starts_with_mem(s
, buf
+ len
- s
, comment_line_str
))
867 if (is_blank_line(s
))
870 end_of_title
= s
- buf
;
873 * Get the start of the trailers by looking starting from the end for a
874 * blank line before a set of non-blank lines that (i) are all
875 * trailers, or (ii) contains at least one Git-generated trailer and
876 * consists of at least 25% trailers.
878 for (l
= last_line(buf
, len
);
880 l
= last_line(buf
, l
)) {
881 const char *bol
= buf
+ l
;
883 ssize_t separator_pos
;
885 if (starts_with_mem(bol
, buf
+ len
- bol
, comment_line_str
)) {
886 non_trailer_lines
+= possible_continuation_lines
;
887 possible_continuation_lines
= 0;
890 if (is_blank_line(bol
)) {
893 non_trailer_lines
+= possible_continuation_lines
;
894 if (recognized_prefix
&&
895 trailer_lines
* 3 >= non_trailer_lines
)
896 return next_line(bol
) - buf
;
897 else if (trailer_lines
&& !non_trailer_lines
)
898 return next_line(bol
) - buf
;
903 for (p
= git_generated_prefixes
; *p
; p
++) {
904 if (starts_with(bol
, *p
)) {
906 possible_continuation_lines
= 0;
907 recognized_prefix
= 1;
908 goto continue_outer_loop
;
912 separator_pos
= find_separator(bol
, separators
);
913 if (separator_pos
>= 1 && !isspace(bol
[0])) {
914 struct list_head
*pos
;
917 possible_continuation_lines
= 0;
918 if (recognized_prefix
)
920 list_for_each(pos
, &conf_head
) {
921 struct arg_item
*item
;
922 item
= list_entry(pos
, struct arg_item
, list
);
923 if (token_matches_item(bol
, item
,
925 recognized_prefix
= 1;
929 } else if (isspace(bol
[0]))
930 possible_continuation_lines
++;
933 non_trailer_lines
+= possible_continuation_lines
;
934 possible_continuation_lines
= 0;
943 static int ends_with_blank_line(const char *buf
, size_t len
)
945 ssize_t ll
= last_line(buf
, len
);
948 return is_blank_line(buf
+ ll
);
951 static void unfold_value(struct strbuf
*val
)
953 struct strbuf out
= STRBUF_INIT
;
956 strbuf_grow(&out
, val
->len
);
958 while (i
< val
->len
) {
959 char c
= val
->buf
[i
++];
961 /* Collapse continuation down to a single space. */
962 while (i
< val
->len
&& isspace(val
->buf
[i
]))
964 strbuf_addch(&out
, ' ');
966 strbuf_addch(&out
, c
);
970 /* Empty lines may have left us with whitespace cruft at the edges */
973 /* output goes back to val as if we modified it in-place */
974 strbuf_swap(&out
, val
);
975 strbuf_release(&out
);
978 static struct trailer_info
*trailer_info_new(void)
980 struct trailer_info
*info
= xcalloc(1, sizeof(*info
));
984 static struct trailer_info
*trailer_info_get(const struct process_trailer_options
*opts
,
987 struct trailer_info
*info
= trailer_info_new();
988 size_t end_of_log_message
= 0, trailer_block_start
= 0;
989 struct strbuf
**trailer_lines
, **ptr
;
990 char **trailer_strings
= NULL
;
991 size_t nr
= 0, alloc
= 0;
994 trailer_config_init();
996 end_of_log_message
= find_end_of_log_message(str
, opts
->no_divider
);
997 trailer_block_start
= find_trailer_block_start(str
, end_of_log_message
);
999 trailer_lines
= strbuf_split_buf(str
+ trailer_block_start
,
1000 end_of_log_message
- trailer_block_start
,
1003 for (ptr
= trailer_lines
; *ptr
; ptr
++) {
1004 if (last
&& isspace((*ptr
)->buf
[0])) {
1005 struct strbuf sb
= STRBUF_INIT
;
1006 strbuf_attach(&sb
, *last
, strlen(*last
), strlen(*last
));
1007 strbuf_addbuf(&sb
, *ptr
);
1008 *last
= strbuf_detach(&sb
, NULL
);
1011 ALLOC_GROW(trailer_strings
, nr
+ 1, alloc
);
1012 trailer_strings
[nr
] = strbuf_detach(*ptr
, NULL
);
1013 last
= find_separator(trailer_strings
[nr
], separators
) >= 1
1014 ? &trailer_strings
[nr
]
1018 strbuf_list_free(trailer_lines
);
1020 info
->blank_line_before_trailer
= ends_with_blank_line(str
,
1021 trailer_block_start
);
1022 info
->trailer_block_start
= trailer_block_start
;
1023 info
->trailer_block_end
= end_of_log_message
;
1024 info
->trailers
= trailer_strings
;
1025 info
->trailer_nr
= nr
;
1031 * Parse trailers in "str", populating the trailer info and "trailer_objects"
1032 * linked list structure.
1034 struct trailer_info
*parse_trailers(const struct process_trailer_options
*opts
,
1036 struct list_head
*trailer_objects
)
1038 struct trailer_info
*info
;
1039 struct strbuf tok
= STRBUF_INIT
;
1040 struct strbuf val
= STRBUF_INIT
;
1043 info
= trailer_info_get(opts
, str
);
1045 for (i
= 0; i
< info
->trailer_nr
; i
++) {
1047 char *trailer
= info
->trailers
[i
];
1048 if (starts_with(trailer
, comment_line_str
))
1050 separator_pos
= find_separator(trailer
, separators
);
1051 if (separator_pos
>= 1) {
1052 parse_trailer(&tok
, &val
, NULL
, trailer
,
1056 add_trailer_item(trailer_objects
,
1057 strbuf_detach(&tok
, NULL
),
1058 strbuf_detach(&val
, NULL
));
1059 } else if (!opts
->only_trailers
) {
1060 strbuf_addstr(&val
, trailer
);
1061 strbuf_strip_suffix(&val
, "\n");
1062 add_trailer_item(trailer_objects
,
1064 strbuf_detach(&val
, NULL
));
1071 void free_trailers(struct list_head
*trailers
)
1073 struct list_head
*pos
, *p
;
1074 list_for_each_safe(pos
, p
, trailers
) {
1076 free_trailer_item(list_entry(pos
, struct trailer_item
, list
));
1080 size_t trailer_block_start(struct trailer_info
*info
)
1082 return info
->trailer_block_start
;
1085 size_t trailer_block_end(struct trailer_info
*info
)
1087 return info
->trailer_block_end
;
1090 int blank_line_before_trailer_block(struct trailer_info
*info
)
1092 return info
->blank_line_before_trailer
;
1095 void trailer_info_release(struct trailer_info
*info
)
1098 for (i
= 0; i
< info
->trailer_nr
; i
++)
1099 free(info
->trailers
[i
]);
1100 free(info
->trailers
);
1104 void format_trailers(const struct process_trailer_options
*opts
,
1105 struct list_head
*trailers
,
1108 size_t origlen
= out
->len
;
1109 struct list_head
*pos
;
1110 struct trailer_item
*item
;
1112 list_for_each(pos
, trailers
) {
1113 item
= list_entry(pos
, struct trailer_item
, list
);
1115 struct strbuf tok
= STRBUF_INIT
;
1116 struct strbuf val
= STRBUF_INIT
;
1117 strbuf_addstr(&tok
, item
->token
);
1118 strbuf_addstr(&val
, item
->value
);
1121 * Skip key/value pairs where the value was empty. This
1122 * can happen from trailers specified without a
1123 * separator, like `--trailer "Reviewed-by"` (no
1124 * corresponding value).
1126 if (opts
->trim_empty
&& !strlen(item
->value
))
1129 if (!opts
->filter
|| opts
->filter(&tok
, opts
->filter_data
)) {
1130 if (opts
->separator
&& out
->len
!= origlen
)
1131 strbuf_addbuf(out
, opts
->separator
);
1132 if (!opts
->value_only
)
1133 strbuf_addbuf(out
, &tok
);
1134 if (!opts
->key_only
&& !opts
->value_only
) {
1135 if (opts
->key_value_separator
)
1136 strbuf_addbuf(out
, opts
->key_value_separator
);
1138 char c
= last_non_space_char(tok
.buf
);
1139 if (c
&& !strchr(separators
, c
))
1140 strbuf_addf(out
, "%c ", separators
[0]);
1143 if (!opts
->key_only
)
1144 strbuf_addbuf(out
, &val
);
1145 if (!opts
->separator
)
1146 strbuf_addch(out
, '\n');
1148 strbuf_release(&tok
);
1149 strbuf_release(&val
);
1151 } else if (!opts
->only_trailers
) {
1152 if (opts
->separator
&& out
->len
!= origlen
) {
1153 strbuf_addbuf(out
, opts
->separator
);
1155 strbuf_addstr(out
, item
->value
);
1156 if (opts
->separator
)
1159 strbuf_addch(out
, '\n');
1164 void format_trailers_from_commit(const struct process_trailer_options
*opts
,
1168 LIST_HEAD(trailer_objects
);
1169 struct trailer_info
*info
= parse_trailers(opts
, msg
, &trailer_objects
);
1171 /* If we want the whole block untouched, we can take the fast path. */
1172 if (!opts
->only_trailers
&& !opts
->unfold
&& !opts
->filter
&&
1173 !opts
->separator
&& !opts
->key_only
&& !opts
->value_only
&&
1174 !opts
->key_value_separator
) {
1175 strbuf_add(out
, msg
+ info
->trailer_block_start
,
1176 info
->trailer_block_end
- info
->trailer_block_start
);
1178 format_trailers(opts
, &trailer_objects
, out
);
1180 free_trailers(&trailer_objects
);
1181 trailer_info_release(info
);
1184 void trailer_iterator_init(struct trailer_iterator
*iter
, const char *msg
)
1186 struct process_trailer_options opts
= PROCESS_TRAILER_OPTIONS_INIT
;
1187 strbuf_init(&iter
->key
, 0);
1188 strbuf_init(&iter
->val
, 0);
1189 opts
.no_divider
= 1;
1190 iter
->internal
.info
= trailer_info_get(&opts
, msg
);
1191 iter
->internal
.cur
= 0;
1194 int trailer_iterator_advance(struct trailer_iterator
*iter
)
1196 if (iter
->internal
.cur
< iter
->internal
.info
->trailer_nr
) {
1197 char *line
= iter
->internal
.info
->trailers
[iter
->internal
.cur
++];
1198 int separator_pos
= find_separator(line
, separators
);
1201 strbuf_reset(&iter
->key
);
1202 strbuf_reset(&iter
->val
);
1203 parse_trailer(&iter
->key
, &iter
->val
, NULL
,
1204 line
, separator_pos
);
1205 /* Always unfold values during iteration. */
1206 unfold_value(&iter
->val
);
1212 void trailer_iterator_release(struct trailer_iterator
*iter
)
1214 trailer_info_release(iter
->internal
.info
);
1215 strbuf_release(&iter
->val
);
1216 strbuf_release(&iter
->key
);
1219 int amend_file_with_trailers(const char *path
, const struct strvec
*trailer_args
)
1221 struct child_process run_trailer
= CHILD_PROCESS_INIT
;
1223 run_trailer
.git_cmd
= 1;
1224 strvec_pushl(&run_trailer
.args
, "interpret-trailers",
1225 "--in-place", "--no-divider",
1227 strvec_pushv(&run_trailer
.args
, trailer_args
->v
);
1228 return run_command(&run_trailer
);