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