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