commit.con commit Merge branch 'dd/find-graft-with-sha1-pos' (a8e1d71)
   1#include "cache.h"
   2#include "tag.h"
   3#include "commit.h"
   4#include "pkt-line.h"
   5#include "utf8.h"
   6#include "diff.h"
   7#include "revision.h"
   8#include "notes.h"
   9#include "gpg-interface.h"
  10#include "mergesort.h"
  11#include "commit-slab.h"
  12#include "prio-queue.h"
  13#include "sha1-lookup.h"
  14
  15static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
  16
  17int save_commit_buffer = 1;
  18
  19const char *commit_type = "commit";
  20static int commit_count;
  21
  22static struct commit *check_commit(struct object *obj,
  23                                   const unsigned char *sha1,
  24                                   int quiet)
  25{
  26        if (obj->type != OBJ_COMMIT) {
  27                if (!quiet)
  28                        error("Object %s is a %s, not a commit",
  29                              sha1_to_hex(sha1), typename(obj->type));
  30                return NULL;
  31        }
  32        return (struct commit *) obj;
  33}
  34
  35struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
  36                                              int quiet)
  37{
  38        struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
  39
  40        if (!obj)
  41                return NULL;
  42        return check_commit(obj, sha1, quiet);
  43}
  44
  45struct commit *lookup_commit_reference(const unsigned char *sha1)
  46{
  47        return lookup_commit_reference_gently(sha1, 0);
  48}
  49
  50struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
  51{
  52        struct commit *c = lookup_commit_reference(sha1);
  53        if (!c)
  54                die(_("could not parse %s"), ref_name);
  55        if (hashcmp(sha1, c->object.sha1)) {
  56                warning(_("%s %s is not a commit!"),
  57                        ref_name, sha1_to_hex(sha1));
  58        }
  59        return c;
  60}
  61
  62struct commit *lookup_commit(const unsigned char *sha1)
  63{
  64        struct object *obj = lookup_object(sha1);
  65        if (!obj) {
  66                struct commit *c = alloc_commit_node();
  67                c->index = commit_count++;
  68                return create_object(sha1, OBJ_COMMIT, c);
  69        }
  70        if (!obj->type)
  71                obj->type = OBJ_COMMIT;
  72        return check_commit(obj, sha1, 0);
  73}
  74
  75struct commit *lookup_commit_reference_by_name(const char *name)
  76{
  77        unsigned char sha1[20];
  78        struct commit *commit;
  79
  80        if (get_sha1_committish(name, sha1))
  81                return NULL;
  82        commit = lookup_commit_reference(sha1);
  83        if (parse_commit(commit))
  84                return NULL;
  85        return commit;
  86}
  87
  88static unsigned long parse_commit_date(const char *buf, const char *tail)
  89{
  90        const char *dateptr;
  91
  92        if (buf + 6 >= tail)
  93                return 0;
  94        if (memcmp(buf, "author", 6))
  95                return 0;
  96        while (buf < tail && *buf++ != '\n')
  97                /* nada */;
  98        if (buf + 9 >= tail)
  99                return 0;
 100        if (memcmp(buf, "committer", 9))
 101                return 0;
 102        while (buf < tail && *buf++ != '>')
 103                /* nada */;
 104        if (buf >= tail)
 105                return 0;
 106        dateptr = buf;
 107        while (buf < tail && *buf++ != '\n')
 108                /* nada */;
 109        if (buf >= tail)
 110                return 0;
 111        /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
 112        return strtoul(dateptr, NULL, 10);
 113}
 114
 115static struct commit_graft **commit_graft;
 116static int commit_graft_alloc, commit_graft_nr;
 117
 118static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
 119{
 120        struct commit_graft **commit_graft_table = table;
 121        return commit_graft_table[index]->sha1;
 122}
 123
 124static int commit_graft_pos(const unsigned char *sha1)
 125{
 126        return sha1_pos(sha1, commit_graft, commit_graft_nr,
 127                        commit_graft_sha1_access);
 128}
 129
 130int register_commit_graft(struct commit_graft *graft, int ignore_dups)
 131{
 132        int pos = commit_graft_pos(graft->sha1);
 133
 134        if (0 <= pos) {
 135                if (ignore_dups)
 136                        free(graft);
 137                else {
 138                        free(commit_graft[pos]);
 139                        commit_graft[pos] = graft;
 140                }
 141                return 1;
 142        }
 143        pos = -pos - 1;
 144        if (commit_graft_alloc <= ++commit_graft_nr) {
 145                commit_graft_alloc = alloc_nr(commit_graft_alloc);
 146                commit_graft = xrealloc(commit_graft,
 147                                        sizeof(*commit_graft) *
 148                                        commit_graft_alloc);
 149        }
 150        if (pos < commit_graft_nr)
 151                memmove(commit_graft + pos + 1,
 152                        commit_graft + pos,
 153                        (commit_graft_nr - pos - 1) *
 154                        sizeof(*commit_graft));
 155        commit_graft[pos] = graft;
 156        return 0;
 157}
 158
 159struct commit_graft *read_graft_line(char *buf, int len)
 160{
 161        /* The format is just "Commit Parent1 Parent2 ...\n" */
 162        int i;
 163        struct commit_graft *graft = NULL;
 164
 165        while (len && isspace(buf[len-1]))
 166                buf[--len] = '\0';
 167        if (buf[0] == '#' || buf[0] == '\0')
 168                return NULL;
 169        if ((len + 1) % 41)
 170                goto bad_graft_data;
 171        i = (len + 1) / 41 - 1;
 172        graft = xmalloc(sizeof(*graft) + 20 * i);
 173        graft->nr_parent = i;
 174        if (get_sha1_hex(buf, graft->sha1))
 175                goto bad_graft_data;
 176        for (i = 40; i < len; i += 41) {
 177                if (buf[i] != ' ')
 178                        goto bad_graft_data;
 179                if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
 180                        goto bad_graft_data;
 181        }
 182        return graft;
 183
 184bad_graft_data:
 185        error("bad graft data: %s", buf);
 186        free(graft);
 187        return NULL;
 188}
 189
 190static int read_graft_file(const char *graft_file)
 191{
 192        FILE *fp = fopen(graft_file, "r");
 193        struct strbuf buf = STRBUF_INIT;
 194        if (!fp)
 195                return -1;
 196        while (!strbuf_getwholeline(&buf, fp, '\n')) {
 197                /* The format is just "Commit Parent1 Parent2 ...\n" */
 198                struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
 199                if (!graft)
 200                        continue;
 201                if (register_commit_graft(graft, 1))
 202                        error("duplicate graft data: %s", buf.buf);
 203        }
 204        fclose(fp);
 205        strbuf_release(&buf);
 206        return 0;
 207}
 208
 209static void prepare_commit_graft(void)
 210{
 211        static int commit_graft_prepared;
 212        char *graft_file;
 213
 214        if (commit_graft_prepared)
 215                return;
 216        graft_file = get_graft_file();
 217        read_graft_file(graft_file);
 218        /* make sure shallows are read */
 219        is_repository_shallow();
 220        commit_graft_prepared = 1;
 221}
 222
 223struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
 224{
 225        int pos;
 226        prepare_commit_graft();
 227        pos = commit_graft_pos(sha1);
 228        if (pos < 0)
 229                return NULL;
 230        return commit_graft[pos];
 231}
 232
 233int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
 234{
 235        int i, ret;
 236        for (i = ret = 0; i < commit_graft_nr && !ret; i++)
 237                ret = fn(commit_graft[i], cb_data);
 238        return ret;
 239}
 240
 241int unregister_shallow(const unsigned char *sha1)
 242{
 243        int pos = commit_graft_pos(sha1);
 244        if (pos < 0)
 245                return -1;
 246        if (pos + 1 < commit_graft_nr)
 247                memmove(commit_graft + pos, commit_graft + pos + 1,
 248                                sizeof(struct commit_graft *)
 249                                * (commit_graft_nr - pos - 1));
 250        commit_graft_nr--;
 251        return 0;
 252}
 253
 254int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
 255{
 256        const char *tail = buffer;
 257        const char *bufptr = buffer;
 258        unsigned char parent[20];
 259        struct commit_list **pptr;
 260        struct commit_graft *graft;
 261
 262        if (item->object.parsed)
 263                return 0;
 264        item->object.parsed = 1;
 265        tail += size;
 266        if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
 267                return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
 268        if (get_sha1_hex(bufptr + 5, parent) < 0)
 269                return error("bad tree pointer in commit %s",
 270                             sha1_to_hex(item->object.sha1));
 271        item->tree = lookup_tree(parent);
 272        bufptr += 46; /* "tree " + "hex sha1" + "\n" */
 273        pptr = &item->parents;
 274
 275        graft = lookup_commit_graft(item->object.sha1);
 276        while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
 277                struct commit *new_parent;
 278
 279                if (tail <= bufptr + 48 ||
 280                    get_sha1_hex(bufptr + 7, parent) ||
 281                    bufptr[47] != '\n')
 282                        return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
 283                bufptr += 48;
 284                /*
 285                 * The clone is shallow if nr_parent < 0, and we must
 286                 * not traverse its real parents even when we unhide them.
 287                 */
 288                if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
 289                        continue;
 290                new_parent = lookup_commit(parent);
 291                if (new_parent)
 292                        pptr = &commit_list_insert(new_parent, pptr)->next;
 293        }
 294        if (graft) {
 295                int i;
 296                struct commit *new_parent;
 297                for (i = 0; i < graft->nr_parent; i++) {
 298                        new_parent = lookup_commit(graft->parent[i]);
 299                        if (!new_parent)
 300                                continue;
 301                        pptr = &commit_list_insert(new_parent, pptr)->next;
 302                }
 303        }
 304        item->date = parse_commit_date(bufptr, tail);
 305
 306        return 0;
 307}
 308
 309int parse_commit(struct commit *item)
 310{
 311        enum object_type type;
 312        void *buffer;
 313        unsigned long size;
 314        int ret;
 315
 316        if (!item)
 317                return -1;
 318        if (item->object.parsed)
 319                return 0;
 320        buffer = read_sha1_file(item->object.sha1, &type, &size);
 321        if (!buffer)
 322                return error("Could not read %s",
 323                             sha1_to_hex(item->object.sha1));
 324        if (type != OBJ_COMMIT) {
 325                free(buffer);
 326                return error("Object %s not a commit",
 327                             sha1_to_hex(item->object.sha1));
 328        }
 329        ret = parse_commit_buffer(item, buffer, size);
 330        if (save_commit_buffer && !ret) {
 331                item->buffer = buffer;
 332                return 0;
 333        }
 334        free(buffer);
 335        return ret;
 336}
 337
 338void parse_commit_or_die(struct commit *item)
 339{
 340        if (parse_commit(item))
 341                die("unable to parse commit %s",
 342                    item ? sha1_to_hex(item->object.sha1) : "(null)");
 343}
 344
 345int find_commit_subject(const char *commit_buffer, const char **subject)
 346{
 347        const char *eol;
 348        const char *p = commit_buffer;
 349
 350        while (*p && (*p != '\n' || p[1] != '\n'))
 351                p++;
 352        if (*p) {
 353                p += 2;
 354                for (eol = p; *eol && *eol != '\n'; eol++)
 355                        ; /* do nothing */
 356        } else
 357                eol = p;
 358
 359        *subject = p;
 360
 361        return eol - p;
 362}
 363
 364struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
 365{
 366        struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
 367        new_list->item = item;
 368        new_list->next = *list_p;
 369        *list_p = new_list;
 370        return new_list;
 371}
 372
 373unsigned commit_list_count(const struct commit_list *l)
 374{
 375        unsigned c = 0;
 376        for (; l; l = l->next )
 377                c++;
 378        return c;
 379}
 380
 381struct commit_list *copy_commit_list(struct commit_list *list)
 382{
 383        struct commit_list *head = NULL;
 384        struct commit_list **pp = &head;
 385        while (list) {
 386                struct commit_list *new;
 387                new = xmalloc(sizeof(struct commit_list));
 388                new->item = list->item;
 389                new->next = NULL;
 390                *pp = new;
 391                pp = &new->next;
 392                list = list->next;
 393        }
 394        return head;
 395}
 396
 397void free_commit_list(struct commit_list *list)
 398{
 399        while (list) {
 400                struct commit_list *temp = list;
 401                list = temp->next;
 402                free(temp);
 403        }
 404}
 405
 406struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
 407{
 408        struct commit_list **pp = list;
 409        struct commit_list *p;
 410        while ((p = *pp) != NULL) {
 411                if (p->item->date < item->date) {
 412                        break;
 413                }
 414                pp = &p->next;
 415        }
 416        return commit_list_insert(item, pp);
 417}
 418
 419static int commit_list_compare_by_date(const void *a, const void *b)
 420{
 421        unsigned long a_date = ((const struct commit_list *)a)->item->date;
 422        unsigned long b_date = ((const struct commit_list *)b)->item->date;
 423        if (a_date < b_date)
 424                return 1;
 425        if (a_date > b_date)
 426                return -1;
 427        return 0;
 428}
 429
 430static void *commit_list_get_next(const void *a)
 431{
 432        return ((const struct commit_list *)a)->next;
 433}
 434
 435static void commit_list_set_next(void *a, void *next)
 436{
 437        ((struct commit_list *)a)->next = next;
 438}
 439
 440void commit_list_sort_by_date(struct commit_list **list)
 441{
 442        *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
 443                                commit_list_compare_by_date);
 444}
 445
 446struct commit *pop_most_recent_commit(struct commit_list **list,
 447                                      unsigned int mark)
 448{
 449        struct commit *ret = (*list)->item;
 450        struct commit_list *parents = ret->parents;
 451        struct commit_list *old = *list;
 452
 453        *list = (*list)->next;
 454        free(old);
 455
 456        while (parents) {
 457                struct commit *commit = parents->item;
 458                if (!parse_commit(commit) && !(commit->object.flags & mark)) {
 459                        commit->object.flags |= mark;
 460                        commit_list_insert_by_date(commit, list);
 461                }
 462                parents = parents->next;
 463        }
 464        return ret;
 465}
 466
 467static void clear_commit_marks_1(struct commit_list **plist,
 468                                 struct commit *commit, unsigned int mark)
 469{
 470        while (commit) {
 471                struct commit_list *parents;
 472
 473                if (!(mark & commit->object.flags))
 474                        return;
 475
 476                commit->object.flags &= ~mark;
 477
 478                parents = commit->parents;
 479                if (!parents)
 480                        return;
 481
 482                while ((parents = parents->next))
 483                        commit_list_insert(parents->item, plist);
 484
 485                commit = commit->parents->item;
 486        }
 487}
 488
 489void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
 490{
 491        struct commit_list *list = NULL;
 492
 493        while (nr--) {
 494                commit_list_insert(*commit, &list);
 495                commit++;
 496        }
 497        while (list)
 498                clear_commit_marks_1(&list, pop_commit(&list), mark);
 499}
 500
 501void clear_commit_marks(struct commit *commit, unsigned int mark)
 502{
 503        clear_commit_marks_many(1, &commit, mark);
 504}
 505
 506void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
 507{
 508        struct object *object;
 509        struct commit *commit;
 510        unsigned int i;
 511
 512        for (i = 0; i < a->nr; i++) {
 513                object = a->objects[i].item;
 514                commit = lookup_commit_reference_gently(object->sha1, 1);
 515                if (commit)
 516                        clear_commit_marks(commit, mark);
 517        }
 518}
 519
 520struct commit *pop_commit(struct commit_list **stack)
 521{
 522        struct commit_list *top = *stack;
 523        struct commit *item = top ? top->item : NULL;
 524
 525        if (top) {
 526                *stack = top->next;
 527                free(top);
 528        }
 529        return item;
 530}
 531
 532/*
 533 * Topological sort support
 534 */
 535
 536/* count number of children that have not been emitted */
 537define_commit_slab(indegree_slab, int);
 538
 539/* record author-date for each commit object */
 540define_commit_slab(author_date_slab, unsigned long);
 541
 542static void record_author_date(struct author_date_slab *author_date,
 543                               struct commit *commit)
 544{
 545        const char *buf, *line_end, *ident_line;
 546        char *buffer = NULL;
 547        struct ident_split ident;
 548        char *date_end;
 549        unsigned long date;
 550
 551        if (!commit->buffer) {
 552                unsigned long size;
 553                enum object_type type;
 554                buffer = read_sha1_file(commit->object.sha1, &type, &size);
 555                if (!buffer)
 556                        return;
 557        }
 558
 559        for (buf = commit->buffer ? commit->buffer : buffer;
 560             buf;
 561             buf = line_end + 1) {
 562                line_end = strchrnul(buf, '\n');
 563                ident_line = skip_prefix(buf, "author ");
 564                if (!ident_line) {
 565                        if (!line_end[0] || line_end[1] == '\n')
 566                                return; /* end of header */
 567                        continue;
 568                }
 569                if (split_ident_line(&ident,
 570                                     ident_line, line_end - ident_line) ||
 571                    !ident.date_begin || !ident.date_end)
 572                        goto fail_exit; /* malformed "author" line */
 573                break;
 574        }
 575
 576        date = strtoul(ident.date_begin, &date_end, 10);
 577        if (date_end != ident.date_end)
 578                goto fail_exit; /* malformed date */
 579        *(author_date_slab_at(author_date, commit)) = date;
 580
 581fail_exit:
 582        free(buffer);
 583}
 584
 585static int compare_commits_by_author_date(const void *a_, const void *b_,
 586                                          void *cb_data)
 587{
 588        const struct commit *a = a_, *b = b_;
 589        struct author_date_slab *author_date = cb_data;
 590        unsigned long a_date = *(author_date_slab_at(author_date, a));
 591        unsigned long b_date = *(author_date_slab_at(author_date, b));
 592
 593        /* newer commits with larger date first */
 594        if (a_date < b_date)
 595                return 1;
 596        else if (a_date > b_date)
 597                return -1;
 598        return 0;
 599}
 600
 601int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
 602{
 603        const struct commit *a = a_, *b = b_;
 604        /* newer commits with larger date first */
 605        if (a->date < b->date)
 606                return 1;
 607        else if (a->date > b->date)
 608                return -1;
 609        return 0;
 610}
 611
 612/*
 613 * Performs an in-place topological sort on the list supplied.
 614 */
 615void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
 616{
 617        struct commit_list *next, *orig = *list;
 618        struct commit_list **pptr;
 619        struct indegree_slab indegree;
 620        struct prio_queue queue;
 621        struct commit *commit;
 622        struct author_date_slab author_date;
 623
 624        if (!orig)
 625                return;
 626        *list = NULL;
 627
 628        init_indegree_slab(&indegree);
 629        memset(&queue, '\0', sizeof(queue));
 630
 631        switch (sort_order) {
 632        default: /* REV_SORT_IN_GRAPH_ORDER */
 633                queue.compare = NULL;
 634                break;
 635        case REV_SORT_BY_COMMIT_DATE:
 636                queue.compare = compare_commits_by_commit_date;
 637                break;
 638        case REV_SORT_BY_AUTHOR_DATE:
 639                init_author_date_slab(&author_date);
 640                queue.compare = compare_commits_by_author_date;
 641                queue.cb_data = &author_date;
 642                break;
 643        }
 644
 645        /* Mark them and clear the indegree */
 646        for (next = orig; next; next = next->next) {
 647                struct commit *commit = next->item;
 648                *(indegree_slab_at(&indegree, commit)) = 1;
 649                /* also record the author dates, if needed */
 650                if (sort_order == REV_SORT_BY_AUTHOR_DATE)
 651                        record_author_date(&author_date, commit);
 652        }
 653
 654        /* update the indegree */
 655        for (next = orig; next; next = next->next) {
 656                struct commit_list *parents = next->item->parents;
 657                while (parents) {
 658                        struct commit *parent = parents->item;
 659                        int *pi = indegree_slab_at(&indegree, parent);
 660
 661                        if (*pi)
 662                                (*pi)++;
 663                        parents = parents->next;
 664                }
 665        }
 666
 667        /*
 668         * find the tips
 669         *
 670         * tips are nodes not reachable from any other node in the list
 671         *
 672         * the tips serve as a starting set for the work queue.
 673         */
 674        for (next = orig; next; next = next->next) {
 675                struct commit *commit = next->item;
 676
 677                if (*(indegree_slab_at(&indegree, commit)) == 1)
 678                        prio_queue_put(&queue, commit);
 679        }
 680
 681        /*
 682         * This is unfortunate; the initial tips need to be shown
 683         * in the order given from the revision traversal machinery.
 684         */
 685        if (sort_order == REV_SORT_IN_GRAPH_ORDER)
 686                prio_queue_reverse(&queue);
 687
 688        /* We no longer need the commit list */
 689        free_commit_list(orig);
 690
 691        pptr = list;
 692        *list = NULL;
 693        while ((commit = prio_queue_get(&queue)) != NULL) {
 694                struct commit_list *parents;
 695
 696                for (parents = commit->parents; parents ; parents = parents->next) {
 697                        struct commit *parent = parents->item;
 698                        int *pi = indegree_slab_at(&indegree, parent);
 699
 700                        if (!*pi)
 701                                continue;
 702
 703                        /*
 704                         * parents are only enqueued for emission
 705                         * when all their children have been emitted thereby
 706                         * guaranteeing topological order.
 707                         */
 708                        if (--(*pi) == 1)
 709                                prio_queue_put(&queue, parent);
 710                }
 711                /*
 712                 * all children of commit have already been
 713                 * emitted. we can emit it now.
 714                 */
 715                *(indegree_slab_at(&indegree, commit)) = 0;
 716
 717                pptr = &commit_list_insert(commit, pptr)->next;
 718        }
 719
 720        clear_indegree_slab(&indegree);
 721        clear_prio_queue(&queue);
 722        if (sort_order == REV_SORT_BY_AUTHOR_DATE)
 723                clear_author_date_slab(&author_date);
 724}
 725
 726/* merge-base stuff */
 727
 728/* bits #0..15 in revision.h */
 729#define PARENT1         (1u<<16)
 730#define PARENT2         (1u<<17)
 731#define STALE           (1u<<18)
 732#define RESULT          (1u<<19)
 733
 734static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
 735
 736static struct commit *interesting(struct commit_list *list)
 737{
 738        while (list) {
 739                struct commit *commit = list->item;
 740                list = list->next;
 741                if (commit->object.flags & STALE)
 742                        continue;
 743                return commit;
 744        }
 745        return NULL;
 746}
 747
 748/* all input commits in one and twos[] must have been parsed! */
 749static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
 750{
 751        struct commit_list *list = NULL;
 752        struct commit_list *result = NULL;
 753        int i;
 754
 755        one->object.flags |= PARENT1;
 756        commit_list_insert_by_date(one, &list);
 757        if (!n)
 758                return list;
 759        for (i = 0; i < n; i++) {
 760                twos[i]->object.flags |= PARENT2;
 761                commit_list_insert_by_date(twos[i], &list);
 762        }
 763
 764        while (interesting(list)) {
 765                struct commit *commit;
 766                struct commit_list *parents;
 767                struct commit_list *next;
 768                int flags;
 769
 770                commit = list->item;
 771                next = list->next;
 772                free(list);
 773                list = next;
 774
 775                flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
 776                if (flags == (PARENT1 | PARENT2)) {
 777                        if (!(commit->object.flags & RESULT)) {
 778                                commit->object.flags |= RESULT;
 779                                commit_list_insert_by_date(commit, &result);
 780                        }
 781                        /* Mark parents of a found merge stale */
 782                        flags |= STALE;
 783                }
 784                parents = commit->parents;
 785                while (parents) {
 786                        struct commit *p = parents->item;
 787                        parents = parents->next;
 788                        if ((p->object.flags & flags) == flags)
 789                                continue;
 790                        if (parse_commit(p))
 791                                return NULL;
 792                        p->object.flags |= flags;
 793                        commit_list_insert_by_date(p, &list);
 794                }
 795        }
 796
 797        free_commit_list(list);
 798        return result;
 799}
 800
 801static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
 802{
 803        struct commit_list *list = NULL;
 804        struct commit_list *result = NULL;
 805        int i;
 806
 807        for (i = 0; i < n; i++) {
 808                if (one == twos[i])
 809                        /*
 810                         * We do not mark this even with RESULT so we do not
 811                         * have to clean it up.
 812                         */
 813                        return commit_list_insert(one, &result);
 814        }
 815
 816        if (parse_commit(one))
 817                return NULL;
 818        for (i = 0; i < n; i++) {
 819                if (parse_commit(twos[i]))
 820                        return NULL;
 821        }
 822
 823        list = paint_down_to_common(one, n, twos);
 824
 825        while (list) {
 826                struct commit_list *next = list->next;
 827                if (!(list->item->object.flags & STALE))
 828                        commit_list_insert_by_date(list->item, &result);
 829                free(list);
 830                list = next;
 831        }
 832        return result;
 833}
 834
 835struct commit_list *get_octopus_merge_bases(struct commit_list *in)
 836{
 837        struct commit_list *i, *j, *k, *ret = NULL;
 838
 839        if (!in)
 840                return ret;
 841
 842        commit_list_insert(in->item, &ret);
 843
 844        for (i = in->next; i; i = i->next) {
 845                struct commit_list *new = NULL, *end = NULL;
 846
 847                for (j = ret; j; j = j->next) {
 848                        struct commit_list *bases;
 849                        bases = get_merge_bases(i->item, j->item, 1);
 850                        if (!new)
 851                                new = bases;
 852                        else
 853                                end->next = bases;
 854                        for (k = bases; k; k = k->next)
 855                                end = k;
 856                }
 857                ret = new;
 858        }
 859        return ret;
 860}
 861
 862static int remove_redundant(struct commit **array, int cnt)
 863{
 864        /*
 865         * Some commit in the array may be an ancestor of
 866         * another commit.  Move such commit to the end of
 867         * the array, and return the number of commits that
 868         * are independent from each other.
 869         */
 870        struct commit **work;
 871        unsigned char *redundant;
 872        int *filled_index;
 873        int i, j, filled;
 874
 875        work = xcalloc(cnt, sizeof(*work));
 876        redundant = xcalloc(cnt, 1);
 877        filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
 878
 879        for (i = 0; i < cnt; i++)
 880                parse_commit(array[i]);
 881        for (i = 0; i < cnt; i++) {
 882                struct commit_list *common;
 883
 884                if (redundant[i])
 885                        continue;
 886                for (j = filled = 0; j < cnt; j++) {
 887                        if (i == j || redundant[j])
 888                                continue;
 889                        filled_index[filled] = j;
 890                        work[filled++] = array[j];
 891                }
 892                common = paint_down_to_common(array[i], filled, work);
 893                if (array[i]->object.flags & PARENT2)
 894                        redundant[i] = 1;
 895                for (j = 0; j < filled; j++)
 896                        if (work[j]->object.flags & PARENT1)
 897                                redundant[filled_index[j]] = 1;
 898                clear_commit_marks(array[i], all_flags);
 899                for (j = 0; j < filled; j++)
 900                        clear_commit_marks(work[j], all_flags);
 901                free_commit_list(common);
 902        }
 903
 904        /* Now collect the result */
 905        memcpy(work, array, sizeof(*array) * cnt);
 906        for (i = filled = 0; i < cnt; i++)
 907                if (!redundant[i])
 908                        array[filled++] = work[i];
 909        for (j = filled, i = 0; i < cnt; i++)
 910                if (redundant[i])
 911                        array[j++] = work[i];
 912        free(work);
 913        free(redundant);
 914        free(filled_index);
 915        return filled;
 916}
 917
 918struct commit_list *get_merge_bases_many(struct commit *one,
 919                                         int n,
 920                                         struct commit **twos,
 921                                         int cleanup)
 922{
 923        struct commit_list *list;
 924        struct commit **rslt;
 925        struct commit_list *result;
 926        int cnt, i;
 927
 928        result = merge_bases_many(one, n, twos);
 929        for (i = 0; i < n; i++) {
 930                if (one == twos[i])
 931                        return result;
 932        }
 933        if (!result || !result->next) {
 934                if (cleanup) {
 935                        clear_commit_marks(one, all_flags);
 936                        clear_commit_marks_many(n, twos, all_flags);
 937                }
 938                return result;
 939        }
 940
 941        /* There are more than one */
 942        cnt = 0;
 943        list = result;
 944        while (list) {
 945                list = list->next;
 946                cnt++;
 947        }
 948        rslt = xcalloc(cnt, sizeof(*rslt));
 949        for (list = result, i = 0; list; list = list->next)
 950                rslt[i++] = list->item;
 951        free_commit_list(result);
 952
 953        clear_commit_marks(one, all_flags);
 954        clear_commit_marks_many(n, twos, all_flags);
 955
 956        cnt = remove_redundant(rslt, cnt);
 957        result = NULL;
 958        for (i = 0; i < cnt; i++)
 959                commit_list_insert_by_date(rslt[i], &result);
 960        free(rslt);
 961        return result;
 962}
 963
 964struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
 965                                    int cleanup)
 966{
 967        return get_merge_bases_many(one, 1, &two, cleanup);
 968}
 969
 970/*
 971 * Is "commit" a descendant of one of the elements on the "with_commit" list?
 972 */
 973int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
 974{
 975        if (!with_commit)
 976                return 1;
 977        while (with_commit) {
 978                struct commit *other;
 979
 980                other = with_commit->item;
 981                with_commit = with_commit->next;
 982                if (in_merge_bases(other, commit))
 983                        return 1;
 984        }
 985        return 0;
 986}
 987
 988/*
 989 * Is "commit" an ancestor of one of the "references"?
 990 */
 991int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
 992{
 993        struct commit_list *bases;
 994        int ret = 0, i;
 995
 996        if (parse_commit(commit))
 997                return ret;
 998        for (i = 0; i < nr_reference; i++)
 999                if (parse_commit(reference[i]))
1000                        return ret;
1001
1002        bases = paint_down_to_common(commit, nr_reference, reference);
1003        if (commit->object.flags & PARENT2)
1004                ret = 1;
1005        clear_commit_marks(commit, all_flags);
1006        clear_commit_marks_many(nr_reference, reference, all_flags);
1007        free_commit_list(bases);
1008        return ret;
1009}
1010
1011/*
1012 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1013 */
1014int in_merge_bases(struct commit *commit, struct commit *reference)
1015{
1016        return in_merge_bases_many(commit, 1, &reference);
1017}
1018
1019struct commit_list *reduce_heads(struct commit_list *heads)
1020{
1021        struct commit_list *p;
1022        struct commit_list *result = NULL, **tail = &result;
1023        struct commit **array;
1024        int num_head, i;
1025
1026        if (!heads)
1027                return NULL;
1028
1029        /* Uniquify */
1030        for (p = heads; p; p = p->next)
1031                p->item->object.flags &= ~STALE;
1032        for (p = heads, num_head = 0; p; p = p->next) {
1033                if (p->item->object.flags & STALE)
1034                        continue;
1035                p->item->object.flags |= STALE;
1036                num_head++;
1037        }
1038        array = xcalloc(sizeof(*array), num_head);
1039        for (p = heads, i = 0; p; p = p->next) {
1040                if (p->item->object.flags & STALE) {
1041                        array[i++] = p->item;
1042                        p->item->object.flags &= ~STALE;
1043                }
1044        }
1045        num_head = remove_redundant(array, num_head);
1046        for (i = 0; i < num_head; i++)
1047                tail = &commit_list_insert(array[i], tail)->next;
1048        return result;
1049}
1050
1051static const char gpg_sig_header[] = "gpgsig";
1052static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1053
1054static int do_sign_commit(struct strbuf *buf, const char *keyid)
1055{
1056        struct strbuf sig = STRBUF_INIT;
1057        int inspos, copypos;
1058
1059        /* find the end of the header */
1060        inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
1061
1062        if (!keyid || !*keyid)
1063                keyid = get_signing_key();
1064        if (sign_buffer(buf, &sig, keyid)) {
1065                strbuf_release(&sig);
1066                return -1;
1067        }
1068
1069        for (copypos = 0; sig.buf[copypos]; ) {
1070                const char *bol = sig.buf + copypos;
1071                const char *eol = strchrnul(bol, '\n');
1072                int len = (eol - bol) + !!*eol;
1073
1074                if (!copypos) {
1075                        strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1076                        inspos += gpg_sig_header_len;
1077                }
1078                strbuf_insert(buf, inspos++, " ", 1);
1079                strbuf_insert(buf, inspos, bol, len);
1080                inspos += len;
1081                copypos += len;
1082        }
1083        strbuf_release(&sig);
1084        return 0;
1085}
1086
1087int parse_signed_commit(const unsigned char *sha1,
1088                        struct strbuf *payload, struct strbuf *signature)
1089{
1090        unsigned long size;
1091        enum object_type type;
1092        char *buffer = read_sha1_file(sha1, &type, &size);
1093        int in_signature, saw_signature = -1;
1094        char *line, *tail;
1095
1096        if (!buffer || type != OBJ_COMMIT)
1097                goto cleanup;
1098
1099        line = buffer;
1100        tail = buffer + size;
1101        in_signature = 0;
1102        saw_signature = 0;
1103        while (line < tail) {
1104                const char *sig = NULL;
1105                char *next = memchr(line, '\n', tail - line);
1106
1107                next = next ? next + 1 : tail;
1108                if (in_signature && line[0] == ' ')
1109                        sig = line + 1;
1110                else if (starts_with(line, gpg_sig_header) &&
1111                         line[gpg_sig_header_len] == ' ')
1112                        sig = line + gpg_sig_header_len + 1;
1113                if (sig) {
1114                        strbuf_add(signature, sig, next - sig);
1115                        saw_signature = 1;
1116                        in_signature = 1;
1117                } else {
1118                        if (*line == '\n')
1119                                /* dump the whole remainder of the buffer */
1120                                next = tail;
1121                        strbuf_add(payload, line, next - line);
1122                        in_signature = 0;
1123                }
1124                line = next;
1125        }
1126 cleanup:
1127        free(buffer);
1128        return saw_signature;
1129}
1130
1131static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1132{
1133        struct merge_remote_desc *desc;
1134        struct commit_extra_header *mergetag;
1135        char *buf;
1136        unsigned long size, len;
1137        enum object_type type;
1138
1139        desc = merge_remote_util(parent);
1140        if (!desc || !desc->obj)
1141                return;
1142        buf = read_sha1_file(desc->obj->sha1, &type, &size);
1143        if (!buf || type != OBJ_TAG)
1144                goto free_return;
1145        len = parse_signature(buf, size);
1146        if (size == len)
1147                goto free_return;
1148        /*
1149         * We could verify this signature and either omit the tag when
1150         * it does not validate, but the integrator may not have the
1151         * public key of the signer of the tag he is merging, while a
1152         * later auditor may have it while auditing, so let's not run
1153         * verify-signed-buffer here for now...
1154         *
1155         * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1156         *      warn("warning: signed tag unverified.");
1157         */
1158        mergetag = xcalloc(1, sizeof(*mergetag));
1159        mergetag->key = xstrdup("mergetag");
1160        mergetag->value = buf;
1161        mergetag->len = size;
1162
1163        **tail = mergetag;
1164        *tail = &mergetag->next;
1165        return;
1166
1167free_return:
1168        free(buf);
1169}
1170
1171static struct {
1172        char result;
1173        const char *check;
1174} sigcheck_gpg_status[] = {
1175        { 'G', "\n[GNUPG:] GOODSIG " },
1176        { 'B', "\n[GNUPG:] BADSIG " },
1177        { 'U', "\n[GNUPG:] TRUST_NEVER" },
1178        { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
1179};
1180
1181static void parse_gpg_output(struct signature_check *sigc)
1182{
1183        const char *buf = sigc->gpg_status;
1184        int i;
1185
1186        /* Iterate over all search strings */
1187        for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
1188                const char *found, *next;
1189
1190                found = skip_prefix(buf, sigcheck_gpg_status[i].check + 1);
1191                if (!found) {
1192                        found = strstr(buf, sigcheck_gpg_status[i].check);
1193                        if (!found)
1194                                continue;
1195                        found += strlen(sigcheck_gpg_status[i].check);
1196                }
1197                sigc->result = sigcheck_gpg_status[i].result;
1198                /* The trust messages are not followed by key/signer information */
1199                if (sigc->result != 'U') {
1200                        sigc->key = xmemdupz(found, 16);
1201                        found += 17;
1202                        next = strchrnul(found, '\n');
1203                        sigc->signer = xmemdupz(found, next - found);
1204                }
1205        }
1206}
1207
1208void check_commit_signature(const struct commit* commit, struct signature_check *sigc)
1209{
1210        struct strbuf payload = STRBUF_INIT;
1211        struct strbuf signature = STRBUF_INIT;
1212        struct strbuf gpg_output = STRBUF_INIT;
1213        struct strbuf gpg_status = STRBUF_INIT;
1214        int status;
1215
1216        sigc->result = 'N';
1217
1218        if (parse_signed_commit(commit->object.sha1,
1219                                &payload, &signature) <= 0)
1220                goto out;
1221        status = verify_signed_buffer(payload.buf, payload.len,
1222                                      signature.buf, signature.len,
1223                                      &gpg_output, &gpg_status);
1224        if (status && !gpg_output.len)
1225                goto out;
1226        sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
1227        sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
1228        parse_gpg_output(sigc);
1229
1230 out:
1231        strbuf_release(&gpg_status);
1232        strbuf_release(&gpg_output);
1233        strbuf_release(&payload);
1234        strbuf_release(&signature);
1235}
1236
1237
1238
1239void append_merge_tag_headers(struct commit_list *parents,
1240                              struct commit_extra_header ***tail)
1241{
1242        while (parents) {
1243                struct commit *parent = parents->item;
1244                handle_signed_tag(parent, tail);
1245                parents = parents->next;
1246        }
1247}
1248
1249static void add_extra_header(struct strbuf *buffer,
1250                             struct commit_extra_header *extra)
1251{
1252        strbuf_addstr(buffer, extra->key);
1253        if (extra->len)
1254                strbuf_add_lines(buffer, " ", extra->value, extra->len);
1255        else
1256                strbuf_addch(buffer, '\n');
1257}
1258
1259struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1260                                                      const char **exclude)
1261{
1262        struct commit_extra_header *extra = NULL;
1263        unsigned long size;
1264        enum object_type type;
1265        char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
1266        if (buffer && type == OBJ_COMMIT)
1267                extra = read_commit_extra_header_lines(buffer, size, exclude);
1268        free(buffer);
1269        return extra;
1270}
1271
1272static inline int standard_header_field(const char *field, size_t len)
1273{
1274        return ((len == 4 && !memcmp(field, "tree ", 5)) ||
1275                (len == 6 && !memcmp(field, "parent ", 7)) ||
1276                (len == 6 && !memcmp(field, "author ", 7)) ||
1277                (len == 9 && !memcmp(field, "committer ", 10)) ||
1278                (len == 8 && !memcmp(field, "encoding ", 9)));
1279}
1280
1281static int excluded_header_field(const char *field, size_t len, const char **exclude)
1282{
1283        if (!exclude)
1284                return 0;
1285
1286        while (*exclude) {
1287                size_t xlen = strlen(*exclude);
1288                if (len == xlen &&
1289                    !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
1290                        return 1;
1291                exclude++;
1292        }
1293        return 0;
1294}
1295
1296static struct commit_extra_header *read_commit_extra_header_lines(
1297        const char *buffer, size_t size,
1298        const char **exclude)
1299{
1300        struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1301        const char *line, *next, *eof, *eob;
1302        struct strbuf buf = STRBUF_INIT;
1303
1304        for (line = buffer, eob = line + size;
1305             line < eob && *line != '\n';
1306             line = next) {
1307                next = memchr(line, '\n', eob - line);
1308                next = next ? next + 1 : eob;
1309                if (*line == ' ') {
1310                        /* continuation */
1311                        if (it)
1312                                strbuf_add(&buf, line + 1, next - (line + 1));
1313                        continue;
1314                }
1315                if (it)
1316                        it->value = strbuf_detach(&buf, &it->len);
1317                strbuf_reset(&buf);
1318                it = NULL;
1319
1320                eof = strchr(line, ' ');
1321                if (next <= eof)
1322                        eof = next;
1323
1324                if (standard_header_field(line, eof - line) ||
1325                    excluded_header_field(line, eof - line, exclude))
1326                        continue;
1327
1328                it = xcalloc(1, sizeof(*it));
1329                it->key = xmemdupz(line, eof-line);
1330                *tail = it;
1331                tail = &it->next;
1332                if (eof + 1 < next)
1333                        strbuf_add(&buf, eof + 1, next - (eof + 1));
1334        }
1335        if (it)
1336                it->value = strbuf_detach(&buf, &it->len);
1337        return extra;
1338}
1339
1340void free_commit_extra_headers(struct commit_extra_header *extra)
1341{
1342        while (extra) {
1343                struct commit_extra_header *next = extra->next;
1344                free(extra->key);
1345                free(extra->value);
1346                free(extra);
1347                extra = next;
1348        }
1349}
1350
1351int commit_tree(const struct strbuf *msg, const unsigned char *tree,
1352                struct commit_list *parents, unsigned char *ret,
1353                const char *author, const char *sign_commit)
1354{
1355        struct commit_extra_header *extra = NULL, **tail = &extra;
1356        int result;
1357
1358        append_merge_tag_headers(parents, &tail);
1359        result = commit_tree_extended(msg, tree, parents, ret,
1360                                      author, sign_commit, extra);
1361        free_commit_extra_headers(extra);
1362        return result;
1363}
1364
1365static int find_invalid_utf8(const char *buf, int len)
1366{
1367        int offset = 0;
1368        static const unsigned int max_codepoint[] = {
1369                0x7f, 0x7ff, 0xffff, 0x10ffff
1370        };
1371
1372        while (len) {
1373                unsigned char c = *buf++;
1374                int bytes, bad_offset;
1375                unsigned int codepoint;
1376                unsigned int min_val, max_val;
1377
1378                len--;
1379                offset++;
1380
1381                /* Simple US-ASCII? No worries. */
1382                if (c < 0x80)
1383                        continue;
1384
1385                bad_offset = offset-1;
1386
1387                /*
1388                 * Count how many more high bits set: that's how
1389                 * many more bytes this sequence should have.
1390                 */
1391                bytes = 0;
1392                while (c & 0x40) {
1393                        c <<= 1;
1394                        bytes++;
1395                }
1396
1397                /*
1398                 * Must be between 1 and 3 more bytes.  Longer sequences result in
1399                 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1400                 */
1401                if (bytes < 1 || 3 < bytes)
1402                        return bad_offset;
1403
1404                /* Do we *have* that many bytes? */
1405                if (len < bytes)
1406                        return bad_offset;
1407
1408                /*
1409                 * Place the encoded bits at the bottom of the value and compute the
1410                 * valid range.
1411                 */
1412                codepoint = (c & 0x7f) >> bytes;
1413                min_val = max_codepoint[bytes-1] + 1;
1414                max_val = max_codepoint[bytes];
1415
1416                offset += bytes;
1417                len -= bytes;
1418
1419                /* And verify that they are good continuation bytes */
1420                do {
1421                        codepoint <<= 6;
1422                        codepoint |= *buf & 0x3f;
1423                        if ((*buf++ & 0xc0) != 0x80)
1424                                return bad_offset;
1425                } while (--bytes);
1426
1427                /* Reject codepoints that are out of range for the sequence length. */
1428                if (codepoint < min_val || codepoint > max_val)
1429                        return bad_offset;
1430                /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1431                if ((codepoint & 0x1ff800) == 0xd800)
1432                        return bad_offset;
1433                /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1434                if ((codepoint & 0xfffe) == 0xfffe)
1435                        return bad_offset;
1436                /* So are anything in the range U+FDD0..U+FDEF. */
1437                if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1438                        return bad_offset;
1439        }
1440        return -1;
1441}
1442
1443/*
1444 * This verifies that the buffer is in proper utf8 format.
1445 *
1446 * If it isn't, it assumes any non-utf8 characters are Latin1,
1447 * and does the conversion.
1448 */
1449static int verify_utf8(struct strbuf *buf)
1450{
1451        int ok = 1;
1452        long pos = 0;
1453
1454        for (;;) {
1455                int bad;
1456                unsigned char c;
1457                unsigned char replace[2];
1458
1459                bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1460                if (bad < 0)
1461                        return ok;
1462                pos += bad;
1463                ok = 0;
1464                c = buf->buf[pos];
1465                strbuf_remove(buf, pos, 1);
1466
1467                /* We know 'c' must be in the range 128-255 */
1468                replace[0] = 0xc0 + (c >> 6);
1469                replace[1] = 0x80 + (c & 0x3f);
1470                strbuf_insert(buf, pos, replace, 2);
1471                pos += 2;
1472        }
1473}
1474
1475static const char commit_utf8_warn[] =
1476"Warning: commit message did not conform to UTF-8.\n"
1477"You may want to amend it after fixing the message, or set the config\n"
1478"variable i18n.commitencoding to the encoding your project uses.\n";
1479
1480int commit_tree_extended(const struct strbuf *msg, const unsigned char *tree,
1481                         struct commit_list *parents, unsigned char *ret,
1482                         const char *author, const char *sign_commit,
1483                         struct commit_extra_header *extra)
1484{
1485        int result;
1486        int encoding_is_utf8;
1487        struct strbuf buffer;
1488
1489        assert_sha1_type(tree, OBJ_TREE);
1490
1491        if (memchr(msg->buf, '\0', msg->len))
1492                return error("a NUL byte in commit log message not allowed.");
1493
1494        /* Not having i18n.commitencoding is the same as having utf-8 */
1495        encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1496
1497        strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1498        strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1499
1500        /*
1501         * NOTE! This ordering means that the same exact tree merged with a
1502         * different order of parents will be a _different_ changeset even
1503         * if everything else stays the same.
1504         */
1505        while (parents) {
1506                struct commit_list *next = parents->next;
1507                struct commit *parent = parents->item;
1508
1509                strbuf_addf(&buffer, "parent %s\n",
1510                            sha1_to_hex(parent->object.sha1));
1511                free(parents);
1512                parents = next;
1513        }
1514
1515        /* Person/date information */
1516        if (!author)
1517                author = git_author_info(IDENT_STRICT);
1518        strbuf_addf(&buffer, "author %s\n", author);
1519        strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
1520        if (!encoding_is_utf8)
1521                strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1522
1523        while (extra) {
1524                add_extra_header(&buffer, extra);
1525                extra = extra->next;
1526        }
1527        strbuf_addch(&buffer, '\n');
1528
1529        /* And add the comment */
1530        strbuf_addbuf(&buffer, msg);
1531
1532        /* And check the encoding */
1533        if (encoding_is_utf8 && !verify_utf8(&buffer))
1534                fprintf(stderr, commit_utf8_warn);
1535
1536        if (sign_commit && do_sign_commit(&buffer, sign_commit))
1537                return -1;
1538
1539        result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1540        strbuf_release(&buffer);
1541        return result;
1542}
1543
1544struct commit *get_merge_parent(const char *name)
1545{
1546        struct object *obj;
1547        struct commit *commit;
1548        unsigned char sha1[20];
1549        if (get_sha1(name, sha1))
1550                return NULL;
1551        obj = parse_object(sha1);
1552        commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1553        if (commit && !commit->util) {
1554                struct merge_remote_desc *desc;
1555                desc = xmalloc(sizeof(*desc));
1556                desc->obj = obj;
1557                desc->name = strdup(name);
1558                commit->util = desc;
1559        }
1560        return commit;
1561}
1562
1563/*
1564 * Append a commit to the end of the commit_list.
1565 *
1566 * next starts by pointing to the variable that holds the head of an
1567 * empty commit_list, and is updated to point to the "next" field of
1568 * the last item on the list as new commits are appended.
1569 *
1570 * Usage example:
1571 *
1572 *     struct commit_list *list;
1573 *     struct commit_list **next = &list;
1574 *
1575 *     next = commit_list_append(c1, next);
1576 *     next = commit_list_append(c2, next);
1577 *     assert(commit_list_count(list) == 2);
1578 *     return list;
1579 */
1580struct commit_list **commit_list_append(struct commit *commit,
1581                                        struct commit_list **next)
1582{
1583        struct commit_list *new = xmalloc(sizeof(struct commit_list));
1584        new->item = commit;
1585        *next = new;
1586        new->next = NULL;
1587        return &new->next;
1588}
1589
1590void print_commit_list(struct commit_list *list,
1591                       const char *format_cur,
1592                       const char *format_last)
1593{
1594        for ( ; list; list = list->next) {
1595                const char *format = list->next ? format_cur : format_last;
1596                printf(format, sha1_to_hex(list->item->object.sha1));
1597        }
1598}