commit.con commit archive: re-allow HEAD:Documentation on a remote invocation (0f544ee)
   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
  10int save_commit_buffer = 1;
  11
  12const char *commit_type = "commit";
  13
  14static struct commit *check_commit(struct object *obj,
  15                                   const unsigned char *sha1,
  16                                   int quiet)
  17{
  18        if (obj->type != OBJ_COMMIT) {
  19                if (!quiet)
  20                        error("Object %s is a %s, not a commit",
  21                              sha1_to_hex(sha1), typename(obj->type));
  22                return NULL;
  23        }
  24        return (struct commit *) obj;
  25}
  26
  27struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
  28                                              int quiet)
  29{
  30        struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
  31
  32        if (!obj)
  33                return NULL;
  34        return check_commit(obj, sha1, quiet);
  35}
  36
  37struct commit *lookup_commit_reference(const unsigned char *sha1)
  38{
  39        return lookup_commit_reference_gently(sha1, 0);
  40}
  41
  42struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
  43{
  44        struct commit *c = lookup_commit_reference(sha1);
  45        if (!c)
  46                die(_("could not parse %s"), ref_name);
  47        if (hashcmp(sha1, c->object.sha1)) {
  48                warning(_("%s %s is not a commit!"),
  49                        ref_name, sha1_to_hex(sha1));
  50        }
  51        return c;
  52}
  53
  54struct commit *lookup_commit(const unsigned char *sha1)
  55{
  56        struct object *obj = lookup_object(sha1);
  57        if (!obj)
  58                return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
  59        if (!obj->type)
  60                obj->type = OBJ_COMMIT;
  61        return check_commit(obj, sha1, 0);
  62}
  63
  64struct commit *lookup_commit_reference_by_name(const char *name)
  65{
  66        unsigned char sha1[20];
  67        struct commit *commit;
  68
  69        if (get_sha1(name, sha1))
  70                return NULL;
  71        commit = lookup_commit_reference(sha1);
  72        if (!commit || parse_commit(commit))
  73                return NULL;
  74        return commit;
  75}
  76
  77static unsigned long parse_commit_date(const char *buf, const char *tail)
  78{
  79        const char *dateptr;
  80
  81        if (buf + 6 >= tail)
  82                return 0;
  83        if (memcmp(buf, "author", 6))
  84                return 0;
  85        while (buf < tail && *buf++ != '\n')
  86                /* nada */;
  87        if (buf + 9 >= tail)
  88                return 0;
  89        if (memcmp(buf, "committer", 9))
  90                return 0;
  91        while (buf < tail && *buf++ != '>')
  92                /* nada */;
  93        if (buf >= tail)
  94                return 0;
  95        dateptr = buf;
  96        while (buf < tail && *buf++ != '\n')
  97                /* nada */;
  98        if (buf >= tail)
  99                return 0;
 100        /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
 101        return strtoul(dateptr, NULL, 10);
 102}
 103
 104static struct commit_graft **commit_graft;
 105static int commit_graft_alloc, commit_graft_nr;
 106
 107static int commit_graft_pos(const unsigned char *sha1)
 108{
 109        int lo, hi;
 110        lo = 0;
 111        hi = commit_graft_nr;
 112        while (lo < hi) {
 113                int mi = (lo + hi) / 2;
 114                struct commit_graft *graft = commit_graft[mi];
 115                int cmp = hashcmp(sha1, graft->sha1);
 116                if (!cmp)
 117                        return mi;
 118                if (cmp < 0)
 119                        hi = mi;
 120                else
 121                        lo = mi + 1;
 122        }
 123        return -lo - 1;
 124}
 125
 126int register_commit_graft(struct commit_graft *graft, int ignore_dups)
 127{
 128        int pos = commit_graft_pos(graft->sha1);
 129
 130        if (0 <= pos) {
 131                if (ignore_dups)
 132                        free(graft);
 133                else {
 134                        free(commit_graft[pos]);
 135                        commit_graft[pos] = graft;
 136                }
 137                return 1;
 138        }
 139        pos = -pos - 1;
 140        if (commit_graft_alloc <= ++commit_graft_nr) {
 141                commit_graft_alloc = alloc_nr(commit_graft_alloc);
 142                commit_graft = xrealloc(commit_graft,
 143                                        sizeof(*commit_graft) *
 144                                        commit_graft_alloc);
 145        }
 146        if (pos < commit_graft_nr)
 147                memmove(commit_graft + pos + 1,
 148                        commit_graft + pos,
 149                        (commit_graft_nr - pos - 1) *
 150                        sizeof(*commit_graft));
 151        commit_graft[pos] = graft;
 152        return 0;
 153}
 154
 155struct commit_graft *read_graft_line(char *buf, int len)
 156{
 157        /* The format is just "Commit Parent1 Parent2 ...\n" */
 158        int i;
 159        struct commit_graft *graft = NULL;
 160
 161        while (len && isspace(buf[len-1]))
 162                buf[--len] = '\0';
 163        if (buf[0] == '#' || buf[0] == '\0')
 164                return NULL;
 165        if ((len + 1) % 41)
 166                goto bad_graft_data;
 167        i = (len + 1) / 41 - 1;
 168        graft = xmalloc(sizeof(*graft) + 20 * i);
 169        graft->nr_parent = i;
 170        if (get_sha1_hex(buf, graft->sha1))
 171                goto bad_graft_data;
 172        for (i = 40; i < len; i += 41) {
 173                if (buf[i] != ' ')
 174                        goto bad_graft_data;
 175                if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
 176                        goto bad_graft_data;
 177        }
 178        return graft;
 179
 180bad_graft_data:
 181        error("bad graft data: %s", buf);
 182        free(graft);
 183        return NULL;
 184}
 185
 186static int read_graft_file(const char *graft_file)
 187{
 188        FILE *fp = fopen(graft_file, "r");
 189        char buf[1024];
 190        if (!fp)
 191                return -1;
 192        while (fgets(buf, sizeof(buf), fp)) {
 193                /* The format is just "Commit Parent1 Parent2 ...\n" */
 194                int len = strlen(buf);
 195                struct commit_graft *graft = read_graft_line(buf, len);
 196                if (!graft)
 197                        continue;
 198                if (register_commit_graft(graft, 1))
 199                        error("duplicate graft data: %s", buf);
 200        }
 201        fclose(fp);
 202        return 0;
 203}
 204
 205static void prepare_commit_graft(void)
 206{
 207        static int commit_graft_prepared;
 208        char *graft_file;
 209
 210        if (commit_graft_prepared)
 211                return;
 212        graft_file = get_graft_file();
 213        read_graft_file(graft_file);
 214        /* make sure shallows are read */
 215        is_repository_shallow();
 216        commit_graft_prepared = 1;
 217}
 218
 219struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
 220{
 221        int pos;
 222        prepare_commit_graft();
 223        pos = commit_graft_pos(sha1);
 224        if (pos < 0)
 225                return NULL;
 226        return commit_graft[pos];
 227}
 228
 229int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
 230{
 231        int i, ret;
 232        for (i = ret = 0; i < commit_graft_nr && !ret; i++)
 233                ret = fn(commit_graft[i], cb_data);
 234        return ret;
 235}
 236
 237int unregister_shallow(const unsigned char *sha1)
 238{
 239        int pos = commit_graft_pos(sha1);
 240        if (pos < 0)
 241                return -1;
 242        if (pos + 1 < commit_graft_nr)
 243                memmove(commit_graft + pos, commit_graft + pos + 1,
 244                                sizeof(struct commit_graft *)
 245                                * (commit_graft_nr - pos - 1));
 246        commit_graft_nr--;
 247        return 0;
 248}
 249
 250int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
 251{
 252        const char *tail = buffer;
 253        const char *bufptr = buffer;
 254        unsigned char parent[20];
 255        struct commit_list **pptr;
 256        struct commit_graft *graft;
 257
 258        if (item->object.parsed)
 259                return 0;
 260        item->object.parsed = 1;
 261        tail += size;
 262        if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
 263                return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
 264        if (get_sha1_hex(bufptr + 5, parent) < 0)
 265                return error("bad tree pointer in commit %s",
 266                             sha1_to_hex(item->object.sha1));
 267        item->tree = lookup_tree(parent);
 268        bufptr += 46; /* "tree " + "hex sha1" + "\n" */
 269        pptr = &item->parents;
 270
 271        graft = lookup_commit_graft(item->object.sha1);
 272        while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
 273                struct commit *new_parent;
 274
 275                if (tail <= bufptr + 48 ||
 276                    get_sha1_hex(bufptr + 7, parent) ||
 277                    bufptr[47] != '\n')
 278                        return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
 279                bufptr += 48;
 280                /*
 281                 * The clone is shallow if nr_parent < 0, and we must
 282                 * not traverse its real parents even when we unhide them.
 283                 */
 284                if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
 285                        continue;
 286                new_parent = lookup_commit(parent);
 287                if (new_parent)
 288                        pptr = &commit_list_insert(new_parent, pptr)->next;
 289        }
 290        if (graft) {
 291                int i;
 292                struct commit *new_parent;
 293                for (i = 0; i < graft->nr_parent; i++) {
 294                        new_parent = lookup_commit(graft->parent[i]);
 295                        if (!new_parent)
 296                                continue;
 297                        pptr = &commit_list_insert(new_parent, pptr)->next;
 298                }
 299        }
 300        item->date = parse_commit_date(bufptr, tail);
 301
 302        return 0;
 303}
 304
 305int parse_commit(struct commit *item)
 306{
 307        enum object_type type;
 308        void *buffer;
 309        unsigned long size;
 310        int ret;
 311
 312        if (!item)
 313                return -1;
 314        if (item->object.parsed)
 315                return 0;
 316        buffer = read_sha1_file(item->object.sha1, &type, &size);
 317        if (!buffer)
 318                return error("Could not read %s",
 319                             sha1_to_hex(item->object.sha1));
 320        if (type != OBJ_COMMIT) {
 321                free(buffer);
 322                return error("Object %s not a commit",
 323                             sha1_to_hex(item->object.sha1));
 324        }
 325        ret = parse_commit_buffer(item, buffer, size);
 326        if (save_commit_buffer && !ret) {
 327                item->buffer = buffer;
 328                return 0;
 329        }
 330        free(buffer);
 331        return ret;
 332}
 333
 334int find_commit_subject(const char *commit_buffer, const char **subject)
 335{
 336        const char *eol;
 337        const char *p = commit_buffer;
 338
 339        while (*p && (*p != '\n' || p[1] != '\n'))
 340                p++;
 341        if (*p) {
 342                p += 2;
 343                for (eol = p; *eol && *eol != '\n'; eol++)
 344                        ; /* do nothing */
 345        } else
 346                eol = p;
 347
 348        *subject = p;
 349
 350        return eol - p;
 351}
 352
 353struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
 354{
 355        struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
 356        new_list->item = item;
 357        new_list->next = *list_p;
 358        *list_p = new_list;
 359        return new_list;
 360}
 361
 362unsigned commit_list_count(const struct commit_list *l)
 363{
 364        unsigned c = 0;
 365        for (; l; l = l->next )
 366                c++;
 367        return c;
 368}
 369
 370void free_commit_list(struct commit_list *list)
 371{
 372        while (list) {
 373                struct commit_list *temp = list;
 374                list = temp->next;
 375                free(temp);
 376        }
 377}
 378
 379struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
 380{
 381        struct commit_list **pp = list;
 382        struct commit_list *p;
 383        while ((p = *pp) != NULL) {
 384                if (p->item->date < item->date) {
 385                        break;
 386                }
 387                pp = &p->next;
 388        }
 389        return commit_list_insert(item, pp);
 390}
 391
 392
 393void commit_list_sort_by_date(struct commit_list **list)
 394{
 395        struct commit_list *ret = NULL;
 396        while (*list) {
 397                commit_list_insert_by_date((*list)->item, &ret);
 398                *list = (*list)->next;
 399        }
 400        *list = ret;
 401}
 402
 403struct commit *pop_most_recent_commit(struct commit_list **list,
 404                                      unsigned int mark)
 405{
 406        struct commit *ret = (*list)->item;
 407        struct commit_list *parents = ret->parents;
 408        struct commit_list *old = *list;
 409
 410        *list = (*list)->next;
 411        free(old);
 412
 413        while (parents) {
 414                struct commit *commit = parents->item;
 415                if (!parse_commit(commit) && !(commit->object.flags & mark)) {
 416                        commit->object.flags |= mark;
 417                        commit_list_insert_by_date(commit, list);
 418                }
 419                parents = parents->next;
 420        }
 421        return ret;
 422}
 423
 424void clear_commit_marks(struct commit *commit, unsigned int mark)
 425{
 426        while (commit) {
 427                struct commit_list *parents;
 428
 429                if (!(mark & commit->object.flags))
 430                        return;
 431
 432                commit->object.flags &= ~mark;
 433
 434                parents = commit->parents;
 435                if (!parents)
 436                        return;
 437
 438                while ((parents = parents->next))
 439                        clear_commit_marks(parents->item, mark);
 440
 441                commit = commit->parents->item;
 442        }
 443}
 444
 445struct commit *pop_commit(struct commit_list **stack)
 446{
 447        struct commit_list *top = *stack;
 448        struct commit *item = top ? top->item : NULL;
 449
 450        if (top) {
 451                *stack = top->next;
 452                free(top);
 453        }
 454        return item;
 455}
 456
 457/*
 458 * Performs an in-place topological sort on the list supplied.
 459 */
 460void sort_in_topological_order(struct commit_list ** list, int lifo)
 461{
 462        struct commit_list *next, *orig = *list;
 463        struct commit_list *work, **insert;
 464        struct commit_list **pptr;
 465
 466        if (!orig)
 467                return;
 468        *list = NULL;
 469
 470        /* Mark them and clear the indegree */
 471        for (next = orig; next; next = next->next) {
 472                struct commit *commit = next->item;
 473                commit->indegree = 1;
 474        }
 475
 476        /* update the indegree */
 477        for (next = orig; next; next = next->next) {
 478                struct commit_list * parents = next->item->parents;
 479                while (parents) {
 480                        struct commit *parent = parents->item;
 481
 482                        if (parent->indegree)
 483                                parent->indegree++;
 484                        parents = parents->next;
 485                }
 486        }
 487
 488        /*
 489         * find the tips
 490         *
 491         * tips are nodes not reachable from any other node in the list
 492         *
 493         * the tips serve as a starting set for the work queue.
 494         */
 495        work = NULL;
 496        insert = &work;
 497        for (next = orig; next; next = next->next) {
 498                struct commit *commit = next->item;
 499
 500                if (commit->indegree == 1)
 501                        insert = &commit_list_insert(commit, insert)->next;
 502        }
 503
 504        /* process the list in topological order */
 505        if (!lifo)
 506                commit_list_sort_by_date(&work);
 507
 508        pptr = list;
 509        *list = NULL;
 510        while (work) {
 511                struct commit *commit;
 512                struct commit_list *parents, *work_item;
 513
 514                work_item = work;
 515                work = work_item->next;
 516                work_item->next = NULL;
 517
 518                commit = work_item->item;
 519                for (parents = commit->parents; parents ; parents = parents->next) {
 520                        struct commit *parent = parents->item;
 521
 522                        if (!parent->indegree)
 523                                continue;
 524
 525                        /*
 526                         * parents are only enqueued for emission
 527                         * when all their children have been emitted thereby
 528                         * guaranteeing topological order.
 529                         */
 530                        if (--parent->indegree == 1) {
 531                                if (!lifo)
 532                                        commit_list_insert_by_date(parent, &work);
 533                                else
 534                                        commit_list_insert(parent, &work);
 535                        }
 536                }
 537                /*
 538                 * work_item is a commit all of whose children
 539                 * have already been emitted. we can emit it now.
 540                 */
 541                commit->indegree = 0;
 542                *pptr = work_item;
 543                pptr = &work_item->next;
 544        }
 545}
 546
 547/* merge-base stuff */
 548
 549/* bits #0..15 in revision.h */
 550#define PARENT1         (1u<<16)
 551#define PARENT2         (1u<<17)
 552#define STALE           (1u<<18)
 553#define RESULT          (1u<<19)
 554
 555static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
 556
 557static struct commit *interesting(struct commit_list *list)
 558{
 559        while (list) {
 560                struct commit *commit = list->item;
 561                list = list->next;
 562                if (commit->object.flags & STALE)
 563                        continue;
 564                return commit;
 565        }
 566        return NULL;
 567}
 568
 569static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
 570{
 571        struct commit_list *list = NULL;
 572        struct commit_list *result = NULL;
 573        int i;
 574
 575        for (i = 0; i < n; i++) {
 576                if (one == twos[i])
 577                        /*
 578                         * We do not mark this even with RESULT so we do not
 579                         * have to clean it up.
 580                         */
 581                        return commit_list_insert(one, &result);
 582        }
 583
 584        if (parse_commit(one))
 585                return NULL;
 586        for (i = 0; i < n; i++) {
 587                if (parse_commit(twos[i]))
 588                        return NULL;
 589        }
 590
 591        one->object.flags |= PARENT1;
 592        commit_list_insert_by_date(one, &list);
 593        for (i = 0; i < n; i++) {
 594                twos[i]->object.flags |= PARENT2;
 595                commit_list_insert_by_date(twos[i], &list);
 596        }
 597
 598        while (interesting(list)) {
 599                struct commit *commit;
 600                struct commit_list *parents;
 601                struct commit_list *next;
 602                int flags;
 603
 604                commit = list->item;
 605                next = list->next;
 606                free(list);
 607                list = next;
 608
 609                flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
 610                if (flags == (PARENT1 | PARENT2)) {
 611                        if (!(commit->object.flags & RESULT)) {
 612                                commit->object.flags |= RESULT;
 613                                commit_list_insert_by_date(commit, &result);
 614                        }
 615                        /* Mark parents of a found merge stale */
 616                        flags |= STALE;
 617                }
 618                parents = commit->parents;
 619                while (parents) {
 620                        struct commit *p = parents->item;
 621                        parents = parents->next;
 622                        if ((p->object.flags & flags) == flags)
 623                                continue;
 624                        if (parse_commit(p))
 625                                return NULL;
 626                        p->object.flags |= flags;
 627                        commit_list_insert_by_date(p, &list);
 628                }
 629        }
 630
 631        /* Clean up the result to remove stale ones */
 632        free_commit_list(list);
 633        list = result; result = NULL;
 634        while (list) {
 635                struct commit_list *next = list->next;
 636                if (!(list->item->object.flags & STALE))
 637                        commit_list_insert_by_date(list->item, &result);
 638                free(list);
 639                list = next;
 640        }
 641        return result;
 642}
 643
 644struct commit_list *get_octopus_merge_bases(struct commit_list *in)
 645{
 646        struct commit_list *i, *j, *k, *ret = NULL;
 647        struct commit_list **pptr = &ret;
 648
 649        for (i = in; i; i = i->next) {
 650                if (!ret)
 651                        pptr = &commit_list_insert(i->item, pptr)->next;
 652                else {
 653                        struct commit_list *new = NULL, *end = NULL;
 654
 655                        for (j = ret; j; j = j->next) {
 656                                struct commit_list *bases;
 657                                bases = get_merge_bases(i->item, j->item, 1);
 658                                if (!new)
 659                                        new = bases;
 660                                else
 661                                        end->next = bases;
 662                                for (k = bases; k; k = k->next)
 663                                        end = k;
 664                        }
 665                        ret = new;
 666                }
 667        }
 668        return ret;
 669}
 670
 671struct commit_list *get_merge_bases_many(struct commit *one,
 672                                         int n,
 673                                         struct commit **twos,
 674                                         int cleanup)
 675{
 676        struct commit_list *list;
 677        struct commit **rslt;
 678        struct commit_list *result;
 679        int cnt, i, j;
 680
 681        result = merge_bases_many(one, n, twos);
 682        for (i = 0; i < n; i++) {
 683                if (one == twos[i])
 684                        return result;
 685        }
 686        if (!result || !result->next) {
 687                if (cleanup) {
 688                        clear_commit_marks(one, all_flags);
 689                        for (i = 0; i < n; i++)
 690                                clear_commit_marks(twos[i], all_flags);
 691                }
 692                return result;
 693        }
 694
 695        /* There are more than one */
 696        cnt = 0;
 697        list = result;
 698        while (list) {
 699                list = list->next;
 700                cnt++;
 701        }
 702        rslt = xcalloc(cnt, sizeof(*rslt));
 703        for (list = result, i = 0; list; list = list->next)
 704                rslt[i++] = list->item;
 705        free_commit_list(result);
 706
 707        clear_commit_marks(one, all_flags);
 708        for (i = 0; i < n; i++)
 709                clear_commit_marks(twos[i], all_flags);
 710        for (i = 0; i < cnt - 1; i++) {
 711                for (j = i+1; j < cnt; j++) {
 712                        if (!rslt[i] || !rslt[j])
 713                                continue;
 714                        result = merge_bases_many(rslt[i], 1, &rslt[j]);
 715                        clear_commit_marks(rslt[i], all_flags);
 716                        clear_commit_marks(rslt[j], all_flags);
 717                        for (list = result; list; list = list->next) {
 718                                if (rslt[i] == list->item)
 719                                        rslt[i] = NULL;
 720                                if (rslt[j] == list->item)
 721                                        rslt[j] = NULL;
 722                        }
 723                }
 724        }
 725
 726        /* Surviving ones in rslt[] are the independent results */
 727        result = NULL;
 728        for (i = 0; i < cnt; i++) {
 729                if (rslt[i])
 730                        commit_list_insert_by_date(rslt[i], &result);
 731        }
 732        free(rslt);
 733        return result;
 734}
 735
 736struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
 737                                    int cleanup)
 738{
 739        return get_merge_bases_many(one, 1, &two, cleanup);
 740}
 741
 742int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
 743{
 744        if (!with_commit)
 745                return 1;
 746        while (with_commit) {
 747                struct commit *other;
 748
 749                other = with_commit->item;
 750                with_commit = with_commit->next;
 751                if (in_merge_bases(other, &commit, 1))
 752                        return 1;
 753        }
 754        return 0;
 755}
 756
 757int in_merge_bases(struct commit *commit, struct commit **reference, int num)
 758{
 759        struct commit_list *bases, *b;
 760        int ret = 0;
 761
 762        if (num == 1)
 763                bases = get_merge_bases(commit, *reference, 1);
 764        else
 765                die("not yet");
 766        for (b = bases; b; b = b->next) {
 767                if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
 768                        ret = 1;
 769                        break;
 770                }
 771        }
 772
 773        free_commit_list(bases);
 774        return ret;
 775}
 776
 777struct commit_list *reduce_heads(struct commit_list *heads)
 778{
 779        struct commit_list *p;
 780        struct commit_list *result = NULL, **tail = &result;
 781        struct commit **other;
 782        size_t num_head, num_other;
 783
 784        if (!heads)
 785                return NULL;
 786
 787        /* Avoid unnecessary reallocations */
 788        for (p = heads, num_head = 0; p; p = p->next)
 789                num_head++;
 790        other = xcalloc(sizeof(*other), num_head);
 791
 792        /* For each commit, see if it can be reached by others */
 793        for (p = heads; p; p = p->next) {
 794                struct commit_list *q, *base;
 795
 796                /* Do we already have this in the result? */
 797                for (q = result; q; q = q->next)
 798                        if (p->item == q->item)
 799                                break;
 800                if (q)
 801                        continue;
 802
 803                num_other = 0;
 804                for (q = heads; q; q = q->next) {
 805                        if (p->item == q->item)
 806                                continue;
 807                        other[num_other++] = q->item;
 808                }
 809                if (num_other)
 810                        base = get_merge_bases_many(p->item, num_other, other, 1);
 811                else
 812                        base = NULL;
 813                /*
 814                 * If p->item does not have anything common with other
 815                 * commits, there won't be any merge base.  If it is
 816                 * reachable from some of the others, p->item will be
 817                 * the merge base.  If its history is connected with
 818                 * others, but p->item is not reachable by others, we
 819                 * will get something other than p->item back.
 820                 */
 821                if (!base || (base->item != p->item))
 822                        tail = &(commit_list_insert(p->item, tail)->next);
 823                free_commit_list(base);
 824        }
 825        free(other);
 826        return result;
 827}
 828
 829static const char commit_utf8_warn[] =
 830"Warning: commit message does not conform to UTF-8.\n"
 831"You may want to amend it after fixing the message, or set the config\n"
 832"variable i18n.commitencoding to the encoding your project uses.\n";
 833
 834int commit_tree(const char *msg, unsigned char *tree,
 835                struct commit_list *parents, unsigned char *ret,
 836                const char *author)
 837{
 838        int result;
 839        int encoding_is_utf8;
 840        struct strbuf buffer;
 841
 842        assert_sha1_type(tree, OBJ_TREE);
 843
 844        /* Not having i18n.commitencoding is the same as having utf-8 */
 845        encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
 846
 847        strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
 848        strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
 849
 850        /*
 851         * NOTE! This ordering means that the same exact tree merged with a
 852         * different order of parents will be a _different_ changeset even
 853         * if everything else stays the same.
 854         */
 855        while (parents) {
 856                struct commit_list *next = parents->next;
 857                strbuf_addf(&buffer, "parent %s\n",
 858                        sha1_to_hex(parents->item->object.sha1));
 859                free(parents);
 860                parents = next;
 861        }
 862
 863        /* Person/date information */
 864        if (!author)
 865                author = git_author_info(IDENT_ERROR_ON_NO_NAME);
 866        strbuf_addf(&buffer, "author %s\n", author);
 867        strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
 868        if (!encoding_is_utf8)
 869                strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
 870        strbuf_addch(&buffer, '\n');
 871
 872        /* And add the comment */
 873        strbuf_addstr(&buffer, msg);
 874
 875        /* And check the encoding */
 876        if (encoding_is_utf8 && !is_utf8(buffer.buf))
 877                fprintf(stderr, commit_utf8_warn);
 878
 879        result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
 880        strbuf_release(&buffer);
 881        return result;
 882}