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