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