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