]>
Commit | Line | Data |
---|---|---|
9385b5d7 | 1 | #include "cache.h" |
f0a90b4e | 2 | #include "string-list.h" |
b1d78d77 | 3 | #include "trailer.h" |
9385b5d7 CC |
4 | /* |
5 | * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org> | |
6 | */ | |
7 | ||
8 | enum action_where { WHERE_END, WHERE_AFTER, WHERE_BEFORE, WHERE_START }; | |
9 | enum action_if_exists { EXISTS_ADD_IF_DIFFERENT_NEIGHBOR, EXISTS_ADD_IF_DIFFERENT, | |
10 | EXISTS_ADD, EXISTS_REPLACE, EXISTS_DO_NOTHING }; | |
11 | enum action_if_missing { MISSING_ADD, MISSING_DO_NOTHING }; | |
12 | ||
13 | struct conf_info { | |
14 | char *name; | |
15 | char *key; | |
16 | char *command; | |
17 | enum action_where where; | |
18 | enum action_if_exists if_exists; | |
19 | enum action_if_missing if_missing; | |
20 | }; | |
21 | ||
22 | static struct conf_info default_conf_info; | |
23 | ||
24 | struct trailer_item { | |
25 | struct trailer_item *previous; | |
26 | struct trailer_item *next; | |
27 | const char *token; | |
28 | const char *value; | |
29 | struct conf_info conf; | |
30 | }; | |
31 | ||
32 | static struct trailer_item *first_conf_item; | |
33 | ||
34 | static char *separators = ":"; | |
35 | ||
36 | static int after_or_end(enum action_where where) | |
37 | { | |
38 | return (where == WHERE_AFTER) || (where == WHERE_END); | |
39 | } | |
40 | ||
41 | /* | |
42 | * Return the length of the string not including any final | |
43 | * punctuation. E.g., the input "Signed-off-by:" would return | |
44 | * 13, stripping the trailing punctuation but retaining | |
45 | * internal punctuation. | |
46 | */ | |
47 | static size_t token_len_without_separator(const char *token, size_t len) | |
48 | { | |
49 | while (len > 0 && !isalnum(token[len - 1])) | |
50 | len--; | |
51 | return len; | |
52 | } | |
53 | ||
54 | static int same_token(struct trailer_item *a, struct trailer_item *b) | |
55 | { | |
56 | size_t a_len = token_len_without_separator(a->token, strlen(a->token)); | |
57 | size_t b_len = token_len_without_separator(b->token, strlen(b->token)); | |
58 | size_t min_len = (a_len > b_len) ? b_len : a_len; | |
59 | ||
60 | return !strncasecmp(a->token, b->token, min_len); | |
61 | } | |
62 | ||
63 | static int same_value(struct trailer_item *a, struct trailer_item *b) | |
64 | { | |
65 | return !strcasecmp(a->value, b->value); | |
66 | } | |
67 | ||
68 | static int same_trailer(struct trailer_item *a, struct trailer_item *b) | |
69 | { | |
70 | return same_token(a, b) && same_value(a, b); | |
71 | } | |
4103818d | 72 | |
2013d850 CC |
73 | static inline int contains_only_spaces(const char *str) |
74 | { | |
75 | const char *s = str; | |
76 | while (*s && isspace(*s)) | |
77 | s++; | |
78 | return !*s; | |
79 | } | |
80 | ||
4103818d CC |
81 | static void free_trailer_item(struct trailer_item *item) |
82 | { | |
83 | free(item->conf.name); | |
84 | free(item->conf.key); | |
85 | free(item->conf.command); | |
86 | free((char *)item->token); | |
87 | free((char *)item->value); | |
88 | free(item); | |
89 | } | |
90 | ||
b1d78d77 CC |
91 | static char last_non_space_char(const char *s) |
92 | { | |
93 | int i; | |
94 | for (i = strlen(s) - 1; i >= 0; i--) | |
95 | if (!isspace(s[i])) | |
96 | return s[i]; | |
97 | return '\0'; | |
98 | } | |
99 | ||
100 | static void print_tok_val(const char *tok, const char *val) | |
101 | { | |
102 | char c = last_non_space_char(tok); | |
103 | if (!c) | |
104 | return; | |
105 | if (strchr(separators, c)) | |
106 | printf("%s%s\n", tok, val); | |
107 | else | |
108 | printf("%s%c %s\n", tok, separators[0], val); | |
109 | } | |
110 | ||
111 | static void print_all(struct trailer_item *first, int trim_empty) | |
112 | { | |
113 | struct trailer_item *item; | |
114 | for (item = first; item; item = item->next) { | |
115 | if (!trim_empty || strlen(item->value) > 0) | |
116 | print_tok_val(item->token, item->value); | |
117 | } | |
118 | } | |
119 | ||
4103818d CC |
120 | static void update_last(struct trailer_item **last) |
121 | { | |
122 | if (*last) | |
123 | while ((*last)->next != NULL) | |
124 | *last = (*last)->next; | |
125 | } | |
126 | ||
127 | static void update_first(struct trailer_item **first) | |
128 | { | |
129 | if (*first) | |
130 | while ((*first)->previous != NULL) | |
131 | *first = (*first)->previous; | |
132 | } | |
133 | ||
134 | static void add_arg_to_input_list(struct trailer_item *on_tok, | |
135 | struct trailer_item *arg_tok, | |
136 | struct trailer_item **first, | |
137 | struct trailer_item **last) | |
138 | { | |
139 | if (after_or_end(arg_tok->conf.where)) { | |
140 | arg_tok->next = on_tok->next; | |
141 | on_tok->next = arg_tok; | |
142 | arg_tok->previous = on_tok; | |
143 | if (arg_tok->next) | |
144 | arg_tok->next->previous = arg_tok; | |
145 | update_last(last); | |
146 | } else { | |
147 | arg_tok->previous = on_tok->previous; | |
148 | on_tok->previous = arg_tok; | |
149 | arg_tok->next = on_tok; | |
150 | if (arg_tok->previous) | |
151 | arg_tok->previous->next = arg_tok; | |
152 | update_first(first); | |
153 | } | |
154 | } | |
155 | ||
156 | static int check_if_different(struct trailer_item *in_tok, | |
157 | struct trailer_item *arg_tok, | |
158 | int check_all) | |
159 | { | |
160 | enum action_where where = arg_tok->conf.where; | |
161 | do { | |
162 | if (!in_tok) | |
163 | return 1; | |
164 | if (same_trailer(in_tok, arg_tok)) | |
165 | return 0; | |
166 | /* | |
167 | * if we want to add a trailer after another one, | |
168 | * we have to check those before this one | |
169 | */ | |
170 | in_tok = after_or_end(where) ? in_tok->previous : in_tok->next; | |
171 | } while (check_all); | |
172 | return 1; | |
173 | } | |
174 | ||
175 | static void remove_from_list(struct trailer_item *item, | |
176 | struct trailer_item **first, | |
177 | struct trailer_item **last) | |
178 | { | |
179 | struct trailer_item *next = item->next; | |
180 | struct trailer_item *previous = item->previous; | |
181 | ||
182 | if (next) { | |
183 | item->next->previous = previous; | |
184 | item->next = NULL; | |
185 | } else if (last) | |
186 | *last = previous; | |
187 | ||
188 | if (previous) { | |
189 | item->previous->next = next; | |
190 | item->previous = NULL; | |
191 | } else if (first) | |
192 | *first = next; | |
193 | } | |
194 | ||
195 | static struct trailer_item *remove_first(struct trailer_item **first) | |
196 | { | |
197 | struct trailer_item *item = *first; | |
198 | *first = item->next; | |
199 | if (item->next) { | |
200 | item->next->previous = NULL; | |
201 | item->next = NULL; | |
202 | } | |
203 | return item; | |
204 | } | |
205 | ||
206 | static void apply_arg_if_exists(struct trailer_item *in_tok, | |
207 | struct trailer_item *arg_tok, | |
208 | struct trailer_item *on_tok, | |
209 | struct trailer_item **in_tok_first, | |
210 | struct trailer_item **in_tok_last) | |
211 | { | |
212 | switch (arg_tok->conf.if_exists) { | |
213 | case EXISTS_DO_NOTHING: | |
214 | free_trailer_item(arg_tok); | |
215 | break; | |
216 | case EXISTS_REPLACE: | |
217 | add_arg_to_input_list(on_tok, arg_tok, | |
218 | in_tok_first, in_tok_last); | |
219 | remove_from_list(in_tok, in_tok_first, in_tok_last); | |
220 | free_trailer_item(in_tok); | |
221 | break; | |
222 | case EXISTS_ADD: | |
223 | add_arg_to_input_list(on_tok, arg_tok, | |
224 | in_tok_first, in_tok_last); | |
225 | break; | |
226 | case EXISTS_ADD_IF_DIFFERENT: | |
227 | if (check_if_different(in_tok, arg_tok, 1)) | |
228 | add_arg_to_input_list(on_tok, arg_tok, | |
229 | in_tok_first, in_tok_last); | |
230 | else | |
231 | free_trailer_item(arg_tok); | |
232 | break; | |
233 | case EXISTS_ADD_IF_DIFFERENT_NEIGHBOR: | |
234 | if (check_if_different(on_tok, arg_tok, 0)) | |
235 | add_arg_to_input_list(on_tok, arg_tok, | |
236 | in_tok_first, in_tok_last); | |
237 | else | |
238 | free_trailer_item(arg_tok); | |
239 | break; | |
240 | } | |
241 | } | |
242 | ||
243 | static void apply_arg_if_missing(struct trailer_item **in_tok_first, | |
244 | struct trailer_item **in_tok_last, | |
245 | struct trailer_item *arg_tok) | |
246 | { | |
247 | struct trailer_item **in_tok; | |
248 | enum action_where where; | |
249 | ||
250 | switch (arg_tok->conf.if_missing) { | |
251 | case MISSING_DO_NOTHING: | |
252 | free_trailer_item(arg_tok); | |
253 | break; | |
254 | case MISSING_ADD: | |
255 | where = arg_tok->conf.where; | |
256 | in_tok = after_or_end(where) ? in_tok_last : in_tok_first; | |
257 | if (*in_tok) { | |
258 | add_arg_to_input_list(*in_tok, arg_tok, | |
259 | in_tok_first, in_tok_last); | |
260 | } else { | |
261 | *in_tok_first = arg_tok; | |
262 | *in_tok_last = arg_tok; | |
263 | } | |
264 | break; | |
265 | } | |
266 | } | |
267 | ||
268 | static int find_same_and_apply_arg(struct trailer_item **in_tok_first, | |
269 | struct trailer_item **in_tok_last, | |
270 | struct trailer_item *arg_tok) | |
271 | { | |
272 | struct trailer_item *in_tok; | |
273 | struct trailer_item *on_tok; | |
274 | struct trailer_item *following_tok; | |
275 | ||
276 | enum action_where where = arg_tok->conf.where; | |
277 | int middle = (where == WHERE_AFTER) || (where == WHERE_BEFORE); | |
278 | int backwards = after_or_end(where); | |
279 | struct trailer_item *start_tok = backwards ? *in_tok_last : *in_tok_first; | |
280 | ||
281 | for (in_tok = start_tok; in_tok; in_tok = following_tok) { | |
282 | following_tok = backwards ? in_tok->previous : in_tok->next; | |
283 | if (!same_token(in_tok, arg_tok)) | |
284 | continue; | |
285 | on_tok = middle ? in_tok : start_tok; | |
286 | apply_arg_if_exists(in_tok, arg_tok, on_tok, | |
287 | in_tok_first, in_tok_last); | |
288 | return 1; | |
289 | } | |
290 | return 0; | |
291 | } | |
292 | ||
293 | static void process_trailers_lists(struct trailer_item **in_tok_first, | |
294 | struct trailer_item **in_tok_last, | |
295 | struct trailer_item **arg_tok_first) | |
296 | { | |
297 | struct trailer_item *arg_tok; | |
298 | struct trailer_item *next_arg; | |
299 | ||
300 | if (!*arg_tok_first) | |
301 | return; | |
302 | ||
303 | for (arg_tok = *arg_tok_first; arg_tok; arg_tok = next_arg) { | |
304 | int applied = 0; | |
305 | ||
306 | next_arg = arg_tok->next; | |
307 | remove_from_list(arg_tok, arg_tok_first, NULL); | |
308 | ||
309 | applied = find_same_and_apply_arg(in_tok_first, | |
310 | in_tok_last, | |
311 | arg_tok); | |
312 | ||
313 | if (!applied) | |
314 | apply_arg_if_missing(in_tok_first, | |
315 | in_tok_last, | |
316 | arg_tok); | |
317 | } | |
318 | } | |
46a0613f CC |
319 | |
320 | static int set_where(struct conf_info *item, const char *value) | |
321 | { | |
322 | if (!strcasecmp("after", value)) | |
323 | item->where = WHERE_AFTER; | |
324 | else if (!strcasecmp("before", value)) | |
325 | item->where = WHERE_BEFORE; | |
326 | else if (!strcasecmp("end", value)) | |
327 | item->where = WHERE_END; | |
328 | else if (!strcasecmp("start", value)) | |
329 | item->where = WHERE_START; | |
330 | else | |
331 | return -1; | |
332 | return 0; | |
333 | } | |
334 | ||
335 | static int set_if_exists(struct conf_info *item, const char *value) | |
336 | { | |
337 | if (!strcasecmp("addIfDifferent", value)) | |
338 | item->if_exists = EXISTS_ADD_IF_DIFFERENT; | |
339 | else if (!strcasecmp("addIfDifferentNeighbor", value)) | |
340 | item->if_exists = EXISTS_ADD_IF_DIFFERENT_NEIGHBOR; | |
341 | else if (!strcasecmp("add", value)) | |
342 | item->if_exists = EXISTS_ADD; | |
343 | else if (!strcasecmp("replace", value)) | |
344 | item->if_exists = EXISTS_REPLACE; | |
345 | else if (!strcasecmp("doNothing", value)) | |
346 | item->if_exists = EXISTS_DO_NOTHING; | |
347 | else | |
348 | return -1; | |
349 | return 0; | |
350 | } | |
351 | ||
352 | static int set_if_missing(struct conf_info *item, const char *value) | |
353 | { | |
354 | if (!strcasecmp("doNothing", value)) | |
355 | item->if_missing = MISSING_DO_NOTHING; | |
356 | else if (!strcasecmp("add", value)) | |
357 | item->if_missing = MISSING_ADD; | |
358 | else | |
359 | return -1; | |
360 | return 0; | |
361 | } | |
362 | ||
363 | static void duplicate_conf(struct conf_info *dst, struct conf_info *src) | |
364 | { | |
365 | *dst = *src; | |
366 | if (src->name) | |
367 | dst->name = xstrdup(src->name); | |
368 | if (src->key) | |
369 | dst->key = xstrdup(src->key); | |
370 | if (src->command) | |
371 | dst->command = xstrdup(src->command); | |
372 | } | |
373 | ||
374 | static struct trailer_item *get_conf_item(const char *name) | |
375 | { | |
376 | struct trailer_item *item; | |
377 | struct trailer_item *previous; | |
378 | ||
379 | /* Look up item with same name */ | |
380 | for (previous = NULL, item = first_conf_item; | |
381 | item; | |
382 | previous = item, item = item->next) { | |
383 | if (!strcasecmp(item->conf.name, name)) | |
384 | return item; | |
385 | } | |
386 | ||
387 | /* Item does not already exists, create it */ | |
388 | item = xcalloc(sizeof(struct trailer_item), 1); | |
389 | duplicate_conf(&item->conf, &default_conf_info); | |
390 | item->conf.name = xstrdup(name); | |
391 | ||
392 | if (!previous) | |
393 | first_conf_item = item; | |
394 | else { | |
395 | previous->next = item; | |
396 | item->previous = previous; | |
397 | } | |
398 | ||
399 | return item; | |
400 | } | |
401 | ||
402 | enum trailer_info_type { TRAILER_KEY, TRAILER_COMMAND, TRAILER_WHERE, | |
403 | TRAILER_IF_EXISTS, TRAILER_IF_MISSING }; | |
404 | ||
405 | static struct { | |
406 | const char *name; | |
407 | enum trailer_info_type type; | |
408 | } trailer_config_items[] = { | |
409 | { "key", TRAILER_KEY }, | |
410 | { "command", TRAILER_COMMAND }, | |
411 | { "where", TRAILER_WHERE }, | |
412 | { "ifexists", TRAILER_IF_EXISTS }, | |
413 | { "ifmissing", TRAILER_IF_MISSING } | |
414 | }; | |
415 | ||
416 | static int git_trailer_default_config(const char *conf_key, const char *value, void *cb) | |
417 | { | |
418 | const char *trailer_item, *variable_name; | |
419 | ||
420 | if (!skip_prefix(conf_key, "trailer.", &trailer_item)) | |
421 | return 0; | |
422 | ||
423 | variable_name = strrchr(trailer_item, '.'); | |
424 | if (!variable_name) { | |
425 | if (!strcmp(trailer_item, "where")) { | |
426 | if (set_where(&default_conf_info, value) < 0) | |
427 | warning(_("unknown value '%s' for key '%s'"), | |
428 | value, conf_key); | |
429 | } else if (!strcmp(trailer_item, "ifexists")) { | |
430 | if (set_if_exists(&default_conf_info, value) < 0) | |
431 | warning(_("unknown value '%s' for key '%s'"), | |
432 | value, conf_key); | |
433 | } else if (!strcmp(trailer_item, "ifmissing")) { | |
434 | if (set_if_missing(&default_conf_info, value) < 0) | |
435 | warning(_("unknown value '%s' for key '%s'"), | |
436 | value, conf_key); | |
437 | } else if (!strcmp(trailer_item, "separators")) { | |
438 | separators = xstrdup(value); | |
439 | } | |
440 | } | |
441 | return 0; | |
442 | } | |
443 | ||
444 | static int git_trailer_config(const char *conf_key, const char *value, void *cb) | |
445 | { | |
446 | const char *trailer_item, *variable_name; | |
447 | struct trailer_item *item; | |
448 | struct conf_info *conf; | |
449 | char *name = NULL; | |
450 | enum trailer_info_type type; | |
451 | int i; | |
452 | ||
453 | if (!skip_prefix(conf_key, "trailer.", &trailer_item)) | |
454 | return 0; | |
455 | ||
456 | variable_name = strrchr(trailer_item, '.'); | |
457 | if (!variable_name) | |
458 | return 0; | |
459 | ||
460 | variable_name++; | |
461 | for (i = 0; i < ARRAY_SIZE(trailer_config_items); i++) { | |
462 | if (strcmp(trailer_config_items[i].name, variable_name)) | |
463 | continue; | |
464 | name = xstrndup(trailer_item, variable_name - trailer_item - 1); | |
465 | type = trailer_config_items[i].type; | |
466 | break; | |
467 | } | |
468 | ||
469 | if (!name) | |
470 | return 0; | |
471 | ||
472 | item = get_conf_item(name); | |
473 | conf = &item->conf; | |
474 | free(name); | |
475 | ||
476 | switch (type) { | |
477 | case TRAILER_KEY: | |
478 | if (conf->key) | |
479 | warning(_("more than one %s"), conf_key); | |
480 | conf->key = xstrdup(value); | |
481 | break; | |
482 | case TRAILER_COMMAND: | |
483 | if (conf->command) | |
484 | warning(_("more than one %s"), conf_key); | |
485 | conf->command = xstrdup(value); | |
486 | break; | |
487 | case TRAILER_WHERE: | |
488 | if (set_where(conf, value)) | |
489 | warning(_("unknown value '%s' for key '%s'"), value, conf_key); | |
490 | break; | |
491 | case TRAILER_IF_EXISTS: | |
492 | if (set_if_exists(conf, value)) | |
493 | warning(_("unknown value '%s' for key '%s'"), value, conf_key); | |
494 | break; | |
495 | case TRAILER_IF_MISSING: | |
496 | if (set_if_missing(conf, value)) | |
497 | warning(_("unknown value '%s' for key '%s'"), value, conf_key); | |
498 | break; | |
499 | default: | |
500 | die("internal bug in trailer.c"); | |
501 | } | |
502 | return 0; | |
503 | } | |
f0a90b4e CC |
504 | |
505 | static int parse_trailer(struct strbuf *tok, struct strbuf *val, const char *trailer) | |
506 | { | |
507 | size_t len; | |
508 | struct strbuf seps = STRBUF_INIT; | |
509 | strbuf_addstr(&seps, separators); | |
510 | strbuf_addch(&seps, '='); | |
511 | len = strcspn(trailer, seps.buf); | |
512 | strbuf_release(&seps); | |
513 | if (len == 0) | |
514 | return error(_("empty trailer token in trailer '%s'"), trailer); | |
515 | if (len < strlen(trailer)) { | |
516 | strbuf_add(tok, trailer, len); | |
517 | strbuf_trim(tok); | |
518 | strbuf_addstr(val, trailer + len + 1); | |
519 | strbuf_trim(val); | |
520 | } else { | |
521 | strbuf_addstr(tok, trailer); | |
522 | strbuf_trim(tok); | |
523 | } | |
524 | return 0; | |
525 | } | |
526 | ||
527 | static const char *token_from_item(struct trailer_item *item, char *tok) | |
528 | { | |
529 | if (item->conf.key) | |
530 | return item->conf.key; | |
531 | if (tok) | |
532 | return tok; | |
533 | return item->conf.name; | |
534 | } | |
535 | ||
536 | static struct trailer_item *new_trailer_item(struct trailer_item *conf_item, | |
537 | char *tok, char *val) | |
538 | { | |
539 | struct trailer_item *new = xcalloc(sizeof(*new), 1); | |
540 | new->value = val; | |
541 | ||
542 | if (conf_item) { | |
543 | duplicate_conf(&new->conf, &conf_item->conf); | |
544 | new->token = xstrdup(token_from_item(conf_item, tok)); | |
545 | free(tok); | |
546 | } else { | |
547 | duplicate_conf(&new->conf, &default_conf_info); | |
548 | new->token = tok; | |
549 | } | |
550 | ||
551 | return new; | |
552 | } | |
553 | ||
554 | static int token_matches_item(const char *tok, struct trailer_item *item, int tok_len) | |
555 | { | |
556 | if (!strncasecmp(tok, item->conf.name, tok_len)) | |
557 | return 1; | |
558 | return item->conf.key ? !strncasecmp(tok, item->conf.key, tok_len) : 0; | |
559 | } | |
560 | ||
561 | static struct trailer_item *create_trailer_item(const char *string) | |
562 | { | |
563 | struct strbuf tok = STRBUF_INIT; | |
564 | struct strbuf val = STRBUF_INIT; | |
565 | struct trailer_item *item; | |
566 | int tok_len; | |
567 | ||
568 | if (parse_trailer(&tok, &val, string)) | |
569 | return NULL; | |
570 | ||
571 | tok_len = token_len_without_separator(tok.buf, tok.len); | |
572 | ||
573 | /* Lookup if the token matches something in the config */ | |
574 | for (item = first_conf_item; item; item = item->next) { | |
575 | if (token_matches_item(tok.buf, item, tok_len)) | |
576 | return new_trailer_item(item, | |
577 | strbuf_detach(&tok, NULL), | |
578 | strbuf_detach(&val, NULL)); | |
579 | } | |
580 | ||
581 | return new_trailer_item(NULL, | |
582 | strbuf_detach(&tok, NULL), | |
583 | strbuf_detach(&val, NULL)); | |
584 | } | |
585 | ||
586 | static void add_trailer_item(struct trailer_item **first, | |
587 | struct trailer_item **last, | |
588 | struct trailer_item *new) | |
589 | { | |
590 | if (!new) | |
591 | return; | |
592 | if (!*last) { | |
593 | *first = new; | |
594 | *last = new; | |
595 | } else { | |
596 | (*last)->next = new; | |
597 | new->previous = *last; | |
598 | *last = new; | |
599 | } | |
600 | } | |
601 | ||
602 | static struct trailer_item *process_command_line_args(struct string_list *trailers) | |
603 | { | |
604 | struct trailer_item *arg_tok_first = NULL; | |
605 | struct trailer_item *arg_tok_last = NULL; | |
606 | struct string_list_item *tr; | |
607 | ||
608 | for_each_string_list_item(tr, trailers) { | |
609 | struct trailer_item *new = create_trailer_item(tr->string); | |
610 | add_trailer_item(&arg_tok_first, &arg_tok_last, new); | |
611 | } | |
612 | ||
613 | return arg_tok_first; | |
614 | } | |
2013d850 CC |
615 | |
616 | static struct strbuf **read_input_file(const char *file) | |
617 | { | |
618 | struct strbuf **lines; | |
619 | struct strbuf sb = STRBUF_INIT; | |
620 | ||
621 | if (file) { | |
622 | if (strbuf_read_file(&sb, file, 0) < 0) | |
623 | die_errno(_("could not read input file '%s'"), file); | |
624 | } else { | |
625 | if (strbuf_read(&sb, fileno(stdin), 0) < 0) | |
626 | die_errno(_("could not read from stdin")); | |
627 | } | |
628 | ||
629 | lines = strbuf_split(&sb, '\n'); | |
630 | ||
631 | strbuf_release(&sb); | |
632 | ||
633 | return lines; | |
634 | } | |
635 | ||
636 | /* | |
637 | * Return the (0 based) index of the start of the patch or the line | |
638 | * count if there is no patch in the message. | |
639 | */ | |
640 | static int find_patch_start(struct strbuf **lines, int count) | |
641 | { | |
642 | int i; | |
643 | ||
644 | /* Get the start of the patch part if any */ | |
645 | for (i = 0; i < count; i++) { | |
646 | if (starts_with(lines[i]->buf, "---")) | |
647 | return i; | |
648 | } | |
649 | ||
650 | return count; | |
651 | } | |
652 | ||
653 | /* | |
654 | * Return the (0 based) index of the first trailer line or count if | |
655 | * there are no trailers. Trailers are searched only in the lines from | |
656 | * index (count - 1) down to index 0. | |
657 | */ | |
658 | static int find_trailer_start(struct strbuf **lines, int count) | |
659 | { | |
660 | int start, only_spaces = 1; | |
661 | ||
662 | /* | |
663 | * Get the start of the trailers by looking starting from the end | |
664 | * for a line with only spaces before lines with one separator. | |
665 | */ | |
666 | for (start = count - 1; start >= 0; start--) { | |
667 | if (lines[start]->buf[0] == comment_line_char) | |
668 | continue; | |
669 | if (contains_only_spaces(lines[start]->buf)) { | |
670 | if (only_spaces) | |
671 | continue; | |
672 | return start + 1; | |
673 | } | |
674 | if (strcspn(lines[start]->buf, separators) < lines[start]->len) { | |
675 | if (only_spaces) | |
676 | only_spaces = 0; | |
677 | continue; | |
678 | } | |
679 | return count; | |
680 | } | |
681 | ||
682 | return only_spaces ? count : 0; | |
683 | } | |
684 | ||
685 | static int has_blank_line_before(struct strbuf **lines, int start) | |
686 | { | |
687 | for (;start >= 0; start--) { | |
688 | if (lines[start]->buf[0] == comment_line_char) | |
689 | continue; | |
690 | return contains_only_spaces(lines[start]->buf); | |
691 | } | |
692 | return 0; | |
693 | } | |
694 | ||
695 | static void print_lines(struct strbuf **lines, int start, int end) | |
696 | { | |
697 | int i; | |
698 | for (i = start; lines[i] && i < end; i++) | |
699 | printf("%s", lines[i]->buf); | |
700 | } | |
701 | ||
702 | static int process_input_file(struct strbuf **lines, | |
703 | struct trailer_item **in_tok_first, | |
704 | struct trailer_item **in_tok_last) | |
705 | { | |
706 | int count = 0; | |
707 | int patch_start, trailer_start, i; | |
708 | ||
709 | /* Get the line count */ | |
710 | while (lines[count]) | |
711 | count++; | |
712 | ||
713 | patch_start = find_patch_start(lines, count); | |
714 | trailer_start = find_trailer_start(lines, patch_start); | |
715 | ||
716 | /* Print lines before the trailers as is */ | |
717 | print_lines(lines, 0, trailer_start); | |
718 | ||
719 | if (!has_blank_line_before(lines, trailer_start - 1)) | |
720 | printf("\n"); | |
721 | ||
722 | /* Parse trailer lines */ | |
723 | for (i = trailer_start; i < patch_start; i++) { | |
724 | struct trailer_item *new = create_trailer_item(lines[i]->buf); | |
725 | add_trailer_item(in_tok_first, in_tok_last, new); | |
726 | } | |
727 | ||
728 | return patch_start; | |
729 | } | |
b1d78d77 CC |
730 | |
731 | static void free_all(struct trailer_item **first) | |
732 | { | |
733 | while (*first) { | |
734 | struct trailer_item *item = remove_first(first); | |
735 | free_trailer_item(item); | |
736 | } | |
737 | } | |
738 | ||
739 | void process_trailers(const char *file, int trim_empty, struct string_list *trailers) | |
740 | { | |
741 | struct trailer_item *in_tok_first = NULL; | |
742 | struct trailer_item *in_tok_last = NULL; | |
743 | struct trailer_item *arg_tok_first; | |
744 | struct strbuf **lines; | |
745 | int patch_start; | |
746 | ||
747 | /* Default config must be setup first */ | |
748 | git_config(git_trailer_default_config, NULL); | |
749 | git_config(git_trailer_config, NULL); | |
750 | ||
751 | lines = read_input_file(file); | |
752 | ||
753 | /* Print the lines before the trailers */ | |
754 | patch_start = process_input_file(lines, &in_tok_first, &in_tok_last); | |
755 | ||
756 | arg_tok_first = process_command_line_args(trailers); | |
757 | ||
758 | process_trailers_lists(&in_tok_first, &in_tok_last, &arg_tok_first); | |
759 | ||
760 | print_all(in_tok_first, trim_empty); | |
761 | ||
762 | free_all(&in_tok_first); | |
763 | ||
764 | /* Print the lines after the trailers as is */ | |
765 | print_lines(lines, patch_start, INT_MAX); | |
766 | ||
767 | strbuf_list_free(lines); | |
768 | } |