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