]>
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; | |
c364b7ef | 17 | char *cmd; |
52fc319d PB |
18 | enum trailer_where where; |
19 | enum trailer_if_exists if_exists; | |
20 | enum trailer_if_missing if_missing; | |
9385b5d7 CC |
21 | }; |
22 | ||
23 | static struct conf_info default_conf_info; | |
24 | ||
25 | struct trailer_item { | |
8966a394 | 26 | struct list_head list; |
14624506 JT |
27 | /* |
28 | * If this is not a trailer line, the line is stored in value | |
29 | * (excluding the terminating newline) and token is NULL. | |
30 | */ | |
d65fd424 JT |
31 | char *token; |
32 | char *value; | |
cc71b0de JT |
33 | }; |
34 | ||
35 | struct arg_item { | |
36 | struct list_head list; | |
37 | char *token; | |
38 | char *value; | |
9385b5d7 CC |
39 | struct conf_info conf; |
40 | }; | |
41 | ||
8966a394 | 42 | static LIST_HEAD(conf_head); |
9385b5d7 CC |
43 | |
44 | static char *separators = ":"; | |
45 | ||
e8c352c3 JT |
46 | static int configured; |
47 | ||
85039fb6 CC |
48 | #define TRAILER_ARG_STRING "$ARG" |
49 | ||
14624506 JT |
50 | static const char *git_generated_prefixes[] = { |
51 | "Signed-off-by: ", | |
52 | "(cherry picked from commit ", | |
53 | NULL | |
54 | }; | |
55 | ||
8966a394 JT |
56 | /* Iterate over the elements of the list. */ |
57 | #define list_for_each_dir(pos, head, is_reverse) \ | |
58 | for (pos = is_reverse ? (head)->prev : (head)->next; \ | |
59 | pos != (head); \ | |
60 | pos = is_reverse ? pos->prev : pos->next) | |
61 | ||
52fc319d | 62 | static int after_or_end(enum trailer_where where) |
9385b5d7 CC |
63 | { |
64 | return (where == WHERE_AFTER) || (where == WHERE_END); | |
65 | } | |
66 | ||
67 | /* | |
68 | * Return the length of the string not including any final | |
69 | * punctuation. E.g., the input "Signed-off-by:" would return | |
70 | * 13, stripping the trailing punctuation but retaining | |
71 | * internal punctuation. | |
72 | */ | |
73 | static size_t token_len_without_separator(const char *token, size_t len) | |
74 | { | |
75 | while (len > 0 && !isalnum(token[len - 1])) | |
76 | len--; | |
77 | return len; | |
78 | } | |
79 | ||
cc71b0de | 80 | static int same_token(struct trailer_item *a, struct arg_item *b) |
9385b5d7 | 81 | { |
14624506 JT |
82 | size_t a_len, b_len, min_len; |
83 | ||
84 | if (!a->token) | |
85 | return 0; | |
86 | ||
87 | a_len = token_len_without_separator(a->token, strlen(a->token)); | |
88 | b_len = token_len_without_separator(b->token, strlen(b->token)); | |
89 | min_len = (a_len > b_len) ? b_len : a_len; | |
9385b5d7 CC |
90 | |
91 | return !strncasecmp(a->token, b->token, min_len); | |
92 | } | |
93 | ||
cc71b0de | 94 | static int same_value(struct trailer_item *a, struct arg_item *b) |
9385b5d7 CC |
95 | { |
96 | return !strcasecmp(a->value, b->value); | |
97 | } | |
98 | ||
cc71b0de | 99 | static int same_trailer(struct trailer_item *a, struct arg_item *b) |
9385b5d7 CC |
100 | { |
101 | return same_token(a, b) && same_value(a, b); | |
102 | } | |
4103818d | 103 | |
022349c3 | 104 | static inline int is_blank_line(const char *str) |
2013d850 CC |
105 | { |
106 | const char *s = str; | |
022349c3 | 107 | while (*s && *s != '\n' && isspace(*s)) |
2013d850 | 108 | s++; |
022349c3 | 109 | return !*s || *s == '\n'; |
2013d850 CC |
110 | } |
111 | ||
85039fb6 CC |
112 | static inline void strbuf_replace(struct strbuf *sb, const char *a, const char *b) |
113 | { | |
114 | const char *ptr = strstr(sb->buf, a); | |
115 | if (ptr) | |
116 | strbuf_splice(sb, ptr - sb->buf, strlen(a), b, strlen(b)); | |
117 | } | |
118 | ||
4103818d | 119 | static void free_trailer_item(struct trailer_item *item) |
cc71b0de JT |
120 | { |
121 | free(item->token); | |
122 | free(item->value); | |
123 | free(item); | |
124 | } | |
125 | ||
126 | static void free_arg_item(struct arg_item *item) | |
4103818d CC |
127 | { |
128 | free(item->conf.name); | |
129 | free(item->conf.key); | |
130 | free(item->conf.command); | |
c364b7ef | 131 | free(item->conf.cmd); |
d65fd424 JT |
132 | free(item->token); |
133 | free(item->value); | |
4103818d CC |
134 | free(item); |
135 | } | |
136 | ||
b1d78d77 CC |
137 | static char last_non_space_char(const char *s) |
138 | { | |
139 | int i; | |
140 | for (i = strlen(s) - 1; i >= 0; i--) | |
141 | if (!isspace(s[i])) | |
142 | return s[i]; | |
143 | return '\0'; | |
144 | } | |
145 | ||
d0d2344a | 146 | static void print_tok_val(FILE *outfile, const char *tok, const char *val) |
b1d78d77 | 147 | { |
14624506 JT |
148 | char c; |
149 | ||
150 | if (!tok) { | |
151 | fprintf(outfile, "%s\n", val); | |
152 | return; | |
153 | } | |
154 | ||
155 | c = last_non_space_char(tok); | |
b1d78d77 CC |
156 | if (!c) |
157 | return; | |
158 | if (strchr(separators, c)) | |
d0d2344a | 159 | fprintf(outfile, "%s%s\n", tok, val); |
b1d78d77 | 160 | else |
d0d2344a | 161 | fprintf(outfile, "%s%c %s\n", tok, separators[0], val); |
b1d78d77 CC |
162 | } |
163 | ||
56c493ed JK |
164 | static void print_all(FILE *outfile, struct list_head *head, |
165 | const struct process_trailer_options *opts) | |
b1d78d77 | 166 | { |
8966a394 | 167 | struct list_head *pos; |
b1d78d77 | 168 | struct trailer_item *item; |
8966a394 JT |
169 | list_for_each(pos, head) { |
170 | item = list_entry(pos, struct trailer_item, list); | |
56c493ed JK |
171 | if ((!opts->trim_empty || strlen(item->value) > 0) && |
172 | (!opts->only_trailers || item->token)) | |
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 | { |
241b5d3e | 179 | struct trailer_item *new_item = xcalloc(1, sizeof(*new_item)); |
17651326 BW |
180 | new_item->token = arg_tok->token; |
181 | new_item->value = arg_tok->value; | |
cc71b0de JT |
182 | arg_tok->token = arg_tok->value = NULL; |
183 | free_arg_item(arg_tok); | |
17651326 | 184 | return new_item; |
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 | 202 | { |
52fc319d | 203 | enum trailer_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 | ||
c364b7ef | 221 | static char *apply_command(struct conf_info *conf, 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; |
d65fd424 | 226 | char *result; |
85039fb6 | 227 | |
c364b7ef ZH |
228 | if (conf->cmd) { |
229 | strbuf_addstr(&cmd, conf->cmd); | |
230 | strvec_push(&cp.args, cmd.buf); | |
231 | if (arg) | |
232 | strvec_push(&cp.args, arg); | |
233 | } else if (conf->command) { | |
234 | strbuf_addstr(&cmd, conf->command); | |
235 | if (arg) | |
236 | strbuf_replace(&cmd, TRAILER_ARG_STRING, arg); | |
237 | strvec_push(&cp.args, cmd.buf); | |
238 | } | |
29fda24d | 239 | strvec_pushv(&cp.env, (const char **)local_repo_env); |
85039fb6 | 240 | cp.no_stdin = 1; |
85039fb6 CC |
241 | cp.use_shell = 1; |
242 | ||
c5eadcaa | 243 | if (capture_command(&cp, &buf, 1024)) { |
13ad56f8 | 244 | error(_("running trailer command '%s' failed"), cmd.buf); |
85039fb6 CC |
245 | strbuf_release(&buf); |
246 | result = xstrdup(""); | |
c5eadcaa JK |
247 | } else { |
248 | strbuf_trim(&buf); | |
85039fb6 | 249 | result = strbuf_detach(&buf, NULL); |
c5eadcaa | 250 | } |
85039fb6 CC |
251 | |
252 | strbuf_release(&cmd); | |
253 | return result; | |
254 | } | |
255 | ||
cc71b0de | 256 | static void apply_item_command(struct trailer_item *in_tok, struct arg_item *arg_tok) |
85039fb6 | 257 | { |
c364b7ef | 258 | if (arg_tok->conf.command || arg_tok->conf.cmd) { |
85039fb6 CC |
259 | const char *arg; |
260 | if (arg_tok->value && arg_tok->value[0]) { | |
261 | arg = arg_tok->value; | |
262 | } else { | |
263 | if (in_tok && in_tok->value) | |
264 | arg = xstrdup(in_tok->value); | |
265 | else | |
266 | arg = xstrdup(""); | |
267 | } | |
c364b7ef | 268 | arg_tok->value = apply_command(&arg_tok->conf, arg); |
85039fb6 CC |
269 | free((char *)arg); |
270 | } | |
271 | } | |
272 | ||
4103818d | 273 | static void apply_arg_if_exists(struct trailer_item *in_tok, |
cc71b0de | 274 | struct arg_item *arg_tok, |
4103818d | 275 | struct trailer_item *on_tok, |
8966a394 | 276 | struct list_head *head) |
4103818d CC |
277 | { |
278 | switch (arg_tok->conf.if_exists) { | |
279 | case EXISTS_DO_NOTHING: | |
cc71b0de | 280 | free_arg_item(arg_tok); |
4103818d CC |
281 | break; |
282 | case EXISTS_REPLACE: | |
85039fb6 | 283 | apply_item_command(in_tok, arg_tok); |
8966a394 JT |
284 | add_arg_to_input_list(on_tok, arg_tok); |
285 | list_del(&in_tok->list); | |
4103818d CC |
286 | free_trailer_item(in_tok); |
287 | break; | |
288 | case EXISTS_ADD: | |
85039fb6 | 289 | apply_item_command(in_tok, arg_tok); |
8966a394 | 290 | add_arg_to_input_list(on_tok, arg_tok); |
4103818d CC |
291 | break; |
292 | case EXISTS_ADD_IF_DIFFERENT: | |
85039fb6 | 293 | apply_item_command(in_tok, arg_tok); |
8966a394 JT |
294 | if (check_if_different(in_tok, arg_tok, 1, head)) |
295 | add_arg_to_input_list(on_tok, arg_tok); | |
4103818d | 296 | else |
cc71b0de | 297 | free_arg_item(arg_tok); |
4103818d CC |
298 | break; |
299 | case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR: | |
85039fb6 | 300 | apply_item_command(in_tok, arg_tok); |
8966a394 JT |
301 | if (check_if_different(on_tok, arg_tok, 0, head)) |
302 | add_arg_to_input_list(on_tok, arg_tok); | |
4103818d | 303 | else |
cc71b0de | 304 | free_arg_item(arg_tok); |
4103818d | 305 | break; |
0ea5292e | 306 | default: |
033abf97 | 307 | BUG("trailer.c: unhandled value %d", |
0ea5292e | 308 | arg_tok->conf.if_exists); |
4103818d CC |
309 | } |
310 | } | |
311 | ||
8966a394 | 312 | static void apply_arg_if_missing(struct list_head *head, |
cc71b0de | 313 | struct arg_item *arg_tok) |
4103818d | 314 | { |
52fc319d | 315 | enum trailer_where where; |
cc71b0de | 316 | struct trailer_item *to_add; |
4103818d CC |
317 | |
318 | switch (arg_tok->conf.if_missing) { | |
319 | case MISSING_DO_NOTHING: | |
cc71b0de | 320 | free_arg_item(arg_tok); |
4103818d CC |
321 | break; |
322 | case MISSING_ADD: | |
323 | where = arg_tok->conf.where; | |
85039fb6 | 324 | apply_item_command(NULL, arg_tok); |
cc71b0de | 325 | to_add = trailer_from_arg(arg_tok); |
8966a394 | 326 | if (after_or_end(where)) |
cc71b0de | 327 | list_add_tail(&to_add->list, head); |
8966a394 | 328 | else |
cc71b0de | 329 | list_add(&to_add->list, head); |
0ea5292e PB |
330 | break; |
331 | default: | |
033abf97 | 332 | BUG("trailer.c: unhandled value %d", |
0ea5292e | 333 | arg_tok->conf.if_missing); |
4103818d CC |
334 | } |
335 | } | |
336 | ||
8966a394 | 337 | static int find_same_and_apply_arg(struct list_head *head, |
cc71b0de | 338 | struct arg_item *arg_tok) |
4103818d | 339 | { |
8966a394 | 340 | struct list_head *pos; |
4103818d CC |
341 | struct trailer_item *in_tok; |
342 | struct trailer_item *on_tok; | |
4103818d | 343 | |
52fc319d | 344 | enum trailer_where where = arg_tok->conf.where; |
4103818d CC |
345 | int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE); |
346 | int backwards = after_or_end(where); | |
8966a394 | 347 | struct trailer_item *start_tok; |
4103818d | 348 | |
8966a394 JT |
349 | if (list_empty(head)) |
350 | return 0; | |
4103818d | 351 | |
8966a394 JT |
352 | start_tok = list_entry(backwards ? head->prev : head->next, |
353 | struct trailer_item, | |
354 | list); | |
355 | ||
356 | list_for_each_dir(pos, head, backwards) { | |
357 | in_tok = list_entry(pos, struct trailer_item, list); | |
4103818d CC |
358 | if (!same_token(in_tok, arg_tok)) |
359 | continue; | |
360 | on_tok = middle ? in_tok : start_tok; | |
8966a394 | 361 | apply_arg_if_exists(in_tok, arg_tok, on_tok, head); |
4103818d CC |
362 | return 1; |
363 | } | |
364 | return 0; | |
365 | } | |
366 | ||
8966a394 JT |
367 | static void process_trailers_lists(struct list_head *head, |
368 | struct list_head *arg_head) | |
4103818d | 369 | { |
8966a394 | 370 | struct list_head *pos, *p; |
cc71b0de | 371 | struct arg_item *arg_tok; |
4103818d | 372 | |
8966a394 | 373 | list_for_each_safe(pos, p, arg_head) { |
4103818d | 374 | int applied = 0; |
cc71b0de | 375 | arg_tok = list_entry(pos, struct arg_item, list); |
4103818d | 376 | |
8966a394 | 377 | list_del(pos); |
4103818d | 378 | |
8966a394 | 379 | applied = find_same_and_apply_arg(head, arg_tok); |
4103818d CC |
380 | |
381 | if (!applied) | |
8966a394 | 382 | apply_arg_if_missing(head, arg_tok); |
4103818d CC |
383 | } |
384 | } | |
46a0613f | 385 | |
52fc319d | 386 | int trailer_set_where(enum trailer_where *item, const char *value) |
46a0613f | 387 | { |
0ea5292e PB |
388 | if (!value) |
389 | *item = WHERE_DEFAULT; | |
390 | else if (!strcasecmp("after", value)) | |
52fc319d | 391 | *item = WHERE_AFTER; |
46a0613f | 392 | else if (!strcasecmp("before", value)) |
52fc319d | 393 | *item = WHERE_BEFORE; |
46a0613f | 394 | else if (!strcasecmp("end", value)) |
52fc319d | 395 | *item = WHERE_END; |
46a0613f | 396 | else if (!strcasecmp("start", value)) |
52fc319d | 397 | *item = WHERE_START; |
46a0613f CC |
398 | else |
399 | return -1; | |
400 | return 0; | |
401 | } | |
402 | ||
52fc319d | 403 | int trailer_set_if_exists(enum trailer_if_exists *item, const char *value) |
46a0613f | 404 | { |
0ea5292e PB |
405 | if (!value) |
406 | *item = EXISTS_DEFAULT; | |
407 | else if (!strcasecmp("addIfDifferent", value)) | |
52fc319d | 408 | *item = EXISTS_ADD_IF_DIFFERENT; |
46a0613f | 409 | else if (!strcasecmp("addIfDifferentNeighbor", value)) |
52fc319d | 410 | *item = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR; |
46a0613f | 411 | else if (!strcasecmp("add", value)) |
52fc319d | 412 | *item = EXISTS_ADD; |
46a0613f | 413 | else if (!strcasecmp("replace", value)) |
52fc319d | 414 | *item = EXISTS_REPLACE; |
46a0613f | 415 | else if (!strcasecmp("doNothing", value)) |
52fc319d | 416 | *item = EXISTS_DO_NOTHING; |
46a0613f CC |
417 | else |
418 | return -1; | |
419 | return 0; | |
420 | } | |
421 | ||
52fc319d | 422 | int trailer_set_if_missing(enum trailer_if_missing *item, const char *value) |
46a0613f | 423 | { |
0ea5292e PB |
424 | if (!value) |
425 | *item = MISSING_DEFAULT; | |
426 | else if (!strcasecmp("doNothing", value)) | |
52fc319d | 427 | *item = MISSING_DO_NOTHING; |
46a0613f | 428 | else if (!strcasecmp("add", value)) |
52fc319d | 429 | *item = MISSING_ADD; |
46a0613f CC |
430 | else |
431 | return -1; | |
432 | return 0; | |
433 | } | |
434 | ||
d65fd424 | 435 | static void duplicate_conf(struct conf_info *dst, const struct conf_info *src) |
46a0613f CC |
436 | { |
437 | *dst = *src; | |
13092a91 JH |
438 | dst->name = xstrdup_or_null(src->name); |
439 | dst->key = xstrdup_or_null(src->key); | |
440 | dst->command = xstrdup_or_null(src->command); | |
c364b7ef | 441 | dst->cmd = xstrdup_or_null(src->cmd); |
46a0613f CC |
442 | } |
443 | ||
cc71b0de | 444 | static struct arg_item *get_conf_item(const char *name) |
46a0613f | 445 | { |
8966a394 | 446 | struct list_head *pos; |
cc71b0de | 447 | struct arg_item *item; |
46a0613f CC |
448 | |
449 | /* Look up item with same name */ | |
8966a394 | 450 | list_for_each(pos, &conf_head) { |
cc71b0de | 451 | item = list_entry(pos, struct arg_item, list); |
46a0613f CC |
452 | if (!strcasecmp(item->conf.name, name)) |
453 | return item; | |
454 | } | |
455 | ||
456 | /* Item does not already exists, create it */ | |
ca56dadb | 457 | CALLOC_ARRAY(item, 1); |
46a0613f CC |
458 | duplicate_conf(&item->conf, &default_conf_info); |
459 | item->conf.name = xstrdup(name); | |
460 | ||
8966a394 | 461 | list_add_tail(&item->list, &conf_head); |
46a0613f CC |
462 | |
463 | return item; | |
464 | } | |
465 | ||
c364b7ef ZH |
466 | enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_CMD, |
467 | TRAILER_WHERE, TRAILER_IF_EXISTS, TRAILER_IF_MISSING }; | |
46a0613f CC |
468 | |
469 | static struct { | |
470 | const char *name; | |
471 | enum trailer_info_type type; | |
472 | } trailer_config_items[] = { | |
473 | { "key", TRAILER_KEY }, | |
474 | { "command", TRAILER_COMMAND }, | |
c364b7ef | 475 | { "cmd", TRAILER_CMD }, |
46a0613f CC |
476 | { "where", TRAILER_WHERE }, |
477 | { "ifexists", TRAILER_IF_EXISTS }, | |
478 | { "ifmissing", TRAILER_IF_MISSING } | |
479 | }; | |
480 | ||
783a86c1 | 481 | static int git_trailer_default_config(const char *conf_key, const char *value, |
5cf88fd8 | 482 | void *cb UNUSED) |
46a0613f CC |
483 | { |
484 | const char *trailer_item, *variable_name; | |
485 | ||
486 | if (!skip_prefix(conf_key, "trailer.", &trailer_item)) | |
487 | return 0; | |
488 | ||
489 | variable_name = strrchr(trailer_item, '.'); | |
490 | if (!variable_name) { | |
491 | if (!strcmp(trailer_item, "where")) { | |
52fc319d PB |
492 | if (trailer_set_where(&default_conf_info.where, |
493 | value) < 0) | |
46a0613f CC |
494 | warning(_("unknown value '%s' for key '%s'"), |
495 | value, conf_key); | |
496 | } else if (!strcmp(trailer_item, "ifexists")) { | |
52fc319d PB |
497 | if (trailer_set_if_exists(&default_conf_info.if_exists, |
498 | value) < 0) | |
46a0613f CC |
499 | warning(_("unknown value '%s' for key '%s'"), |
500 | value, conf_key); | |
501 | } else if (!strcmp(trailer_item, "ifmissing")) { | |
52fc319d PB |
502 | if (trailer_set_if_missing(&default_conf_info.if_missing, |
503 | value) < 0) | |
46a0613f CC |
504 | warning(_("unknown value '%s' for key '%s'"), |
505 | value, conf_key); | |
506 | } else if (!strcmp(trailer_item, "separators")) { | |
507 | separators = xstrdup(value); | |
508 | } | |
509 | } | |
510 | return 0; | |
511 | } | |
512 | ||
783a86c1 | 513 | static int git_trailer_config(const char *conf_key, const char *value, |
5cf88fd8 | 514 | void *cb UNUSED) |
46a0613f CC |
515 | { |
516 | const char *trailer_item, *variable_name; | |
cc71b0de | 517 | struct arg_item *item; |
46a0613f CC |
518 | struct conf_info *conf; |
519 | char *name = NULL; | |
520 | enum trailer_info_type type; | |
521 | int i; | |
522 | ||
523 | if (!skip_prefix(conf_key, "trailer.", &trailer_item)) | |
524 | return 0; | |
525 | ||
526 | variable_name = strrchr(trailer_item, '.'); | |
527 | if (!variable_name) | |
528 | return 0; | |
529 | ||
530 | variable_name++; | |
531 | for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) { | |
532 | if (strcmp(trailer_config_items[i].name, variable_name)) | |
533 | continue; | |
534 | name = xstrndup(trailer_item, variable_name - trailer_item - 1); | |
535 | type = trailer_config_items[i].type; | |
536 | break; | |
537 | } | |
538 | ||
539 | if (!name) | |
540 | return 0; | |
541 | ||
542 | item = get_conf_item(name); | |
543 | conf = &item->conf; | |
544 | free(name); | |
545 | ||
546 | switch (type) { | |
547 | case TRAILER_KEY: | |
548 | if (conf->key) | |
549 | warning(_("more than one %s"), conf_key); | |
550 | conf->key = xstrdup(value); | |
551 | break; | |
552 | case TRAILER_COMMAND: | |
553 | if (conf->command) | |
554 | warning(_("more than one %s"), conf_key); | |
555 | conf->command = xstrdup(value); | |
556 | break; | |
c364b7ef ZH |
557 | case TRAILER_CMD: |
558 | if (conf->cmd) | |
559 | warning(_("more than one %s"), conf_key); | |
560 | conf->cmd = xstrdup(value); | |
561 | break; | |
46a0613f | 562 | case TRAILER_WHERE: |
52fc319d | 563 | if (trailer_set_where(&conf->where, value)) |
46a0613f CC |
564 | warning(_("unknown value '%s' for key '%s'"), value, conf_key); |
565 | break; | |
566 | case TRAILER_IF_EXISTS: | |
52fc319d | 567 | if (trailer_set_if_exists(&conf->if_exists, value)) |
46a0613f CC |
568 | warning(_("unknown value '%s' for key '%s'"), value, conf_key); |
569 | break; | |
570 | case TRAILER_IF_MISSING: | |
52fc319d | 571 | if (trailer_set_if_missing(&conf->if_missing, value)) |
46a0613f CC |
572 | warning(_("unknown value '%s' for key '%s'"), value, conf_key); |
573 | break; | |
574 | default: | |
033abf97 | 575 | BUG("trailer.c: unhandled type %d", type); |
46a0613f CC |
576 | } |
577 | return 0; | |
578 | } | |
f0a90b4e | 579 | |
e8c352c3 JT |
580 | static void ensure_configured(void) |
581 | { | |
582 | if (configured) | |
583 | return; | |
584 | ||
585 | /* Default config must be setup first */ | |
52fc319d PB |
586 | default_conf_info.where = WHERE_END; |
587 | default_conf_info.if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR; | |
588 | default_conf_info.if_missing = MISSING_ADD; | |
e8c352c3 JT |
589 | git_config(git_trailer_default_config, NULL); |
590 | git_config(git_trailer_config, NULL); | |
591 | configured = 1; | |
592 | } | |
593 | ||
cc71b0de | 594 | static const char *token_from_item(struct arg_item *item, char *tok) |
f0a90b4e CC |
595 | { |
596 | if (item->conf.key) | |
597 | return item->conf.key; | |
598 | if (tok) | |
599 | return tok; | |
600 | return item->conf.name; | |
601 | } | |
602 | ||
0d2db00e | 603 | static int token_matches_item(const char *tok, struct arg_item *item, size_t tok_len) |
f0a90b4e CC |
604 | { |
605 | if (!strncasecmp(tok, item->conf.name, tok_len)) | |
606 | return 1; | |
607 | return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0; | |
608 | } | |
609 | ||
fdbf4510 | 610 | /* |
e4319562 JT |
611 | * If the given line is of the form |
612 | * "<token><optional whitespace><separator>..." or "<separator>...", return the | |
613 | * location of the separator. Otherwise, return -1. The optional whitespace | |
614 | * is allowed there primarily to allow things like "Bug #43" where <token> is | |
615 | * "Bug" and <separator> is "#". | |
616 | * | |
617 | * The separator-starts-line case (in which this function returns 0) is | |
618 | * distinguished from the non-well-formed-line case (in which this function | |
619 | * returns -1) because some callers of this function need such a distinction. | |
fdbf4510 | 620 | */ |
0d2db00e | 621 | static ssize_t find_separator(const char *line, const char *separators) |
f0a90b4e | 622 | { |
e4319562 JT |
623 | int whitespace_found = 0; |
624 | const char *c; | |
625 | for (c = line; *c; c++) { | |
626 | if (strchr(separators, *c)) | |
627 | return c - line; | |
628 | if (!whitespace_found && (isalnum(*c) || *c == '-')) | |
629 | continue; | |
630 | if (c != line && (*c == ' ' || *c == '\t')) { | |
631 | whitespace_found = 1; | |
632 | continue; | |
633 | } | |
634 | break; | |
635 | } | |
636 | return -1; | |
fdbf4510 | 637 | } |
f0a90b4e | 638 | |
fdbf4510 JT |
639 | /* |
640 | * Obtain the token, value, and conf from the given trailer. | |
641 | * | |
642 | * separator_pos must not be 0, since the token cannot be an empty string. | |
643 | * | |
644 | * If separator_pos is -1, interpret the whole trailer as a token. | |
645 | */ | |
646 | static void parse_trailer(struct strbuf *tok, struct strbuf *val, | |
647 | const struct conf_info **conf, const char *trailer, | |
0d2db00e | 648 | ssize_t separator_pos) |
f0a90b4e | 649 | { |
cc71b0de | 650 | struct arg_item *item; |
0d2db00e | 651 | size_t tok_len; |
63ab3f34 | 652 | struct list_head *pos; |
f0a90b4e | 653 | |
fdbf4510 JT |
654 | if (separator_pos != -1) { |
655 | strbuf_add(tok, trailer, separator_pos); | |
f0a90b4e | 656 | strbuf_trim(tok); |
fdbf4510 | 657 | strbuf_addstr(val, trailer + separator_pos + 1); |
f0a90b4e CC |
658 | strbuf_trim(val); |
659 | } else { | |
660 | strbuf_addstr(tok, trailer); | |
661 | strbuf_trim(tok); | |
662 | } | |
f0a90b4e CC |
663 | |
664 | /* Lookup if the token matches something in the config */ | |
63ab3f34 | 665 | tok_len = token_len_without_separator(tok->buf, tok->len); |
cc71b0de JT |
666 | if (conf) |
667 | *conf = &default_conf_info; | |
8966a394 | 668 | list_for_each(pos, &conf_head) { |
cc71b0de | 669 | item = list_entry(pos, struct arg_item, list); |
63ab3f34 JT |
670 | if (token_matches_item(tok->buf, item, tok_len)) { |
671 | char *tok_buf = strbuf_detach(tok, NULL); | |
cc71b0de JT |
672 | if (conf) |
673 | *conf = &item->conf; | |
63ab3f34 JT |
674 | strbuf_addstr(tok, token_from_item(item, tok_buf)); |
675 | free(tok_buf); | |
676 | break; | |
677 | } | |
f0a90b4e | 678 | } |
f0a90b4e | 679 | } |
f0a90b4e | 680 | |
60ef86a1 JT |
681 | static struct trailer_item *add_trailer_item(struct list_head *head, char *tok, |
682 | char *val) | |
f0a90b4e | 683 | { |
241b5d3e | 684 | struct trailer_item *new_item = xcalloc(1, sizeof(*new_item)); |
17651326 BW |
685 | new_item->token = tok; |
686 | new_item->value = val; | |
687 | list_add_tail(&new_item->list, head); | |
688 | return new_item; | |
f0a90b4e CC |
689 | } |
690 | ||
cc71b0de | 691 | static void add_arg_item(struct list_head *arg_head, char *tok, char *val, |
0ea5292e PB |
692 | const struct conf_info *conf, |
693 | const struct new_trailer_item *new_trailer_item) | |
f0a90b4e | 694 | { |
241b5d3e | 695 | struct arg_item *new_item = xcalloc(1, sizeof(*new_item)); |
17651326 BW |
696 | new_item->token = tok; |
697 | new_item->value = val; | |
698 | duplicate_conf(&new_item->conf, conf); | |
0ea5292e PB |
699 | if (new_trailer_item) { |
700 | if (new_trailer_item->where != WHERE_DEFAULT) | |
17651326 | 701 | new_item->conf.where = new_trailer_item->where; |
0ea5292e | 702 | if (new_trailer_item->if_exists != EXISTS_DEFAULT) |
17651326 | 703 | new_item->conf.if_exists = new_trailer_item->if_exists; |
0ea5292e | 704 | if (new_trailer_item->if_missing != MISSING_DEFAULT) |
17651326 | 705 | new_item->conf.if_missing = new_trailer_item->if_missing; |
0ea5292e | 706 | } |
17651326 | 707 | list_add_tail(&new_item->list, arg_head); |
f0a90b4e CC |
708 | } |
709 | ||
8966a394 | 710 | static void process_command_line_args(struct list_head *arg_head, |
51166b87 | 711 | struct list_head *new_trailer_head) |
f0a90b4e | 712 | { |
cc71b0de | 713 | struct arg_item *item; |
63ab3f34 JT |
714 | struct strbuf tok = STRBUF_INIT; |
715 | struct strbuf val = STRBUF_INIT; | |
716 | const struct conf_info *conf; | |
8966a394 | 717 | struct list_head *pos; |
85039fb6 | 718 | |
fdbf4510 JT |
719 | /* |
720 | * In command-line arguments, '=' is accepted (in addition to the | |
721 | * separators that are defined). | |
722 | */ | |
723 | char *cl_separators = xstrfmt("=%s", separators); | |
724 | ||
cc71b0de | 725 | /* Add an arg item for each configured trailer with a command */ |
8966a394 | 726 | list_for_each(pos, &conf_head) { |
cc71b0de | 727 | item = list_entry(pos, struct arg_item, list); |
63ab3f34 | 728 | if (item->conf.command) |
cc71b0de JT |
729 | add_arg_item(arg_head, |
730 | xstrdup(token_from_item(item, NULL)), | |
731 | xstrdup(""), | |
0ea5292e | 732 | &item->conf, NULL); |
85039fb6 | 733 | } |
f0a90b4e | 734 | |
cc71b0de | 735 | /* Add an arg item for each trailer on the command line */ |
51166b87 PB |
736 | list_for_each(pos, new_trailer_head) { |
737 | struct new_trailer_item *tr = | |
738 | list_entry(pos, struct new_trailer_item, list); | |
0d2db00e | 739 | ssize_t separator_pos = find_separator(tr->text, cl_separators); |
51166b87 | 740 | |
fdbf4510 JT |
741 | if (separator_pos == 0) { |
742 | struct strbuf sb = STRBUF_INIT; | |
51166b87 | 743 | strbuf_addstr(&sb, tr->text); |
fdbf4510 JT |
744 | strbuf_trim(&sb); |
745 | error(_("empty trailer token in trailer '%.*s'"), | |
746 | (int) sb.len, sb.buf); | |
747 | strbuf_release(&sb); | |
748 | } else { | |
51166b87 | 749 | parse_trailer(&tok, &val, &conf, tr->text, |
fdbf4510 | 750 | separator_pos); |
cc71b0de JT |
751 | add_arg_item(arg_head, |
752 | strbuf_detach(&tok, NULL), | |
753 | strbuf_detach(&val, NULL), | |
0ea5292e | 754 | conf, tr); |
fdbf4510 | 755 | } |
f0a90b4e CC |
756 | } |
757 | ||
fdbf4510 | 758 | free(cl_separators); |
f0a90b4e | 759 | } |
2013d850 | 760 | |
022349c3 | 761 | static void read_input_file(struct strbuf *sb, const char *file) |
2013d850 | 762 | { |
2013d850 | 763 | if (file) { |
022349c3 | 764 | if (strbuf_read_file(sb, file, 0) < 0) |
2013d850 CC |
765 | die_errno(_("could not read input file '%s'"), file); |
766 | } else { | |
022349c3 | 767 | if (strbuf_read(sb, fileno(stdin), 0) < 0) |
2013d850 CC |
768 | die_errno(_("could not read from stdin")); |
769 | } | |
022349c3 | 770 | } |
2013d850 | 771 | |
022349c3 JT |
772 | static const char *next_line(const char *str) |
773 | { | |
774 | const char *nl = strchrnul(str, '\n'); | |
775 | return nl + !!*nl; | |
776 | } | |
2013d850 | 777 | |
022349c3 JT |
778 | /* |
779 | * Return the position of the start of the last line. If len is 0, return -1. | |
780 | */ | |
0d2db00e | 781 | static ssize_t last_line(const char *buf, size_t len) |
022349c3 | 782 | { |
0d2db00e | 783 | ssize_t i; |
022349c3 JT |
784 | if (len == 0) |
785 | return -1; | |
786 | if (len == 1) | |
787 | return 0; | |
788 | /* | |
789 | * Skip the last character (in addition to the null terminator), | |
790 | * because if the last character is a newline, it is considered as part | |
791 | * of the last line anyway. | |
792 | */ | |
793 | i = len - 2; | |
2013d850 | 794 | |
022349c3 JT |
795 | for (; i >= 0; i--) { |
796 | if (buf[i] == '\n') | |
797 | return i + 1; | |
798 | } | |
799 | return 0; | |
2013d850 CC |
800 | } |
801 | ||
802 | /* | |
022349c3 JT |
803 | * Return the position of the start of the patch or the length of str if there |
804 | * is no patch in the message. | |
2013d850 | 805 | */ |
0d2db00e | 806 | static size_t find_patch_start(const char *str) |
2013d850 | 807 | { |
022349c3 | 808 | const char *s; |
2013d850 | 809 | |
022349c3 | 810 | for (s = str; *s; s = next_line(s)) { |
c188668e JK |
811 | const char *v; |
812 | ||
813 | if (skip_prefix(s, "---", &v) && isspace(*v)) | |
022349c3 | 814 | return s - str; |
2013d850 CC |
815 | } |
816 | ||
022349c3 | 817 | return s - str; |
2013d850 CC |
818 | } |
819 | ||
820 | /* | |
022349c3 JT |
821 | * Return the position of the first trailer line or len if there are no |
822 | * trailers. | |
2013d850 | 823 | */ |
0d2db00e | 824 | static size_t find_trailer_start(const char *buf, size_t len) |
2013d850 | 825 | { |
022349c3 | 826 | const char *s; |
0d2db00e JK |
827 | ssize_t end_of_title, l; |
828 | int only_spaces = 1; | |
14624506 | 829 | int recognized_prefix = 0, trailer_lines = 0, non_trailer_lines = 0; |
60ef86a1 JT |
830 | /* |
831 | * Number of possible continuation lines encountered. This will be | |
832 | * reset to 0 if we encounter a trailer (since those lines are to be | |
833 | * considered continuations of that trailer), and added to | |
834 | * non_trailer_lines if we encounter a non-trailer (since those lines | |
835 | * are to be considered non-trailers). | |
836 | */ | |
837 | int possible_continuation_lines = 0; | |
5c99995d CC |
838 | |
839 | /* The first paragraph is the title and cannot be trailers */ | |
022349c3 JT |
840 | for (s = buf; s < buf + len; s = next_line(s)) { |
841 | if (s[0] == comment_line_char) | |
5c99995d | 842 | continue; |
022349c3 | 843 | if (is_blank_line(s)) |
5c99995d CC |
844 | break; |
845 | } | |
022349c3 | 846 | end_of_title = s - buf; |
2013d850 CC |
847 | |
848 | /* | |
14624506 JT |
849 | * Get the start of the trailers by looking starting from the end for a |
850 | * blank line before a set of non-blank lines that (i) are all | |
851 | * trailers, or (ii) contains at least one Git-generated trailer and | |
852 | * consists of at least 25% trailers. | |
2013d850 | 853 | */ |
022349c3 JT |
854 | for (l = last_line(buf, len); |
855 | l >= end_of_title; | |
856 | l = last_line(buf, l)) { | |
857 | const char *bol = buf + l; | |
14624506 | 858 | const char **p; |
0d2db00e | 859 | ssize_t separator_pos; |
14624506 | 860 | |
022349c3 | 861 | if (bol[0] == comment_line_char) { |
60ef86a1 JT |
862 | non_trailer_lines += possible_continuation_lines; |
863 | possible_continuation_lines = 0; | |
2013d850 | 864 | continue; |
60ef86a1 | 865 | } |
022349c3 | 866 | if (is_blank_line(bol)) { |
2013d850 CC |
867 | if (only_spaces) |
868 | continue; | |
60ef86a1 | 869 | non_trailer_lines += possible_continuation_lines; |
14624506 JT |
870 | if (recognized_prefix && |
871 | trailer_lines * 3 >= non_trailer_lines) | |
022349c3 JT |
872 | return next_line(bol) - buf; |
873 | else if (trailer_lines && !non_trailer_lines) | |
874 | return next_line(bol) - buf; | |
875 | return len; | |
2013d850 | 876 | } |
14624506 JT |
877 | only_spaces = 0; |
878 | ||
879 | for (p = git_generated_prefixes; *p; p++) { | |
022349c3 | 880 | if (starts_with(bol, *p)) { |
14624506 | 881 | trailer_lines++; |
60ef86a1 | 882 | possible_continuation_lines = 0; |
14624506 JT |
883 | recognized_prefix = 1; |
884 | goto continue_outer_loop; | |
885 | } | |
2013d850 | 886 | } |
14624506 | 887 | |
022349c3 JT |
888 | separator_pos = find_separator(bol, separators); |
889 | if (separator_pos >= 1 && !isspace(bol[0])) { | |
14624506 JT |
890 | struct list_head *pos; |
891 | ||
892 | trailer_lines++; | |
60ef86a1 | 893 | possible_continuation_lines = 0; |
14624506 JT |
894 | if (recognized_prefix) |
895 | continue; | |
896 | list_for_each(pos, &conf_head) { | |
897 | struct arg_item *item; | |
898 | item = list_entry(pos, struct arg_item, list); | |
022349c3 | 899 | if (token_matches_item(bol, item, |
14624506 JT |
900 | separator_pos)) { |
901 | recognized_prefix = 1; | |
902 | break; | |
903 | } | |
904 | } | |
022349c3 | 905 | } else if (isspace(bol[0])) |
60ef86a1 JT |
906 | possible_continuation_lines++; |
907 | else { | |
14624506 | 908 | non_trailer_lines++; |
60ef86a1 JT |
909 | non_trailer_lines += possible_continuation_lines; |
910 | possible_continuation_lines = 0; | |
2013d850 | 911 | } |
14624506 JT |
912 | continue_outer_loop: |
913 | ; | |
2013d850 CC |
914 | } |
915 | ||
022349c3 | 916 | return len; |
61cfef4c CC |
917 | } |
918 | ||
022349c3 | 919 | /* Return the position of the end of the trailers. */ |
0d2db00e | 920 | static size_t find_trailer_end(const char *buf, size_t len) |
2013d850 | 921 | { |
022349c3 | 922 | return len - ignore_non_trailer(buf, len); |
2013d850 CC |
923 | } |
924 | ||
022349c3 | 925 | static int ends_with_blank_line(const char *buf, size_t len) |
2013d850 | 926 | { |
0d2db00e | 927 | ssize_t ll = last_line(buf, len); |
022349c3 JT |
928 | if (ll < 0) |
929 | return 0; | |
930 | return is_blank_line(buf + ll); | |
2013d850 CC |
931 | } |
932 | ||
00002396 JK |
933 | static void unfold_value(struct strbuf *val) |
934 | { | |
935 | struct strbuf out = STRBUF_INIT; | |
936 | size_t i; | |
937 | ||
938 | strbuf_grow(&out, val->len); | |
939 | i = 0; | |
940 | while (i < val->len) { | |
941 | char c = val->buf[i++]; | |
942 | if (c == '\n') { | |
943 | /* Collapse continuation down to a single space. */ | |
944 | while (i < val->len && isspace(val->buf[i])) | |
945 | i++; | |
946 | strbuf_addch(&out, ' '); | |
947 | } else { | |
948 | strbuf_addch(&out, c); | |
949 | } | |
950 | } | |
951 | ||
952 | /* Empty lines may have left us with whitespace cruft at the edges */ | |
953 | strbuf_trim(&out); | |
954 | ||
955 | /* output goes back to val as if we modified it in-place */ | |
956 | strbuf_swap(&out, val); | |
957 | strbuf_release(&out); | |
958 | } | |
959 | ||
0d2db00e JK |
960 | static size_t process_input_file(FILE *outfile, |
961 | const char *str, | |
962 | struct list_head *head, | |
963 | const struct process_trailer_options *opts) | |
2013d850 | 964 | { |
e8c352c3 | 965 | struct trailer_info info; |
63ab3f34 JT |
966 | struct strbuf tok = STRBUF_INIT; |
967 | struct strbuf val = STRBUF_INIT; | |
a3b636e2 | 968 | size_t i; |
2013d850 | 969 | |
00a21f5c | 970 | trailer_info_get(&info, str, opts); |
2013d850 CC |
971 | |
972 | /* Print lines before the trailers as is */ | |
56c493ed JK |
973 | if (!opts->only_trailers) |
974 | fwrite(str, 1, info.trailer_start - str, outfile); | |
2013d850 | 975 | |
56c493ed | 976 | if (!opts->only_trailers && !info.blank_line_before_trailer) |
d0d2344a | 977 | fprintf(outfile, "\n"); |
2013d850 | 978 | |
e8c352c3 | 979 | for (i = 0; i < info.trailer_nr; i++) { |
fdbf4510 | 980 | int separator_pos; |
e8c352c3 JT |
981 | char *trailer = info.trailers[i]; |
982 | if (trailer[0] == comment_line_char) | |
60ef86a1 | 983 | continue; |
e8c352c3 | 984 | separator_pos = find_separator(trailer, separators); |
fdbf4510 | 985 | if (separator_pos >= 1) { |
e8c352c3 | 986 | parse_trailer(&tok, &val, NULL, trailer, |
fdbf4510 | 987 | separator_pos); |
00002396 JK |
988 | if (opts->unfold) |
989 | unfold_value(&val); | |
e8c352c3 JT |
990 | add_trailer_item(head, |
991 | strbuf_detach(&tok, NULL), | |
992 | strbuf_detach(&val, NULL)); | |
56c493ed | 993 | } else if (!opts->only_trailers) { |
e8c352c3 | 994 | strbuf_addstr(&val, trailer); |
14624506 JT |
995 | strbuf_strip_suffix(&val, "\n"); |
996 | add_trailer_item(head, | |
997 | NULL, | |
998 | strbuf_detach(&val, NULL)); | |
2887103b | 999 | } |
2013d850 CC |
1000 | } |
1001 | ||
e8c352c3 JT |
1002 | trailer_info_release(&info); |
1003 | ||
1004 | return info.trailer_end - str; | |
2013d850 | 1005 | } |
b1d78d77 | 1006 | |
8966a394 | 1007 | static void free_all(struct list_head *head) |
b1d78d77 | 1008 | { |
8966a394 JT |
1009 | struct list_head *pos, *p; |
1010 | list_for_each_safe(pos, p, head) { | |
1011 | list_del(pos); | |
1012 | free_trailer_item(list_entry(pos, struct trailer_item, list)); | |
b1d78d77 CC |
1013 | } |
1014 | } | |
1015 | ||
076aa2cb | 1016 | static struct tempfile *trailers_tempfile; |
e1f89863 TK |
1017 | |
1018 | static FILE *create_in_place_tempfile(const char *file) | |
1019 | { | |
1020 | struct stat st; | |
31c2c7a0 | 1021 | struct strbuf filename_template = STRBUF_INIT; |
e1f89863 TK |
1022 | const char *tail; |
1023 | FILE *outfile; | |
1024 | ||
1025 | if (stat(file, &st)) | |
1026 | die_errno(_("could not stat %s"), file); | |
1027 | if (!S_ISREG(st.st_mode)) | |
1028 | die(_("file %s is not a regular file"), file); | |
1029 | if (!(st.st_mode & S_IWUSR)) | |
1030 | die(_("file %s is not writable by user"), file); | |
1031 | ||
1032 | /* Create temporary file in the same directory as the original */ | |
1033 | tail = strrchr(file, '/'); | |
afe8a907 | 1034 | if (tail) |
31c2c7a0 BW |
1035 | strbuf_add(&filename_template, file, tail - file + 1); |
1036 | strbuf_addstr(&filename_template, "git-interpret-trailers-XXXXXX"); | |
e1f89863 | 1037 | |
31c2c7a0 BW |
1038 | trailers_tempfile = xmks_tempfile_m(filename_template.buf, st.st_mode); |
1039 | strbuf_release(&filename_template); | |
076aa2cb | 1040 | outfile = fdopen_tempfile(trailers_tempfile, "w"); |
e1f89863 TK |
1041 | if (!outfile) |
1042 | die_errno(_("could not open temporary file")); | |
1043 | ||
1044 | return outfile; | |
1045 | } | |
1046 | ||
8abc8980 JK |
1047 | void process_trailers(const char *file, |
1048 | const struct process_trailer_options *opts, | |
51166b87 | 1049 | struct list_head *new_trailer_head) |
b1d78d77 | 1050 | { |
8966a394 | 1051 | LIST_HEAD(head); |
022349c3 | 1052 | struct strbuf sb = STRBUF_INIT; |
0d2db00e | 1053 | size_t trailer_end; |
d0d2344a | 1054 | FILE *outfile = stdout; |
b1d78d77 | 1055 | |
e8c352c3 | 1056 | ensure_configured(); |
b1d78d77 | 1057 | |
022349c3 | 1058 | read_input_file(&sb, file); |
b1d78d77 | 1059 | |
8abc8980 | 1060 | if (opts->in_place) |
e1f89863 TK |
1061 | outfile = create_in_place_tempfile(file); |
1062 | ||
b1d78d77 | 1063 | /* Print the lines before the trailers */ |
56c493ed | 1064 | trailer_end = process_input_file(outfile, sb.buf, &head, opts); |
b1d78d77 | 1065 | |
fdbdb64f JK |
1066 | if (!opts->only_input) { |
1067 | LIST_HEAD(arg_head); | |
06cf4f2d | 1068 | process_command_line_args(&arg_head, new_trailer_head); |
fdbdb64f JK |
1069 | process_trailers_lists(&head, &arg_head); |
1070 | } | |
b1d78d77 | 1071 | |
56c493ed | 1072 | print_all(outfile, &head, opts); |
b1d78d77 | 1073 | |
8966a394 | 1074 | free_all(&head); |
b1d78d77 CC |
1075 | |
1076 | /* Print the lines after the trailers as is */ | |
56c493ed JK |
1077 | if (!opts->only_trailers) |
1078 | fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile); | |
b1d78d77 | 1079 | |
8abc8980 | 1080 | if (opts->in_place) |
e1f89863 TK |
1081 | if (rename_tempfile(&trailers_tempfile, file)) |
1082 | die_errno(_("could not rename temporary file to %s"), file); | |
1083 | ||
022349c3 | 1084 | strbuf_release(&sb); |
b1d78d77 | 1085 | } |
e8c352c3 | 1086 | |
00a21f5c JK |
1087 | void trailer_info_get(struct trailer_info *info, const char *str, |
1088 | const struct process_trailer_options *opts) | |
e8c352c3 JT |
1089 | { |
1090 | int patch_start, trailer_end, trailer_start; | |
1091 | struct strbuf **trailer_lines, **ptr; | |
1092 | char **trailer_strings = NULL; | |
1093 | size_t nr = 0, alloc = 0; | |
1094 | char **last = NULL; | |
1095 | ||
1096 | ensure_configured(); | |
1097 | ||
1688c9a4 JK |
1098 | if (opts->no_divider) |
1099 | patch_start = strlen(str); | |
1100 | else | |
1101 | patch_start = find_patch_start(str); | |
1102 | ||
e8c352c3 JT |
1103 | trailer_end = find_trailer_end(str, patch_start); |
1104 | trailer_start = find_trailer_start(str, trailer_end); | |
1105 | ||
1106 | trailer_lines = strbuf_split_buf(str + trailer_start, | |
1107 | trailer_end - trailer_start, | |
1108 | '\n', | |
1109 | 0); | |
1110 | for (ptr = trailer_lines; *ptr; ptr++) { | |
1111 | if (last && isspace((*ptr)->buf[0])) { | |
1112 | struct strbuf sb = STRBUF_INIT; | |
1113 | strbuf_attach(&sb, *last, strlen(*last), strlen(*last)); | |
1114 | strbuf_addbuf(&sb, *ptr); | |
1115 | *last = strbuf_detach(&sb, NULL); | |
1116 | continue; | |
1117 | } | |
1118 | ALLOC_GROW(trailer_strings, nr + 1, alloc); | |
1119 | trailer_strings[nr] = strbuf_detach(*ptr, NULL); | |
1120 | last = find_separator(trailer_strings[nr], separators) >= 1 | |
1121 | ? &trailer_strings[nr] | |
1122 | : NULL; | |
1123 | nr++; | |
1124 | } | |
1125 | strbuf_list_free(trailer_lines); | |
1126 | ||
1127 | info->blank_line_before_trailer = ends_with_blank_line(str, | |
1128 | trailer_start); | |
1129 | info->trailer_start = str + trailer_start; | |
1130 | info->trailer_end = str + trailer_end; | |
1131 | info->trailers = trailer_strings; | |
1132 | info->trailer_nr = nr; | |
1133 | } | |
1134 | ||
1135 | void trailer_info_release(struct trailer_info *info) | |
1136 | { | |
a3b636e2 | 1137 | size_t i; |
e8c352c3 JT |
1138 | for (i = 0; i < info->trailer_nr; i++) |
1139 | free(info->trailers[i]); | |
1140 | free(info->trailers); | |
1141 | } | |
a388b10f JK |
1142 | |
1143 | static void format_trailer_info(struct strbuf *out, | |
1144 | const struct trailer_info *info, | |
1145 | const struct process_trailer_options *opts) | |
1146 | { | |
0b691d86 | 1147 | size_t origlen = out->len; |
a3b636e2 | 1148 | size_t i; |
58311c66 JK |
1149 | |
1150 | /* If we want the whole block untouched, we can take the fast path. */ | |
8b966a05 | 1151 | if (!opts->only_trailers && !opts->unfold && !opts->filter && |
058761f1 ÆAB |
1152 | !opts->separator && !opts->key_only && !opts->value_only && |
1153 | !opts->key_value_separator) { | |
58311c66 JK |
1154 | strbuf_add(out, info->trailer_start, |
1155 | info->trailer_end - info->trailer_start); | |
1156 | return; | |
1157 | } | |
1158 | ||
1159 | for (i = 0; i < info->trailer_nr; i++) { | |
1160 | char *trailer = info->trailers[i]; | |
0d2db00e | 1161 | ssize_t separator_pos = find_separator(trailer, separators); |
58311c66 JK |
1162 | |
1163 | if (separator_pos >= 1) { | |
1164 | struct strbuf tok = STRBUF_INIT; | |
1165 | struct strbuf val = STRBUF_INIT; | |
1166 | ||
1167 | parse_trailer(&tok, &val, NULL, trailer, separator_pos); | |
250bea0c AW |
1168 | if (!opts->filter || opts->filter(&tok, opts->filter_data)) { |
1169 | if (opts->unfold) | |
1170 | unfold_value(&val); | |
0b691d86 AW |
1171 | |
1172 | if (opts->separator && out->len != origlen) | |
1173 | strbuf_addbuf(out, opts->separator); | |
d9b936db | 1174 | if (!opts->value_only) |
9d87d5ae | 1175 | strbuf_addbuf(out, &tok); |
058761f1 ÆAB |
1176 | if (!opts->key_only && !opts->value_only) { |
1177 | if (opts->key_value_separator) | |
1178 | strbuf_addbuf(out, opts->key_value_separator); | |
1179 | else | |
1180 | strbuf_addstr(out, ": "); | |
1181 | } | |
9d87d5ae ÆAB |
1182 | if (!opts->key_only) |
1183 | strbuf_addbuf(out, &val); | |
0b691d86 AW |
1184 | if (!opts->separator) |
1185 | strbuf_addch(out, '\n'); | |
250bea0c | 1186 | } |
58311c66 JK |
1187 | strbuf_release(&tok); |
1188 | strbuf_release(&val); | |
1189 | ||
1190 | } else if (!opts->only_trailers) { | |
0b691d86 AW |
1191 | if (opts->separator && out->len != origlen) { |
1192 | strbuf_addbuf(out, opts->separator); | |
1193 | } | |
58311c66 | 1194 | strbuf_addstr(out, trailer); |
0b691d86 AW |
1195 | if (opts->separator) { |
1196 | strbuf_rtrim(out); | |
1197 | } | |
58311c66 JK |
1198 | } |
1199 | } | |
1200 | ||
a388b10f JK |
1201 | } |
1202 | ||
1203 | void format_trailers_from_commit(struct strbuf *out, const char *msg, | |
1204 | const struct process_trailer_options *opts) | |
1205 | { | |
1206 | struct trailer_info info; | |
1207 | ||
00a21f5c | 1208 | trailer_info_get(&info, msg, opts); |
a388b10f JK |
1209 | format_trailer_info(out, &info, opts); |
1210 | trailer_info_release(&info); | |
1211 | } | |
f0939a0e JK |
1212 | |
1213 | void trailer_iterator_init(struct trailer_iterator *iter, const char *msg) | |
1214 | { | |
1215 | struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; | |
1216 | strbuf_init(&iter->key, 0); | |
1217 | strbuf_init(&iter->val, 0); | |
1218 | opts.no_divider = 1; | |
1219 | trailer_info_get(&iter->info, msg, &opts); | |
1220 | iter->cur = 0; | |
1221 | } | |
1222 | ||
1223 | int trailer_iterator_advance(struct trailer_iterator *iter) | |
1224 | { | |
1225 | while (iter->cur < iter->info.trailer_nr) { | |
1226 | char *trailer = iter->info.trailers[iter->cur++]; | |
1227 | int separator_pos = find_separator(trailer, separators); | |
1228 | ||
1229 | if (separator_pos < 1) | |
1230 | continue; /* not a real trailer */ | |
1231 | ||
1232 | strbuf_reset(&iter->key); | |
1233 | strbuf_reset(&iter->val); | |
1234 | parse_trailer(&iter->key, &iter->val, NULL, | |
1235 | trailer, separator_pos); | |
1236 | unfold_value(&iter->val); | |
1237 | return 1; | |
1238 | } | |
1239 | return 0; | |
1240 | } | |
1241 | ||
1242 | void trailer_iterator_release(struct trailer_iterator *iter) | |
1243 | { | |
1244 | trailer_info_release(&iter->info); | |
1245 | strbuf_release(&iter->val); | |
1246 | strbuf_release(&iter->key); | |
1247 | } |