2 #include "string-list.h"
3 #include "run-command.h"
9 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
12 enum action_where
{ WHERE_END
, WHERE_AFTER
, WHERE_BEFORE
, WHERE_START
};
13 enum action_if_exists
{ EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
, EXISTS_ADD_IF_DIFFERENT
,
14 EXISTS_ADD
, EXISTS_REPLACE
, EXISTS_DO_NOTHING
};
15 enum action_if_missing
{ MISSING_ADD
, MISSING_DO_NOTHING
};
21 enum action_where where
;
22 enum action_if_exists if_exists
;
23 enum action_if_missing if_missing
;
26 static struct conf_info default_conf_info
;
29 struct list_head list
;
31 * If this is not a trailer line, the line is stored in value
32 * (excluding the terminating newline) and token is NULL.
39 struct list_head list
;
42 struct conf_info conf
;
45 static LIST_HEAD(conf_head
);
47 static char *separators
= ":";
49 static int configured
;
51 #define TRAILER_ARG_STRING "$ARG"
53 static const char *git_generated_prefixes
[] = {
55 "(cherry picked from commit ",
59 /* Iterate over the elements of the list. */
60 #define list_for_each_dir(pos, head, is_reverse) \
61 for (pos = is_reverse ? (head)->prev : (head)->next; \
63 pos = is_reverse ? pos->prev : pos->next)
65 static int after_or_end(enum action_where where
)
67 return (where
== WHERE_AFTER
) || (where
== WHERE_END
);
71 * Return the length of the string not including any final
72 * punctuation. E.g., the input "Signed-off-by:" would return
73 * 13, stripping the trailing punctuation but retaining
74 * internal punctuation.
76 static size_t token_len_without_separator(const char *token
, size_t len
)
78 while (len
> 0 && !isalnum(token
[len
- 1]))
83 static int same_token(struct trailer_item
*a
, struct arg_item
*b
)
85 size_t a_len
, b_len
, min_len
;
90 a_len
= token_len_without_separator(a
->token
, strlen(a
->token
));
91 b_len
= token_len_without_separator(b
->token
, strlen(b
->token
));
92 min_len
= (a_len
> b_len
) ? b_len
: a_len
;
94 return !strncasecmp(a
->token
, b
->token
, min_len
);
97 static int same_value(struct trailer_item
*a
, struct arg_item
*b
)
99 return !strcasecmp(a
->value
, b
->value
);
102 static int same_trailer(struct trailer_item
*a
, struct arg_item
*b
)
104 return same_token(a
, b
) && same_value(a
, b
);
107 static inline int is_blank_line(const char *str
)
110 while (*s
&& *s
!= '\n' && isspace(*s
))
112 return !*s
|| *s
== '\n';
115 static inline void strbuf_replace(struct strbuf
*sb
, const char *a
, const char *b
)
117 const char *ptr
= strstr(sb
->buf
, a
);
119 strbuf_splice(sb
, ptr
- sb
->buf
, strlen(a
), b
, strlen(b
));
122 static void free_trailer_item(struct trailer_item
*item
)
129 static void free_arg_item(struct arg_item
*item
)
131 free(item
->conf
.name
);
132 free(item
->conf
.key
);
133 free(item
->conf
.command
);
139 static char last_non_space_char(const char *s
)
142 for (i
= strlen(s
) - 1; i
>= 0; i
--)
148 static void print_tok_val(FILE *outfile
, const char *tok
, const char *val
)
153 fprintf(outfile
, "%s\n", val
);
157 c
= last_non_space_char(tok
);
160 if (strchr(separators
, c
))
161 fprintf(outfile
, "%s%s\n", tok
, val
);
163 fprintf(outfile
, "%s%c %s\n", tok
, separators
[0], val
);
166 static void print_all(FILE *outfile
, struct list_head
*head
, int trim_empty
)
168 struct list_head
*pos
;
169 struct trailer_item
*item
;
170 list_for_each(pos
, head
) {
171 item
= list_entry(pos
, struct trailer_item
, list
);
172 if (!trim_empty
|| strlen(item
->value
) > 0)
173 print_tok_val(outfile
, item
->token
, item
->value
);
177 static struct trailer_item
*trailer_from_arg(struct arg_item
*arg_tok
)
179 struct trailer_item
*new = xcalloc(sizeof(*new), 1);
180 new->token
= arg_tok
->token
;
181 new->value
= arg_tok
->value
;
182 arg_tok
->token
= arg_tok
->value
= NULL
;
183 free_arg_item(arg_tok
);
187 static void add_arg_to_input_list(struct trailer_item
*on_tok
,
188 struct arg_item
*arg_tok
)
190 int aoe
= after_or_end(arg_tok
->conf
.where
);
191 struct trailer_item
*to_add
= trailer_from_arg(arg_tok
);
193 list_add(&to_add
->list
, &on_tok
->list
);
195 list_add_tail(&to_add
->list
, &on_tok
->list
);
198 static int check_if_different(struct trailer_item
*in_tok
,
199 struct arg_item
*arg_tok
,
201 struct list_head
*head
)
203 enum action_where where
= arg_tok
->conf
.where
;
204 struct list_head
*next_head
;
206 if (same_trailer(in_tok
, arg_tok
))
209 * if we want to add a trailer after another one,
210 * we have to check those before this one
212 next_head
= after_or_end(where
) ? in_tok
->list
.prev
214 if (next_head
== head
)
216 in_tok
= list_entry(next_head
, struct trailer_item
, list
);
221 static char *apply_command(const char *command
, const char *arg
)
223 struct strbuf cmd
= STRBUF_INIT
;
224 struct strbuf buf
= STRBUF_INIT
;
225 struct child_process cp
= CHILD_PROCESS_INIT
;
226 const char *argv
[] = {NULL
, NULL
};
229 strbuf_addstr(&cmd
, command
);
231 strbuf_replace(&cmd
, TRAILER_ARG_STRING
, arg
);
235 cp
.env
= local_repo_env
;
239 if (capture_command(&cp
, &buf
, 1024)) {
240 error(_("running trailer command '%s' failed"), cmd
.buf
);
241 strbuf_release(&buf
);
242 result
= xstrdup("");
245 result
= strbuf_detach(&buf
, NULL
);
248 strbuf_release(&cmd
);
252 static void apply_item_command(struct trailer_item
*in_tok
, struct arg_item
*arg_tok
)
254 if (arg_tok
->conf
.command
) {
256 if (arg_tok
->value
&& arg_tok
->value
[0]) {
257 arg
= arg_tok
->value
;
259 if (in_tok
&& in_tok
->value
)
260 arg
= xstrdup(in_tok
->value
);
264 arg_tok
->value
= apply_command(arg_tok
->conf
.command
, arg
);
269 static void apply_arg_if_exists(struct trailer_item
*in_tok
,
270 struct arg_item
*arg_tok
,
271 struct trailer_item
*on_tok
,
272 struct list_head
*head
)
274 switch (arg_tok
->conf
.if_exists
) {
275 case EXISTS_DO_NOTHING
:
276 free_arg_item(arg_tok
);
279 apply_item_command(in_tok
, arg_tok
);
280 add_arg_to_input_list(on_tok
, arg_tok
);
281 list_del(&in_tok
->list
);
282 free_trailer_item(in_tok
);
285 apply_item_command(in_tok
, arg_tok
);
286 add_arg_to_input_list(on_tok
, arg_tok
);
288 case EXISTS_ADD_IF_DIFFERENT
:
289 apply_item_command(in_tok
, arg_tok
);
290 if (check_if_different(in_tok
, arg_tok
, 1, head
))
291 add_arg_to_input_list(on_tok
, arg_tok
);
293 free_arg_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, head
))
298 add_arg_to_input_list(on_tok
, arg_tok
);
300 free_arg_item(arg_tok
);
305 static void apply_arg_if_missing(struct list_head
*head
,
306 struct arg_item
*arg_tok
)
308 enum action_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
);
326 static int find_same_and_apply_arg(struct list_head
*head
,
327 struct arg_item
*arg_tok
)
329 struct list_head
*pos
;
330 struct trailer_item
*in_tok
;
331 struct trailer_item
*on_tok
;
333 enum action_where where
= arg_tok
->conf
.where
;
334 int middle
= (where
== WHERE_AFTER
) || (where
== WHERE_BEFORE
);
335 int backwards
= after_or_end(where
);
336 struct trailer_item
*start_tok
;
338 if (list_empty(head
))
341 start_tok
= list_entry(backwards
? head
->prev
: head
->next
,
345 list_for_each_dir(pos
, head
, backwards
) {
346 in_tok
= list_entry(pos
, struct trailer_item
, list
);
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
, head
);
356 static void process_trailers_lists(struct list_head
*head
,
357 struct list_head
*arg_head
)
359 struct list_head
*pos
, *p
;
360 struct arg_item
*arg_tok
;
362 list_for_each_safe(pos
, p
, arg_head
) {
364 arg_tok
= list_entry(pos
, struct arg_item
, list
);
368 applied
= find_same_and_apply_arg(head
, arg_tok
);
371 apply_arg_if_missing(head
, arg_tok
);
375 static int set_where(struct conf_info
*item
, const char *value
)
377 if (!strcasecmp("after", value
))
378 item
->where
= WHERE_AFTER
;
379 else if (!strcasecmp("before", value
))
380 item
->where
= WHERE_BEFORE
;
381 else if (!strcasecmp("end", value
))
382 item
->where
= WHERE_END
;
383 else if (!strcasecmp("start", value
))
384 item
->where
= WHERE_START
;
390 static int set_if_exists(struct conf_info
*item
, const char *value
)
392 if (!strcasecmp("addIfDifferent", value
))
393 item
->if_exists
= EXISTS_ADD_IF_DIFFERENT
;
394 else if (!strcasecmp("addIfDifferentNeighbor", value
))
395 item
->if_exists
= EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
;
396 else if (!strcasecmp("add", value
))
397 item
->if_exists
= EXISTS_ADD
;
398 else if (!strcasecmp("replace", value
))
399 item
->if_exists
= EXISTS_REPLACE
;
400 else if (!strcasecmp("doNothing", value
))
401 item
->if_exists
= EXISTS_DO_NOTHING
;
407 static int set_if_missing(struct conf_info
*item
, const char *value
)
409 if (!strcasecmp("doNothing", value
))
410 item
->if_missing
= MISSING_DO_NOTHING
;
411 else if (!strcasecmp("add", value
))
412 item
->if_missing
= MISSING_ADD
;
418 static void duplicate_conf(struct conf_info
*dst
, const struct conf_info
*src
)
421 dst
->name
= xstrdup_or_null(src
->name
);
422 dst
->key
= xstrdup_or_null(src
->key
);
423 dst
->command
= xstrdup_or_null(src
->command
);
426 static struct arg_item
*get_conf_item(const char *name
)
428 struct list_head
*pos
;
429 struct arg_item
*item
;
431 /* Look up item with same name */
432 list_for_each(pos
, &conf_head
) {
433 item
= list_entry(pos
, struct arg_item
, list
);
434 if (!strcasecmp(item
->conf
.name
, name
))
438 /* Item does not already exists, create it */
439 item
= xcalloc(sizeof(*item
), 1);
440 duplicate_conf(&item
->conf
, &default_conf_info
);
441 item
->conf
.name
= xstrdup(name
);
443 list_add_tail(&item
->list
, &conf_head
);
448 enum trailer_info_type
{ TRAILER_KEY
, TRAILER_COMMAND
, TRAILER_WHERE
,
449 TRAILER_IF_EXISTS
, TRAILER_IF_MISSING
};
453 enum trailer_info_type type
;
454 } trailer_config_items
[] = {
455 { "key", TRAILER_KEY
},
456 { "command", TRAILER_COMMAND
},
457 { "where", TRAILER_WHERE
},
458 { "ifexists", TRAILER_IF_EXISTS
},
459 { "ifmissing", TRAILER_IF_MISSING
}
462 static int git_trailer_default_config(const char *conf_key
, const char *value
, void *cb
)
464 const char *trailer_item
, *variable_name
;
466 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
469 variable_name
= strrchr(trailer_item
, '.');
470 if (!variable_name
) {
471 if (!strcmp(trailer_item
, "where")) {
472 if (set_where(&default_conf_info
, value
) < 0)
473 warning(_("unknown value '%s' for key '%s'"),
475 } else if (!strcmp(trailer_item
, "ifexists")) {
476 if (set_if_exists(&default_conf_info
, value
) < 0)
477 warning(_("unknown value '%s' for key '%s'"),
479 } else if (!strcmp(trailer_item
, "ifmissing")) {
480 if (set_if_missing(&default_conf_info
, value
) < 0)
481 warning(_("unknown value '%s' for key '%s'"),
483 } else if (!strcmp(trailer_item
, "separators")) {
484 separators
= xstrdup(value
);
490 static int git_trailer_config(const char *conf_key
, const char *value
, void *cb
)
492 const char *trailer_item
, *variable_name
;
493 struct arg_item
*item
;
494 struct conf_info
*conf
;
496 enum trailer_info_type type
;
499 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
502 variable_name
= strrchr(trailer_item
, '.');
507 for (i
= 0; i
< ARRAY_SIZE(trailer_config_items
); i
++) {
508 if (strcmp(trailer_config_items
[i
].name
, variable_name
))
510 name
= xstrndup(trailer_item
, variable_name
- trailer_item
- 1);
511 type
= trailer_config_items
[i
].type
;
518 item
= get_conf_item(name
);
525 warning(_("more than one %s"), conf_key
);
526 conf
->key
= xstrdup(value
);
528 case TRAILER_COMMAND
:
530 warning(_("more than one %s"), conf_key
);
531 conf
->command
= xstrdup(value
);
534 if (set_where(conf
, value
))
535 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
537 case TRAILER_IF_EXISTS
:
538 if (set_if_exists(conf
, value
))
539 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
541 case TRAILER_IF_MISSING
:
542 if (set_if_missing(conf
, value
))
543 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
546 die("BUG: trailer.c: unhandled type %d", type
);
551 static void ensure_configured(void)
556 /* Default config must be setup first */
557 git_config(git_trailer_default_config
, NULL
);
558 git_config(git_trailer_config
, NULL
);
562 static const char *token_from_item(struct arg_item
*item
, char *tok
)
565 return item
->conf
.key
;
568 return item
->conf
.name
;
571 static int token_matches_item(const char *tok
, struct arg_item
*item
, int tok_len
)
573 if (!strncasecmp(tok
, item
->conf
.name
, tok_len
))
575 return item
->conf
.key
? !strncasecmp(tok
, item
->conf
.key
, tok_len
) : 0;
579 * If the given line is of the form
580 * "<token><optional whitespace><separator>..." or "<separator>...", return the
581 * location of the separator. Otherwise, return -1. The optional whitespace
582 * is allowed there primarily to allow things like "Bug #43" where <token> is
583 * "Bug" and <separator> is "#".
585 * The separator-starts-line case (in which this function returns 0) is
586 * distinguished from the non-well-formed-line case (in which this function
587 * returns -1) because some callers of this function need such a distinction.
589 static int find_separator(const char *line
, const char *separators
)
591 int whitespace_found
= 0;
593 for (c
= line
; *c
; c
++) {
594 if (strchr(separators
, *c
))
596 if (!whitespace_found
&& (isalnum(*c
) || *c
== '-'))
598 if (c
!= line
&& (*c
== ' ' || *c
== '\t')) {
599 whitespace_found
= 1;
608 * Obtain the token, value, and conf from the given trailer.
610 * separator_pos must not be 0, since the token cannot be an empty string.
612 * If separator_pos is -1, interpret the whole trailer as a token.
614 static void parse_trailer(struct strbuf
*tok
, struct strbuf
*val
,
615 const struct conf_info
**conf
, const char *trailer
,
618 struct arg_item
*item
;
620 struct list_head
*pos
;
622 if (separator_pos
!= -1) {
623 strbuf_add(tok
, trailer
, separator_pos
);
625 strbuf_addstr(val
, trailer
+ separator_pos
+ 1);
628 strbuf_addstr(tok
, trailer
);
632 /* Lookup if the token matches something in the config */
633 tok_len
= token_len_without_separator(tok
->buf
, tok
->len
);
635 *conf
= &default_conf_info
;
636 list_for_each(pos
, &conf_head
) {
637 item
= list_entry(pos
, struct arg_item
, list
);
638 if (token_matches_item(tok
->buf
, item
, tok_len
)) {
639 char *tok_buf
= strbuf_detach(tok
, NULL
);
642 strbuf_addstr(tok
, token_from_item(item
, tok_buf
));
649 static struct trailer_item
*add_trailer_item(struct list_head
*head
, char *tok
,
652 struct trailer_item
*new = xcalloc(sizeof(*new), 1);
655 list_add_tail(&new->list
, head
);
659 static void add_arg_item(struct list_head
*arg_head
, char *tok
, char *val
,
660 const struct conf_info
*conf
)
662 struct arg_item
*new = xcalloc(sizeof(*new), 1);
665 duplicate_conf(&new->conf
, conf
);
666 list_add_tail(&new->list
, arg_head
);
669 static void process_command_line_args(struct list_head
*arg_head
,
670 struct string_list
*trailers
)
672 struct string_list_item
*tr
;
673 struct arg_item
*item
;
674 struct strbuf tok
= STRBUF_INIT
;
675 struct strbuf val
= STRBUF_INIT
;
676 const struct conf_info
*conf
;
677 struct list_head
*pos
;
680 * In command-line arguments, '=' is accepted (in addition to the
681 * separators that are defined).
683 char *cl_separators
= xstrfmt("=%s", separators
);
685 /* Add an arg item for each configured trailer with a command */
686 list_for_each(pos
, &conf_head
) {
687 item
= list_entry(pos
, struct arg_item
, list
);
688 if (item
->conf
.command
)
689 add_arg_item(arg_head
,
690 xstrdup(token_from_item(item
, NULL
)),
695 /* Add an arg item for each trailer on the command line */
696 for_each_string_list_item(tr
, trailers
) {
697 int separator_pos
= find_separator(tr
->string
, cl_separators
);
698 if (separator_pos
== 0) {
699 struct strbuf sb
= STRBUF_INIT
;
700 strbuf_addstr(&sb
, tr
->string
);
702 error(_("empty trailer token in trailer '%.*s'"),
703 (int) sb
.len
, sb
.buf
);
706 parse_trailer(&tok
, &val
, &conf
, tr
->string
,
708 add_arg_item(arg_head
,
709 strbuf_detach(&tok
, NULL
),
710 strbuf_detach(&val
, NULL
),
718 static void read_input_file(struct strbuf
*sb
, const char *file
)
721 if (strbuf_read_file(sb
, file
, 0) < 0)
722 die_errno(_("could not read input file '%s'"), file
);
724 if (strbuf_read(sb
, fileno(stdin
), 0) < 0)
725 die_errno(_("could not read from stdin"));
729 static const char *next_line(const char *str
)
731 const char *nl
= strchrnul(str
, '\n');
736 * Return the position of the start of the last line. If len is 0, return -1.
738 static int last_line(const char *buf
, size_t len
)
746 * Skip the last character (in addition to the null terminator),
747 * because if the last character is a newline, it is considered as part
748 * of the last line anyway.
752 for (; i
>= 0; i
--) {
760 * Return the position of the start of the patch or the length of str if there
761 * is no patch in the message.
763 static int find_patch_start(const char *str
)
767 for (s
= str
; *s
; s
= next_line(s
)) {
768 if (starts_with(s
, "---"))
776 * Return the position of the first trailer line or len if there are no
779 static int find_trailer_start(const char *buf
, size_t len
)
782 int end_of_title
, l
, only_spaces
= 1;
783 int recognized_prefix
= 0, trailer_lines
= 0, non_trailer_lines
= 0;
785 * Number of possible continuation lines encountered. This will be
786 * reset to 0 if we encounter a trailer (since those lines are to be
787 * considered continuations of that trailer), and added to
788 * non_trailer_lines if we encounter a non-trailer (since those lines
789 * are to be considered non-trailers).
791 int possible_continuation_lines
= 0;
793 /* The first paragraph is the title and cannot be trailers */
794 for (s
= buf
; s
< buf
+ len
; s
= next_line(s
)) {
795 if (s
[0] == comment_line_char
)
797 if (is_blank_line(s
))
800 end_of_title
= s
- buf
;
803 * Get the start of the trailers by looking starting from the end for a
804 * blank line before a set of non-blank lines that (i) are all
805 * trailers, or (ii) contains at least one Git-generated trailer and
806 * consists of at least 25% trailers.
808 for (l
= last_line(buf
, len
);
810 l
= last_line(buf
, l
)) {
811 const char *bol
= buf
+ l
;
815 if (bol
[0] == comment_line_char
) {
816 non_trailer_lines
+= possible_continuation_lines
;
817 possible_continuation_lines
= 0;
820 if (is_blank_line(bol
)) {
823 non_trailer_lines
+= possible_continuation_lines
;
824 if (recognized_prefix
&&
825 trailer_lines
* 3 >= non_trailer_lines
)
826 return next_line(bol
) - buf
;
827 else if (trailer_lines
&& !non_trailer_lines
)
828 return next_line(bol
) - buf
;
833 for (p
= git_generated_prefixes
; *p
; p
++) {
834 if (starts_with(bol
, *p
)) {
836 possible_continuation_lines
= 0;
837 recognized_prefix
= 1;
838 goto continue_outer_loop
;
842 separator_pos
= find_separator(bol
, separators
);
843 if (separator_pos
>= 1 && !isspace(bol
[0])) {
844 struct list_head
*pos
;
847 possible_continuation_lines
= 0;
848 if (recognized_prefix
)
850 list_for_each(pos
, &conf_head
) {
851 struct arg_item
*item
;
852 item
= list_entry(pos
, struct arg_item
, list
);
853 if (token_matches_item(bol
, item
,
855 recognized_prefix
= 1;
859 } else if (isspace(bol
[0]))
860 possible_continuation_lines
++;
863 non_trailer_lines
+= possible_continuation_lines
;
864 possible_continuation_lines
= 0;
873 /* Return the position of the end of the trailers. */
874 static int find_trailer_end(const char *buf
, size_t len
)
876 return len
- ignore_non_trailer(buf
, len
);
879 static int ends_with_blank_line(const char *buf
, size_t len
)
881 int ll
= last_line(buf
, len
);
884 return is_blank_line(buf
+ ll
);
887 static int process_input_file(FILE *outfile
,
889 struct list_head
*head
)
891 struct trailer_info info
;
892 struct strbuf tok
= STRBUF_INIT
;
893 struct strbuf val
= STRBUF_INIT
;
896 trailer_info_get(&info
, str
);
898 /* Print lines before the trailers as is */
899 fwrite(str
, 1, info
.trailer_start
- str
, outfile
);
901 if (!info
.blank_line_before_trailer
)
902 fprintf(outfile
, "\n");
904 for (i
= 0; i
< info
.trailer_nr
; i
++) {
906 char *trailer
= info
.trailers
[i
];
907 if (trailer
[0] == comment_line_char
)
909 separator_pos
= find_separator(trailer
, separators
);
910 if (separator_pos
>= 1) {
911 parse_trailer(&tok
, &val
, NULL
, trailer
,
913 add_trailer_item(head
,
914 strbuf_detach(&tok
, NULL
),
915 strbuf_detach(&val
, NULL
));
917 strbuf_addstr(&val
, trailer
);
918 strbuf_strip_suffix(&val
, "\n");
919 add_trailer_item(head
,
921 strbuf_detach(&val
, NULL
));
925 trailer_info_release(&info
);
927 return info
.trailer_end
- str
;
930 static void free_all(struct list_head
*head
)
932 struct list_head
*pos
, *p
;
933 list_for_each_safe(pos
, p
, head
) {
935 free_trailer_item(list_entry(pos
, struct trailer_item
, list
));
939 static struct tempfile trailers_tempfile
;
941 static FILE *create_in_place_tempfile(const char *file
)
944 struct strbuf
template = STRBUF_INIT
;
949 die_errno(_("could not stat %s"), file
);
950 if (!S_ISREG(st
.st_mode
))
951 die(_("file %s is not a regular file"), file
);
952 if (!(st
.st_mode
& S_IWUSR
))
953 die(_("file %s is not writable by user"), file
);
955 /* Create temporary file in the same directory as the original */
956 tail
= strrchr(file
, '/');
958 strbuf_add(&template, file
, tail
- file
+ 1);
959 strbuf_addstr(&template, "git-interpret-trailers-XXXXXX");
961 xmks_tempfile_m(&trailers_tempfile
, template.buf
, st
.st_mode
);
962 strbuf_release(&template);
963 outfile
= fdopen_tempfile(&trailers_tempfile
, "w");
965 die_errno(_("could not open temporary file"));
970 void process_trailers(const char *file
, int in_place
, int trim_empty
, struct string_list
*trailers
)
974 struct strbuf sb
= STRBUF_INIT
;
976 FILE *outfile
= stdout
;
980 read_input_file(&sb
, file
);
983 outfile
= create_in_place_tempfile(file
);
985 /* Print the lines before the trailers */
986 trailer_end
= process_input_file(outfile
, sb
.buf
, &head
);
988 process_command_line_args(&arg_head
, trailers
);
990 process_trailers_lists(&head
, &arg_head
);
992 print_all(outfile
, &head
, trim_empty
);
996 /* Print the lines after the trailers as is */
997 fwrite(sb
.buf
+ trailer_end
, 1, sb
.len
- trailer_end
, outfile
);
1000 if (rename_tempfile(&trailers_tempfile
, file
))
1001 die_errno(_("could not rename temporary file to %s"), file
);
1003 strbuf_release(&sb
);
1006 void trailer_info_get(struct trailer_info
*info
, const char *str
)
1008 int patch_start
, trailer_end
, trailer_start
;
1009 struct strbuf
**trailer_lines
, **ptr
;
1010 char **trailer_strings
= NULL
;
1011 size_t nr
= 0, alloc
= 0;
1014 ensure_configured();
1016 patch_start
= find_patch_start(str
);
1017 trailer_end
= find_trailer_end(str
, patch_start
);
1018 trailer_start
= find_trailer_start(str
, trailer_end
);
1020 trailer_lines
= strbuf_split_buf(str
+ trailer_start
,
1021 trailer_end
- trailer_start
,
1024 for (ptr
= trailer_lines
; *ptr
; ptr
++) {
1025 if (last
&& isspace((*ptr
)->buf
[0])) {
1026 struct strbuf sb
= STRBUF_INIT
;
1027 strbuf_attach(&sb
, *last
, strlen(*last
), strlen(*last
));
1028 strbuf_addbuf(&sb
, *ptr
);
1029 *last
= strbuf_detach(&sb
, NULL
);
1032 ALLOC_GROW(trailer_strings
, nr
+ 1, alloc
);
1033 trailer_strings
[nr
] = strbuf_detach(*ptr
, NULL
);
1034 last
= find_separator(trailer_strings
[nr
], separators
) >= 1
1035 ? &trailer_strings
[nr
]
1039 strbuf_list_free(trailer_lines
);
1041 info
->blank_line_before_trailer
= ends_with_blank_line(str
,
1043 info
->trailer_start
= str
+ trailer_start
;
1044 info
->trailer_end
= str
+ trailer_end
;
1045 info
->trailers
= trailer_strings
;
1046 info
->trailer_nr
= nr
;
1049 void trailer_info_release(struct trailer_info
*info
)
1052 for (i
= 0; i
< info
->trailer_nr
; i
++)
1053 free(info
->trailers
[i
]);
1054 free(info
->trailers
);