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