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>
19 enum trailer_where where
;
20 enum trailer_if_exists if_exists
;
21 enum trailer_if_missing if_missing
;
24 static struct conf_info default_conf_info
;
27 struct list_head list
;
29 * If this is not a trailer line, the line is stored in value
30 * (excluding the terminating newline) and token is NULL.
37 struct list_head list
;
40 struct conf_info conf
;
43 static LIST_HEAD(conf_head
);
45 static char *separators
= ":";
47 static int configured
;
49 #define TRAILER_ARG_STRING "$ARG"
51 static const char *git_generated_prefixes
[] = {
53 "(cherry picked from commit ",
57 /* Iterate over the elements of the list. */
58 #define list_for_each_dir(pos, head, is_reverse) \
59 for (pos = is_reverse ? (head)->prev : (head)->next; \
61 pos = is_reverse ? pos->prev : pos->next)
63 static int after_or_end(enum trailer_where where
)
65 return (where
== WHERE_AFTER
) || (where
== WHERE_END
);
69 * Return the length of the string not including any final
70 * punctuation. E.g., the input "Signed-off-by:" would return
71 * 13, stripping the trailing punctuation but retaining
72 * internal punctuation.
74 static size_t token_len_without_separator(const char *token
, size_t len
)
76 while (len
> 0 && !isalnum(token
[len
- 1]))
81 static int same_token(struct trailer_item
*a
, struct arg_item
*b
)
83 size_t a_len
, b_len
, min_len
;
88 a_len
= token_len_without_separator(a
->token
, strlen(a
->token
));
89 b_len
= token_len_without_separator(b
->token
, strlen(b
->token
));
90 min_len
= (a_len
> b_len
) ? b_len
: a_len
;
92 return !strncasecmp(a
->token
, b
->token
, min_len
);
95 static int same_value(struct trailer_item
*a
, struct arg_item
*b
)
97 return !strcasecmp(a
->value
, b
->value
);
100 static int same_trailer(struct trailer_item
*a
, struct arg_item
*b
)
102 return same_token(a
, b
) && same_value(a
, b
);
105 static inline int is_blank_line(const char *str
)
108 while (*s
&& *s
!= '\n' && isspace(*s
))
110 return !*s
|| *s
== '\n';
113 static inline void strbuf_replace(struct strbuf
*sb
, const char *a
, const char *b
)
115 const char *ptr
= strstr(sb
->buf
, a
);
117 strbuf_splice(sb
, ptr
- sb
->buf
, strlen(a
), b
, strlen(b
));
120 static void free_trailer_item(struct trailer_item
*item
)
127 static void free_arg_item(struct arg_item
*item
)
129 free(item
->conf
.name
);
130 free(item
->conf
.key
);
131 free(item
->conf
.command
);
132 free(item
->conf
.cmd
);
138 static char last_non_space_char(const char *s
)
141 for (i
= strlen(s
) - 1; i
>= 0; i
--)
147 static void print_tok_val(FILE *outfile
, const char *tok
, const char *val
)
152 fprintf(outfile
, "%s\n", val
);
156 c
= last_non_space_char(tok
);
159 if (strchr(separators
, c
))
160 fprintf(outfile
, "%s%s\n", tok
, val
);
162 fprintf(outfile
, "%s%c %s\n", tok
, separators
[0], val
);
165 void format_trailers(const struct process_trailer_options
*opts
,
166 struct list_head
*trailers
, FILE *outfile
)
168 struct list_head
*pos
;
169 struct trailer_item
*item
;
170 list_for_each(pos
, trailers
) {
171 item
= list_entry(pos
, struct trailer_item
, list
);
172 if ((!opts
->trim_empty
|| strlen(item
->value
) > 0) &&
173 (!opts
->only_trailers
|| item
->token
))
174 print_tok_val(outfile
, item
->token
, item
->value
);
178 static struct trailer_item
*trailer_from_arg(struct arg_item
*arg_tok
)
180 struct trailer_item
*new_item
= xcalloc(1, sizeof(*new_item
));
181 new_item
->token
= arg_tok
->token
;
182 new_item
->value
= arg_tok
->value
;
183 arg_tok
->token
= arg_tok
->value
= NULL
;
184 free_arg_item(arg_tok
);
188 static void add_arg_to_input_list(struct trailer_item
*on_tok
,
189 struct arg_item
*arg_tok
)
191 int aoe
= after_or_end(arg_tok
->conf
.where
);
192 struct trailer_item
*to_add
= trailer_from_arg(arg_tok
);
194 list_add(&to_add
->list
, &on_tok
->list
);
196 list_add_tail(&to_add
->list
, &on_tok
->list
);
199 static int check_if_different(struct trailer_item
*in_tok
,
200 struct arg_item
*arg_tok
,
202 struct list_head
*head
)
204 enum trailer_where where
= arg_tok
->conf
.where
;
205 struct list_head
*next_head
;
207 if (same_trailer(in_tok
, arg_tok
))
210 * if we want to add a trailer after another one,
211 * we have to check those before this one
213 next_head
= after_or_end(where
) ? in_tok
->list
.prev
215 if (next_head
== head
)
217 in_tok
= list_entry(next_head
, struct trailer_item
, list
);
222 static char *apply_command(struct conf_info
*conf
, const char *arg
)
224 struct strbuf cmd
= STRBUF_INIT
;
225 struct strbuf buf
= STRBUF_INIT
;
226 struct child_process cp
= CHILD_PROCESS_INIT
;
230 strbuf_addstr(&cmd
, conf
->cmd
);
231 strvec_push(&cp
.args
, cmd
.buf
);
233 strvec_push(&cp
.args
, arg
);
234 } else if (conf
->command
) {
235 strbuf_addstr(&cmd
, conf
->command
);
237 strbuf_replace(&cmd
, TRAILER_ARG_STRING
, arg
);
238 strvec_push(&cp
.args
, cmd
.buf
);
240 strvec_pushv(&cp
.env
, (const char **)local_repo_env
);
244 if (capture_command(&cp
, &buf
, 1024)) {
245 error(_("running trailer command '%s' failed"), cmd
.buf
);
246 strbuf_release(&buf
);
247 result
= xstrdup("");
250 result
= strbuf_detach(&buf
, NULL
);
253 strbuf_release(&cmd
);
257 static void apply_item_command(struct trailer_item
*in_tok
, struct arg_item
*arg_tok
)
259 if (arg_tok
->conf
.command
|| arg_tok
->conf
.cmd
) {
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
, arg
);
274 static void apply_arg_if_exists(struct trailer_item
*in_tok
,
275 struct arg_item
*arg_tok
,
276 struct trailer_item
*on_tok
,
277 struct list_head
*head
)
279 switch (arg_tok
->conf
.if_exists
) {
280 case EXISTS_DO_NOTHING
:
281 free_arg_item(arg_tok
);
284 apply_item_command(in_tok
, arg_tok
);
285 add_arg_to_input_list(on_tok
, arg_tok
);
286 list_del(&in_tok
->list
);
287 free_trailer_item(in_tok
);
290 apply_item_command(in_tok
, arg_tok
);
291 add_arg_to_input_list(on_tok
, arg_tok
);
293 case EXISTS_ADD_IF_DIFFERENT
:
294 apply_item_command(in_tok
, arg_tok
);
295 if (check_if_different(in_tok
, arg_tok
, 1, head
))
296 add_arg_to_input_list(on_tok
, arg_tok
);
298 free_arg_item(arg_tok
);
300 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
:
301 apply_item_command(in_tok
, arg_tok
);
302 if (check_if_different(on_tok
, arg_tok
, 0, head
))
303 add_arg_to_input_list(on_tok
, arg_tok
);
305 free_arg_item(arg_tok
);
308 BUG("trailer.c: unhandled value %d",
309 arg_tok
->conf
.if_exists
);
313 static void apply_arg_if_missing(struct list_head
*head
,
314 struct arg_item
*arg_tok
)
316 enum trailer_where where
;
317 struct trailer_item
*to_add
;
319 switch (arg_tok
->conf
.if_missing
) {
320 case MISSING_DO_NOTHING
:
321 free_arg_item(arg_tok
);
324 where
= arg_tok
->conf
.where
;
325 apply_item_command(NULL
, arg_tok
);
326 to_add
= trailer_from_arg(arg_tok
);
327 if (after_or_end(where
))
328 list_add_tail(&to_add
->list
, head
);
330 list_add(&to_add
->list
, head
);
333 BUG("trailer.c: unhandled value %d",
334 arg_tok
->conf
.if_missing
);
338 static int find_same_and_apply_arg(struct list_head
*head
,
339 struct arg_item
*arg_tok
)
341 struct list_head
*pos
;
342 struct trailer_item
*in_tok
;
343 struct trailer_item
*on_tok
;
345 enum trailer_where where
= arg_tok
->conf
.where
;
346 int middle
= (where
== WHERE_AFTER
) || (where
== WHERE_BEFORE
);
347 int backwards
= after_or_end(where
);
348 struct trailer_item
*start_tok
;
350 if (list_empty(head
))
353 start_tok
= list_entry(backwards
? head
->prev
: head
->next
,
357 list_for_each_dir(pos
, head
, backwards
) {
358 in_tok
= list_entry(pos
, struct trailer_item
, list
);
359 if (!same_token(in_tok
, arg_tok
))
361 on_tok
= middle
? in_tok
: start_tok
;
362 apply_arg_if_exists(in_tok
, arg_tok
, on_tok
, head
);
368 void process_trailers_lists(struct list_head
*head
,
369 struct list_head
*arg_head
)
371 struct list_head
*pos
, *p
;
372 struct arg_item
*arg_tok
;
374 list_for_each_safe(pos
, p
, arg_head
) {
376 arg_tok
= list_entry(pos
, struct arg_item
, list
);
380 applied
= find_same_and_apply_arg(head
, arg_tok
);
383 apply_arg_if_missing(head
, arg_tok
);
387 int trailer_set_where(enum trailer_where
*item
, const char *value
)
390 *item
= WHERE_DEFAULT
;
391 else if (!strcasecmp("after", value
))
393 else if (!strcasecmp("before", value
))
394 *item
= WHERE_BEFORE
;
395 else if (!strcasecmp("end", value
))
397 else if (!strcasecmp("start", value
))
404 int trailer_set_if_exists(enum trailer_if_exists
*item
, const char *value
)
407 *item
= EXISTS_DEFAULT
;
408 else if (!strcasecmp("addIfDifferent", value
))
409 *item
= EXISTS_ADD_IF_DIFFERENT
;
410 else if (!strcasecmp("addIfDifferentNeighbor", value
))
411 *item
= EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
;
412 else if (!strcasecmp("add", value
))
414 else if (!strcasecmp("replace", value
))
415 *item
= EXISTS_REPLACE
;
416 else if (!strcasecmp("doNothing", value
))
417 *item
= EXISTS_DO_NOTHING
;
423 int trailer_set_if_missing(enum trailer_if_missing
*item
, const char *value
)
426 *item
= MISSING_DEFAULT
;
427 else if (!strcasecmp("doNothing", value
))
428 *item
= MISSING_DO_NOTHING
;
429 else if (!strcasecmp("add", value
))
436 static void duplicate_conf(struct conf_info
*dst
, const struct conf_info
*src
)
439 dst
->name
= xstrdup_or_null(src
->name
);
440 dst
->key
= xstrdup_or_null(src
->key
);
441 dst
->command
= xstrdup_or_null(src
->command
);
442 dst
->cmd
= xstrdup_or_null(src
->cmd
);
445 static struct arg_item
*get_conf_item(const char *name
)
447 struct list_head
*pos
;
448 struct arg_item
*item
;
450 /* Look up item with same name */
451 list_for_each(pos
, &conf_head
) {
452 item
= list_entry(pos
, struct arg_item
, list
);
453 if (!strcasecmp(item
->conf
.name
, name
))
457 /* Item does not already exists, create it */
458 CALLOC_ARRAY(item
, 1);
459 duplicate_conf(&item
->conf
, &default_conf_info
);
460 item
->conf
.name
= xstrdup(name
);
462 list_add_tail(&item
->list
, &conf_head
);
467 enum trailer_info_type
{ TRAILER_KEY
, TRAILER_COMMAND
, TRAILER_CMD
,
468 TRAILER_WHERE
, TRAILER_IF_EXISTS
, TRAILER_IF_MISSING
};
472 enum trailer_info_type type
;
473 } trailer_config_items
[] = {
474 { "key", TRAILER_KEY
},
475 { "command", TRAILER_COMMAND
},
476 { "cmd", TRAILER_CMD
},
477 { "where", TRAILER_WHERE
},
478 { "ifexists", TRAILER_IF_EXISTS
},
479 { "ifmissing", TRAILER_IF_MISSING
}
482 static int git_trailer_default_config(const char *conf_key
, const char *value
,
483 const struct config_context
*ctx UNUSED
,
486 const char *trailer_item
, *variable_name
;
488 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
491 variable_name
= strrchr(trailer_item
, '.');
492 if (!variable_name
) {
493 if (!strcmp(trailer_item
, "where")) {
494 if (trailer_set_where(&default_conf_info
.where
,
496 warning(_("unknown value '%s' for key '%s'"),
498 } else if (!strcmp(trailer_item
, "ifexists")) {
499 if (trailer_set_if_exists(&default_conf_info
.if_exists
,
501 warning(_("unknown value '%s' for key '%s'"),
503 } else if (!strcmp(trailer_item
, "ifmissing")) {
504 if (trailer_set_if_missing(&default_conf_info
.if_missing
,
506 warning(_("unknown value '%s' for key '%s'"),
508 } else if (!strcmp(trailer_item
, "separators")) {
510 return config_error_nonbool(conf_key
);
511 separators
= xstrdup(value
);
517 static int git_trailer_config(const char *conf_key
, const char *value
,
518 const struct config_context
*ctx UNUSED
,
521 const char *trailer_item
, *variable_name
;
522 struct arg_item
*item
;
523 struct conf_info
*conf
;
525 enum trailer_info_type type
;
528 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
531 variable_name
= strrchr(trailer_item
, '.');
536 for (i
= 0; i
< ARRAY_SIZE(trailer_config_items
); i
++) {
537 if (strcmp(trailer_config_items
[i
].name
, variable_name
))
539 name
= xstrndup(trailer_item
, variable_name
- trailer_item
- 1);
540 type
= trailer_config_items
[i
].type
;
547 item
= get_conf_item(name
);
554 warning(_("more than one %s"), conf_key
);
556 return config_error_nonbool(conf_key
);
557 conf
->key
= xstrdup(value
);
559 case TRAILER_COMMAND
:
561 warning(_("more than one %s"), conf_key
);
563 return config_error_nonbool(conf_key
);
564 conf
->command
= xstrdup(value
);
568 warning(_("more than one %s"), conf_key
);
570 return config_error_nonbool(conf_key
);
571 conf
->cmd
= xstrdup(value
);
574 if (trailer_set_where(&conf
->where
, value
))
575 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
577 case TRAILER_IF_EXISTS
:
578 if (trailer_set_if_exists(&conf
->if_exists
, value
))
579 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
581 case TRAILER_IF_MISSING
:
582 if (trailer_set_if_missing(&conf
->if_missing
, value
))
583 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
586 BUG("trailer.c: unhandled type %d", type
);
591 void trailer_config_init(void)
596 /* Default config must be setup first */
597 default_conf_info
.where
= WHERE_END
;
598 default_conf_info
.if_exists
= EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
;
599 default_conf_info
.if_missing
= MISSING_ADD
;
600 git_config(git_trailer_default_config
, NULL
);
601 git_config(git_trailer_config
, NULL
);
605 static const char *token_from_item(struct arg_item
*item
, char *tok
)
608 return item
->conf
.key
;
611 return item
->conf
.name
;
614 static int token_matches_item(const char *tok
, struct arg_item
*item
, size_t tok_len
)
616 if (!strncasecmp(tok
, item
->conf
.name
, tok_len
))
618 return item
->conf
.key
? !strncasecmp(tok
, item
->conf
.key
, tok_len
) : 0;
622 * If the given line is of the form
623 * "<token><optional whitespace><separator>..." or "<separator>...", return the
624 * location of the separator. Otherwise, return -1. The optional whitespace
625 * is allowed there primarily to allow things like "Bug #43" where <token> is
626 * "Bug" and <separator> is "#".
628 * The separator-starts-line case (in which this function returns 0) is
629 * distinguished from the non-well-formed-line case (in which this function
630 * returns -1) because some callers of this function need such a distinction.
632 static ssize_t
find_separator(const char *line
, const char *separators
)
634 int whitespace_found
= 0;
636 for (c
= line
; *c
; c
++) {
637 if (strchr(separators
, *c
))
639 if (!whitespace_found
&& (isalnum(*c
) || *c
== '-'))
641 if (c
!= line
&& (*c
== ' ' || *c
== '\t')) {
642 whitespace_found
= 1;
651 * Obtain the token, value, and conf from the given trailer.
653 * separator_pos must not be 0, since the token cannot be an empty string.
655 * If separator_pos is -1, interpret the whole trailer as a token.
657 static void parse_trailer(struct strbuf
*tok
, struct strbuf
*val
,
658 const struct conf_info
**conf
, const char *trailer
,
659 ssize_t separator_pos
)
661 struct arg_item
*item
;
663 struct list_head
*pos
;
665 if (separator_pos
!= -1) {
666 strbuf_add(tok
, trailer
, separator_pos
);
668 strbuf_addstr(val
, trailer
+ separator_pos
+ 1);
671 strbuf_addstr(tok
, trailer
);
675 /* Lookup if the token matches something in the config */
676 tok_len
= token_len_without_separator(tok
->buf
, tok
->len
);
678 *conf
= &default_conf_info
;
679 list_for_each(pos
, &conf_head
) {
680 item
= list_entry(pos
, struct arg_item
, list
);
681 if (token_matches_item(tok
->buf
, item
, tok_len
)) {
682 char *tok_buf
= strbuf_detach(tok
, NULL
);
685 strbuf_addstr(tok
, token_from_item(item
, tok_buf
));
692 static struct trailer_item
*add_trailer_item(struct list_head
*head
, char *tok
,
695 struct trailer_item
*new_item
= xcalloc(1, sizeof(*new_item
));
696 new_item
->token
= tok
;
697 new_item
->value
= val
;
698 list_add_tail(&new_item
->list
, head
);
702 static void add_arg_item(struct list_head
*arg_head
, char *tok
, char *val
,
703 const struct conf_info
*conf
,
704 const struct new_trailer_item
*new_trailer_item
)
706 struct arg_item
*new_item
= xcalloc(1, sizeof(*new_item
));
707 new_item
->token
= tok
;
708 new_item
->value
= val
;
709 duplicate_conf(&new_item
->conf
, conf
);
710 if (new_trailer_item
) {
711 if (new_trailer_item
->where
!= WHERE_DEFAULT
)
712 new_item
->conf
.where
= new_trailer_item
->where
;
713 if (new_trailer_item
->if_exists
!= EXISTS_DEFAULT
)
714 new_item
->conf
.if_exists
= new_trailer_item
->if_exists
;
715 if (new_trailer_item
->if_missing
!= MISSING_DEFAULT
)
716 new_item
->conf
.if_missing
= new_trailer_item
->if_missing
;
718 list_add_tail(&new_item
->list
, arg_head
);
721 void parse_trailers_from_config(struct list_head
*config_head
)
723 struct arg_item
*item
;
724 struct list_head
*pos
;
726 /* Add an arg item for each configured trailer with a command */
727 list_for_each(pos
, &conf_head
) {
728 item
= list_entry(pos
, struct arg_item
, list
);
729 if (item
->conf
.command
)
730 add_arg_item(config_head
,
731 xstrdup(token_from_item(item
, NULL
)),
737 void parse_trailers_from_command_line_args(struct list_head
*arg_head
,
738 struct list_head
*new_trailer_head
)
740 struct strbuf tok
= STRBUF_INIT
;
741 struct strbuf val
= STRBUF_INIT
;
742 const struct conf_info
*conf
;
743 struct list_head
*pos
;
746 * In command-line arguments, '=' is accepted (in addition to the
747 * separators that are defined).
749 char *cl_separators
= xstrfmt("=%s", separators
);
751 /* Add an arg item for each trailer on the command line */
752 list_for_each(pos
, new_trailer_head
) {
753 struct new_trailer_item
*tr
=
754 list_entry(pos
, struct new_trailer_item
, list
);
755 ssize_t separator_pos
= find_separator(tr
->text
, cl_separators
);
757 if (separator_pos
== 0) {
758 struct strbuf sb
= STRBUF_INIT
;
759 strbuf_addstr(&sb
, tr
->text
);
761 error(_("empty trailer token in trailer '%.*s'"),
762 (int) sb
.len
, sb
.buf
);
765 parse_trailer(&tok
, &val
, &conf
, tr
->text
,
767 add_arg_item(arg_head
,
768 strbuf_detach(&tok
, NULL
),
769 strbuf_detach(&val
, NULL
),
777 static const char *next_line(const char *str
)
779 const char *nl
= strchrnul(str
, '\n');
784 * Return the position of the start of the last line. If len is 0, return -1.
786 static ssize_t
last_line(const char *buf
, size_t len
)
794 * Skip the last character (in addition to the null terminator),
795 * because if the last character is a newline, it is considered as part
796 * of the last line anyway.
800 for (; i
>= 0; i
--) {
808 * Find the end of the log message as an offset from the start of the input
809 * (where callers of this function are interested in looking for a trailers
810 * block in the same input). We have to consider two categories of content that
811 * can come at the end of the input which we want to ignore (because they don't
812 * belong in the log message):
814 * (1) the "patch part" which begins with a "---" divider and has patch
815 * information (like the output of git-format-patch), and
817 * (2) any trailing comment lines, blank lines like in the output of "git
818 * commit -v", or stuff below the "cut" (scissor) line.
820 * As a formula, the situation looks like this:
822 * INPUT = LOG MESSAGE + IGNORED
824 * where IGNORED can be either of the two categories described above. It may be
825 * that there is nothing to ignore. Now it may be the case that the LOG MESSAGE
826 * contains a trailer block, but that's not the concern of this function.
828 static size_t find_end_of_log_message(const char *input
, int no_divider
)
833 /* Assume the naive end of the input is already what we want. */
839 /* Optionally skip over any patch part ("---" line and below). */
840 for (s
= input
; *s
; s
= next_line(s
)) {
843 if (skip_prefix(s
, "---", &v
) && isspace(*v
)) {
849 /* Skip over other ignorable bits. */
850 return end
- ignored_log_message_bytes(input
, end
);
854 * Return the position of the first trailer line or len if there are no
857 static size_t find_trailer_block_start(const char *buf
, size_t len
)
860 ssize_t end_of_title
, l
;
862 int recognized_prefix
= 0, trailer_lines
= 0, non_trailer_lines
= 0;
864 * Number of possible continuation lines encountered. This will be
865 * reset to 0 if we encounter a trailer (since those lines are to be
866 * considered continuations of that trailer), and added to
867 * non_trailer_lines if we encounter a non-trailer (since those lines
868 * are to be considered non-trailers).
870 int possible_continuation_lines
= 0;
872 /* The first paragraph is the title and cannot be trailers */
873 for (s
= buf
; s
< buf
+ len
; s
= next_line(s
)) {
874 if (s
[0] == comment_line_char
)
876 if (is_blank_line(s
))
879 end_of_title
= s
- buf
;
882 * Get the start of the trailers by looking starting from the end for a
883 * blank line before a set of non-blank lines that (i) are all
884 * trailers, or (ii) contains at least one Git-generated trailer and
885 * consists of at least 25% trailers.
887 for (l
= last_line(buf
, len
);
889 l
= last_line(buf
, l
)) {
890 const char *bol
= buf
+ l
;
892 ssize_t separator_pos
;
894 if (bol
[0] == comment_line_char
) {
895 non_trailer_lines
+= possible_continuation_lines
;
896 possible_continuation_lines
= 0;
899 if (is_blank_line(bol
)) {
902 non_trailer_lines
+= possible_continuation_lines
;
903 if (recognized_prefix
&&
904 trailer_lines
* 3 >= non_trailer_lines
)
905 return next_line(bol
) - buf
;
906 else if (trailer_lines
&& !non_trailer_lines
)
907 return next_line(bol
) - buf
;
912 for (p
= git_generated_prefixes
; *p
; p
++) {
913 if (starts_with(bol
, *p
)) {
915 possible_continuation_lines
= 0;
916 recognized_prefix
= 1;
917 goto continue_outer_loop
;
921 separator_pos
= find_separator(bol
, separators
);
922 if (separator_pos
>= 1 && !isspace(bol
[0])) {
923 struct list_head
*pos
;
926 possible_continuation_lines
= 0;
927 if (recognized_prefix
)
929 list_for_each(pos
, &conf_head
) {
930 struct arg_item
*item
;
931 item
= list_entry(pos
, struct arg_item
, list
);
932 if (token_matches_item(bol
, item
,
934 recognized_prefix
= 1;
938 } else if (isspace(bol
[0]))
939 possible_continuation_lines
++;
942 non_trailer_lines
+= possible_continuation_lines
;
943 possible_continuation_lines
= 0;
952 static int ends_with_blank_line(const char *buf
, size_t len
)
954 ssize_t ll
= last_line(buf
, len
);
957 return is_blank_line(buf
+ ll
);
960 static void unfold_value(struct strbuf
*val
)
962 struct strbuf out
= STRBUF_INIT
;
965 strbuf_grow(&out
, val
->len
);
967 while (i
< val
->len
) {
968 char c
= val
->buf
[i
++];
970 /* Collapse continuation down to a single space. */
971 while (i
< val
->len
&& isspace(val
->buf
[i
]))
973 strbuf_addch(&out
, ' ');
975 strbuf_addch(&out
, c
);
979 /* Empty lines may have left us with whitespace cruft at the edges */
982 /* output goes back to val as if we modified it in-place */
983 strbuf_swap(&out
, val
);
984 strbuf_release(&out
);
988 * Parse trailers in "str", populating the trailer info and "head"
989 * linked list structure.
991 void parse_trailers(const struct process_trailer_options
*opts
,
992 struct trailer_info
*info
,
994 struct list_head
*head
)
996 struct strbuf tok
= STRBUF_INIT
;
997 struct strbuf val
= STRBUF_INIT
;
1000 trailer_info_get(info
, str
, opts
);
1002 for (i
= 0; i
< info
->trailer_nr
; i
++) {
1004 char *trailer
= info
->trailers
[i
];
1005 if (trailer
[0] == comment_line_char
)
1007 separator_pos
= find_separator(trailer
, separators
);
1008 if (separator_pos
>= 1) {
1009 parse_trailer(&tok
, &val
, NULL
, trailer
,
1013 add_trailer_item(head
,
1014 strbuf_detach(&tok
, NULL
),
1015 strbuf_detach(&val
, NULL
));
1016 } else if (!opts
->only_trailers
) {
1017 strbuf_addstr(&val
, trailer
);
1018 strbuf_strip_suffix(&val
, "\n");
1019 add_trailer_item(head
,
1021 strbuf_detach(&val
, NULL
));
1026 void free_trailers(struct list_head
*trailers
)
1028 struct list_head
*pos
, *p
;
1029 list_for_each_safe(pos
, p
, trailers
) {
1031 free_trailer_item(list_entry(pos
, struct trailer_item
, list
));
1035 void trailer_info_get(struct trailer_info
*info
, const char *str
,
1036 const struct process_trailer_options
*opts
)
1038 size_t end_of_log_message
= 0, trailer_block_start
= 0;
1039 struct strbuf
**trailer_lines
, **ptr
;
1040 char **trailer_strings
= NULL
;
1041 size_t nr
= 0, alloc
= 0;
1044 trailer_config_init();
1046 end_of_log_message
= find_end_of_log_message(str
, opts
->no_divider
);
1047 trailer_block_start
= find_trailer_block_start(str
, end_of_log_message
);
1049 trailer_lines
= strbuf_split_buf(str
+ trailer_block_start
,
1050 end_of_log_message
- trailer_block_start
,
1053 for (ptr
= trailer_lines
; *ptr
; ptr
++) {
1054 if (last
&& isspace((*ptr
)->buf
[0])) {
1055 struct strbuf sb
= STRBUF_INIT
;
1056 strbuf_attach(&sb
, *last
, strlen(*last
), strlen(*last
));
1057 strbuf_addbuf(&sb
, *ptr
);
1058 *last
= strbuf_detach(&sb
, NULL
);
1061 ALLOC_GROW(trailer_strings
, nr
+ 1, alloc
);
1062 trailer_strings
[nr
] = strbuf_detach(*ptr
, NULL
);
1063 last
= find_separator(trailer_strings
[nr
], separators
) >= 1
1064 ? &trailer_strings
[nr
]
1068 strbuf_list_free(trailer_lines
);
1070 info
->blank_line_before_trailer
= ends_with_blank_line(str
,
1071 trailer_block_start
);
1072 info
->trailer_block_start
= trailer_block_start
;
1073 info
->trailer_block_end
= end_of_log_message
;
1074 info
->trailers
= trailer_strings
;
1075 info
->trailer_nr
= nr
;
1078 void trailer_info_release(struct trailer_info
*info
)
1081 for (i
= 0; i
< info
->trailer_nr
; i
++)
1082 free(info
->trailers
[i
]);
1083 free(info
->trailers
);
1086 static void format_trailer_info(const struct process_trailer_options
*opts
,
1087 const struct trailer_info
*info
,
1091 size_t origlen
= out
->len
;
1094 /* If we want the whole block untouched, we can take the fast path. */
1095 if (!opts
->only_trailers
&& !opts
->unfold
&& !opts
->filter
&&
1096 !opts
->separator
&& !opts
->key_only
&& !opts
->value_only
&&
1097 !opts
->key_value_separator
) {
1098 strbuf_add(out
, msg
+ info
->trailer_block_start
,
1099 info
->trailer_block_end
- info
->trailer_block_start
);
1103 for (i
= 0; i
< info
->trailer_nr
; i
++) {
1104 char *trailer
= info
->trailers
[i
];
1105 ssize_t separator_pos
= find_separator(trailer
, separators
);
1107 if (separator_pos
>= 1) {
1108 struct strbuf tok
= STRBUF_INIT
;
1109 struct strbuf val
= STRBUF_INIT
;
1111 parse_trailer(&tok
, &val
, NULL
, trailer
, separator_pos
);
1112 if (!opts
->filter
|| opts
->filter(&tok
, opts
->filter_data
)) {
1116 if (opts
->separator
&& out
->len
!= origlen
)
1117 strbuf_addbuf(out
, opts
->separator
);
1118 if (!opts
->value_only
)
1119 strbuf_addbuf(out
, &tok
);
1120 if (!opts
->key_only
&& !opts
->value_only
) {
1121 if (opts
->key_value_separator
)
1122 strbuf_addbuf(out
, opts
->key_value_separator
);
1124 strbuf_addstr(out
, ": ");
1126 if (!opts
->key_only
)
1127 strbuf_addbuf(out
, &val
);
1128 if (!opts
->separator
)
1129 strbuf_addch(out
, '\n');
1131 strbuf_release(&tok
);
1132 strbuf_release(&val
);
1134 } else if (!opts
->only_trailers
) {
1135 if (opts
->separator
&& out
->len
!= origlen
) {
1136 strbuf_addbuf(out
, opts
->separator
);
1138 strbuf_addstr(out
, trailer
);
1139 if (opts
->separator
) {
1147 void format_trailers_from_commit(const struct process_trailer_options
*opts
,
1151 struct trailer_info info
;
1153 trailer_info_get(&info
, msg
, opts
);
1154 format_trailer_info(opts
, &info
, msg
, out
);
1155 trailer_info_release(&info
);
1158 void trailer_iterator_init(struct trailer_iterator
*iter
, const char *msg
)
1160 struct process_trailer_options opts
= PROCESS_TRAILER_OPTIONS_INIT
;
1161 strbuf_init(&iter
->key
, 0);
1162 strbuf_init(&iter
->val
, 0);
1163 opts
.no_divider
= 1;
1164 trailer_info_get(&iter
->internal
.info
, msg
, &opts
);
1165 iter
->internal
.cur
= 0;
1168 int trailer_iterator_advance(struct trailer_iterator
*iter
)
1170 while (iter
->internal
.cur
< iter
->internal
.info
.trailer_nr
) {
1171 char *trailer
= iter
->internal
.info
.trailers
[iter
->internal
.cur
++];
1172 int separator_pos
= find_separator(trailer
, separators
);
1174 if (separator_pos
< 1)
1175 continue; /* not a real trailer */
1177 strbuf_reset(&iter
->key
);
1178 strbuf_reset(&iter
->val
);
1179 parse_trailer(&iter
->key
, &iter
->val
, NULL
,
1180 trailer
, separator_pos
);
1181 /* Always unfold values during iteration. */
1182 unfold_value(&iter
->val
);
1188 void trailer_iterator_release(struct trailer_iterator
*iter
)
1190 trailer_info_release(&iter
->internal
.info
);
1191 strbuf_release(&iter
->val
);
1192 strbuf_release(&iter
->key
);