]>
Commit | Line | Data |
---|---|---|
9385b5d7 | 1 | #include "cache.h" |
f0a90b4e | 2 | #include "string-list.h" |
85039fb6 | 3 | #include "run-command.h" |
61cfef4c | 4 | #include "commit.h" |
e1f89863 | 5 | #include "tempfile.h" |
b1d78d77 | 6 | #include "trailer.h" |
8966a394 | 7 | #include "list.h" |
9385b5d7 CC |
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 { | |
8966a394 | 29 | struct list_head list; |
14624506 JT |
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 | */ | |
d65fd424 JT |
34 | char *token; |
35 | char *value; | |
cc71b0de JT |
36 | }; |
37 | ||
38 | struct arg_item { | |
39 | struct list_head list; | |
40 | char *token; | |
41 | char *value; | |
9385b5d7 CC |
42 | struct conf_info conf; |
43 | }; | |
44 | ||
8966a394 | 45 | static LIST_HEAD(conf_head); |
9385b5d7 CC |
46 | |
47 | static char *separators = ":"; | |
48 | ||
e8c352c3 JT |
49 | static int configured; |
50 | ||
85039fb6 CC |
51 | #define TRAILER_ARG_STRING "$ARG" |
52 | ||
14624506 JT |
53 | static const char *git_generated_prefixes[] = { |
54 | "Signed-off-by: ", | |
55 | "(cherry picked from commit ", | |
56 | NULL | |
57 | }; | |
58 | ||
8966a394 JT |
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 | ||
9385b5d7 CC |
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 | ||
cc71b0de | 83 | static int same_token(struct trailer_item *a, struct arg_item *b) |
9385b5d7 | 84 | { |
14624506 JT |
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; | |
9385b5d7 CC |
93 | |
94 | return !strncasecmp(a->token, b->token, min_len); | |
95 | } | |
96 | ||
cc71b0de | 97 | static int same_value(struct trailer_item *a, struct arg_item *b) |
9385b5d7 CC |
98 | { |
99 | return !strcasecmp(a->value, b->value); | |
100 | } | |
101 | ||
cc71b0de | 102 | static int same_trailer(struct trailer_item *a, struct arg_item *b) |
9385b5d7 CC |
103 | { |
104 | return same_token(a, b) && same_value(a, b); | |
105 | } | |
4103818d | 106 | |
022349c3 | 107 | static inline int is_blank_line(const char *str) |
2013d850 CC |
108 | { |
109 | const char *s = str; | |
022349c3 | 110 | while (*s && *s != '\n' && isspace(*s)) |
2013d850 | 111 | s++; |
022349c3 | 112 | return !*s || *s == '\n'; |
2013d850 CC |
113 | } |
114 | ||
85039fb6 CC |
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 | ||
4103818d | 122 | static void free_trailer_item(struct trailer_item *item) |
cc71b0de JT |
123 | { |
124 | free(item->token); | |
125 | free(item->value); | |
126 | free(item); | |
127 | } | |
128 | ||
129 | static void free_arg_item(struct arg_item *item) | |
4103818d CC |
130 | { |
131 | free(item->conf.name); | |
132 | free(item->conf.key); | |
133 | free(item->conf.command); | |
d65fd424 JT |
134 | free(item->token); |
135 | free(item->value); | |
4103818d CC |
136 | free(item); |
137 | } | |
138 | ||
b1d78d77 CC |
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 | ||
d0d2344a | 148 | static void print_tok_val(FILE *outfile, const char *tok, const char *val) |
b1d78d77 | 149 | { |
14624506 JT |
150 | char c; |
151 | ||
152 | if (!tok) { | |
153 | fprintf(outfile, "%s\n", val); | |
154 | return; | |
155 | } | |
156 | ||
157 | c = last_non_space_char(tok); | |
b1d78d77 CC |
158 | if (!c) |
159 | return; | |
160 | if (strchr(separators, c)) | |
d0d2344a | 161 | fprintf(outfile, "%s%s\n", tok, val); |
b1d78d77 | 162 | else |
d0d2344a | 163 | fprintf(outfile, "%s%c %s\n", tok, separators[0], val); |
b1d78d77 CC |
164 | } |
165 | ||
8966a394 | 166 | static void print_all(FILE *outfile, struct list_head *head, int trim_empty) |
b1d78d77 | 167 | { |
8966a394 | 168 | struct list_head *pos; |
b1d78d77 | 169 | struct trailer_item *item; |
8966a394 JT |
170 | list_for_each(pos, head) { |
171 | item = list_entry(pos, struct trailer_item, list); | |
b1d78d77 | 172 | if (!trim_empty || strlen(item->value) > 0) |
d0d2344a | 173 | print_tok_val(outfile, item->token, item->value); |
b1d78d77 CC |
174 | } |
175 | } | |
176 | ||
cc71b0de | 177 | static struct trailer_item *trailer_from_arg(struct arg_item *arg_tok) |
4103818d | 178 | { |
cc71b0de JT |
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; | |
4103818d CC |
185 | } |
186 | ||
187 | static void add_arg_to_input_list(struct trailer_item *on_tok, | |
cc71b0de | 188 | struct arg_item *arg_tok) |
8966a394 | 189 | { |
cc71b0de JT |
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); | |
8966a394 | 194 | else |
cc71b0de | 195 | list_add_tail(&to_add->list, &on_tok->list); |
4103818d CC |
196 | } |
197 | ||
198 | static int check_if_different(struct trailer_item *in_tok, | |
cc71b0de | 199 | struct arg_item *arg_tok, |
8966a394 JT |
200 | int check_all, |
201 | struct list_head *head) | |
4103818d CC |
202 | { |
203 | enum action_where where = arg_tok->conf.where; | |
8966a394 | 204 | struct list_head *next_head; |
4103818d | 205 | do { |
4103818d CC |
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 | */ | |
8966a394 JT |
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); | |
4103818d CC |
217 | } while (check_all); |
218 | return 1; | |
219 | } | |
220 | ||
d65fd424 | 221 | static char *apply_command(const char *command, const char *arg) |
85039fb6 CC |
222 | { |
223 | struct strbuf cmd = STRBUF_INIT; | |
224 | struct strbuf buf = STRBUF_INIT; | |
b226293b | 225 | struct child_process cp = CHILD_PROCESS_INIT; |
85039fb6 | 226 | const char *argv[] = {NULL, NULL}; |
d65fd424 | 227 | char *result; |
85039fb6 CC |
228 | |
229 | strbuf_addstr(&cmd, command); | |
230 | if (arg) | |
231 | strbuf_replace(&cmd, TRAILER_ARG_STRING, arg); | |
232 | ||
233 | argv[0] = cmd.buf; | |
85039fb6 CC |
234 | cp.argv = argv; |
235 | cp.env = local_repo_env; | |
236 | cp.no_stdin = 1; | |
85039fb6 CC |
237 | cp.use_shell = 1; |
238 | ||
c5eadcaa | 239 | if (capture_command(&cp, &buf, 1024)) { |
13ad56f8 | 240 | error(_("running trailer command '%s' failed"), cmd.buf); |
85039fb6 CC |
241 | strbuf_release(&buf); |
242 | result = xstrdup(""); | |
c5eadcaa JK |
243 | } else { |
244 | strbuf_trim(&buf); | |
85039fb6 | 245 | result = strbuf_detach(&buf, NULL); |
c5eadcaa | 246 | } |
85039fb6 CC |
247 | |
248 | strbuf_release(&cmd); | |
249 | return result; | |
250 | } | |
251 | ||
cc71b0de | 252 | static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok) |
85039fb6 CC |
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 | ||
4103818d | 269 | static void apply_arg_if_exists(struct trailer_item *in_tok, |
cc71b0de | 270 | struct arg_item *arg_tok, |
4103818d | 271 | struct trailer_item *on_tok, |
8966a394 | 272 | struct list_head *head) |
4103818d CC |
273 | { |
274 | switch (arg_tok->conf.if_exists) { | |
275 | case EXISTS_DO_NOTHING: | |
cc71b0de | 276 | free_arg_item(arg_tok); |
4103818d CC |
277 | break; |
278 | case EXISTS_REPLACE: | |
85039fb6 | 279 | apply_item_command(in_tok, arg_tok); |
8966a394 JT |
280 | add_arg_to_input_list(on_tok, arg_tok); |
281 | list_del(&in_tok->list); | |
4103818d CC |
282 | free_trailer_item(in_tok); |
283 | break; | |
284 | case EXISTS_ADD: | |
85039fb6 | 285 | apply_item_command(in_tok, arg_tok); |
8966a394 | 286 | add_arg_to_input_list(on_tok, arg_tok); |
4103818d CC |
287 | break; |
288 | case EXISTS_ADD_IF_DIFFERENT: | |
85039fb6 | 289 | apply_item_command(in_tok, arg_tok); |
8966a394 JT |
290 | if (check_if_different(in_tok, arg_tok, 1, head)) |
291 | add_arg_to_input_list(on_tok, arg_tok); | |
4103818d | 292 | else |
cc71b0de | 293 | free_arg_item(arg_tok); |
4103818d CC |
294 | break; |
295 | case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR: | |
85039fb6 | 296 | apply_item_command(in_tok, arg_tok); |
8966a394 JT |
297 | if (check_if_different(on_tok, arg_tok, 0, head)) |
298 | add_arg_to_input_list(on_tok, arg_tok); | |
4103818d | 299 | else |
cc71b0de | 300 | free_arg_item(arg_tok); |
4103818d CC |
301 | break; |
302 | } | |
303 | } | |
304 | ||
8966a394 | 305 | static void apply_arg_if_missing(struct list_head *head, |
cc71b0de | 306 | struct arg_item *arg_tok) |
4103818d | 307 | { |
4103818d | 308 | enum action_where where; |
cc71b0de | 309 | struct trailer_item *to_add; |
4103818d CC |
310 | |
311 | switch (arg_tok->conf.if_missing) { | |
312 | case MISSING_DO_NOTHING: | |
cc71b0de | 313 | free_arg_item(arg_tok); |
4103818d CC |
314 | break; |
315 | case MISSING_ADD: | |
316 | where = arg_tok->conf.where; | |
85039fb6 | 317 | apply_item_command(NULL, arg_tok); |
cc71b0de | 318 | to_add = trailer_from_arg(arg_tok); |
8966a394 | 319 | if (after_or_end(where)) |
cc71b0de | 320 | list_add_tail(&to_add->list, head); |
8966a394 | 321 | else |
cc71b0de | 322 | list_add(&to_add->list, head); |
4103818d CC |
323 | } |
324 | } | |
325 | ||
8966a394 | 326 | static int find_same_and_apply_arg(struct list_head *head, |
cc71b0de | 327 | struct arg_item *arg_tok) |
4103818d | 328 | { |
8966a394 | 329 | struct list_head *pos; |
4103818d CC |
330 | struct trailer_item *in_tok; |
331 | struct trailer_item *on_tok; | |
4103818d CC |
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); | |
8966a394 | 336 | struct trailer_item *start_tok; |
4103818d | 337 | |
8966a394 JT |
338 | if (list_empty(head)) |
339 | return 0; | |
4103818d | 340 | |
8966a394 JT |
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); | |
4103818d CC |
347 | if (!same_token(in_tok, arg_tok)) |
348 | continue; | |
349 | on_tok = middle ? in_tok : start_tok; | |
8966a394 | 350 | apply_arg_if_exists(in_tok, arg_tok, on_tok, head); |
4103818d CC |
351 | return 1; |
352 | } | |
353 | return 0; | |
354 | } | |
355 | ||
8966a394 JT |
356 | static void process_trailers_lists(struct list_head *head, |
357 | struct list_head *arg_head) | |
4103818d | 358 | { |
8966a394 | 359 | struct list_head *pos, *p; |
cc71b0de | 360 | struct arg_item *arg_tok; |
4103818d | 361 | |
8966a394 | 362 | list_for_each_safe(pos, p, arg_head) { |
4103818d | 363 | int applied = 0; |
cc71b0de | 364 | arg_tok = list_entry(pos, struct arg_item, list); |
4103818d | 365 | |
8966a394 | 366 | list_del(pos); |
4103818d | 367 | |
8966a394 | 368 | applied = find_same_and_apply_arg(head, arg_tok); |
4103818d CC |
369 | |
370 | if (!applied) | |
8966a394 | 371 | apply_arg_if_missing(head, arg_tok); |
4103818d CC |
372 | } |
373 | } | |
46a0613f CC |
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 | ||
d65fd424 | 418 | static void duplicate_conf(struct conf_info *dst, const struct conf_info *src) |
46a0613f CC |
419 | { |
420 | *dst = *src; | |
13092a91 JH |
421 | dst->name = xstrdup_or_null(src->name); |
422 | dst->key = xstrdup_or_null(src->key); | |
423 | dst->command = xstrdup_or_null(src->command); | |
46a0613f CC |
424 | } |
425 | ||
cc71b0de | 426 | static struct arg_item *get_conf_item(const char *name) |
46a0613f | 427 | { |
8966a394 | 428 | struct list_head *pos; |
cc71b0de | 429 | struct arg_item *item; |
46a0613f CC |
430 | |
431 | /* Look up item with same name */ | |
8966a394 | 432 | list_for_each(pos, &conf_head) { |
cc71b0de | 433 | item = list_entry(pos, struct arg_item, list); |
46a0613f CC |
434 | if (!strcasecmp(item->conf.name, name)) |
435 | return item; | |
436 | } | |
437 | ||
438 | /* Item does not already exists, create it */ | |
cc71b0de | 439 | item = xcalloc(sizeof(*item), 1); |
46a0613f CC |
440 | duplicate_conf(&item->conf, &default_conf_info); |
441 | item->conf.name = xstrdup(name); | |
442 | ||
8966a394 | 443 | list_add_tail(&item->list, &conf_head); |
46a0613f CC |
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; | |
cc71b0de | 493 | struct arg_item *item; |
46a0613f CC |
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: | |
ef1177d1 | 546 | die("BUG: trailer.c: unhandled type %d", type); |
46a0613f CC |
547 | } |
548 | return 0; | |
549 | } | |
f0a90b4e | 550 | |
e8c352c3 JT |
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 | ||
cc71b0de | 562 | static const char *token_from_item(struct arg_item *item, char *tok) |
f0a90b4e CC |
563 | { |
564 | if (item->conf.key) | |
565 | return item->conf.key; | |
566 | if (tok) | |
567 | return tok; | |
568 | return item->conf.name; | |
569 | } | |
570 | ||
cc71b0de | 571 | static int token_matches_item(const char *tok, struct arg_item *item, int tok_len) |
f0a90b4e CC |
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 | ||
fdbf4510 | 578 | /* |
e4319562 JT |
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. | |
fdbf4510 JT |
588 | */ |
589 | static int find_separator(const char *line, const char *separators) | |
f0a90b4e | 590 | { |
e4319562 JT |
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; | |
fdbf4510 | 605 | } |
f0a90b4e | 606 | |
fdbf4510 JT |
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) | |
f0a90b4e | 617 | { |
cc71b0de | 618 | struct arg_item *item; |
63ab3f34 JT |
619 | int tok_len; |
620 | struct list_head *pos; | |
f0a90b4e | 621 | |
fdbf4510 JT |
622 | if (separator_pos != -1) { |
623 | strbuf_add(tok, trailer, separator_pos); | |
f0a90b4e | 624 | strbuf_trim(tok); |
fdbf4510 | 625 | strbuf_addstr(val, trailer + separator_pos + 1); |
f0a90b4e CC |
626 | strbuf_trim(val); |
627 | } else { | |
628 | strbuf_addstr(tok, trailer); | |
629 | strbuf_trim(tok); | |
630 | } | |
f0a90b4e CC |
631 | |
632 | /* Lookup if the token matches something in the config */ | |
63ab3f34 | 633 | tok_len = token_len_without_separator(tok->buf, tok->len); |
cc71b0de JT |
634 | if (conf) |
635 | *conf = &default_conf_info; | |
8966a394 | 636 | list_for_each(pos, &conf_head) { |
cc71b0de | 637 | item = list_entry(pos, struct arg_item, list); |
63ab3f34 JT |
638 | if (token_matches_item(tok->buf, item, tok_len)) { |
639 | char *tok_buf = strbuf_detach(tok, NULL); | |
cc71b0de JT |
640 | if (conf) |
641 | *conf = &item->conf; | |
63ab3f34 JT |
642 | strbuf_addstr(tok, token_from_item(item, tok_buf)); |
643 | free(tok_buf); | |
644 | break; | |
645 | } | |
f0a90b4e | 646 | } |
f0a90b4e | 647 | } |
f0a90b4e | 648 | |
60ef86a1 JT |
649 | static struct trailer_item *add_trailer_item(struct list_head *head, char *tok, |
650 | char *val) | |
f0a90b4e | 651 | { |
63ab3f34 JT |
652 | struct trailer_item *new = xcalloc(sizeof(*new), 1); |
653 | new->token = tok; | |
654 | new->value = val; | |
8966a394 | 655 | list_add_tail(&new->list, head); |
60ef86a1 | 656 | return new; |
f0a90b4e CC |
657 | } |
658 | ||
cc71b0de JT |
659 | static void add_arg_item(struct list_head *arg_head, char *tok, char *val, |
660 | const struct conf_info *conf) | |
f0a90b4e | 661 | { |
cc71b0de JT |
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); | |
f0a90b4e CC |
667 | } |
668 | ||
8966a394 JT |
669 | static void process_command_line_args(struct list_head *arg_head, |
670 | struct string_list *trailers) | |
f0a90b4e | 671 | { |
f0a90b4e | 672 | struct string_list_item *tr; |
cc71b0de | 673 | struct arg_item *item; |
63ab3f34 JT |
674 | struct strbuf tok = STRBUF_INIT; |
675 | struct strbuf val = STRBUF_INIT; | |
676 | const struct conf_info *conf; | |
8966a394 | 677 | struct list_head *pos; |
85039fb6 | 678 | |
fdbf4510 JT |
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 | ||
cc71b0de | 685 | /* Add an arg item for each configured trailer with a command */ |
8966a394 | 686 | list_for_each(pos, &conf_head) { |
cc71b0de | 687 | item = list_entry(pos, struct arg_item, list); |
63ab3f34 | 688 | if (item->conf.command) |
cc71b0de JT |
689 | add_arg_item(arg_head, |
690 | xstrdup(token_from_item(item, NULL)), | |
691 | xstrdup(""), | |
692 | &item->conf); | |
85039fb6 | 693 | } |
f0a90b4e | 694 | |
cc71b0de | 695 | /* Add an arg item for each trailer on the command line */ |
f0a90b4e | 696 | for_each_string_list_item(tr, trailers) { |
fdbf4510 JT |
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); | |
cc71b0de JT |
708 | add_arg_item(arg_head, |
709 | strbuf_detach(&tok, NULL), | |
710 | strbuf_detach(&val, NULL), | |
711 | conf); | |
fdbf4510 | 712 | } |
f0a90b4e CC |
713 | } |
714 | ||
fdbf4510 | 715 | free(cl_separators); |
f0a90b4e | 716 | } |
2013d850 | 717 | |
022349c3 | 718 | static void read_input_file(struct strbuf *sb, const char *file) |
2013d850 | 719 | { |
2013d850 | 720 | if (file) { |
022349c3 | 721 | if (strbuf_read_file(sb, file, 0) < 0) |
2013d850 CC |
722 | die_errno(_("could not read input file '%s'"), file); |
723 | } else { | |
022349c3 | 724 | if (strbuf_read(sb, fileno(stdin), 0) < 0) |
2013d850 CC |
725 | die_errno(_("could not read from stdin")); |
726 | } | |
022349c3 | 727 | } |
2013d850 | 728 | |
022349c3 JT |
729 | static const char *next_line(const char *str) |
730 | { | |
731 | const char *nl = strchrnul(str, '\n'); | |
732 | return nl + !!*nl; | |
733 | } | |
2013d850 | 734 | |
022349c3 JT |
735 | /* |
736 | * Return the position of the start of the last line. If len is 0, return -1. | |
737 | */ | |
738 | static int last_line(const char *buf, size_t len) | |
739 | { | |
740 | int i; | |
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; | |
2013d850 | 751 | |
022349c3 JT |
752 | for (; i >= 0; i--) { |
753 | if (buf[i] == '\n') | |
754 | return i + 1; | |
755 | } | |
756 | return 0; | |
2013d850 CC |
757 | } |
758 | ||
759 | /* | |
022349c3 JT |
760 | * Return the position of the start of the patch or the length of str if there |
761 | * is no patch in the message. | |
2013d850 | 762 | */ |
022349c3 | 763 | static int find_patch_start(const char *str) |
2013d850 | 764 | { |
022349c3 | 765 | const char *s; |
2013d850 | 766 | |
022349c3 JT |
767 | for (s = str; *s; s = next_line(s)) { |
768 | if (starts_with(s, "---")) | |
769 | return s - str; | |
2013d850 CC |
770 | } |
771 | ||
022349c3 | 772 | return s - str; |
2013d850 CC |
773 | } |
774 | ||
775 | /* | |
022349c3 JT |
776 | * Return the position of the first trailer line or len if there are no |
777 | * trailers. | |
2013d850 | 778 | */ |
022349c3 | 779 | static int find_trailer_start(const char *buf, size_t len) |
2013d850 | 780 | { |
022349c3 JT |
781 | const char *s; |
782 | int end_of_title, l, only_spaces = 1; | |
14624506 | 783 | int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0; |
60ef86a1 JT |
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; | |
5c99995d CC |
792 | |
793 | /* The first paragraph is the title and cannot be trailers */ | |
022349c3 JT |
794 | for (s = buf; s < buf + len; s = next_line(s)) { |
795 | if (s[0] == comment_line_char) | |
5c99995d | 796 | continue; |
022349c3 | 797 | if (is_blank_line(s)) |
5c99995d CC |
798 | break; |
799 | } | |
022349c3 | 800 | end_of_title = s - buf; |
2013d850 CC |
801 | |
802 | /* | |
14624506 JT |
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. | |
2013d850 | 807 | */ |
022349c3 JT |
808 | for (l = last_line(buf, len); |
809 | l >= end_of_title; | |
810 | l = last_line(buf, l)) { | |
811 | const char *bol = buf + l; | |
14624506 JT |
812 | const char **p; |
813 | int separator_pos; | |
814 | ||
022349c3 | 815 | if (bol[0] == comment_line_char) { |
60ef86a1 JT |
816 | non_trailer_lines += possible_continuation_lines; |
817 | possible_continuation_lines = 0; | |
2013d850 | 818 | continue; |
60ef86a1 | 819 | } |
022349c3 | 820 | if (is_blank_line(bol)) { |
2013d850 CC |
821 | if (only_spaces) |
822 | continue; | |
60ef86a1 | 823 | non_trailer_lines += possible_continuation_lines; |
14624506 JT |
824 | if (recognized_prefix && |
825 | trailer_lines * 3 >= non_trailer_lines) | |
022349c3 JT |
826 | return next_line(bol) - buf; |
827 | else if (trailer_lines && !non_trailer_lines) | |
828 | return next_line(bol) - buf; | |
829 | return len; | |
2013d850 | 830 | } |
14624506 JT |
831 | only_spaces = 0; |
832 | ||
833 | for (p = git_generated_prefixes; *p; p++) { | |
022349c3 | 834 | if (starts_with(bol, *p)) { |
14624506 | 835 | trailer_lines++; |
60ef86a1 | 836 | possible_continuation_lines = 0; |
14624506 JT |
837 | recognized_prefix = 1; |
838 | goto continue_outer_loop; | |
839 | } | |
2013d850 | 840 | } |
14624506 | 841 | |
022349c3 JT |
842 | separator_pos = find_separator(bol, separators); |
843 | if (separator_pos >= 1 && !isspace(bol[0])) { | |
14624506 JT |
844 | struct list_head *pos; |
845 | ||
846 | trailer_lines++; | |
60ef86a1 | 847 | possible_continuation_lines = 0; |
14624506 JT |
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); | |
022349c3 | 853 | if (token_matches_item(bol, item, |
14624506 JT |
854 | separator_pos)) { |
855 | recognized_prefix = 1; | |
856 | break; | |
857 | } | |
858 | } | |
022349c3 | 859 | } else if (isspace(bol[0])) |
60ef86a1 JT |
860 | possible_continuation_lines++; |
861 | else { | |
14624506 | 862 | non_trailer_lines++; |
60ef86a1 JT |
863 | non_trailer_lines += possible_continuation_lines; |
864 | possible_continuation_lines = 0; | |
2013d850 | 865 | } |
14624506 JT |
866 | continue_outer_loop: |
867 | ; | |
2013d850 CC |
868 | } |
869 | ||
022349c3 | 870 | return len; |
61cfef4c CC |
871 | } |
872 | ||
022349c3 JT |
873 | /* Return the position of the end of the trailers. */ |
874 | static int find_trailer_end(const char *buf, size_t len) | |
2013d850 | 875 | { |
022349c3 | 876 | return len - ignore_non_trailer(buf, len); |
2013d850 CC |
877 | } |
878 | ||
022349c3 | 879 | static int ends_with_blank_line(const char *buf, size_t len) |
2013d850 | 880 | { |
022349c3 JT |
881 | int ll = last_line(buf, len); |
882 | if (ll < 0) | |
883 | return 0; | |
884 | return is_blank_line(buf + ll); | |
2013d850 CC |
885 | } |
886 | ||
d0d2344a | 887 | static int process_input_file(FILE *outfile, |
022349c3 | 888 | const char *str, |
8966a394 | 889 | struct list_head *head) |
2013d850 | 890 | { |
e8c352c3 | 891 | struct trailer_info info; |
63ab3f34 JT |
892 | struct strbuf tok = STRBUF_INIT; |
893 | struct strbuf val = STRBUF_INIT; | |
e8c352c3 | 894 | int i; |
2013d850 | 895 | |
e8c352c3 | 896 | trailer_info_get(&info, str); |
2013d850 CC |
897 | |
898 | /* Print lines before the trailers as is */ | |
e8c352c3 | 899 | fwrite(str, 1, info.trailer_start - str, outfile); |
2013d850 | 900 | |
e8c352c3 | 901 | if (!info.blank_line_before_trailer) |
d0d2344a | 902 | fprintf(outfile, "\n"); |
2013d850 | 903 | |
e8c352c3 | 904 | for (i = 0; i < info.trailer_nr; i++) { |
fdbf4510 | 905 | int separator_pos; |
e8c352c3 JT |
906 | char *trailer = info.trailers[i]; |
907 | if (trailer[0] == comment_line_char) | |
60ef86a1 | 908 | continue; |
e8c352c3 | 909 | separator_pos = find_separator(trailer, separators); |
fdbf4510 | 910 | if (separator_pos >= 1) { |
e8c352c3 | 911 | parse_trailer(&tok, &val, NULL, trailer, |
fdbf4510 | 912 | separator_pos); |
e8c352c3 JT |
913 | add_trailer_item(head, |
914 | strbuf_detach(&tok, NULL), | |
915 | strbuf_detach(&val, NULL)); | |
14624506 | 916 | } else { |
e8c352c3 | 917 | strbuf_addstr(&val, trailer); |
14624506 JT |
918 | strbuf_strip_suffix(&val, "\n"); |
919 | add_trailer_item(head, | |
920 | NULL, | |
921 | strbuf_detach(&val, NULL)); | |
2887103b | 922 | } |
2013d850 CC |
923 | } |
924 | ||
e8c352c3 JT |
925 | trailer_info_release(&info); |
926 | ||
927 | return info.trailer_end - str; | |
2013d850 | 928 | } |
b1d78d77 | 929 | |
8966a394 | 930 | static void free_all(struct list_head *head) |
b1d78d77 | 931 | { |
8966a394 JT |
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)); | |
b1d78d77 CC |
936 | } |
937 | } | |
938 | ||
e1f89863 TK |
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) | |
b1d78d77 | 971 | { |
8966a394 JT |
972 | LIST_HEAD(head); |
973 | LIST_HEAD(arg_head); | |
022349c3 | 974 | struct strbuf sb = STRBUF_INIT; |
61cfef4c | 975 | int trailer_end; |
d0d2344a | 976 | FILE *outfile = stdout; |
b1d78d77 | 977 | |
e8c352c3 | 978 | ensure_configured(); |
b1d78d77 | 979 | |
022349c3 | 980 | read_input_file(&sb, file); |
b1d78d77 | 981 | |
e1f89863 TK |
982 | if (in_place) |
983 | outfile = create_in_place_tempfile(file); | |
984 | ||
b1d78d77 | 985 | /* Print the lines before the trailers */ |
022349c3 | 986 | trailer_end = process_input_file(outfile, sb.buf, &head); |
b1d78d77 | 987 | |
8966a394 | 988 | process_command_line_args(&arg_head, trailers); |
b1d78d77 | 989 | |
8966a394 | 990 | process_trailers_lists(&head, &arg_head); |
b1d78d77 | 991 | |
8966a394 | 992 | print_all(outfile, &head, trim_empty); |
b1d78d77 | 993 | |
8966a394 | 994 | free_all(&head); |
b1d78d77 CC |
995 | |
996 | /* Print the lines after the trailers as is */ | |
022349c3 | 997 | fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile); |
b1d78d77 | 998 | |
e1f89863 TK |
999 | if (in_place) |
1000 | if (rename_tempfile(&trailers_tempfile, file)) | |
1001 | die_errno(_("could not rename temporary file to %s"), file); | |
1002 | ||
022349c3 | 1003 | strbuf_release(&sb); |
b1d78d77 | 1004 | } |
e8c352c3 JT |
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 | } |