Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 1 | #include "cache.h" |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 2 | #include "string-list.h" |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 3 | #include "run-command.h" |
Christian Couder | 61cfef4 | 2014-11-09 10:23:42 +0100 | [diff] [blame] | 4 | #include "commit.h" |
Tobias Klauser | e1f8986 | 2016-01-14 17:57:55 +0100 | [diff] [blame] | 5 | #include "tempfile.h" |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 6 | #include "trailer.h" |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 7 | #include "list.h" |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 8 | /* |
| 9 | * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org> |
| 10 | */ |
| 11 | |
| 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 }; |
| 16 | |
| 17 | struct conf_info { |
| 18 | char *name; |
| 19 | char *key; |
| 20 | char *command; |
| 21 | enum action_where where; |
| 22 | enum action_if_exists if_exists; |
| 23 | enum action_if_missing if_missing; |
| 24 | }; |
| 25 | |
| 26 | static struct conf_info default_conf_info; |
| 27 | |
| 28 | struct trailer_item { |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 29 | struct list_head list; |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 30 | /* |
| 31 | * If this is not a trailer line, the line is stored in value |
| 32 | * (excluding the terminating newline) and token is NULL. |
| 33 | */ |
Jonathan Tan | d65fd42 | 2016-10-14 10:37:58 -0700 | [diff] [blame] | 34 | char *token; |
| 35 | char *value; |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | struct arg_item { |
| 39 | struct list_head list; |
| 40 | char *token; |
| 41 | char *value; |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 42 | struct conf_info conf; |
| 43 | }; |
| 44 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 45 | static LIST_HEAD(conf_head); |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 46 | |
| 47 | static char *separators = ":"; |
| 48 | |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 49 | static int configured; |
| 50 | |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 51 | #define TRAILER_ARG_STRING "$ARG" |
| 52 | |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 53 | static const char *git_generated_prefixes[] = { |
| 54 | "Signed-off-by: ", |
| 55 | "(cherry picked from commit ", |
| 56 | NULL |
| 57 | }; |
| 58 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 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; \ |
| 62 | pos != (head); \ |
| 63 | pos = is_reverse ? pos->prev : pos->next) |
| 64 | |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 65 | static int after_or_end(enum action_where where) |
| 66 | { |
| 67 | return (where == WHERE_AFTER) || (where == WHERE_END); |
| 68 | } |
| 69 | |
| 70 | /* |
| 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. |
| 75 | */ |
| 76 | static size_t token_len_without_separator(const char *token, size_t len) |
| 77 | { |
| 78 | while (len > 0 && !isalnum(token[len - 1])) |
| 79 | len--; |
| 80 | return len; |
| 81 | } |
| 82 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 83 | static int same_token(struct trailer_item *a, struct arg_item *b) |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 84 | { |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 85 | size_t a_len, b_len, min_len; |
| 86 | |
| 87 | if (!a->token) |
| 88 | return 0; |
| 89 | |
| 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; |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 93 | |
| 94 | return !strncasecmp(a->token, b->token, min_len); |
| 95 | } |
| 96 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 97 | static int same_value(struct trailer_item *a, struct arg_item *b) |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 98 | { |
| 99 | return !strcasecmp(a->value, b->value); |
| 100 | } |
| 101 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 102 | static int same_trailer(struct trailer_item *a, struct arg_item *b) |
Christian Couder | 9385b5d | 2014-10-13 20:16:23 +0200 | [diff] [blame] | 103 | { |
| 104 | return same_token(a, b) && same_value(a, b); |
| 105 | } |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 106 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 107 | static inline int is_blank_line(const char *str) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 108 | { |
| 109 | const char *s = str; |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 110 | while (*s && *s != '\n' && isspace(*s)) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 111 | s++; |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 112 | return !*s || *s == '\n'; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 115 | static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b) |
| 116 | { |
| 117 | const char *ptr = strstr(sb->buf, a); |
| 118 | if (ptr) |
| 119 | strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b)); |
| 120 | } |
| 121 | |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 122 | static void free_trailer_item(struct trailer_item *item) |
| 123 | { |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 124 | free(item->token); |
| 125 | free(item->value); |
| 126 | free(item); |
| 127 | } |
| 128 | |
| 129 | static void free_arg_item(struct arg_item *item) |
| 130 | { |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 131 | free(item->conf.name); |
| 132 | free(item->conf.key); |
| 133 | free(item->conf.command); |
Jonathan Tan | d65fd42 | 2016-10-14 10:37:58 -0700 | [diff] [blame] | 134 | free(item->token); |
| 135 | free(item->value); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 136 | free(item); |
| 137 | } |
| 138 | |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 139 | static char last_non_space_char(const char *s) |
| 140 | { |
| 141 | int i; |
| 142 | for (i = strlen(s) - 1; i >= 0; i--) |
| 143 | if (!isspace(s[i])) |
| 144 | return s[i]; |
| 145 | return '\0'; |
| 146 | } |
| 147 | |
Tobias Klauser | d0d2344 | 2016-01-14 17:57:54 +0100 | [diff] [blame] | 148 | static void print_tok_val(FILE *outfile, const char *tok, const char *val) |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 149 | { |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 150 | char c; |
| 151 | |
| 152 | if (!tok) { |
| 153 | fprintf(outfile, "%s\n", val); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | c = last_non_space_char(tok); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 158 | if (!c) |
| 159 | return; |
| 160 | if (strchr(separators, c)) |
Tobias Klauser | d0d2344 | 2016-01-14 17:57:54 +0100 | [diff] [blame] | 161 | fprintf(outfile, "%s%s\n", tok, val); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 162 | else |
Tobias Klauser | d0d2344 | 2016-01-14 17:57:54 +0100 | [diff] [blame] | 163 | fprintf(outfile, "%s%c %s\n", tok, separators[0], val); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 166 | static void print_all(FILE *outfile, struct list_head *head, int trim_empty) |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 167 | { |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 168 | struct list_head *pos; |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 169 | struct trailer_item *item; |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 170 | list_for_each(pos, head) { |
| 171 | item = list_entry(pos, struct trailer_item, list); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 172 | if (!trim_empty || strlen(item->value) > 0) |
Tobias Klauser | d0d2344 | 2016-01-14 17:57:54 +0100 | [diff] [blame] | 173 | print_tok_val(outfile, item->token, item->value); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 177 | static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok) |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 178 | { |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 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); |
| 184 | return new; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static void add_arg_to_input_list(struct trailer_item *on_tok, |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 188 | struct arg_item *arg_tok) |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 189 | { |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 190 | int aoe = after_or_end(arg_tok->conf.where); |
| 191 | struct trailer_item *to_add = trailer_from_arg(arg_tok); |
| 192 | if (aoe) |
| 193 | list_add(&to_add->list, &on_tok->list); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 194 | else |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 195 | list_add_tail(&to_add->list, &on_tok->list); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static int check_if_different(struct trailer_item *in_tok, |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 199 | struct arg_item *arg_tok, |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 200 | int check_all, |
| 201 | struct list_head *head) |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 202 | { |
| 203 | enum action_where where = arg_tok->conf.where; |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 204 | struct list_head *next_head; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 205 | do { |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 206 | if (same_trailer(in_tok, arg_tok)) |
| 207 | return 0; |
| 208 | /* |
| 209 | * if we want to add a trailer after another one, |
| 210 | * we have to check those before this one |
| 211 | */ |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 212 | next_head = after_or_end(where) ? in_tok->list.prev |
| 213 | : in_tok->list.next; |
| 214 | if (next_head == head) |
| 215 | break; |
| 216 | in_tok = list_entry(next_head, struct trailer_item, list); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 217 | } while (check_all); |
| 218 | return 1; |
| 219 | } |
| 220 | |
Jonathan Tan | d65fd42 | 2016-10-14 10:37:58 -0700 | [diff] [blame] | 221 | static char *apply_command(const char *command, const char *arg) |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 222 | { |
| 223 | struct strbuf cmd = STRBUF_INIT; |
| 224 | struct strbuf buf = STRBUF_INIT; |
René Scharfe | b226293 | 2014-11-09 14:49:58 +0100 | [diff] [blame] | 225 | struct child_process cp = CHILD_PROCESS_INIT; |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 226 | const char *argv[] = {NULL, NULL}; |
Jonathan Tan | d65fd42 | 2016-10-14 10:37:58 -0700 | [diff] [blame] | 227 | char *result; |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 228 | |
| 229 | strbuf_addstr(&cmd, command); |
| 230 | if (arg) |
| 231 | strbuf_replace(&cmd, TRAILER_ARG_STRING, arg); |
| 232 | |
| 233 | argv[0] = cmd.buf; |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 234 | cp.argv = argv; |
| 235 | cp.env = local_repo_env; |
| 236 | cp.no_stdin = 1; |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 237 | cp.use_shell = 1; |
| 238 | |
Jeff King | c5eadca | 2015-03-22 23:54:00 -0400 | [diff] [blame] | 239 | if (capture_command(&cp, &buf, 1024)) { |
Nguyễn Thái Ngọc Duy | 13ad56f | 2016-02-27 13:42:10 +0700 | [diff] [blame] | 240 | error(_("running trailer command '%s' failed"), cmd.buf); |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 241 | strbuf_release(&buf); |
| 242 | result = xstrdup(""); |
Jeff King | c5eadca | 2015-03-22 23:54:00 -0400 | [diff] [blame] | 243 | } else { |
| 244 | strbuf_trim(&buf); |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 245 | result = strbuf_detach(&buf, NULL); |
Jeff King | c5eadca | 2015-03-22 23:54:00 -0400 | [diff] [blame] | 246 | } |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 247 | |
| 248 | strbuf_release(&cmd); |
| 249 | return result; |
| 250 | } |
| 251 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 252 | static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok) |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 253 | { |
| 254 | if (arg_tok->conf.command) { |
| 255 | const char *arg; |
| 256 | if (arg_tok->value && arg_tok->value[0]) { |
| 257 | arg = arg_tok->value; |
| 258 | } else { |
| 259 | if (in_tok && in_tok->value) |
| 260 | arg = xstrdup(in_tok->value); |
| 261 | else |
| 262 | arg = xstrdup(""); |
| 263 | } |
| 264 | arg_tok->value = apply_command(arg_tok->conf.command, arg); |
| 265 | free((char *)arg); |
| 266 | } |
| 267 | } |
| 268 | |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 269 | static void apply_arg_if_exists(struct trailer_item *in_tok, |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 270 | struct arg_item *arg_tok, |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 271 | struct trailer_item *on_tok, |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 272 | struct list_head *head) |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 273 | { |
| 274 | switch (arg_tok->conf.if_exists) { |
| 275 | case EXISTS_DO_NOTHING: |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 276 | free_arg_item(arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 277 | break; |
| 278 | case EXISTS_REPLACE: |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 279 | apply_item_command(in_tok, arg_tok); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 280 | add_arg_to_input_list(on_tok, arg_tok); |
| 281 | list_del(&in_tok->list); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 282 | free_trailer_item(in_tok); |
| 283 | break; |
| 284 | case EXISTS_ADD: |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 285 | apply_item_command(in_tok, arg_tok); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 286 | add_arg_to_input_list(on_tok, arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 287 | break; |
| 288 | case EXISTS_ADD_IF_DIFFERENT: |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 289 | apply_item_command(in_tok, arg_tok); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 290 | if (check_if_different(in_tok, arg_tok, 1, head)) |
| 291 | add_arg_to_input_list(on_tok, arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 292 | else |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 293 | free_arg_item(arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 294 | break; |
| 295 | case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR: |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 296 | apply_item_command(in_tok, arg_tok); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 297 | if (check_if_different(on_tok, arg_tok, 0, head)) |
| 298 | add_arg_to_input_list(on_tok, arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 299 | else |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 300 | free_arg_item(arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 301 | break; |
| 302 | } |
| 303 | } |
| 304 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 305 | static void apply_arg_if_missing(struct list_head *head, |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 306 | struct arg_item *arg_tok) |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 307 | { |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 308 | enum action_where where; |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 309 | struct trailer_item *to_add; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 310 | |
| 311 | switch (arg_tok->conf.if_missing) { |
| 312 | case MISSING_DO_NOTHING: |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 313 | free_arg_item(arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 314 | break; |
| 315 | case MISSING_ADD: |
| 316 | where = arg_tok->conf.where; |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 317 | apply_item_command(NULL, arg_tok); |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 318 | to_add = trailer_from_arg(arg_tok); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 319 | if (after_or_end(where)) |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 320 | list_add_tail(&to_add->list, head); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 321 | else |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 322 | list_add(&to_add->list, head); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 326 | static int find_same_and_apply_arg(struct list_head *head, |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 327 | struct arg_item *arg_tok) |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 328 | { |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 329 | struct list_head *pos; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 330 | struct trailer_item *in_tok; |
| 331 | struct trailer_item *on_tok; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 332 | |
| 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); |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 336 | struct trailer_item *start_tok; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 337 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 338 | if (list_empty(head)) |
| 339 | return 0; |
| 340 | |
| 341 | start_tok = list_entry(backwards ? head->prev : head->next, |
| 342 | struct trailer_item, |
| 343 | list); |
| 344 | |
| 345 | list_for_each_dir(pos, head, backwards) { |
| 346 | in_tok = list_entry(pos, struct trailer_item, list); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 347 | if (!same_token(in_tok, arg_tok)) |
| 348 | continue; |
| 349 | on_tok = middle ? in_tok : start_tok; |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 350 | apply_arg_if_exists(in_tok, arg_tok, on_tok, head); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 351 | return 1; |
| 352 | } |
| 353 | return 0; |
| 354 | } |
| 355 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 356 | static void process_trailers_lists(struct list_head *head, |
| 357 | struct list_head *arg_head) |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 358 | { |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 359 | struct list_head *pos, *p; |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 360 | struct arg_item *arg_tok; |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 361 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 362 | list_for_each_safe(pos, p, arg_head) { |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 363 | int applied = 0; |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 364 | arg_tok = list_entry(pos, struct arg_item, list); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 365 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 366 | list_del(pos); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 367 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 368 | applied = find_same_and_apply_arg(head, arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 369 | |
| 370 | if (!applied) |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 371 | apply_arg_if_missing(head, arg_tok); |
Christian Couder | 4103818 | 2014-10-13 20:16:24 +0200 | [diff] [blame] | 372 | } |
| 373 | } |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 374 | |
| 375 | static int set_where(struct conf_info *item, const char *value) |
| 376 | { |
| 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; |
| 385 | else |
| 386 | return -1; |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int set_if_exists(struct conf_info *item, const char *value) |
| 391 | { |
| 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; |
| 402 | else |
| 403 | return -1; |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static int set_if_missing(struct conf_info *item, const char *value) |
| 408 | { |
| 409 | if (!strcasecmp("doNothing", value)) |
| 410 | item->if_missing = MISSING_DO_NOTHING; |
| 411 | else if (!strcasecmp("add", value)) |
| 412 | item->if_missing = MISSING_ADD; |
| 413 | else |
| 414 | return -1; |
| 415 | return 0; |
| 416 | } |
| 417 | |
Jonathan Tan | d65fd42 | 2016-10-14 10:37:58 -0700 | [diff] [blame] | 418 | static void duplicate_conf(struct conf_info *dst, const struct conf_info *src) |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 419 | { |
| 420 | *dst = *src; |
Junio C Hamano | 13092a9 | 2016-10-12 11:20:23 -0700 | [diff] [blame] | 421 | dst->name = xstrdup_or_null(src->name); |
| 422 | dst->key = xstrdup_or_null(src->key); |
| 423 | dst->command = xstrdup_or_null(src->command); |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 424 | } |
| 425 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 426 | static struct arg_item *get_conf_item(const char *name) |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 427 | { |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 428 | struct list_head *pos; |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 429 | struct arg_item *item; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 430 | |
| 431 | /* Look up item with same name */ |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 432 | list_for_each(pos, &conf_head) { |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 433 | item = list_entry(pos, struct arg_item, list); |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 434 | if (!strcasecmp(item->conf.name, name)) |
| 435 | return item; |
| 436 | } |
| 437 | |
| 438 | /* Item does not already exists, create it */ |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 439 | item = xcalloc(sizeof(*item), 1); |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 440 | duplicate_conf(&item->conf, &default_conf_info); |
| 441 | item->conf.name = xstrdup(name); |
| 442 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 443 | list_add_tail(&item->list, &conf_head); |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 444 | |
| 445 | return item; |
| 446 | } |
| 447 | |
| 448 | enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_WHERE, |
| 449 | TRAILER_IF_EXISTS, TRAILER_IF_MISSING }; |
| 450 | |
| 451 | static struct { |
| 452 | const char *name; |
| 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 } |
| 460 | }; |
| 461 | |
| 462 | static int git_trailer_default_config(const char *conf_key, const char *value, void *cb) |
| 463 | { |
| 464 | const char *trailer_item, *variable_name; |
| 465 | |
| 466 | if (!skip_prefix(conf_key, "trailer.", &trailer_item)) |
| 467 | return 0; |
| 468 | |
| 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'"), |
| 474 | value, conf_key); |
| 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'"), |
| 478 | value, conf_key); |
| 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'"), |
| 482 | value, conf_key); |
| 483 | } else if (!strcmp(trailer_item, "separators")) { |
| 484 | separators = xstrdup(value); |
| 485 | } |
| 486 | } |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static int git_trailer_config(const char *conf_key, const char *value, void *cb) |
| 491 | { |
| 492 | const char *trailer_item, *variable_name; |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 493 | struct arg_item *item; |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 494 | struct conf_info *conf; |
| 495 | char *name = NULL; |
| 496 | enum trailer_info_type type; |
| 497 | int i; |
| 498 | |
| 499 | if (!skip_prefix(conf_key, "trailer.", &trailer_item)) |
| 500 | return 0; |
| 501 | |
| 502 | variable_name = strrchr(trailer_item, '.'); |
| 503 | if (!variable_name) |
| 504 | return 0; |
| 505 | |
| 506 | variable_name++; |
| 507 | for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) { |
| 508 | if (strcmp(trailer_config_items[i].name, variable_name)) |
| 509 | continue; |
| 510 | name = xstrndup(trailer_item, variable_name - trailer_item - 1); |
| 511 | type = trailer_config_items[i].type; |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | if (!name) |
| 516 | return 0; |
| 517 | |
| 518 | item = get_conf_item(name); |
| 519 | conf = &item->conf; |
| 520 | free(name); |
| 521 | |
| 522 | switch (type) { |
| 523 | case TRAILER_KEY: |
| 524 | if (conf->key) |
| 525 | warning(_("more than one %s"), conf_key); |
| 526 | conf->key = xstrdup(value); |
| 527 | break; |
| 528 | case TRAILER_COMMAND: |
| 529 | if (conf->command) |
| 530 | warning(_("more than one %s"), conf_key); |
| 531 | conf->command = xstrdup(value); |
| 532 | break; |
| 533 | case TRAILER_WHERE: |
| 534 | if (set_where(conf, value)) |
| 535 | warning(_("unknown value '%s' for key '%s'"), value, conf_key); |
| 536 | break; |
| 537 | case TRAILER_IF_EXISTS: |
| 538 | if (set_if_exists(conf, value)) |
| 539 | warning(_("unknown value '%s' for key '%s'"), value, conf_key); |
| 540 | break; |
| 541 | case TRAILER_IF_MISSING: |
| 542 | if (set_if_missing(conf, value)) |
| 543 | warning(_("unknown value '%s' for key '%s'"), value, conf_key); |
| 544 | break; |
| 545 | default: |
Johannes Schindelin | ef1177d1 | 2016-07-26 18:05:50 +0200 | [diff] [blame] | 546 | die("BUG: trailer.c: unhandled type %d", type); |
Christian Couder | 46a0613 | 2014-10-13 20:16:25 +0200 | [diff] [blame] | 547 | } |
| 548 | return 0; |
| 549 | } |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 550 | |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 551 | static void ensure_configured(void) |
| 552 | { |
| 553 | if (configured) |
| 554 | return; |
| 555 | |
| 556 | /* Default config must be setup first */ |
| 557 | git_config(git_trailer_default_config, NULL); |
| 558 | git_config(git_trailer_config, NULL); |
| 559 | configured = 1; |
| 560 | } |
| 561 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 562 | static const char *token_from_item(struct arg_item *item, char *tok) |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 563 | { |
| 564 | if (item->conf.key) |
| 565 | return item->conf.key; |
| 566 | if (tok) |
| 567 | return tok; |
| 568 | return item->conf.name; |
| 569 | } |
| 570 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 571 | static int token_matches_item(const char *tok, struct arg_item *item, int tok_len) |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 572 | { |
| 573 | if (!strncasecmp(tok, item->conf.name, tok_len)) |
| 574 | return 1; |
| 575 | return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0; |
| 576 | } |
| 577 | |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 578 | /* |
Jonathan Tan | e431956 | 2016-11-02 10:29:16 -0700 | [diff] [blame] | 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 "#". |
| 584 | * |
| 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. |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 588 | */ |
| 589 | static int find_separator(const char *line, const char *separators) |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 590 | { |
Jonathan Tan | e431956 | 2016-11-02 10:29:16 -0700 | [diff] [blame] | 591 | int whitespace_found = 0; |
| 592 | const char *c; |
| 593 | for (c = line; *c; c++) { |
| 594 | if (strchr(separators, *c)) |
| 595 | return c - line; |
| 596 | if (!whitespace_found && (isalnum(*c) || *c == '-')) |
| 597 | continue; |
| 598 | if (c != line && (*c == ' ' || *c == '\t')) { |
| 599 | whitespace_found = 1; |
| 600 | continue; |
| 601 | } |
| 602 | break; |
| 603 | } |
| 604 | return -1; |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /* |
| 608 | * Obtain the token, value, and conf from the given trailer. |
| 609 | * |
| 610 | * separator_pos must not be 0, since the token cannot be an empty string. |
| 611 | * |
| 612 | * If separator_pos is -1, interpret the whole trailer as a token. |
| 613 | */ |
| 614 | static void parse_trailer(struct strbuf *tok, struct strbuf *val, |
| 615 | const struct conf_info **conf, const char *trailer, |
| 616 | int separator_pos) |
| 617 | { |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 618 | struct arg_item *item; |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 619 | int tok_len; |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 620 | struct list_head *pos; |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 621 | |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 622 | if (separator_pos != -1) { |
| 623 | strbuf_add(tok, trailer, separator_pos); |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 624 | strbuf_trim(tok); |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 625 | strbuf_addstr(val, trailer + separator_pos + 1); |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 626 | strbuf_trim(val); |
| 627 | } else { |
| 628 | strbuf_addstr(tok, trailer); |
| 629 | strbuf_trim(tok); |
| 630 | } |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 631 | |
| 632 | /* Lookup if the token matches something in the config */ |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 633 | tok_len = token_len_without_separator(tok->buf, tok->len); |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 634 | if (conf) |
| 635 | *conf = &default_conf_info; |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 636 | list_for_each(pos, &conf_head) { |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 637 | item = list_entry(pos, struct arg_item, list); |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 638 | if (token_matches_item(tok->buf, item, tok_len)) { |
| 639 | char *tok_buf = strbuf_detach(tok, NULL); |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 640 | if (conf) |
| 641 | *conf = &item->conf; |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 642 | strbuf_addstr(tok, token_from_item(item, tok_buf)); |
| 643 | free(tok_buf); |
| 644 | break; |
| 645 | } |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 649 | static struct trailer_item *add_trailer_item(struct list_head *head, char *tok, |
| 650 | char *val) |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 651 | { |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 652 | struct trailer_item *new = xcalloc(sizeof(*new), 1); |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 653 | new->token = tok; |
| 654 | new->value = val; |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 655 | list_add_tail(&new->list, head); |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 656 | return new; |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 657 | } |
| 658 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 659 | static void add_arg_item(struct list_head *arg_head, char *tok, char *val, |
| 660 | const struct conf_info *conf) |
| 661 | { |
| 662 | struct arg_item *new = xcalloc(sizeof(*new), 1); |
| 663 | new->token = tok; |
| 664 | new->value = val; |
| 665 | duplicate_conf(&new->conf, conf); |
| 666 | list_add_tail(&new->list, arg_head); |
| 667 | } |
| 668 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 669 | static void process_command_line_args(struct list_head *arg_head, |
| 670 | struct string_list *trailers) |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 671 | { |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 672 | struct string_list_item *tr; |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 673 | struct arg_item *item; |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 674 | struct strbuf tok = STRBUF_INIT; |
| 675 | struct strbuf val = STRBUF_INIT; |
| 676 | const struct conf_info *conf; |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 677 | struct list_head *pos; |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 678 | |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 679 | /* |
| 680 | * In command-line arguments, '=' is accepted (in addition to the |
| 681 | * separators that are defined). |
| 682 | */ |
| 683 | char *cl_separators = xstrfmt("=%s", separators); |
| 684 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 685 | /* Add an arg item for each configured trailer with a command */ |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 686 | list_for_each(pos, &conf_head) { |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 687 | item = list_entry(pos, struct arg_item, list); |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 688 | if (item->conf.command) |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 689 | add_arg_item(arg_head, |
| 690 | xstrdup(token_from_item(item, NULL)), |
| 691 | xstrdup(""), |
| 692 | &item->conf); |
Christian Couder | 85039fb | 2014-10-13 20:16:31 +0200 | [diff] [blame] | 693 | } |
| 694 | |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 695 | /* Add an arg item for each trailer on the command line */ |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 696 | for_each_string_list_item(tr, trailers) { |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 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); |
| 701 | strbuf_trim(&sb); |
| 702 | error(_("empty trailer token in trailer '%.*s'"), |
| 703 | (int) sb.len, sb.buf); |
| 704 | strbuf_release(&sb); |
| 705 | } else { |
| 706 | parse_trailer(&tok, &val, &conf, tr->string, |
| 707 | separator_pos); |
Jonathan Tan | cc71b0d | 2016-10-20 14:39:49 -0700 | [diff] [blame] | 708 | add_arg_item(arg_head, |
| 709 | strbuf_detach(&tok, NULL), |
| 710 | strbuf_detach(&val, NULL), |
| 711 | conf); |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 715 | free(cl_separators); |
Christian Couder | f0a90b4 | 2014-10-13 20:16:26 +0200 | [diff] [blame] | 716 | } |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 717 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 718 | static void read_input_file(struct strbuf *sb, const char *file) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 719 | { |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 720 | if (file) { |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 721 | if (strbuf_read_file(sb, file, 0) < 0) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 722 | die_errno(_("could not read input file '%s'"), file); |
| 723 | } else { |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 724 | if (strbuf_read(sb, fileno(stdin), 0) < 0) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 725 | die_errno(_("could not read from stdin")); |
| 726 | } |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 727 | } |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 728 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 729 | static const char *next_line(const char *str) |
| 730 | { |
| 731 | const char *nl = strchrnul(str, '\n'); |
| 732 | return nl + !!*nl; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | /* |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 736 | * Return the position of the start of the last line. If len is 0, return -1. |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 737 | */ |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 738 | static int last_line(const char *buf, size_t len) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 739 | { |
| 740 | int i; |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 741 | if (len == 0) |
| 742 | return -1; |
| 743 | if (len == 1) |
| 744 | return 0; |
| 745 | /* |
| 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. |
| 749 | */ |
| 750 | i = len - 2; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 751 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 752 | for (; i >= 0; i--) { |
| 753 | if (buf[i] == '\n') |
| 754 | return i + 1; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 755 | } |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 756 | return 0; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /* |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 760 | * Return the position of the start of the patch or the length of str if there |
| 761 | * is no patch in the message. |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 762 | */ |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 763 | static int find_patch_start(const char *str) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 764 | { |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 765 | const char *s; |
| 766 | |
| 767 | for (s = str; *s; s = next_line(s)) { |
| 768 | if (starts_with(s, "---")) |
| 769 | return s - str; |
| 770 | } |
| 771 | |
| 772 | return s - str; |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Return the position of the first trailer line or len if there are no |
| 777 | * trailers. |
| 778 | */ |
| 779 | static int find_trailer_start(const char *buf, size_t len) |
| 780 | { |
| 781 | const char *s; |
| 782 | int end_of_title, l, only_spaces = 1; |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 783 | int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0; |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 784 | /* |
| 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). |
| 790 | */ |
| 791 | int possible_continuation_lines = 0; |
Christian Couder | 5c99995 | 2015-08-30 21:14:40 +0200 | [diff] [blame] | 792 | |
| 793 | /* The first paragraph is the title and cannot be trailers */ |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 794 | for (s = buf; s < buf + len; s = next_line(s)) { |
| 795 | if (s[0] == comment_line_char) |
Christian Couder | 5c99995 | 2015-08-30 21:14:40 +0200 | [diff] [blame] | 796 | continue; |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 797 | if (is_blank_line(s)) |
Christian Couder | 5c99995 | 2015-08-30 21:14:40 +0200 | [diff] [blame] | 798 | break; |
| 799 | } |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 800 | end_of_title = s - buf; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 801 | |
| 802 | /* |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 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. |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 807 | */ |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 808 | for (l = last_line(buf, len); |
| 809 | l >= end_of_title; |
| 810 | l = last_line(buf, l)) { |
| 811 | const char *bol = buf + l; |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 812 | const char **p; |
| 813 | int separator_pos; |
| 814 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 815 | if (bol[0] == comment_line_char) { |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 816 | non_trailer_lines += possible_continuation_lines; |
| 817 | possible_continuation_lines = 0; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 818 | continue; |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 819 | } |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 820 | if (is_blank_line(bol)) { |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 821 | if (only_spaces) |
| 822 | continue; |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 823 | non_trailer_lines += possible_continuation_lines; |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 824 | if (recognized_prefix && |
| 825 | trailer_lines * 3 >= non_trailer_lines) |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 826 | return next_line(bol) - buf; |
| 827 | else if (trailer_lines && !non_trailer_lines) |
| 828 | return next_line(bol) - buf; |
| 829 | return len; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 830 | } |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 831 | only_spaces = 0; |
| 832 | |
| 833 | for (p = git_generated_prefixes; *p; p++) { |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 834 | if (starts_with(bol, *p)) { |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 835 | trailer_lines++; |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 836 | possible_continuation_lines = 0; |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 837 | recognized_prefix = 1; |
| 838 | goto continue_outer_loop; |
| 839 | } |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 840 | } |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 841 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 842 | separator_pos = find_separator(bol, separators); |
| 843 | if (separator_pos >= 1 && !isspace(bol[0])) { |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 844 | struct list_head *pos; |
| 845 | |
| 846 | trailer_lines++; |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 847 | possible_continuation_lines = 0; |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 848 | if (recognized_prefix) |
| 849 | continue; |
| 850 | list_for_each(pos, &conf_head) { |
| 851 | struct arg_item *item; |
| 852 | item = list_entry(pos, struct arg_item, list); |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 853 | if (token_matches_item(bol, item, |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 854 | separator_pos)) { |
| 855 | recognized_prefix = 1; |
| 856 | break; |
| 857 | } |
| 858 | } |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 859 | } else if (isspace(bol[0])) |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 860 | possible_continuation_lines++; |
| 861 | else { |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 862 | non_trailer_lines++; |
Jonathan Tan | 60ef86a | 2016-10-21 10:55:03 -0700 | [diff] [blame] | 863 | non_trailer_lines += possible_continuation_lines; |
| 864 | possible_continuation_lines = 0; |
| 865 | } |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 866 | continue_outer_loop: |
| 867 | ; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 868 | } |
| 869 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 870 | return len; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 871 | } |
| 872 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 873 | /* Return the position of the end of the trailers. */ |
| 874 | static int find_trailer_end(const char *buf, size_t len) |
Christian Couder | 61cfef4 | 2014-11-09 10:23:42 +0100 | [diff] [blame] | 875 | { |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 876 | return len - ignore_non_trailer(buf, len); |
Christian Couder | 61cfef4 | 2014-11-09 10:23:42 +0100 | [diff] [blame] | 877 | } |
| 878 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 879 | static int ends_with_blank_line(const char *buf, size_t len) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 880 | { |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 881 | int ll = last_line(buf, len); |
| 882 | if (ll < 0) |
| 883 | return 0; |
| 884 | return is_blank_line(buf + ll); |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 885 | } |
| 886 | |
Tobias Klauser | d0d2344 | 2016-01-14 17:57:54 +0100 | [diff] [blame] | 887 | static int process_input_file(FILE *outfile, |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 888 | const char *str, |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 889 | struct list_head *head) |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 890 | { |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 891 | struct trailer_info info; |
Jonathan Tan | 63ab3f3 | 2016-10-20 14:39:48 -0700 | [diff] [blame] | 892 | struct strbuf tok = STRBUF_INIT; |
| 893 | struct strbuf val = STRBUF_INIT; |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 894 | int i; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 895 | |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 896 | trailer_info_get(&info, str); |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 897 | |
| 898 | /* Print lines before the trailers as is */ |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 899 | fwrite(str, 1, info.trailer_start - str, outfile); |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 900 | |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 901 | if (!info.blank_line_before_trailer) |
Tobias Klauser | d0d2344 | 2016-01-14 17:57:54 +0100 | [diff] [blame] | 902 | fprintf(outfile, "\n"); |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 903 | |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 904 | for (i = 0; i < info.trailer_nr; i++) { |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 905 | int separator_pos; |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 906 | char *trailer = info.trailers[i]; |
| 907 | if (trailer[0] == comment_line_char) |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 908 | continue; |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 909 | separator_pos = find_separator(trailer, separators); |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 910 | if (separator_pos >= 1) { |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 911 | parse_trailer(&tok, &val, NULL, trailer, |
Jonathan Tan | fdbf451 | 2016-10-21 10:55:00 -0700 | [diff] [blame] | 912 | separator_pos); |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 913 | add_trailer_item(head, |
| 914 | strbuf_detach(&tok, NULL), |
| 915 | strbuf_detach(&val, NULL)); |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 916 | } else { |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 917 | strbuf_addstr(&val, trailer); |
Jonathan Tan | 1462450 | 2016-10-21 10:55:01 -0700 | [diff] [blame] | 918 | strbuf_strip_suffix(&val, "\n"); |
| 919 | add_trailer_item(head, |
| 920 | NULL, |
| 921 | strbuf_detach(&val, NULL)); |
Christian Couder | 2887103 | 2014-11-09 10:23:39 +0100 | [diff] [blame] | 922 | } |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 923 | } |
| 924 | |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 925 | trailer_info_release(&info); |
| 926 | |
| 927 | return info.trailer_end - str; |
Christian Couder | 2013d85 | 2014-10-13 20:16:27 +0200 | [diff] [blame] | 928 | } |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 929 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 930 | static void free_all(struct list_head *head) |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 931 | { |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 932 | struct list_head *pos, *p; |
| 933 | list_for_each_safe(pos, p, head) { |
| 934 | list_del(pos); |
| 935 | free_trailer_item(list_entry(pos, struct trailer_item, list)); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | |
Tobias Klauser | e1f8986 | 2016-01-14 17:57:55 +0100 | [diff] [blame] | 939 | static struct tempfile trailers_tempfile; |
| 940 | |
| 941 | static FILE *create_in_place_tempfile(const char *file) |
| 942 | { |
| 943 | struct stat st; |
| 944 | struct strbuf template = STRBUF_INIT; |
| 945 | const char *tail; |
| 946 | FILE *outfile; |
| 947 | |
| 948 | if (stat(file, &st)) |
| 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); |
| 954 | |
| 955 | /* Create temporary file in the same directory as the original */ |
| 956 | tail = strrchr(file, '/'); |
| 957 | if (tail != NULL) |
| 958 | strbuf_add(&template, file, tail - file + 1); |
| 959 | strbuf_addstr(&template, "git-interpret-trailers-XXXXXX"); |
| 960 | |
| 961 | xmks_tempfile_m(&trailers_tempfile, template.buf, st.st_mode); |
| 962 | strbuf_release(&template); |
| 963 | outfile = fdopen_tempfile(&trailers_tempfile, "w"); |
| 964 | if (!outfile) |
| 965 | die_errno(_("could not open temporary file")); |
| 966 | |
| 967 | return outfile; |
| 968 | } |
| 969 | |
| 970 | void process_trailers(const char *file, int in_place, int trim_empty, struct string_list *trailers) |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 971 | { |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 972 | LIST_HEAD(head); |
| 973 | LIST_HEAD(arg_head); |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 974 | struct strbuf sb = STRBUF_INIT; |
Christian Couder | 61cfef4 | 2014-11-09 10:23:42 +0100 | [diff] [blame] | 975 | int trailer_end; |
Tobias Klauser | d0d2344 | 2016-01-14 17:57:54 +0100 | [diff] [blame] | 976 | FILE *outfile = stdout; |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 977 | |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 978 | ensure_configured(); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 979 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 980 | read_input_file(&sb, file); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 981 | |
Tobias Klauser | e1f8986 | 2016-01-14 17:57:55 +0100 | [diff] [blame] | 982 | if (in_place) |
| 983 | outfile = create_in_place_tempfile(file); |
| 984 | |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 985 | /* Print the lines before the trailers */ |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 986 | trailer_end = process_input_file(outfile, sb.buf, &head); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 987 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 988 | process_command_line_args(&arg_head, trailers); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 989 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 990 | process_trailers_lists(&head, &arg_head); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 991 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 992 | print_all(outfile, &head, trim_empty); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 993 | |
Jonathan Tan | 8966a39 | 2016-10-20 14:39:47 -0700 | [diff] [blame] | 994 | free_all(&head); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 995 | |
| 996 | /* Print the lines after the trailers as is */ |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 997 | fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 998 | |
Tobias Klauser | e1f8986 | 2016-01-14 17:57:55 +0100 | [diff] [blame] | 999 | if (in_place) |
| 1000 | if (rename_tempfile(&trailers_tempfile, file)) |
| 1001 | die_errno(_("could not rename temporary file to %s"), file); |
| 1002 | |
Jonathan Tan | 022349c | 2016-11-02 10:29:18 -0700 | [diff] [blame] | 1003 | strbuf_release(&sb); |
Christian Couder | b1d78d7 | 2014-10-13 20:16:28 +0200 | [diff] [blame] | 1004 | } |
Jonathan Tan | e8c352c | 2016-11-02 10:29:19 -0700 | [diff] [blame] | 1005 | |
| 1006 | void trailer_info_get(struct trailer_info *info, const char *str) |
| 1007 | { |
| 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; |
| 1012 | char **last = NULL; |
| 1013 | |
| 1014 | ensure_configured(); |
| 1015 | |
| 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); |
| 1019 | |
| 1020 | trailer_lines = strbuf_split_buf(str + trailer_start, |
| 1021 | trailer_end - trailer_start, |
| 1022 | '\n', |
| 1023 | 0); |
| 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); |
| 1030 | continue; |
| 1031 | } |
| 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] |
| 1036 | : NULL; |
| 1037 | nr++; |
| 1038 | } |
| 1039 | strbuf_list_free(trailer_lines); |
| 1040 | |
| 1041 | info->blank_line_before_trailer = ends_with_blank_line(str, |
| 1042 | trailer_start); |
| 1043 | info->trailer_start = str + trailer_start; |
| 1044 | info->trailer_end = str + trailer_end; |
| 1045 | info->trailers = trailer_strings; |
| 1046 | info->trailer_nr = nr; |
| 1047 | } |
| 1048 | |
| 1049 | void trailer_info_release(struct trailer_info *info) |
| 1050 | { |
| 1051 | int i; |
| 1052 | for (i = 0; i < info->trailer_nr; i++) |
| 1053 | free(info->trailers[i]); |
| 1054 | free(info->trailers); |
| 1055 | } |