3 #include "string-list.h"
4 #include "run-command.h"
10 * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org>
13 enum action_where
{ WHERE_END
, WHERE_AFTER
, WHERE_BEFORE
, WHERE_START
};
14 enum action_if_exists
{ EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
, EXISTS_ADD_IF_DIFFERENT
,
15 EXISTS_ADD
, EXISTS_REPLACE
, EXISTS_DO_NOTHING
};
16 enum action_if_missing
{ MISSING_ADD
, MISSING_DO_NOTHING
};
22 enum action_where where
;
23 enum action_if_exists if_exists
;
24 enum action_if_missing if_missing
;
27 static struct conf_info default_conf_info
;
30 struct list_head list
;
32 * If this is not a trailer line, the line is stored in value
33 * (excluding the terminating newline) and token is NULL.
40 struct list_head list
;
43 struct conf_info conf
;
46 static LIST_HEAD(conf_head
);
48 static char *separators
= ":";
50 static int configured
;
52 #define TRAILER_ARG_STRING "$ARG"
54 static const char *git_generated_prefixes
[] = {
56 "(cherry picked from commit ",
60 /* Iterate over the elements of the list. */
61 #define list_for_each_dir(pos, head, is_reverse) \
62 for (pos = is_reverse ? (head)->prev : (head)->next; \
64 pos = is_reverse ? pos->prev : pos->next)
66 static int after_or_end(enum action_where where
)
68 return (where
== WHERE_AFTER
) || (where
== WHERE_END
);
72 * Return the length of the string not including any final
73 * punctuation. E.g., the input "Signed-off-by:" would return
74 * 13, stripping the trailing punctuation but retaining
75 * internal punctuation.
77 static size_t token_len_without_separator(const char *token
, size_t len
)
79 while (len
> 0 && !isalnum(token
[len
- 1]))
84 static int same_token(struct trailer_item
*a
, struct arg_item
*b
)
86 size_t a_len
, b_len
, min_len
;
91 a_len
= token_len_without_separator(a
->token
, strlen(a
->token
));
92 b_len
= token_len_without_separator(b
->token
, strlen(b
->token
));
93 min_len
= (a_len
> b_len
) ? b_len
: a_len
;
95 return !strncasecmp(a
->token
, b
->token
, min_len
);
98 static int same_value(struct trailer_item
*a
, struct arg_item
*b
)
100 return !strcasecmp(a
->value
, b
->value
);
103 static int same_trailer(struct trailer_item
*a
, struct arg_item
*b
)
105 return same_token(a
, b
) && same_value(a
, b
);
108 static inline int is_blank_line(const char *str
)
111 while (*s
&& *s
!= '\n' && isspace(*s
))
113 return !*s
|| *s
== '\n';
116 static inline void strbuf_replace(struct strbuf
*sb
, const char *a
, const char *b
)
118 const char *ptr
= strstr(sb
->buf
, a
);
120 strbuf_splice(sb
, ptr
- sb
->buf
, strlen(a
), b
, strlen(b
));
123 static void free_trailer_item(struct trailer_item
*item
)
130 static void free_arg_item(struct arg_item
*item
)
132 free(item
->conf
.name
);
133 free(item
->conf
.key
);
134 free(item
->conf
.command
);
140 static char last_non_space_char(const char *s
)
143 for (i
= strlen(s
) - 1; i
>= 0; i
--)
149 static void print_tok_val(FILE *outfile
, const char *tok
, const char *val
)
154 fprintf(outfile
, "%s\n", val
);
158 c
= last_non_space_char(tok
);
161 if (strchr(separators
, c
))
162 fprintf(outfile
, "%s%s\n", tok
, val
);
164 fprintf(outfile
, "%s%c %s\n", tok
, separators
[0], val
);
167 static void print_all(FILE *outfile
, struct list_head
*head
, int trim_empty
)
169 struct list_head
*pos
;
170 struct trailer_item
*item
;
171 list_for_each(pos
, head
) {
172 item
= list_entry(pos
, struct trailer_item
, list
);
173 if (!trim_empty
|| strlen(item
->value
) > 0)
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 = xcalloc(sizeof(*new), 1);
181 new->token
= arg_tok
->token
;
182 new->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 action_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(const char *command
, const char *arg
)
224 struct strbuf cmd
= STRBUF_INIT
;
225 struct strbuf buf
= STRBUF_INIT
;
226 struct child_process cp
= CHILD_PROCESS_INIT
;
227 const char *argv
[] = {NULL
, NULL
};
230 strbuf_addstr(&cmd
, command
);
232 strbuf_replace(&cmd
, TRAILER_ARG_STRING
, arg
);
236 cp
.env
= local_repo_env
;
240 if (capture_command(&cp
, &buf
, 1024)) {
241 error(_("running trailer command '%s' failed"), cmd
.buf
);
242 strbuf_release(&buf
);
243 result
= xstrdup("");
246 result
= strbuf_detach(&buf
, NULL
);
249 strbuf_release(&cmd
);
253 static void apply_item_command(struct trailer_item
*in_tok
, struct arg_item
*arg_tok
)
255 if (arg_tok
->conf
.command
) {
257 if (arg_tok
->value
&& arg_tok
->value
[0]) {
258 arg
= arg_tok
->value
;
260 if (in_tok
&& in_tok
->value
)
261 arg
= xstrdup(in_tok
->value
);
265 arg_tok
->value
= apply_command(arg_tok
->conf
.command
, arg
);
270 static void apply_arg_if_exists(struct trailer_item
*in_tok
,
271 struct arg_item
*arg_tok
,
272 struct trailer_item
*on_tok
,
273 struct list_head
*head
)
275 switch (arg_tok
->conf
.if_exists
) {
276 case EXISTS_DO_NOTHING
:
277 free_arg_item(arg_tok
);
280 apply_item_command(in_tok
, arg_tok
);
281 add_arg_to_input_list(on_tok
, arg_tok
);
282 list_del(&in_tok
->list
);
283 free_trailer_item(in_tok
);
286 apply_item_command(in_tok
, arg_tok
);
287 add_arg_to_input_list(on_tok
, arg_tok
);
289 case EXISTS_ADD_IF_DIFFERENT
:
290 apply_item_command(in_tok
, arg_tok
);
291 if (check_if_different(in_tok
, arg_tok
, 1, head
))
292 add_arg_to_input_list(on_tok
, arg_tok
);
294 free_arg_item(arg_tok
);
296 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
:
297 apply_item_command(in_tok
, arg_tok
);
298 if (check_if_different(on_tok
, arg_tok
, 0, head
))
299 add_arg_to_input_list(on_tok
, arg_tok
);
301 free_arg_item(arg_tok
);
306 static void apply_arg_if_missing(struct list_head
*head
,
307 struct arg_item
*arg_tok
)
309 enum action_where where
;
310 struct trailer_item
*to_add
;
312 switch (arg_tok
->conf
.if_missing
) {
313 case MISSING_DO_NOTHING
:
314 free_arg_item(arg_tok
);
317 where
= arg_tok
->conf
.where
;
318 apply_item_command(NULL
, arg_tok
);
319 to_add
= trailer_from_arg(arg_tok
);
320 if (after_or_end(where
))
321 list_add_tail(&to_add
->list
, head
);
323 list_add(&to_add
->list
, head
);
327 static int find_same_and_apply_arg(struct list_head
*head
,
328 struct arg_item
*arg_tok
)
330 struct list_head
*pos
;
331 struct trailer_item
*in_tok
;
332 struct trailer_item
*on_tok
;
334 enum action_where where
= arg_tok
->conf
.where
;
335 int middle
= (where
== WHERE_AFTER
) || (where
== WHERE_BEFORE
);
336 int backwards
= after_or_end(where
);
337 struct trailer_item
*start_tok
;
339 if (list_empty(head
))
342 start_tok
= list_entry(backwards
? head
->prev
: head
->next
,
346 list_for_each_dir(pos
, head
, backwards
) {
347 in_tok
= list_entry(pos
, struct trailer_item
, list
);
348 if (!same_token(in_tok
, arg_tok
))
350 on_tok
= middle
? in_tok
: start_tok
;
351 apply_arg_if_exists(in_tok
, arg_tok
, on_tok
, head
);
357 static void process_trailers_lists(struct list_head
*head
,
358 struct list_head
*arg_head
)
360 struct list_head
*pos
, *p
;
361 struct arg_item
*arg_tok
;
363 list_for_each_safe(pos
, p
, arg_head
) {
365 arg_tok
= list_entry(pos
, struct arg_item
, list
);
369 applied
= find_same_and_apply_arg(head
, arg_tok
);
372 apply_arg_if_missing(head
, arg_tok
);
376 static int set_where(struct conf_info
*item
, const char *value
)
378 if (!strcasecmp("after", value
))
379 item
->where
= WHERE_AFTER
;
380 else if (!strcasecmp("before", value
))
381 item
->where
= WHERE_BEFORE
;
382 else if (!strcasecmp("end", value
))
383 item
->where
= WHERE_END
;
384 else if (!strcasecmp("start", value
))
385 item
->where
= WHERE_START
;
391 static int set_if_exists(struct conf_info
*item
, const char *value
)
393 if (!strcasecmp("addIfDifferent", value
))
394 item
->if_exists
= EXISTS_ADD_IF_DIFFERENT
;
395 else if (!strcasecmp("addIfDifferentNeighbor", value
))
396 item
->if_exists
= EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
;
397 else if (!strcasecmp("add", value
))
398 item
->if_exists
= EXISTS_ADD
;
399 else if (!strcasecmp("replace", value
))
400 item
->if_exists
= EXISTS_REPLACE
;
401 else if (!strcasecmp("doNothing", value
))
402 item
->if_exists
= EXISTS_DO_NOTHING
;
408 static int set_if_missing(struct conf_info
*item
, const char *value
)
410 if (!strcasecmp("doNothing", value
))
411 item
->if_missing
= MISSING_DO_NOTHING
;
412 else if (!strcasecmp("add", value
))
413 item
->if_missing
= MISSING_ADD
;
419 static void duplicate_conf(struct conf_info
*dst
, const struct conf_info
*src
)
422 dst
->name
= xstrdup_or_null(src
->name
);
423 dst
->key
= xstrdup_or_null(src
->key
);
424 dst
->command
= xstrdup_or_null(src
->command
);
427 static struct arg_item
*get_conf_item(const char *name
)
429 struct list_head
*pos
;
430 struct arg_item
*item
;
432 /* Look up item with same name */
433 list_for_each(pos
, &conf_head
) {
434 item
= list_entry(pos
, struct arg_item
, list
);
435 if (!strcasecmp(item
->conf
.name
, name
))
439 /* Item does not already exists, create it */
440 item
= xcalloc(sizeof(*item
), 1);
441 duplicate_conf(&item
->conf
, &default_conf_info
);
442 item
->conf
.name
= xstrdup(name
);
444 list_add_tail(&item
->list
, &conf_head
);
449 enum trailer_info_type
{ TRAILER_KEY
, TRAILER_COMMAND
, TRAILER_WHERE
,
450 TRAILER_IF_EXISTS
, TRAILER_IF_MISSING
};
454 enum trailer_info_type type
;
455 } trailer_config_items
[] = {
456 { "key", TRAILER_KEY
},
457 { "command", TRAILER_COMMAND
},
458 { "where", TRAILER_WHERE
},
459 { "ifexists", TRAILER_IF_EXISTS
},
460 { "ifmissing", TRAILER_IF_MISSING
}
463 static int git_trailer_default_config(const char *conf_key
, const char *value
, void *cb
)
465 const char *trailer_item
, *variable_name
;
467 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
470 variable_name
= strrchr(trailer_item
, '.');
471 if (!variable_name
) {
472 if (!strcmp(trailer_item
, "where")) {
473 if (set_where(&default_conf_info
, value
) < 0)
474 warning(_("unknown value '%s' for key '%s'"),
476 } else if (!strcmp(trailer_item
, "ifexists")) {
477 if (set_if_exists(&default_conf_info
, value
) < 0)
478 warning(_("unknown value '%s' for key '%s'"),
480 } else if (!strcmp(trailer_item
, "ifmissing")) {
481 if (set_if_missing(&default_conf_info
, value
) < 0)
482 warning(_("unknown value '%s' for key '%s'"),
484 } else if (!strcmp(trailer_item
, "separators")) {
485 separators
= xstrdup(value
);
491 static int git_trailer_config(const char *conf_key
, const char *value
, void *cb
)
493 const char *trailer_item
, *variable_name
;
494 struct arg_item
*item
;
495 struct conf_info
*conf
;
497 enum trailer_info_type type
;
500 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
503 variable_name
= strrchr(trailer_item
, '.');
508 for (i
= 0; i
< ARRAY_SIZE(trailer_config_items
); i
++) {
509 if (strcmp(trailer_config_items
[i
].name
, variable_name
))
511 name
= xstrndup(trailer_item
, variable_name
- trailer_item
- 1);
512 type
= trailer_config_items
[i
].type
;
519 item
= get_conf_item(name
);
526 warning(_("more than one %s"), conf_key
);
527 conf
->key
= xstrdup(value
);
529 case TRAILER_COMMAND
:
531 warning(_("more than one %s"), conf_key
);
532 conf
->command
= xstrdup(value
);
535 if (set_where(conf
, value
))
536 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
538 case TRAILER_IF_EXISTS
:
539 if (set_if_exists(conf
, value
))
540 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
542 case TRAILER_IF_MISSING
:
543 if (set_if_missing(conf
, value
))
544 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
547 die("BUG: trailer.c: unhandled type %d", type
);
552 static void ensure_configured(void)
557 /* Default config must be setup first */
558 git_config(git_trailer_default_config
, NULL
);
559 git_config(git_trailer_config
, NULL
);
563 static const char *token_from_item(struct arg_item
*item
, char *tok
)
566 return item
->conf
.key
;
569 return item
->conf
.name
;
572 static int token_matches_item(const char *tok
, struct arg_item
*item
, int tok_len
)
574 if (!strncasecmp(tok
, item
->conf
.name
, tok_len
))
576 return item
->conf
.key
? !strncasecmp(tok
, item
->conf
.key
, tok_len
) : 0;
580 * If the given line is of the form
581 * "<token><optional whitespace><separator>..." or "<separator>...", return the
582 * location of the separator. Otherwise, return -1. The optional whitespace
583 * is allowed there primarily to allow things like "Bug #43" where <token> is
584 * "Bug" and <separator> is "#".
586 * The separator-starts-line case (in which this function returns 0) is
587 * distinguished from the non-well-formed-line case (in which this function
588 * returns -1) because some callers of this function need such a distinction.
590 static int find_separator(const char *line
, const char *separators
)
592 int whitespace_found
= 0;
594 for (c
= line
; *c
; c
++) {
595 if (strchr(separators
, *c
))
597 if (!whitespace_found
&& (isalnum(*c
) || *c
== '-'))
599 if (c
!= line
&& (*c
== ' ' || *c
== '\t')) {
600 whitespace_found
= 1;
609 * Obtain the token, value, and conf from the given trailer.
611 * separator_pos must not be 0, since the token cannot be an empty string.
613 * If separator_pos is -1, interpret the whole trailer as a token.
615 static void parse_trailer(struct strbuf
*tok
, struct strbuf
*val
,
616 const struct conf_info
**conf
, const char *trailer
,
619 struct arg_item
*item
;
621 struct list_head
*pos
;
623 if (separator_pos
!= -1) {
624 strbuf_add(tok
, trailer
, separator_pos
);
626 strbuf_addstr(val
, trailer
+ separator_pos
+ 1);
629 strbuf_addstr(tok
, trailer
);
633 /* Lookup if the token matches something in the config */
634 tok_len
= token_len_without_separator(tok
->buf
, tok
->len
);
636 *conf
= &default_conf_info
;
637 list_for_each(pos
, &conf_head
) {
638 item
= list_entry(pos
, struct arg_item
, list
);
639 if (token_matches_item(tok
->buf
, item
, tok_len
)) {
640 char *tok_buf
= strbuf_detach(tok
, NULL
);
643 strbuf_addstr(tok
, token_from_item(item
, tok_buf
));
650 static struct trailer_item
*add_trailer_item(struct list_head
*head
, char *tok
,
653 struct trailer_item
*new = xcalloc(sizeof(*new), 1);
656 list_add_tail(&new->list
, head
);
660 static void add_arg_item(struct list_head
*arg_head
, char *tok
, char *val
,
661 const struct conf_info
*conf
)
663 struct arg_item
*new = xcalloc(sizeof(*new), 1);
666 duplicate_conf(&new->conf
, conf
);
667 list_add_tail(&new->list
, arg_head
);
670 static void process_command_line_args(struct list_head
*arg_head
,
671 struct string_list
*trailers
)
673 struct string_list_item
*tr
;
674 struct arg_item
*item
;
675 struct strbuf tok
= STRBUF_INIT
;
676 struct strbuf val
= STRBUF_INIT
;
677 const struct conf_info
*conf
;
678 struct list_head
*pos
;
681 * In command-line arguments, '=' is accepted (in addition to the
682 * separators that are defined).
684 char *cl_separators
= xstrfmt("=%s", separators
);
686 /* Add an arg item for each configured trailer with a command */
687 list_for_each(pos
, &conf_head
) {
688 item
= list_entry(pos
, struct arg_item
, list
);
689 if (item
->conf
.command
)
690 add_arg_item(arg_head
,
691 xstrdup(token_from_item(item
, NULL
)),
696 /* Add an arg item for each trailer on the command line */
697 for_each_string_list_item(tr
, trailers
) {
698 int separator_pos
= find_separator(tr
->string
, cl_separators
);
699 if (separator_pos
== 0) {
700 struct strbuf sb
= STRBUF_INIT
;
701 strbuf_addstr(&sb
, tr
->string
);
703 error(_("empty trailer token in trailer '%.*s'"),
704 (int) sb
.len
, sb
.buf
);
707 parse_trailer(&tok
, &val
, &conf
, tr
->string
,
709 add_arg_item(arg_head
,
710 strbuf_detach(&tok
, NULL
),
711 strbuf_detach(&val
, NULL
),
719 static void read_input_file(struct strbuf
*sb
, const char *file
)
722 if (strbuf_read_file(sb
, file
, 0) < 0)
723 die_errno(_("could not read input file '%s'"), file
);
725 if (strbuf_read(sb
, fileno(stdin
), 0) < 0)
726 die_errno(_("could not read from stdin"));
730 static const char *next_line(const char *str
)
732 const char *nl
= strchrnul(str
, '\n');
737 * Return the position of the start of the last line. If len is 0, return -1.
739 static int last_line(const char *buf
, size_t len
)
747 * Skip the last character (in addition to the null terminator),
748 * because if the last character is a newline, it is considered as part
749 * of the last line anyway.
753 for (; i
>= 0; i
--) {
761 * Return the position of the start of the patch or the length of str if there
762 * is no patch in the message.
764 static int find_patch_start(const char *str
)
768 for (s
= str
; *s
; s
= next_line(s
)) {
769 if (starts_with(s
, "---"))
777 * Return the position of the first trailer line or len if there are no
780 static int find_trailer_start(const char *buf
, size_t len
)
783 int end_of_title
, l
, only_spaces
= 1;
784 int recognized_prefix
= 0, trailer_lines
= 0, non_trailer_lines
= 0;
786 * Number of possible continuation lines encountered. This will be
787 * reset to 0 if we encounter a trailer (since those lines are to be
788 * considered continuations of that trailer), and added to
789 * non_trailer_lines if we encounter a non-trailer (since those lines
790 * are to be considered non-trailers).
792 int possible_continuation_lines
= 0;
794 /* The first paragraph is the title and cannot be trailers */
795 for (s
= buf
; s
< buf
+ len
; s
= next_line(s
)) {
796 if (s
[0] == comment_line_char
)
798 if (is_blank_line(s
))
801 end_of_title
= s
- buf
;
804 * Get the start of the trailers by looking starting from the end for a
805 * blank line before a set of non-blank lines that (i) are all
806 * trailers, or (ii) contains at least one Git-generated trailer and
807 * consists of at least 25% trailers.
809 for (l
= last_line(buf
, len
);
811 l
= last_line(buf
, l
)) {
812 const char *bol
= buf
+ l
;
816 if (bol
[0] == comment_line_char
) {
817 non_trailer_lines
+= possible_continuation_lines
;
818 possible_continuation_lines
= 0;
821 if (is_blank_line(bol
)) {
824 non_trailer_lines
+= possible_continuation_lines
;
825 if (recognized_prefix
&&
826 trailer_lines
* 3 >= non_trailer_lines
)
827 return next_line(bol
) - buf
;
828 else if (trailer_lines
&& !non_trailer_lines
)
829 return next_line(bol
) - buf
;
834 for (p
= git_generated_prefixes
; *p
; p
++) {
835 if (starts_with(bol
, *p
)) {
837 possible_continuation_lines
= 0;
838 recognized_prefix
= 1;
839 goto continue_outer_loop
;
843 separator_pos
= find_separator(bol
, separators
);
844 if (separator_pos
>= 1 && !isspace(bol
[0])) {
845 struct list_head
*pos
;
848 possible_continuation_lines
= 0;
849 if (recognized_prefix
)
851 list_for_each(pos
, &conf_head
) {
852 struct arg_item
*item
;
853 item
= list_entry(pos
, struct arg_item
, list
);
854 if (token_matches_item(bol
, item
,
856 recognized_prefix
= 1;
860 } else if (isspace(bol
[0]))
861 possible_continuation_lines
++;
864 non_trailer_lines
+= possible_continuation_lines
;
865 possible_continuation_lines
= 0;
874 /* Return the position of the end of the trailers. */
875 static int find_trailer_end(const char *buf
, size_t len
)
877 return len
- ignore_non_trailer(buf
, len
);
880 static int ends_with_blank_line(const char *buf
, size_t len
)
882 int ll
= last_line(buf
, len
);
885 return is_blank_line(buf
+ ll
);
888 static int process_input_file(FILE *outfile
,
890 struct list_head
*head
)
892 struct trailer_info info
;
893 struct strbuf tok
= STRBUF_INIT
;
894 struct strbuf val
= STRBUF_INIT
;
897 trailer_info_get(&info
, str
);
899 /* Print lines before the trailers as is */
900 fwrite(str
, 1, info
.trailer_start
- str
, outfile
);
902 if (!info
.blank_line_before_trailer
)
903 fprintf(outfile
, "\n");
905 for (i
= 0; i
< info
.trailer_nr
; i
++) {
907 char *trailer
= info
.trailers
[i
];
908 if (trailer
[0] == comment_line_char
)
910 separator_pos
= find_separator(trailer
, separators
);
911 if (separator_pos
>= 1) {
912 parse_trailer(&tok
, &val
, NULL
, trailer
,
914 add_trailer_item(head
,
915 strbuf_detach(&tok
, NULL
),
916 strbuf_detach(&val
, NULL
));
918 strbuf_addstr(&val
, trailer
);
919 strbuf_strip_suffix(&val
, "\n");
920 add_trailer_item(head
,
922 strbuf_detach(&val
, NULL
));
926 trailer_info_release(&info
);
928 return info
.trailer_end
- str
;
931 static void free_all(struct list_head
*head
)
933 struct list_head
*pos
, *p
;
934 list_for_each_safe(pos
, p
, head
) {
936 free_trailer_item(list_entry(pos
, struct trailer_item
, list
));
940 static struct tempfile trailers_tempfile
;
942 static FILE *create_in_place_tempfile(const char *file
)
945 struct strbuf
template = STRBUF_INIT
;
950 die_errno(_("could not stat %s"), file
);
951 if (!S_ISREG(st
.st_mode
))
952 die(_("file %s is not a regular file"), file
);
953 if (!(st
.st_mode
& S_IWUSR
))
954 die(_("file %s is not writable by user"), file
);
956 /* Create temporary file in the same directory as the original */
957 tail
= strrchr(file
, '/');
959 strbuf_add(&template, file
, tail
- file
+ 1);
960 strbuf_addstr(&template, "git-interpret-trailers-XXXXXX");
962 xmks_tempfile_m(&trailers_tempfile
, template.buf
, st
.st_mode
);
963 strbuf_release(&template);
964 outfile
= fdopen_tempfile(&trailers_tempfile
, "w");
966 die_errno(_("could not open temporary file"));
971 void process_trailers(const char *file
, int in_place
, int trim_empty
, struct string_list
*trailers
)
975 struct strbuf sb
= STRBUF_INIT
;
977 FILE *outfile
= stdout
;
981 read_input_file(&sb
, file
);
984 outfile
= create_in_place_tempfile(file
);
986 /* Print the lines before the trailers */
987 trailer_end
= process_input_file(outfile
, sb
.buf
, &head
);
989 process_command_line_args(&arg_head
, trailers
);
991 process_trailers_lists(&head
, &arg_head
);
993 print_all(outfile
, &head
, trim_empty
);
997 /* Print the lines after the trailers as is */
998 fwrite(sb
.buf
+ trailer_end
, 1, sb
.len
- trailer_end
, outfile
);
1001 if (rename_tempfile(&trailers_tempfile
, file
))
1002 die_errno(_("could not rename temporary file to %s"), file
);
1004 strbuf_release(&sb
);
1007 void trailer_info_get(struct trailer_info
*info
, const char *str
)
1009 int patch_start
, trailer_end
, trailer_start
;
1010 struct strbuf
**trailer_lines
, **ptr
;
1011 char **trailer_strings
= NULL
;
1012 size_t nr
= 0, alloc
= 0;
1015 ensure_configured();
1017 patch_start
= find_patch_start(str
);
1018 trailer_end
= find_trailer_end(str
, patch_start
);
1019 trailer_start
= find_trailer_start(str
, trailer_end
);
1021 trailer_lines
= strbuf_split_buf(str
+ trailer_start
,
1022 trailer_end
- trailer_start
,
1025 for (ptr
= trailer_lines
; *ptr
; ptr
++) {
1026 if (last
&& isspace((*ptr
)->buf
[0])) {
1027 struct strbuf sb
= STRBUF_INIT
;
1028 strbuf_attach(&sb
, *last
, strlen(*last
), strlen(*last
));
1029 strbuf_addbuf(&sb
, *ptr
);
1030 *last
= strbuf_detach(&sb
, NULL
);
1033 ALLOC_GROW(trailer_strings
, nr
+ 1, alloc
);
1034 trailer_strings
[nr
] = strbuf_detach(*ptr
, NULL
);
1035 last
= find_separator(trailer_strings
[nr
], separators
) >= 1
1036 ? &trailer_strings
[nr
]
1040 strbuf_list_free(trailer_lines
);
1042 info
->blank_line_before_trailer
= ends_with_blank_line(str
,
1044 info
->trailer_start
= str
+ trailer_start
;
1045 info
->trailer_end
= str
+ trailer_end
;
1046 info
->trailers
= trailer_strings
;
1047 info
->trailer_nr
= nr
;
1050 void trailer_info_release(struct trailer_info
*info
)
1053 for (i
= 0; i
< info
->trailer_nr
; i
++)
1054 free(info
->trailers
[i
]);
1055 free(info
->trailers
);