commit.con commit get_ref_dir(): require that the dirname argument ends in '/' (abc3909)
   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
  12int save_commit_buffer = 1;
  13
  14const char *commit_type = "commit";
  15
  16static struct commit *check_commit(struct object *obj,
  17                                   const unsigned char *sha1,
  18                                   int quiet)
  19{
  20        if (obj->type != OBJ_COMMIT) {
  21                if (!quiet)
  22                        error("Object %s is a %s, not a commit",
  23                              sha1_to_hex(sha1), typename(obj->type));
  24                return NULL;
  25        }
  26        return (struct commit *) obj;
  27}
  28
  29struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
  30                                              int quiet)
  31{
  32        struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
  33
  34        if (!obj)
  35                return NULL;
  36        return check_commit(obj, sha1, quiet);
  37}
  38
  39struct commit *lookup_commit_reference(const unsigned char *sha1)
  40{
  41        return lookup_commit_reference_gently(sha1, 0);
  42}
  43
  44struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
  45{
  46        struct commit *c = lookup_commit_reference(sha1);
  47        if (!c)
  48                die(_("could not parse %s"), ref_name);
  49        if (hashcmp(sha1, c->object.sha1)) {
  50                warning(_("%s %s is not a commit!"),
  51                        ref_name, sha1_to_hex(sha1));
  52        }
  53        return c;
  54}
  55
  56struct commit *lookup_commit(const unsigned char *sha1)
  57{
  58        struct object *obj = lookup_object(sha1);
  59        if (!obj)
  60                return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
  61        if (!obj->type)
  62                obj->type = OBJ_COMMIT;
  63        return check_commit(obj, sha1, 0);
  64}
  65
  66struct commit *lookup_commit_reference_by_name(const char *name)
  67{
  68        unsigned char sha1[20];
  69        struct commit *commit;
  70
  71        if (get_sha1(name, sha1))
  72                return NULL;
  73        commit = lookup_commit_reference(sha1);
  74        if (!commit || parse_commit(commit))
  75                return NULL;
  76        return commit;
  77}
  78
  79static unsigned long parse_commit_date(const char *buf, const char *tail)
  80{
  81        const char *dateptr;
  82
  83        if (buf + 6 >= tail)
  84                return 0;
  85        if (memcmp(buf, "author", 6))
  86                return 0;
  87        while (buf < tail && *buf++ != '\n')
  88                /* nada */;
  89        if (buf + 9 >= tail)
  90                return 0;
  91        if (memcmp(buf, "committer", 9))
  92                return 0;
  93        while (buf < tail && *buf++ != '>')
  94                /* nada */;
  95        if (buf >= tail)
  96                return 0;
  97        dateptr = buf;
  98        while (buf < tail && *buf++ != '\n')
  99                /* nada */;
 100        if (buf >= tail)
 101                return 0;
 102        /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
 103        return strtoul(dateptr, NULL, 10);
 104}
 105
 106static struct commit_graft **commit_graft;
 107static int commit_graft_alloc, commit_graft_nr;
 108
 109static int commit_graft_pos(const unsigned char *sha1)
 110{
 111        int lo, hi;
 112        lo = 0;
 113        hi = commit_graft_nr;
 114        while (lo < hi) {
 115                int mi = (lo + hi) / 2;
 116                struct commit_graft *graft = commit_graft[mi];
 117                int cmp = hashcmp(sha1, graft->sha1);
 118                if (!cmp)
 119                        return mi;
 120                if (cmp < 0)
 121                        hi = mi;
 122                else
 123                        lo = mi + 1;
 124        }
 125        return -lo - 1;
 126}
 127
 128int register_commit_graft(struct commit_graft *graft, int ignore_dups)
 129{
 130        int pos = commit_graft_pos(graft->sha1);
 131
 132        if (0 <= pos) {
 133                if (ignore_dups)
 134                        free(graft);
 135                else {
 136                        free(commit_graft[pos]);
 137                        commit_graft[pos] = graft;
 138                }
 139                return 1;
 140        }
 141        pos = -pos - 1;
 142        if (commit_graft_alloc <= ++commit_graft_nr) {
 143                commit_graft_alloc = alloc_nr(commit_graft_alloc);
 144                commit_graft = xrealloc(commit_graft,
 145                                        sizeof(*commit_graft) *
 146                                        commit_graft_alloc);
 147        }
 148        if (pos < commit_graft_nr)
 149                memmove(commit_graft + pos + 1,
 150                        commit_graft + pos,
 151                        (commit_graft_nr - pos - 1) *
 152                        sizeof(*commit_graft));
 153        commit_graft[pos] = graft;
 154        return 0;
 155}
 156
 157struct commit_graft *read_graft_line(char *buf, int len)
 158{
 159        /* The format is just "Commit Parent1 Parent2 ...\n" */
 160        int i;
 161        struct commit_graft *graft = NULL;
 162
 163        while (len && isspace(buf[len-1]))
 164                buf[--len] = '\0';
 165        if (buf[0] == '#' || buf[0] == '\0')
 166                return NULL;
 167        if ((len + 1) % 41)
 168                goto bad_graft_data;
 169        i = (len + 1) / 41 - 1;
 170        graft = xmalloc(sizeof(*graft) + 20 * i);
 171        graft->nr_parent = i;
 172        if (get_sha1_hex(buf, graft->sha1))
 173                goto bad_graft_data;
 174        for (i = 40; i < len; i += 41) {
 175                if (buf[i] != ' ')
 176                        goto bad_graft_data;
 177                if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
 178                        goto bad_graft_data;
 179        }
 180        return graft;
 181
 182bad_graft_data:
 183        error("bad graft data: %s", buf);
 184        free(graft);
 185        return NULL;
 186}
 187
 188static int read_graft_file(const char *graft_file)
 189{
 190        FILE *fp = fopen(graft_file, "r");
 191        char buf[1024];
 192        if (!fp)
 193                return -1;
 194        while (fgets(buf, sizeof(buf), fp)) {
 195                /* The format is just "Commit Parent1 Parent2 ...\n" */
 196                int len = strlen(buf);
 197                struct commit_graft *graft = read_graft_line(buf, len);
 198                if (!graft)
 199                        continue;
 200                if (register_commit_graft(graft, 1))
 201                        error("duplicate graft data: %s", buf);
 202        }
 203        fclose(fp);
 204        return 0;
 205}
 206
 207static void prepare_commit_graft(void)
 208{
 209        static int commit_graft_prepared;
 210        char *graft_file;
 211
 212        if (commit_graft_prepared)
 213                return;
 214        graft_file = get_graft_file();
 215        read_graft_file(graft_file);
 216        /* make sure shallows are read */
 217        is_repository_shallow();
 218        commit_graft_prepared = 1;
 219}
 220
 221struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
 222{
 223        int pos;
 224        prepare_commit_graft();
 225        pos = commit_graft_pos(sha1);
 226        if (pos < 0)
 227                return NULL;
 228        return commit_graft[pos];
 229}
 230
 231int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
 232{
 233        int i, ret;
 234        for (i = ret = 0; i < commit_graft_nr && !ret; i++)
 235                ret = fn(commit_graft[i], cb_data);
 236        return ret;
 237}
 238
 239int unregister_shallow(const unsigned char *sha1)
 240{
 241        int pos = commit_graft_pos(sha1);
 242        if (pos < 0)
 243                return -1;
 244        if (pos + 1 < commit_graft_nr)
 245                memmove(commit_graft + pos, commit_graft + pos + 1,
 246                                sizeof(struct commit_graft *)
 247                                * (commit_graft_nr - pos - 1));
 248        commit_graft_nr--;
 249        return 0;
 250}
 251
 252int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
 253{
 254        const char *tail = buffer;
 255        const char *bufptr = buffer;
 256        unsigned char parent[20];
 257        struct commit_list **pptr;
 258        struct commit_graft *graft;
 259
 260        if (item->object.parsed)
 261                return 0;
 262        item->object.parsed = 1;
 263        tail += size;
 264        if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
 265                return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
 266        if (get_sha1_hex(bufptr + 5, parent) < 0)
 267                return error("bad tree pointer in commit %s",
 268                             sha1_to_hex(item->object.sha1));
 269        item->tree = lookup_tree(parent);
 270        bufptr += 46; /* "tree " + "hex sha1" + "\n" */
 271        pptr = &item->parents;
 272
 273        graft = lookup_commit_graft(item->object.sha1);
 274        while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
 275                struct commit *new_parent;
 276
 277                if (tail <= bufptr + 48 ||
 278                    get_sha1_hex(bufptr + 7, parent) ||
 279                    bufptr[47] != '\n')
 280                        return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
 281                bufptr += 48;
 282                /*
 283                 * The clone is shallow if nr_parent < 0, and we must
 284                 * not traverse its real parents even when we unhide them.
 285                 */
 286                if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
 287                        continue;
 288                new_parent = lookup_commit(parent);
 289                if (new_parent)
 290                        pptr = &commit_list_insert(new_parent, pptr)->next;
 291        }
 292        if (graft) {
 293                int i;
 294                struct commit *new_parent;
 295                for (i = 0; i < graft->nr_parent; i++) {
 296                        new_parent = lookup_commit(graft->parent[i]);
 297                        if (!new_parent)
 298                                continue;
 299                        pptr = &commit_list_insert(new_parent, pptr)->next;
 300                }
 301        }
 302        item->date = parse_commit_date(bufptr, tail);
 303
 304        return 0;
 305}
 306
 307int parse_commit(struct commit *item)
 308{
 309        enum object_type type;
 310        void *buffer;
 311        unsigned long size;
 312        int ret;
 313
 314        if (!item)
 315                return -1;
 316        if (item->object.parsed)
 317                return 0;
 318        buffer = read_sha1_file(item->object.sha1, &type, &size);
 319        if (!buffer)
 320                return error("Could not read %s",
 321                             sha1_to_hex(item->object.sha1));
 322        if (type != OBJ_COMMIT) {
 323                free(buffer);
 324                return error("Object %s not a commit",
 325                             sha1_to_hex(item->object.sha1));
 326        }
 327        ret = parse_commit_buffer(item, buffer, size);
 328        if (save_commit_buffer && !ret) {
 329                item->buffer = buffer;
 330                return 0;
 331        }
 332        free(buffer);
 333        return ret;
 334}
 335
 336int find_commit_subject(const char *commit_buffer, const char **subject)
 337{
 338        const char *eol;
 339        const char *p = commit_buffer;
 340
 341        while (*p && (*p != '\n' || p[1] != '\n'))
 342                p++;
 343        if (*p) {
 344                p += 2;
 345                for (eol = p; *eol && *eol != '\n'; eol++)
 346                        ; /* do nothing */
 347        } else
 348                eol = p;
 349
 350        *subject = p;
 351
 352        return eol - p;
 353}
 354
 355struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
 356{
 357        struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
 358        new_list->item = item;
 359        new_list->next = *list_p;
 360        *list_p = new_list;
 361        return new_list;
 362}
 363
 364void commit_list_reverse(struct commit_list **list_p)
 365{
 366        struct commit_list *prev = NULL, *curr = *list_p, *next;
 367
 368        if (!list_p)
 369                return;
 370        while (curr) {
 371                next = curr->next;
 372                curr->next = prev;
 373                prev = curr;
 374                curr = next;
 375        }
 376        *list_p = prev;
 377}
 378
 379unsigned commit_list_count(const struct commit_list *l)
 380{
 381        unsigned c = 0;
 382        for (; l; l = l->next )
 383                c++;
 384        return c;
 385}
 386
 387void free_commit_list(struct commit_list *list)
 388{
 389        while (list) {
 390                struct commit_list *temp = list;
 391                list = temp->next;
 392                free(temp);
 393        }
 394}
 395
 396struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
 397{
 398        struct commit_list **pp = list;
 399        struct commit_list *p;
 400        while ((p = *pp) != NULL) {
 401                if (p->item->date < item->date) {
 402                        break;
 403                }
 404                pp = &p->next;
 405        }
 406        return commit_list_insert(item, pp);
 407}
 408
 409static int commit_list_compare_by_date(const void *a, const void *b)
 410{
 411        unsigned long a_date = ((const struct commit_list *)a)->item->date;
 412        unsigned long b_date = ((const struct commit_list *)b)->item->date;
 413        if (a_date < b_date)
 414                return 1;
 415        if (a_date > b_date)
 416                return -1;
 417        return 0;
 418}
 419
 420static void *commit_list_get_next(const void *a)
 421{
 422        return ((const struct commit_list *)a)->next;
 423}
 424
 425static void commit_list_set_next(void *a, void *next)
 426{
 427        ((struct commit_list *)a)->next = next;
 428}
 429
 430void commit_list_sort_by_date(struct commit_list **list)
 431{
 432        *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
 433                                commit_list_compare_by_date);
 434}
 435
 436struct commit *pop_most_recent_commit(struct commit_list **list,
 437                                      unsigned int mark)
 438{
 439        struct commit *ret = (*list)->item;
 440        struct commit_list *parents = ret->parents;
 441        struct commit_list *old = *list;
 442
 443        *list = (*list)->next;
 444        free(old);
 445
 446        while (parents) {
 447                struct commit *commit = parents->item;
 448                if (!parse_commit(commit) && !(commit->object.flags & mark)) {
 449                        commit->object.flags |= mark;
 450                        commit_list_insert_by_date(commit, list);
 451                }
 452                parents = parents->next;
 453        }
 454        return ret;
 455}
 456
 457static void clear_commit_marks_1(struct commit_list **plist,
 458                                 struct commit *commit, unsigned int mark)
 459{
 460        while (commit) {
 461                struct commit_list *parents;
 462
 463                if (!(mark & commit->object.flags))
 464                        return;
 465
 466                commit->object.flags &= ~mark;
 467
 468                parents = commit->parents;
 469                if (!parents)
 470                        return;
 471
 472                while ((parents = parents->next))
 473                        commit_list_insert(parents->item, plist);
 474
 475                commit = commit->parents->item;
 476        }
 477}
 478
 479void clear_commit_marks(struct commit *commit, unsigned int mark)
 480{
 481        struct commit_list *list = NULL;
 482        commit_list_insert(commit, &list);
 483        while (list)
 484                clear_commit_marks_1(&list, pop_commit(&list), mark);
 485}
 486
 487void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
 488{
 489        struct object *object;
 490        struct commit *commit;
 491        unsigned int i;
 492
 493        for (i = 0; i < a->nr; i++) {
 494                object = a->objects[i].item;
 495                commit = lookup_commit_reference_gently(object->sha1, 1);
 496                if (commit)
 497                        clear_commit_marks(commit, mark);
 498        }
 499}
 500
 501struct commit *pop_commit(struct commit_list **stack)
 502{
 503        struct commit_list *top = *stack;
 504        struct commit *item = top ? top->item : NULL;
 505
 506        if (top) {
 507                *stack = top->next;
 508                free(top);
 509        }
 510        return item;
 511}
 512
 513/*
 514 * Performs an in-place topological sort on the list supplied.
 515 */
 516void sort_in_topological_order(struct commit_list ** list, int lifo)
 517{
 518        struct commit_list *next, *orig = *list;
 519        struct commit_list *work, **insert;
 520        struct commit_list **pptr;
 521
 522        if (!orig)
 523                return;
 524        *list = NULL;
 525
 526        /* Mark them and clear the indegree */
 527        for (next = orig; next; next = next->next) {
 528                struct commit *commit = next->item;
 529                commit->indegree = 1;
 530        }
 531
 532        /* update the indegree */
 533        for (next = orig; next; next = next->next) {
 534                struct commit_list * parents = next->item->parents;
 535                while (parents) {
 536                        struct commit *parent = parents->item;
 537
 538                        if (parent->indegree)
 539                                parent->indegree++;
 540                        parents = parents->next;
 541                }
 542        }
 543
 544        /*
 545         * find the tips
 546         *
 547         * tips are nodes not reachable from any other node in the list
 548         *
 549         * the tips serve as a starting set for the work queue.
 550         */
 551        work = NULL;
 552        insert = &work;
 553        for (next = orig; next; next = next->next) {
 554                struct commit *commit = next->item;
 555
 556                if (commit->indegree == 1)
 557                        insert = &commit_list_insert(commit, insert)->next;
 558        }
 559
 560        /* process the list in topological order */
 561        if (!lifo)
 562                commit_list_sort_by_date(&work);
 563
 564        pptr = list;
 565        *list = NULL;
 566        while (work) {
 567                struct commit *commit;
 568                struct commit_list *parents, *work_item;
 569
 570                work_item = work;
 571                work = work_item->next;
 572                work_item->next = NULL;
 573
 574                commit = work_item->item;
 575                for (parents = commit->parents; parents ; parents = parents->next) {
 576                        struct commit *parent = parents->item;
 577
 578                        if (!parent->indegree)
 579                                continue;
 580
 581                        /*
 582                         * parents are only enqueued for emission
 583                         * when all their children have been emitted thereby
 584                         * guaranteeing topological order.
 585                         */
 586                        if (--parent->indegree == 1) {
 587                                if (!lifo)
 588                                        commit_list_insert_by_date(parent, &work);
 589                                else
 590                                        commit_list_insert(parent, &work);
 591                        }
 592                }
 593                /*
 594                 * work_item is a commit all of whose children
 595                 * have already been emitted. we can emit it now.
 596                 */
 597                commit->indegree = 0;
 598                *pptr = work_item;
 599                pptr = &work_item->next;
 600        }
 601}
 602
 603/* merge-base stuff */
 604
 605/* bits #0..15 in revision.h */
 606#define PARENT1         (1u<<16)
 607#define PARENT2         (1u<<17)
 608#define STALE           (1u<<18)
 609#define RESULT          (1u<<19)
 610
 611static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
 612
 613static struct commit *interesting(struct commit_list *list)
 614{
 615        while (list) {
 616                struct commit *commit = list->item;
 617                list = list->next;
 618                if (commit->object.flags & STALE)
 619                        continue;
 620                return commit;
 621        }
 622        return NULL;
 623}
 624
 625static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
 626{
 627        struct commit_list *list = NULL;
 628        struct commit_list *result = NULL;
 629        int i;
 630
 631        for (i = 0; i < n; i++) {
 632                if (one == twos[i])
 633                        /*
 634                         * We do not mark this even with RESULT so we do not
 635                         * have to clean it up.
 636                         */
 637                        return commit_list_insert(one, &result);
 638        }
 639
 640        if (parse_commit(one))
 641                return NULL;
 642        for (i = 0; i < n; i++) {
 643                if (parse_commit(twos[i]))
 644                        return NULL;
 645        }
 646
 647        one->object.flags |= PARENT1;
 648        commit_list_insert_by_date(one, &list);
 649        for (i = 0; i < n; i++) {
 650                twos[i]->object.flags |= PARENT2;
 651                commit_list_insert_by_date(twos[i], &list);
 652        }
 653
 654        while (interesting(list)) {
 655                struct commit *commit;
 656                struct commit_list *parents;
 657                struct commit_list *next;
 658                int flags;
 659
 660                commit = list->item;
 661                next = list->next;
 662                free(list);
 663                list = next;
 664
 665                flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
 666                if (flags == (PARENT1 | PARENT2)) {
 667                        if (!(commit->object.flags & RESULT)) {
 668                                commit->object.flags |= RESULT;
 669                                commit_list_insert_by_date(commit, &result);
 670                        }
 671                        /* Mark parents of a found merge stale */
 672                        flags |= STALE;
 673                }
 674                parents = commit->parents;
 675                while (parents) {
 676                        struct commit *p = parents->item;
 677                        parents = parents->next;
 678                        if ((p->object.flags & flags) == flags)
 679                                continue;
 680                        if (parse_commit(p))
 681                                return NULL;
 682                        p->object.flags |= flags;
 683                        commit_list_insert_by_date(p, &list);
 684                }
 685        }
 686
 687        /* Clean up the result to remove stale ones */
 688        free_commit_list(list);
 689        list = result; result = NULL;
 690        while (list) {
 691                struct commit_list *next = list->next;
 692                if (!(list->item->object.flags & STALE))
 693                        commit_list_insert_by_date(list->item, &result);
 694                free(list);
 695                list = next;
 696        }
 697        return result;
 698}
 699
 700struct commit_list *get_octopus_merge_bases(struct commit_list *in)
 701{
 702        struct commit_list *i, *j, *k, *ret = NULL;
 703        struct commit_list **pptr = &ret;
 704
 705        for (i = in; i; i = i->next) {
 706                if (!ret)
 707                        pptr = &commit_list_insert(i->item, pptr)->next;
 708                else {
 709                        struct commit_list *new = NULL, *end = NULL;
 710
 711                        for (j = ret; j; j = j->next) {
 712                                struct commit_list *bases;
 713                                bases = get_merge_bases(i->item, j->item, 1);
 714                                if (!new)
 715                                        new = bases;
 716                                else
 717                                        end->next = bases;
 718                                for (k = bases; k; k = k->next)
 719                                        end = k;
 720                        }
 721                        ret = new;
 722                }
 723        }
 724        return ret;
 725}
 726
 727struct commit_list *get_merge_bases_many(struct commit *one,
 728                                         int n,
 729                                         struct commit **twos,
 730                                         int cleanup)
 731{
 732        struct commit_list *list;
 733        struct commit **rslt;
 734        struct commit_list *result;
 735        int cnt, i, j;
 736
 737        result = merge_bases_many(one, n, twos);
 738        for (i = 0; i < n; i++) {
 739                if (one == twos[i])
 740                        return result;
 741        }
 742        if (!result || !result->next) {
 743                if (cleanup) {
 744                        clear_commit_marks(one, all_flags);
 745                        for (i = 0; i < n; i++)
 746                                clear_commit_marks(twos[i], all_flags);
 747                }
 748                return result;
 749        }
 750
 751        /* There are more than one */
 752        cnt = 0;
 753        list = result;
 754        while (list) {
 755                list = list->next;
 756                cnt++;
 757        }
 758        rslt = xcalloc(cnt, sizeof(*rslt));
 759        for (list = result, i = 0; list; list = list->next)
 760                rslt[i++] = list->item;
 761        free_commit_list(result);
 762
 763        clear_commit_marks(one, all_flags);
 764        for (i = 0; i < n; i++)
 765                clear_commit_marks(twos[i], all_flags);
 766        for (i = 0; i < cnt - 1; i++) {
 767                for (j = i+1; j < cnt; j++) {
 768                        if (!rslt[i] || !rslt[j])
 769                                continue;
 770                        result = merge_bases_many(rslt[i], 1, &rslt[j]);
 771                        clear_commit_marks(rslt[i], all_flags);
 772                        clear_commit_marks(rslt[j], all_flags);
 773                        for (list = result; list; list = list->next) {
 774                                if (rslt[i] == list->item)
 775                                        rslt[i] = NULL;
 776                                if (rslt[j] == list->item)
 777                                        rslt[j] = NULL;
 778                        }
 779                }
 780        }
 781
 782        /* Surviving ones in rslt[] are the independent results */
 783        result = NULL;
 784        for (i = 0; i < cnt; i++) {
 785                if (rslt[i])
 786                        commit_list_insert_by_date(rslt[i], &result);
 787        }
 788        free(rslt);
 789        return result;
 790}
 791
 792struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
 793                                    int cleanup)
 794{
 795        return get_merge_bases_many(one, 1, &two, cleanup);
 796}
 797
 798int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
 799{
 800        if (!with_commit)
 801                return 1;
 802        while (with_commit) {
 803                struct commit *other;
 804
 805                other = with_commit->item;
 806                with_commit = with_commit->next;
 807                if (in_merge_bases(other, &commit, 1))
 808                        return 1;
 809        }
 810        return 0;
 811}
 812
 813int in_merge_bases(struct commit *commit, struct commit **reference, int num)
 814{
 815        struct commit_list *bases, *b;
 816        int ret = 0;
 817
 818        if (num == 1)
 819                bases = get_merge_bases(commit, *reference, 1);
 820        else
 821                die("not yet");
 822        for (b = bases; b; b = b->next) {
 823                if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
 824                        ret = 1;
 825                        break;
 826                }
 827        }
 828
 829        free_commit_list(bases);
 830        return ret;
 831}
 832
 833struct commit_list *reduce_heads(struct commit_list *heads)
 834{
 835        struct commit_list *p;
 836        struct commit_list *result = NULL, **tail = &result;
 837        struct commit **other;
 838        size_t num_head, num_other;
 839
 840        if (!heads)
 841                return NULL;
 842
 843        /* Avoid unnecessary reallocations */
 844        for (p = heads, num_head = 0; p; p = p->next)
 845                num_head++;
 846        other = xcalloc(sizeof(*other), num_head);
 847
 848        /* For each commit, see if it can be reached by others */
 849        for (p = heads; p; p = p->next) {
 850                struct commit_list *q, *base;
 851
 852                /* Do we already have this in the result? */
 853                for (q = result; q; q = q->next)
 854                        if (p->item == q->item)
 855                                break;
 856                if (q)
 857                        continue;
 858
 859                num_other = 0;
 860                for (q = heads; q; q = q->next) {
 861                        if (p->item == q->item)
 862                                continue;
 863                        other[num_other++] = q->item;
 864                }
 865                if (num_other)
 866                        base = get_merge_bases_many(p->item, num_other, other, 1);
 867                else
 868                        base = NULL;
 869                /*
 870                 * If p->item does not have anything common with other
 871                 * commits, there won't be any merge base.  If it is
 872                 * reachable from some of the others, p->item will be
 873                 * the merge base.  If its history is connected with
 874                 * others, but p->item is not reachable by others, we
 875                 * will get something other than p->item back.
 876                 */
 877                if (!base || (base->item != p->item))
 878                        tail = &(commit_list_insert(p->item, tail)->next);
 879                free_commit_list(base);
 880        }
 881        free(other);
 882        return result;
 883}
 884
 885static const char gpg_sig_header[] = "gpgsig";
 886static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
 887
 888static int do_sign_commit(struct strbuf *buf, const char *keyid)
 889{
 890        struct strbuf sig = STRBUF_INIT;
 891        int inspos, copypos;
 892
 893        /* find the end of the header */
 894        inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
 895
 896        if (!keyid || !*keyid)
 897                keyid = get_signing_key();
 898        if (sign_buffer(buf, &sig, keyid)) {
 899                strbuf_release(&sig);
 900                return -1;
 901        }
 902
 903        for (copypos = 0; sig.buf[copypos]; ) {
 904                const char *bol = sig.buf + copypos;
 905                const char *eol = strchrnul(bol, '\n');
 906                int len = (eol - bol) + !!*eol;
 907
 908                if (!copypos) {
 909                        strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
 910                        inspos += gpg_sig_header_len;
 911                }
 912                strbuf_insert(buf, inspos++, " ", 1);
 913                strbuf_insert(buf, inspos, bol, len);
 914                inspos += len;
 915                copypos += len;
 916        }
 917        strbuf_release(&sig);
 918        return 0;
 919}
 920
 921int parse_signed_commit(const unsigned char *sha1,
 922                        struct strbuf *payload, struct strbuf *signature)
 923{
 924        unsigned long size;
 925        enum object_type type;
 926        char *buffer = read_sha1_file(sha1, &type, &size);
 927        int in_signature, saw_signature = -1;
 928        char *line, *tail;
 929
 930        if (!buffer || type != OBJ_COMMIT)
 931                goto cleanup;
 932
 933        line = buffer;
 934        tail = buffer + size;
 935        in_signature = 0;
 936        saw_signature = 0;
 937        while (line < tail) {
 938                const char *sig = NULL;
 939                char *next = memchr(line, '\n', tail - line);
 940
 941                next = next ? next + 1 : tail;
 942                if (in_signature && line[0] == ' ')
 943                        sig = line + 1;
 944                else if (!prefixcmp(line, gpg_sig_header) &&
 945                         line[gpg_sig_header_len] == ' ')
 946                        sig = line + gpg_sig_header_len + 1;
 947                if (sig) {
 948                        strbuf_add(signature, sig, next - sig);
 949                        saw_signature = 1;
 950                        in_signature = 1;
 951                } else {
 952                        if (*line == '\n')
 953                                /* dump the whole remainder of the buffer */
 954                                next = tail;
 955                        strbuf_add(payload, line, next - line);
 956                        in_signature = 0;
 957                }
 958                line = next;
 959        }
 960 cleanup:
 961        free(buffer);
 962        return saw_signature;
 963}
 964
 965static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
 966{
 967        struct merge_remote_desc *desc;
 968        struct commit_extra_header *mergetag;
 969        char *buf;
 970        unsigned long size, len;
 971        enum object_type type;
 972
 973        desc = merge_remote_util(parent);
 974        if (!desc || !desc->obj)
 975                return;
 976        buf = read_sha1_file(desc->obj->sha1, &type, &size);
 977        if (!buf || type != OBJ_TAG)
 978                goto free_return;
 979        len = parse_signature(buf, size);
 980        if (size == len)
 981                goto free_return;
 982        /*
 983         * We could verify this signature and either omit the tag when
 984         * it does not validate, but the integrator may not have the
 985         * public key of the signer of the tag he is merging, while a
 986         * later auditor may have it while auditing, so let's not run
 987         * verify-signed-buffer here for now...
 988         *
 989         * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
 990         *      warn("warning: signed tag unverified.");
 991         */
 992        mergetag = xcalloc(1, sizeof(*mergetag));
 993        mergetag->key = xstrdup("mergetag");
 994        mergetag->value = buf;
 995        mergetag->len = size;
 996
 997        **tail = mergetag;
 998        *tail = &mergetag->next;
 999        return;
1000
1001free_return:
1002        free(buf);
1003}
1004
1005void append_merge_tag_headers(struct commit_list *parents,
1006                              struct commit_extra_header ***tail)
1007{
1008        while (parents) {
1009                struct commit *parent = parents->item;
1010                handle_signed_tag(parent, tail);
1011                parents = parents->next;
1012        }
1013}
1014
1015static void add_extra_header(struct strbuf *buffer,
1016                             struct commit_extra_header *extra)
1017{
1018        strbuf_addstr(buffer, extra->key);
1019        if (extra->len)
1020                strbuf_add_lines(buffer, " ", extra->value, extra->len);
1021        else
1022                strbuf_addch(buffer, '\n');
1023}
1024
1025struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1026                                                      const char **exclude)
1027{
1028        struct commit_extra_header *extra = NULL;
1029        unsigned long size;
1030        enum object_type type;
1031        char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
1032        if (buffer && type == OBJ_COMMIT)
1033                extra = read_commit_extra_header_lines(buffer, size, exclude);
1034        free(buffer);
1035        return extra;
1036}
1037
1038static inline int standard_header_field(const char *field, size_t len)
1039{
1040        return ((len == 4 && !memcmp(field, "tree ", 5)) ||
1041                (len == 6 && !memcmp(field, "parent ", 7)) ||
1042                (len == 6 && !memcmp(field, "author ", 7)) ||
1043                (len == 9 && !memcmp(field, "committer ", 10)) ||
1044                (len == 8 && !memcmp(field, "encoding ", 9)));
1045}
1046
1047static int excluded_header_field(const char *field, size_t len, const char **exclude)
1048{
1049        if (!exclude)
1050                return 0;
1051
1052        while (*exclude) {
1053                size_t xlen = strlen(*exclude);
1054                if (len == xlen &&
1055                    !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
1056                        return 1;
1057                exclude++;
1058        }
1059        return 0;
1060}
1061
1062struct commit_extra_header *read_commit_extra_header_lines(const char *buffer, size_t size,
1063                                                           const char **exclude)
1064{
1065        struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1066        const char *line, *next, *eof, *eob;
1067        struct strbuf buf = STRBUF_INIT;
1068
1069        for (line = buffer, eob = line + size;
1070             line < eob && *line != '\n';
1071             line = next) {
1072                next = memchr(line, '\n', eob - line);
1073                next = next ? next + 1 : eob;
1074                if (*line == ' ') {
1075                        /* continuation */
1076                        if (it)
1077                                strbuf_add(&buf, line + 1, next - (line + 1));
1078                        continue;
1079                }
1080                if (it)
1081                        it->value = strbuf_detach(&buf, &it->len);
1082                strbuf_reset(&buf);
1083                it = NULL;
1084
1085                eof = strchr(line, ' ');
1086                if (next <= eof)
1087                        eof = next;
1088
1089                if (standard_header_field(line, eof - line) ||
1090                    excluded_header_field(line, eof - line, exclude))
1091                        continue;
1092
1093                it = xcalloc(1, sizeof(*it));
1094                it->key = xmemdupz(line, eof-line);
1095                *tail = it;
1096                tail = &it->next;
1097                if (eof + 1 < next)
1098                        strbuf_add(&buf, eof + 1, next - (eof + 1));
1099        }
1100        if (it)
1101                it->value = strbuf_detach(&buf, &it->len);
1102        return extra;
1103}
1104
1105void free_commit_extra_headers(struct commit_extra_header *extra)
1106{
1107        while (extra) {
1108                struct commit_extra_header *next = extra->next;
1109                free(extra->key);
1110                free(extra->value);
1111                free(extra);
1112                extra = next;
1113        }
1114}
1115
1116int commit_tree(const struct strbuf *msg, unsigned char *tree,
1117                struct commit_list *parents, unsigned char *ret,
1118                const char *author, const char *sign_commit)
1119{
1120        struct commit_extra_header *extra = NULL, **tail = &extra;
1121        int result;
1122
1123        append_merge_tag_headers(parents, &tail);
1124        result = commit_tree_extended(msg, tree, parents, ret,
1125                                      author, sign_commit, extra);
1126        free_commit_extra_headers(extra);
1127        return result;
1128}
1129
1130static const char commit_utf8_warn[] =
1131"Warning: commit message does not conform to UTF-8.\n"
1132"You may want to amend it after fixing the message, or set the config\n"
1133"variable i18n.commitencoding to the encoding your project uses.\n";
1134
1135int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
1136                         struct commit_list *parents, unsigned char *ret,
1137                         const char *author, const char *sign_commit,
1138                         struct commit_extra_header *extra)
1139{
1140        int result;
1141        int encoding_is_utf8;
1142        struct strbuf buffer;
1143
1144        assert_sha1_type(tree, OBJ_TREE);
1145
1146        if (memchr(msg->buf, '\0', msg->len))
1147                return error("a NUL byte in commit log message not allowed.");
1148
1149        /* Not having i18n.commitencoding is the same as having utf-8 */
1150        encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1151
1152        strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1153        strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1154
1155        /*
1156         * NOTE! This ordering means that the same exact tree merged with a
1157         * different order of parents will be a _different_ changeset even
1158         * if everything else stays the same.
1159         */
1160        while (parents) {
1161                struct commit_list *next = parents->next;
1162                struct commit *parent = parents->item;
1163
1164                strbuf_addf(&buffer, "parent %s\n",
1165                            sha1_to_hex(parent->object.sha1));
1166                free(parents);
1167                parents = next;
1168        }
1169
1170        /* Person/date information */
1171        if (!author)
1172                author = git_author_info(IDENT_ERROR_ON_NO_NAME);
1173        strbuf_addf(&buffer, "author %s\n", author);
1174        strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
1175        if (!encoding_is_utf8)
1176                strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
1177
1178        while (extra) {
1179                add_extra_header(&buffer, extra);
1180                extra = extra->next;
1181        }
1182        strbuf_addch(&buffer, '\n');
1183
1184        /* And add the comment */
1185        strbuf_addbuf(&buffer, msg);
1186
1187        /* And check the encoding */
1188        if (encoding_is_utf8 && !is_utf8(buffer.buf))
1189                fprintf(stderr, commit_utf8_warn);
1190
1191        if (sign_commit && do_sign_commit(&buffer, sign_commit))
1192                return -1;
1193
1194        result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1195        strbuf_release(&buffer);
1196        return result;
1197}
1198
1199struct commit *get_merge_parent(const char *name)
1200{
1201        struct object *obj;
1202        struct commit *commit;
1203        unsigned char sha1[20];
1204        if (get_sha1(name, sha1))
1205                return NULL;
1206        obj = parse_object(sha1);
1207        commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1208        if (commit && !commit->util) {
1209                struct merge_remote_desc *desc;
1210                desc = xmalloc(sizeof(*desc));
1211                desc->obj = obj;
1212                desc->name = strdup(name);
1213                commit->util = desc;
1214        }
1215        return commit;
1216}