|
1 | 1 | #include "cache.h"
|
2 | 2 | #include "string-list.h"
|
| 3 | +#include "trailer.h" |
3 | 4 | /*
|
4 | 5 | * Copyright (c) 2013, 2014 Christian Couder <[email protected]>
|
5 | 6 | */
|
@@ -87,6 +88,35 @@ static void free_trailer_item(struct trailer_item *item)
|
87 | 88 | free(item);
|
88 | 89 | }
|
89 | 90 |
|
| 91 | +static char last_non_space_char(const char *s) |
| 92 | +{ |
| 93 | + int i; |
| 94 | + for (i = strlen(s) - 1; i >= 0; i--) |
| 95 | + if (!isspace(s[i])) |
| 96 | + return s[i]; |
| 97 | + return '\0'; |
| 98 | +} |
| 99 | + |
| 100 | +static void print_tok_val(const char *tok, const char *val) |
| 101 | +{ |
| 102 | + char c = last_non_space_char(tok); |
| 103 | + if (!c) |
| 104 | + return; |
| 105 | + if (strchr(separators, c)) |
| 106 | + printf("%s%s\n", tok, val); |
| 107 | + else |
| 108 | + printf("%s%c %s\n", tok, separators[0], val); |
| 109 | +} |
| 110 | + |
| 111 | +static void print_all(struct trailer_item *first, int trim_empty) |
| 112 | +{ |
| 113 | + struct trailer_item *item; |
| 114 | + for (item = first; item; item = item->next) { |
| 115 | + if (!trim_empty || strlen(item->value) > 0) |
| 116 | + print_tok_val(item->token, item->value); |
| 117 | + } |
| 118 | +} |
| 119 | + |
90 | 120 | static void update_last(struct trailer_item **last)
|
91 | 121 | {
|
92 | 122 | if (*last)
|
@@ -697,3 +727,42 @@ static int process_input_file(struct strbuf **lines,
|
697 | 727 |
|
698 | 728 | return patch_start;
|
699 | 729 | }
|
| 730 | + |
| 731 | +static void free_all(struct trailer_item **first) |
| 732 | +{ |
| 733 | + while (*first) { |
| 734 | + struct trailer_item *item = remove_first(first); |
| 735 | + free_trailer_item(item); |
| 736 | + } |
| 737 | +} |
| 738 | + |
| 739 | +void process_trailers(const char *file, int trim_empty, struct string_list *trailers) |
| 740 | +{ |
| 741 | + struct trailer_item *in_tok_first = NULL; |
| 742 | + struct trailer_item *in_tok_last = NULL; |
| 743 | + struct trailer_item *arg_tok_first; |
| 744 | + struct strbuf **lines; |
| 745 | + int patch_start; |
| 746 | + |
| 747 | + /* Default config must be setup first */ |
| 748 | + git_config(git_trailer_default_config, NULL); |
| 749 | + git_config(git_trailer_config, NULL); |
| 750 | + |
| 751 | + lines = read_input_file(file); |
| 752 | + |
| 753 | + /* Print the lines before the trailers */ |
| 754 | + patch_start = process_input_file(lines, &in_tok_first, &in_tok_last); |
| 755 | + |
| 756 | + arg_tok_first = process_command_line_args(trailers); |
| 757 | + |
| 758 | + process_trailers_lists(&in_tok_first, &in_tok_last, &arg_tok_first); |
| 759 | + |
| 760 | + print_all(in_tok_first, trim_empty); |
| 761 | + |
| 762 | + free_all(&in_tok_first); |
| 763 | + |
| 764 | + /* Print the lines after the trailers as is */ |
| 765 | + print_lines(lines, patch_start, INT_MAX); |
| 766 | + |
| 767 | + strbuf_list_free(lines); |
| 768 | +} |
0 commit comments