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 #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 action_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 contains_only_spaces(const char *str
)
108 while (*s
&& isspace(*s
))
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
);
137 static char last_non_space_char(const char *s
)
140 for (i
= strlen(s
) - 1; i
>= 0; i
--)
146 static void print_tok_val(FILE *outfile
, const char *tok
, const char *val
)
151 fprintf(outfile
, "%s\n", val
);
155 c
= last_non_space_char(tok
);
158 if (strchr(separators
, c
))
159 fprintf(outfile
, "%s%s\n", tok
, val
);
161 fprintf(outfile
, "%s%c %s\n", tok
, separators
[0], val
);
164 static void print_all(FILE *outfile
, struct list_head
*head
, int trim_empty
)
166 struct list_head
*pos
;
167 struct trailer_item
*item
;
168 list_for_each(pos
, head
) {
169 item
= list_entry(pos
, struct trailer_item
, list
);
170 if (!trim_empty
|| strlen(item
->value
) > 0)
171 print_tok_val(outfile
, item
->token
, item
->value
);
175 static struct trailer_item
*trailer_from_arg(struct arg_item
*arg_tok
)
177 struct trailer_item
*new = xcalloc(sizeof(*new), 1);
178 new->token
= arg_tok
->token
;
179 new->value
= arg_tok
->value
;
180 arg_tok
->token
= arg_tok
->value
= NULL
;
181 free_arg_item(arg_tok
);
185 static void add_arg_to_input_list(struct trailer_item
*on_tok
,
186 struct arg_item
*arg_tok
)
188 int aoe
= after_or_end(arg_tok
->conf
.where
);
189 struct trailer_item
*to_add
= trailer_from_arg(arg_tok
);
191 list_add(&to_add
->list
, &on_tok
->list
);
193 list_add_tail(&to_add
->list
, &on_tok
->list
);
196 static int check_if_different(struct trailer_item
*in_tok
,
197 struct arg_item
*arg_tok
,
199 struct list_head
*head
)
201 enum action_where where
= arg_tok
->conf
.where
;
202 struct list_head
*next_head
;
204 if (same_trailer(in_tok
, arg_tok
))
207 * if we want to add a trailer after another one,
208 * we have to check those before this one
210 next_head
= after_or_end(where
) ? in_tok
->list
.prev
212 if (next_head
== head
)
214 in_tok
= list_entry(next_head
, struct trailer_item
, list
);
219 static char *apply_command(const char *command
, const char *arg
)
221 struct strbuf cmd
= STRBUF_INIT
;
222 struct strbuf buf
= STRBUF_INIT
;
223 struct child_process cp
= CHILD_PROCESS_INIT
;
224 const char *argv
[] = {NULL
, NULL
};
227 strbuf_addstr(&cmd
, command
);
229 strbuf_replace(&cmd
, TRAILER_ARG_STRING
, arg
);
233 cp
.env
= local_repo_env
;
237 if (capture_command(&cp
, &buf
, 1024)) {
238 error(_("running trailer command '%s' failed"), cmd
.buf
);
239 strbuf_release(&buf
);
240 result
= xstrdup("");
243 result
= strbuf_detach(&buf
, NULL
);
246 strbuf_release(&cmd
);
250 static void apply_item_command(struct trailer_item
*in_tok
, struct arg_item
*arg_tok
)
252 if (arg_tok
->conf
.command
) {
254 if (arg_tok
->value
&& arg_tok
->value
[0]) {
255 arg
= arg_tok
->value
;
257 if (in_tok
&& in_tok
->value
)
258 arg
= xstrdup(in_tok
->value
);
262 arg_tok
->value
= apply_command(arg_tok
->conf
.command
, arg
);
267 static void apply_arg_if_exists(struct trailer_item
*in_tok
,
268 struct arg_item
*arg_tok
,
269 struct trailer_item
*on_tok
,
270 struct list_head
*head
)
272 switch (arg_tok
->conf
.if_exists
) {
273 case EXISTS_DO_NOTHING
:
274 free_arg_item(arg_tok
);
277 apply_item_command(in_tok
, arg_tok
);
278 add_arg_to_input_list(on_tok
, arg_tok
);
279 list_del(&in_tok
->list
);
280 free_trailer_item(in_tok
);
283 apply_item_command(in_tok
, arg_tok
);
284 add_arg_to_input_list(on_tok
, arg_tok
);
286 case EXISTS_ADD_IF_DIFFERENT
:
287 apply_item_command(in_tok
, arg_tok
);
288 if (check_if_different(in_tok
, arg_tok
, 1, head
))
289 add_arg_to_input_list(on_tok
, arg_tok
);
291 free_arg_item(arg_tok
);
293 case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
:
294 apply_item_command(in_tok
, arg_tok
);
295 if (check_if_different(on_tok
, arg_tok
, 0, head
))
296 add_arg_to_input_list(on_tok
, arg_tok
);
298 free_arg_item(arg_tok
);
303 static void apply_arg_if_missing(struct list_head
*head
,
304 struct arg_item
*arg_tok
)
306 enum action_where where
;
307 struct trailer_item
*to_add
;
309 switch (arg_tok
->conf
.if_missing
) {
310 case MISSING_DO_NOTHING
:
311 free_arg_item(arg_tok
);
314 where
= arg_tok
->conf
.where
;
315 apply_item_command(NULL
, arg_tok
);
316 to_add
= trailer_from_arg(arg_tok
);
317 if (after_or_end(where
))
318 list_add_tail(&to_add
->list
, head
);
320 list_add(&to_add
->list
, head
);
324 static int find_same_and_apply_arg(struct list_head
*head
,
325 struct arg_item
*arg_tok
)
327 struct list_head
*pos
;
328 struct trailer_item
*in_tok
;
329 struct trailer_item
*on_tok
;
331 enum action_where where
= arg_tok
->conf
.where
;
332 int middle
= (where
== WHERE_AFTER
) || (where
== WHERE_BEFORE
);
333 int backwards
= after_or_end(where
);
334 struct trailer_item
*start_tok
;
336 if (list_empty(head
))
339 start_tok
= list_entry(backwards
? head
->prev
: head
->next
,
343 list_for_each_dir(pos
, head
, backwards
) {
344 in_tok
= list_entry(pos
, struct trailer_item
, list
);
345 if (!same_token(in_tok
, arg_tok
))
347 on_tok
= middle
? in_tok
: start_tok
;
348 apply_arg_if_exists(in_tok
, arg_tok
, on_tok
, head
);
354 static void process_trailers_lists(struct list_head
*head
,
355 struct list_head
*arg_head
)
357 struct list_head
*pos
, *p
;
358 struct arg_item
*arg_tok
;
360 list_for_each_safe(pos
, p
, arg_head
) {
362 arg_tok
= list_entry(pos
, struct arg_item
, list
);
366 applied
= find_same_and_apply_arg(head
, arg_tok
);
369 apply_arg_if_missing(head
, arg_tok
);
373 static int set_where(struct conf_info
*item
, const char *value
)
375 if (!strcasecmp("after", value
))
376 item
->where
= WHERE_AFTER
;
377 else if (!strcasecmp("before", value
))
378 item
->where
= WHERE_BEFORE
;
379 else if (!strcasecmp("end", value
))
380 item
->where
= WHERE_END
;
381 else if (!strcasecmp("start", value
))
382 item
->where
= WHERE_START
;
388 static int set_if_exists(struct conf_info
*item
, const char *value
)
390 if (!strcasecmp("addIfDifferent", value
))
391 item
->if_exists
= EXISTS_ADD_IF_DIFFERENT
;
392 else if (!strcasecmp("addIfDifferentNeighbor", value
))
393 item
->if_exists
= EXISTS_ADD_IF_DIFFERENT_NEIGHBOR
;
394 else if (!strcasecmp("add", value
))
395 item
->if_exists
= EXISTS_ADD
;
396 else if (!strcasecmp("replace", value
))
397 item
->if_exists
= EXISTS_REPLACE
;
398 else if (!strcasecmp("doNothing", value
))
399 item
->if_exists
= EXISTS_DO_NOTHING
;
405 static int set_if_missing(struct conf_info
*item
, const char *value
)
407 if (!strcasecmp("doNothing", value
))
408 item
->if_missing
= MISSING_DO_NOTHING
;
409 else if (!strcasecmp("add", value
))
410 item
->if_missing
= MISSING_ADD
;
416 static void duplicate_conf(struct conf_info
*dst
, const struct conf_info
*src
)
419 dst
->name
= xstrdup_or_null(src
->name
);
420 dst
->key
= xstrdup_or_null(src
->key
);
421 dst
->command
= xstrdup_or_null(src
->command
);
424 static struct arg_item
*get_conf_item(const char *name
)
426 struct list_head
*pos
;
427 struct arg_item
*item
;
429 /* Look up item with same name */
430 list_for_each(pos
, &conf_head
) {
431 item
= list_entry(pos
, struct arg_item
, list
);
432 if (!strcasecmp(item
->conf
.name
, name
))
436 /* Item does not already exists, create it */
437 item
= xcalloc(sizeof(*item
), 1);
438 duplicate_conf(&item
->conf
, &default_conf_info
);
439 item
->conf
.name
= xstrdup(name
);
441 list_add_tail(&item
->list
, &conf_head
);
446 enum trailer_info_type
{ TRAILER_KEY
, TRAILER_COMMAND
, TRAILER_WHERE
,
447 TRAILER_IF_EXISTS
, TRAILER_IF_MISSING
};
451 enum trailer_info_type type
;
452 } trailer_config_items
[] = {
453 { "key", TRAILER_KEY
},
454 { "command", TRAILER_COMMAND
},
455 { "where", TRAILER_WHERE
},
456 { "ifexists", TRAILER_IF_EXISTS
},
457 { "ifmissing", TRAILER_IF_MISSING
}
460 static int git_trailer_default_config(const char *conf_key
, const char *value
, void *cb
)
462 const char *trailer_item
, *variable_name
;
464 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
467 variable_name
= strrchr(trailer_item
, '.');
468 if (!variable_name
) {
469 if (!strcmp(trailer_item
, "where")) {
470 if (set_where(&default_conf_info
, value
) < 0)
471 warning(_("unknown value '%s' for key '%s'"),
473 } else if (!strcmp(trailer_item
, "ifexists")) {
474 if (set_if_exists(&default_conf_info
, value
) < 0)
475 warning(_("unknown value '%s' for key '%s'"),
477 } else if (!strcmp(trailer_item
, "ifmissing")) {
478 if (set_if_missing(&default_conf_info
, value
) < 0)
479 warning(_("unknown value '%s' for key '%s'"),
481 } else if (!strcmp(trailer_item
, "separators")) {
482 separators
= xstrdup(value
);
488 static int git_trailer_config(const char *conf_key
, const char *value
, void *cb
)
490 const char *trailer_item
, *variable_name
;
491 struct arg_item
*item
;
492 struct conf_info
*conf
;
494 enum trailer_info_type type
;
497 if (!skip_prefix(conf_key
, "trailer.", &trailer_item
))
500 variable_name
= strrchr(trailer_item
, '.');
505 for (i
= 0; i
< ARRAY_SIZE(trailer_config_items
); i
++) {
506 if (strcmp(trailer_config_items
[i
].name
, variable_name
))
508 name
= xstrndup(trailer_item
, variable_name
- trailer_item
- 1);
509 type
= trailer_config_items
[i
].type
;
516 item
= get_conf_item(name
);
523 warning(_("more than one %s"), conf_key
);
524 conf
->key
= xstrdup(value
);
526 case TRAILER_COMMAND
:
528 warning(_("more than one %s"), conf_key
);
529 conf
->command
= xstrdup(value
);
532 if (set_where(conf
, value
))
533 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
535 case TRAILER_IF_EXISTS
:
536 if (set_if_exists(conf
, value
))
537 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
539 case TRAILER_IF_MISSING
:
540 if (set_if_missing(conf
, value
))
541 warning(_("unknown value '%s' for key '%s'"), value
, conf_key
);
544 die("BUG: trailer.c: unhandled type %d", type
);
549 static const char *token_from_item(struct arg_item
*item
, char *tok
)
552 return item
->conf
.key
;
555 return item
->conf
.name
;
558 static int token_matches_item(const char *tok
, struct arg_item
*item
, int tok_len
)
560 if (!strncasecmp(tok
, item
->conf
.name
, tok_len
))
562 return item
->conf
.key
? !strncasecmp(tok
, item
->conf
.key
, tok_len
) : 0;
566 * Return the location of the first separator in line, or -1 if there is no
569 static int find_separator(const char *line
, const char *separators
)
571 int loc
= strcspn(line
, separators
);
578 * Obtain the token, value, and conf from the given trailer.
580 * separator_pos must not be 0, since the token cannot be an empty string.
582 * If separator_pos is -1, interpret the whole trailer as a token.
584 static void parse_trailer(struct strbuf
*tok
, struct strbuf
*val
,
585 const struct conf_info
**conf
, const char *trailer
,
588 struct arg_item
*item
;
590 struct list_head
*pos
;
592 if (separator_pos
!= -1) {
593 strbuf_add(tok
, trailer
, separator_pos
);
595 strbuf_addstr(val
, trailer
+ separator_pos
+ 1);
598 strbuf_addstr(tok
, trailer
);
602 /* Lookup if the token matches something in the config */
603 tok_len
= token_len_without_separator(tok
->buf
, tok
->len
);
605 *conf
= &default_conf_info
;
606 list_for_each(pos
, &conf_head
) {
607 item
= list_entry(pos
, struct arg_item
, list
);
608 if (token_matches_item(tok
->buf
, item
, tok_len
)) {
609 char *tok_buf
= strbuf_detach(tok
, NULL
);
612 strbuf_addstr(tok
, token_from_item(item
, tok_buf
));
619 static struct trailer_item
*add_trailer_item(struct list_head
*head
, char *tok
,
622 struct trailer_item
*new = xcalloc(sizeof(*new), 1);
625 list_add_tail(&new->list
, head
);
629 static void add_arg_item(struct list_head
*arg_head
, char *tok
, char *val
,
630 const struct conf_info
*conf
)
632 struct arg_item
*new = xcalloc(sizeof(*new), 1);
635 duplicate_conf(&new->conf
, conf
);
636 list_add_tail(&new->list
, arg_head
);
639 static void process_command_line_args(struct list_head
*arg_head
,
640 struct string_list
*trailers
)
642 struct string_list_item
*tr
;
643 struct arg_item
*item
;
644 struct strbuf tok
= STRBUF_INIT
;
645 struct strbuf val
= STRBUF_INIT
;
646 const struct conf_info
*conf
;
647 struct list_head
*pos
;
650 * In command-line arguments, '=' is accepted (in addition to the
651 * separators that are defined).
653 char *cl_separators
= xstrfmt("=%s", separators
);
655 /* Add an arg item for each configured trailer with a command */
656 list_for_each(pos
, &conf_head
) {
657 item
= list_entry(pos
, struct arg_item
, list
);
658 if (item
->conf
.command
)
659 add_arg_item(arg_head
,
660 xstrdup(token_from_item(item
, NULL
)),
665 /* Add an arg item for each trailer on the command line */
666 for_each_string_list_item(tr
, trailers
) {
667 int separator_pos
= find_separator(tr
->string
, cl_separators
);
668 if (separator_pos
== 0) {
669 struct strbuf sb
= STRBUF_INIT
;
670 strbuf_addstr(&sb
, tr
->string
);
672 error(_("empty trailer token in trailer '%.*s'"),
673 (int) sb
.len
, sb
.buf
);
676 parse_trailer(&tok
, &val
, &conf
, tr
->string
,
678 add_arg_item(arg_head
,
679 strbuf_detach(&tok
, NULL
),
680 strbuf_detach(&val
, NULL
),
688 static struct strbuf
**read_input_file(const char *file
)
690 struct strbuf
**lines
;
691 struct strbuf sb
= STRBUF_INIT
;
694 if (strbuf_read_file(&sb
, file
, 0) < 0)
695 die_errno(_("could not read input file '%s'"), file
);
697 if (strbuf_read(&sb
, fileno(stdin
), 0) < 0)
698 die_errno(_("could not read from stdin"));
701 lines
= strbuf_split(&sb
, '\n');
709 * Return the (0 based) index of the start of the patch or the line
710 * count if there is no patch in the message.
712 static int find_patch_start(struct strbuf
**lines
, int count
)
716 /* Get the start of the patch part if any */
717 for (i
= 0; i
< count
; i
++) {
718 if (starts_with(lines
[i
]->buf
, "---"))
726 * Return the (0 based) index of the first trailer line or count if
727 * there are no trailers. Trailers are searched only in the lines from
728 * index (count - 1) down to index 0.
730 static int find_trailer_start(struct strbuf
**lines
, int count
)
732 int start
, end_of_title
, only_spaces
= 1;
733 int recognized_prefix
= 0, trailer_lines
= 0, non_trailer_lines
= 0;
735 * Number of possible continuation lines encountered. This will be
736 * reset to 0 if we encounter a trailer (since those lines are to be
737 * considered continuations of that trailer), and added to
738 * non_trailer_lines if we encounter a non-trailer (since those lines
739 * are to be considered non-trailers).
741 int possible_continuation_lines
= 0;
743 /* The first paragraph is the title and cannot be trailers */
744 for (start
= 0; start
< count
; start
++) {
745 if (lines
[start
]->buf
[0] == comment_line_char
)
747 if (contains_only_spaces(lines
[start
]->buf
))
750 end_of_title
= start
;
753 * Get the start of the trailers by looking starting from the end for a
754 * blank line before a set of non-blank lines that (i) are all
755 * trailers, or (ii) contains at least one Git-generated trailer and
756 * consists of at least 25% trailers.
758 for (start
= count
- 1; start
>= end_of_title
; start
--) {
762 if (lines
[start
]->buf
[0] == comment_line_char
) {
763 non_trailer_lines
+= possible_continuation_lines
;
764 possible_continuation_lines
= 0;
767 if (contains_only_spaces(lines
[start
]->buf
)) {
770 non_trailer_lines
+= possible_continuation_lines
;
771 if (recognized_prefix
&&
772 trailer_lines
* 3 >= non_trailer_lines
)
774 if (trailer_lines
&& !non_trailer_lines
)
780 for (p
= git_generated_prefixes
; *p
; p
++) {
781 if (starts_with(lines
[start
]->buf
, *p
)) {
783 possible_continuation_lines
= 0;
784 recognized_prefix
= 1;
785 goto continue_outer_loop
;
789 separator_pos
= find_separator(lines
[start
]->buf
, separators
);
790 if (separator_pos
>= 1 && !isspace(lines
[start
]->buf
[0])) {
791 struct list_head
*pos
;
794 possible_continuation_lines
= 0;
795 if (recognized_prefix
)
797 list_for_each(pos
, &conf_head
) {
798 struct arg_item
*item
;
799 item
= list_entry(pos
, struct arg_item
, list
);
800 if (token_matches_item(lines
[start
]->buf
, item
,
802 recognized_prefix
= 1;
806 } else if (isspace(lines
[start
]->buf
[0]))
807 possible_continuation_lines
++;
810 non_trailer_lines
+= possible_continuation_lines
;
811 possible_continuation_lines
= 0;
820 /* Get the index of the end of the trailers */
821 static int find_trailer_end(struct strbuf
**lines
, int patch_start
)
823 struct strbuf sb
= STRBUF_INIT
;
826 for (i
= 0; i
< patch_start
; i
++)
827 strbuf_addbuf(&sb
, lines
[i
]);
828 ignore_bytes
= ignore_non_trailer(&sb
);
830 for (i
= patch_start
- 1; i
>= 0 && ignore_bytes
> 0; i
--)
831 ignore_bytes
-= lines
[i
]->len
;
836 static int has_blank_line_before(struct strbuf
**lines
, int start
)
838 for (;start
>= 0; start
--) {
839 if (lines
[start
]->buf
[0] == comment_line_char
)
841 return contains_only_spaces(lines
[start
]->buf
);
846 static void print_lines(FILE *outfile
, struct strbuf
**lines
, int start
, int end
)
849 for (i
= start
; lines
[i
] && i
< end
; i
++)
850 fprintf(outfile
, "%s", lines
[i
]->buf
);
853 static int process_input_file(FILE *outfile
,
854 struct strbuf
**lines
,
855 struct list_head
*head
)
858 int patch_start
, trailer_start
, trailer_end
, i
;
859 struct strbuf tok
= STRBUF_INIT
;
860 struct strbuf val
= STRBUF_INIT
;
861 struct trailer_item
*last
= NULL
;
863 /* Get the line count */
867 patch_start
= find_patch_start(lines
, count
);
868 trailer_end
= find_trailer_end(lines
, patch_start
);
869 trailer_start
= find_trailer_start(lines
, trailer_end
);
871 /* Print lines before the trailers as is */
872 print_lines(outfile
, lines
, 0, trailer_start
);
874 if (!has_blank_line_before(lines
, trailer_start
- 1))
875 fprintf(outfile
, "\n");
877 /* Parse trailer lines */
878 for (i
= trailer_start
; i
< trailer_end
; i
++) {
880 if (lines
[i
]->buf
[0] == comment_line_char
)
882 if (last
&& isspace(lines
[i
]->buf
[0])) {
883 struct strbuf sb
= STRBUF_INIT
;
884 strbuf_addf(&sb
, "%s\n%s", last
->value
, lines
[i
]->buf
);
885 strbuf_strip_suffix(&sb
, "\n");
887 last
->value
= strbuf_detach(&sb
, NULL
);
890 separator_pos
= find_separator(lines
[i
]->buf
, separators
);
891 if (separator_pos
>= 1) {
892 parse_trailer(&tok
, &val
, NULL
, lines
[i
]->buf
,
894 last
= add_trailer_item(head
,
895 strbuf_detach(&tok
, NULL
),
896 strbuf_detach(&val
, NULL
));
898 strbuf_addbuf(&val
, lines
[i
]);
899 strbuf_strip_suffix(&val
, "\n");
900 add_trailer_item(head
,
902 strbuf_detach(&val
, NULL
));
910 static void free_all(struct list_head
*head
)
912 struct list_head
*pos
, *p
;
913 list_for_each_safe(pos
, p
, head
) {
915 free_trailer_item(list_entry(pos
, struct trailer_item
, list
));
919 static struct tempfile trailers_tempfile
;
921 static FILE *create_in_place_tempfile(const char *file
)
924 struct strbuf
template = STRBUF_INIT
;
929 die_errno(_("could not stat %s"), file
);
930 if (!S_ISREG(st
.st_mode
))
931 die(_("file %s is not a regular file"), file
);
932 if (!(st
.st_mode
& S_IWUSR
))
933 die(_("file %s is not writable by user"), file
);
935 /* Create temporary file in the same directory as the original */
936 tail
= strrchr(file
, '/');
938 strbuf_add(&template, file
, tail
- file
+ 1);
939 strbuf_addstr(&template, "git-interpret-trailers-XXXXXX");
941 xmks_tempfile_m(&trailers_tempfile
, template.buf
, st
.st_mode
);
942 strbuf_release(&template);
943 outfile
= fdopen_tempfile(&trailers_tempfile
, "w");
945 die_errno(_("could not open temporary file"));
950 void process_trailers(const char *file
, int in_place
, int trim_empty
, struct string_list
*trailers
)
954 struct strbuf
**lines
;
956 FILE *outfile
= stdout
;
958 /* Default config must be setup first */
959 git_config(git_trailer_default_config
, NULL
);
960 git_config(git_trailer_config
, NULL
);
962 lines
= read_input_file(file
);
965 outfile
= create_in_place_tempfile(file
);
967 /* Print the lines before the trailers */
968 trailer_end
= process_input_file(outfile
, lines
, &head
);
970 process_command_line_args(&arg_head
, trailers
);
972 process_trailers_lists(&head
, &arg_head
);
974 print_all(outfile
, &head
, trim_empty
);
978 /* Print the lines after the trailers as is */
979 print_lines(outfile
, lines
, trailer_end
, INT_MAX
);
982 if (rename_tempfile(&trailers_tempfile
, file
))
983 die_errno(_("could not rename temporary file to %s"), file
);
985 strbuf_list_free(lines
);