commit.con commit Revert "daemon: add upload-tar service." (d9edcbd)
   1#include "cache.h"
   2#include "tag.h"
   3#include "commit.h"
   4
   5int save_commit_buffer = 1;
   6
   7struct sort_node
   8{
   9        /*
  10         * the number of children of the associated commit
  11         * that also occur in the list being sorted.
  12         */
  13        unsigned int indegree;
  14
  15        /*
  16         * reference to original list item that we will re-use
  17         * on output.
  18         */
  19        struct commit_list * list_item;
  20
  21};
  22
  23const char *commit_type = "commit";
  24
  25struct cmt_fmt_map {
  26        const char *n;
  27        size_t cmp_len;
  28        enum cmit_fmt v;
  29} cmt_fmts[] = {
  30        { "raw",        1,      CMIT_FMT_RAW },
  31        { "medium",     1,      CMIT_FMT_MEDIUM },
  32        { "short",      1,      CMIT_FMT_SHORT },
  33        { "email",      1,      CMIT_FMT_EMAIL },
  34        { "full",       5,      CMIT_FMT_FULL },
  35        { "fuller",     5,      CMIT_FMT_FULLER },
  36        { "oneline",    1,      CMIT_FMT_ONELINE },
  37};
  38
  39enum cmit_fmt get_commit_format(const char *arg)
  40{
  41        int i;
  42
  43        if (!arg || !*arg)
  44                return CMIT_FMT_DEFAULT;
  45        if (*arg == '=')
  46                arg++;
  47        for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
  48                if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len))
  49                        return cmt_fmts[i].v;
  50        }
  51
  52        die("invalid --pretty format: %s", arg);
  53}
  54
  55static struct commit *check_commit(struct object *obj,
  56                                   const unsigned char *sha1,
  57                                   int quiet)
  58{
  59        if (obj->type != OBJ_COMMIT) {
  60                if (!quiet)
  61                        error("Object %s is a %s, not a commit",
  62                              sha1_to_hex(sha1), typename(obj->type));
  63                return NULL;
  64        }
  65        return (struct commit *) obj;
  66}
  67
  68struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
  69                                              int quiet)
  70{
  71        struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
  72
  73        if (!obj)
  74                return NULL;
  75        return check_commit(obj, sha1, quiet);
  76}
  77
  78struct commit *lookup_commit_reference(const unsigned char *sha1)
  79{
  80        return lookup_commit_reference_gently(sha1, 0);
  81}
  82
  83struct commit *lookup_commit(const unsigned char *sha1)
  84{
  85        struct object *obj = lookup_object(sha1);
  86        if (!obj) {
  87                struct commit *ret = alloc_commit_node();
  88                created_object(sha1, &ret->object);
  89                ret->object.type = OBJ_COMMIT;
  90                return ret;
  91        }
  92        if (!obj->type)
  93                obj->type = OBJ_COMMIT;
  94        return check_commit(obj, sha1, 0);
  95}
  96
  97static unsigned long parse_commit_date(const char *buf)
  98{
  99        unsigned long date;
 100
 101        if (memcmp(buf, "author", 6))
 102                return 0;
 103        while (*buf++ != '\n')
 104                /* nada */;
 105        if (memcmp(buf, "committer", 9))
 106                return 0;
 107        while (*buf++ != '>')
 108                /* nada */;
 109        date = strtoul(buf, NULL, 10);
 110        if (date == ULONG_MAX)
 111                date = 0;
 112        return date;
 113}
 114
 115static struct commit_graft **commit_graft;
 116static int commit_graft_alloc, commit_graft_nr;
 117
 118static int commit_graft_pos(const unsigned char *sha1)
 119{
 120        int lo, hi;
 121        lo = 0;
 122        hi = commit_graft_nr;
 123        while (lo < hi) {
 124                int mi = (lo + hi) / 2;
 125                struct commit_graft *graft = commit_graft[mi];
 126                int cmp = hashcmp(sha1, graft->sha1);
 127                if (!cmp)
 128                        return mi;
 129                if (cmp < 0)
 130                        hi = mi;
 131                else
 132                        lo = mi + 1;
 133        }
 134        return -lo - 1;
 135}
 136
 137int register_commit_graft(struct commit_graft *graft, int ignore_dups)
 138{
 139        int pos = commit_graft_pos(graft->sha1);
 140        
 141        if (0 <= pos) {
 142                if (ignore_dups)
 143                        free(graft);
 144                else {
 145                        free(commit_graft[pos]);
 146                        commit_graft[pos] = graft;
 147                }
 148                return 1;
 149        }
 150        pos = -pos - 1;
 151        if (commit_graft_alloc <= ++commit_graft_nr) {
 152                commit_graft_alloc = alloc_nr(commit_graft_alloc);
 153                commit_graft = xrealloc(commit_graft,
 154                                        sizeof(*commit_graft) *
 155                                        commit_graft_alloc);
 156        }
 157        if (pos < commit_graft_nr)
 158                memmove(commit_graft + pos + 1,
 159                        commit_graft + pos,
 160                        (commit_graft_nr - pos - 1) *
 161                        sizeof(*commit_graft));
 162        commit_graft[pos] = graft;
 163        return 0;
 164}
 165
 166struct commit_graft *read_graft_line(char *buf, int len)
 167{
 168        /* The format is just "Commit Parent1 Parent2 ...\n" */
 169        int i;
 170        struct commit_graft *graft = NULL;
 171
 172        if (buf[len-1] == '\n')
 173                buf[--len] = 0;
 174        if (buf[0] == '#' || buf[0] == '\0')
 175                return NULL;
 176        if ((len + 1) % 41) {
 177        bad_graft_data:
 178                error("bad graft data: %s", buf);
 179                free(graft);
 180                return NULL;
 181        }
 182        i = (len + 1) / 41 - 1;
 183        graft = xmalloc(sizeof(*graft) + 20 * i);
 184        graft->nr_parent = i;
 185        if (get_sha1_hex(buf, graft->sha1))
 186                goto bad_graft_data;
 187        for (i = 40; i < len; i += 41) {
 188                if (buf[i] != ' ')
 189                        goto bad_graft_data;
 190                if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
 191                        goto bad_graft_data;
 192        }
 193        return graft;
 194}
 195
 196int read_graft_file(const char *graft_file)
 197{
 198        FILE *fp = fopen(graft_file, "r");
 199        char buf[1024];
 200        if (!fp)
 201                return -1;
 202        while (fgets(buf, sizeof(buf), fp)) {
 203                /* The format is just "Commit Parent1 Parent2 ...\n" */
 204                int len = strlen(buf);
 205                struct commit_graft *graft = read_graft_line(buf, len);
 206                if (!graft)
 207                        continue;
 208                if (register_commit_graft(graft, 1))
 209                        error("duplicate graft data: %s", buf);
 210        }
 211        fclose(fp);
 212        return 0;
 213}
 214
 215static void prepare_commit_graft(void)
 216{
 217        static int commit_graft_prepared;
 218        char *graft_file;
 219
 220        if (commit_graft_prepared)
 221                return;
 222        graft_file = get_graft_file();
 223        read_graft_file(graft_file);
 224        commit_graft_prepared = 1;
 225}
 226
 227static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
 228{
 229        int pos;
 230        prepare_commit_graft();
 231        pos = commit_graft_pos(sha1);
 232        if (pos < 0)
 233                return NULL;
 234        return commit_graft[pos];
 235}
 236
 237int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
 238{
 239        char *tail = buffer;
 240        char *bufptr = buffer;
 241        unsigned char parent[20];
 242        struct commit_list **pptr;
 243        struct commit_graft *graft;
 244        unsigned n_refs = 0;
 245
 246        if (item->object.parsed)
 247                return 0;
 248        item->object.parsed = 1;
 249        tail += size;
 250        if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
 251                return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
 252        if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
 253                return error("bad tree pointer in commit %s",
 254                             sha1_to_hex(item->object.sha1));
 255        item->tree = lookup_tree(parent);
 256        if (item->tree)
 257                n_refs++;
 258        bufptr += 46; /* "tree " + "hex sha1" + "\n" */
 259        pptr = &item->parents;
 260
 261        graft = lookup_commit_graft(item->object.sha1);
 262        while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
 263                struct commit *new_parent;
 264
 265                if (tail <= bufptr + 48 ||
 266                    get_sha1_hex(bufptr + 7, parent) ||
 267                    bufptr[47] != '\n')
 268                        return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
 269                bufptr += 48;
 270                if (graft)
 271                        continue;
 272                new_parent = lookup_commit(parent);
 273                if (new_parent) {
 274                        pptr = &commit_list_insert(new_parent, pptr)->next;
 275                        n_refs++;
 276                }
 277        }
 278        if (graft) {
 279                int i;
 280                struct commit *new_parent;
 281                for (i = 0; i < graft->nr_parent; i++) {
 282                        new_parent = lookup_commit(graft->parent[i]);
 283                        if (!new_parent)
 284                                continue;
 285                        pptr = &commit_list_insert(new_parent, pptr)->next;
 286                        n_refs++;
 287                }
 288        }
 289        item->date = parse_commit_date(bufptr);
 290
 291        if (track_object_refs) {
 292                unsigned i = 0;
 293                struct commit_list *p;
 294                struct object_refs *refs = alloc_object_refs(n_refs);
 295                if (item->tree)
 296                        refs->ref[i++] = &item->tree->object;
 297                for (p = item->parents; p; p = p->next)
 298                        refs->ref[i++] = &p->item->object;
 299                set_object_refs(&item->object, refs);
 300        }
 301
 302        return 0;
 303}
 304
 305int parse_commit(struct commit *item)
 306{
 307        char type[20];
 308        void *buffer;
 309        unsigned long size;
 310        int ret;
 311
 312        if (item->object.parsed)
 313                return 0;
 314        buffer = read_sha1_file(item->object.sha1, type, &size);
 315        if (!buffer)
 316                return error("Could not read %s",
 317                             sha1_to_hex(item->object.sha1));
 318        if (strcmp(type, commit_type)) {
 319                free(buffer);
 320                return error("Object %s not a commit",
 321                             sha1_to_hex(item->object.sha1));
 322        }
 323        ret = parse_commit_buffer(item, buffer, size);
 324        if (save_commit_buffer && !ret) {
 325                item->buffer = buffer;
 326                return 0;
 327        }
 328        free(buffer);
 329        return ret;
 330}
 331
 332struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
 333{
 334        struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
 335        new_list->item = item;
 336        new_list->next = *list_p;
 337        *list_p = new_list;
 338        return new_list;
 339}
 340
 341void free_commit_list(struct commit_list *list)
 342{
 343        while (list) {
 344                struct commit_list *temp = list;
 345                list = temp->next;
 346                free(temp);
 347        }
 348}
 349
 350struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
 351{
 352        struct commit_list **pp = list;
 353        struct commit_list *p;
 354        while ((p = *pp) != NULL) {
 355                if (p->item->date < item->date) {
 356                        break;
 357                }
 358                pp = &p->next;
 359        }
 360        return commit_list_insert(item, pp);
 361}
 362
 363        
 364void sort_by_date(struct commit_list **list)
 365{
 366        struct commit_list *ret = NULL;
 367        while (*list) {
 368                insert_by_date((*list)->item, &ret);
 369                *list = (*list)->next;
 370        }
 371        *list = ret;
 372}
 373
 374struct commit *pop_most_recent_commit(struct commit_list **list,
 375                                      unsigned int mark)
 376{
 377        struct commit *ret = (*list)->item;
 378        struct commit_list *parents = ret->parents;
 379        struct commit_list *old = *list;
 380
 381        *list = (*list)->next;
 382        free(old);
 383
 384        while (parents) {
 385                struct commit *commit = parents->item;
 386                parse_commit(commit);
 387                if (!(commit->object.flags & mark)) {
 388                        commit->object.flags |= mark;
 389                        insert_by_date(commit, list);
 390                }
 391                parents = parents->next;
 392        }
 393        return ret;
 394}
 395
 396void clear_commit_marks(struct commit *commit, unsigned int mark)
 397{
 398        struct commit_list *parents;
 399
 400        commit->object.flags &= ~mark;
 401        parents = commit->parents;
 402        while (parents) {
 403                struct commit *parent = parents->item;
 404
 405                /* Have we already cleared this? */
 406                if (mark & parent->object.flags)
 407                        clear_commit_marks(parent, mark);
 408                parents = parents->next;
 409        }
 410}
 411
 412/*
 413 * Generic support for pretty-printing the header
 414 */
 415static int get_one_line(const char *msg, unsigned long len)
 416{
 417        int ret = 0;
 418
 419        while (len--) {
 420                char c = *msg++;
 421                if (!c)
 422                        break;
 423                ret++;
 424                if (c == '\n')
 425                        break;
 426        }
 427        return ret;
 428}
 429
 430static int is_rfc2047_special(char ch)
 431{
 432        return ((ch & 0x80) || (ch == '=') || (ch == '?') || (ch == '_'));
 433}
 434
 435static int add_rfc2047(char *buf, const char *line, int len)
 436{
 437        char *bp = buf;
 438        int i, needquote;
 439        static const char q_utf8[] = "=?utf-8?q?";
 440
 441        for (i = needquote = 0; !needquote && i < len; i++) {
 442                unsigned ch = line[i];
 443                if (ch & 0x80)
 444                        needquote++;
 445                if ((i + 1 < len) &&
 446                    (ch == '=' && line[i+1] == '?'))
 447                        needquote++;
 448        }
 449        if (!needquote)
 450                return sprintf(buf, "%.*s", len, line);
 451
 452        memcpy(bp, q_utf8, sizeof(q_utf8)-1);
 453        bp += sizeof(q_utf8)-1;
 454        for (i = 0; i < len; i++) {
 455                unsigned ch = line[i] & 0xFF;
 456                if (is_rfc2047_special(ch)) {
 457                        sprintf(bp, "=%02X", ch);
 458                        bp += 3;
 459                }
 460                else if (ch == ' ')
 461                        *bp++ = '_';
 462                else
 463                        *bp++ = ch;
 464        }
 465        memcpy(bp, "?=", 2);
 466        bp += 2;
 467        return bp - buf;
 468}
 469
 470static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
 471{
 472        char *date;
 473        int namelen;
 474        unsigned long time;
 475        int tz, ret;
 476        const char *filler = "    ";
 477
 478        if (fmt == CMIT_FMT_ONELINE)
 479                return 0;
 480        date = strchr(line, '>');
 481        if (!date)
 482                return 0;
 483        namelen = ++date - line;
 484        time = strtoul(date, &date, 10);
 485        tz = strtol(date, NULL, 10);
 486
 487        if (fmt == CMIT_FMT_EMAIL) {
 488                char *name_tail = strchr(line, '<');
 489                int display_name_length;
 490                if (!name_tail)
 491                        return 0;
 492                while (line < name_tail && isspace(name_tail[-1]))
 493                        name_tail--;
 494                display_name_length = name_tail - line;
 495                filler = "";
 496                strcpy(buf, "From: ");
 497                ret = strlen(buf);
 498                ret += add_rfc2047(buf + ret, line, display_name_length);
 499                memcpy(buf + ret, name_tail, namelen - display_name_length);
 500                ret += namelen - display_name_length;
 501                buf[ret++] = '\n';
 502        }
 503        else {
 504                ret = sprintf(buf, "%s: %.*s%.*s\n", what,
 505                              (fmt == CMIT_FMT_FULLER) ? 4 : 0,
 506                              filler, namelen, line);
 507        }
 508        switch (fmt) {
 509        case CMIT_FMT_MEDIUM:
 510                ret += sprintf(buf + ret, "Date:   %s\n", show_date(time, tz, 0));
 511                break;
 512        case CMIT_FMT_EMAIL:
 513                ret += sprintf(buf + ret, "Date: %s\n",
 514                               show_rfc2822_date(time, tz));
 515                break;
 516        case CMIT_FMT_FULLER:
 517                ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz, 0));
 518                break;
 519        default:
 520                /* notin' */
 521                break;
 522        }
 523        return ret;
 524}
 525
 526static int is_empty_line(const char *line, int *len_p)
 527{
 528        int len = *len_p;
 529        while (len && isspace(line[len-1]))
 530                len--;
 531        *len_p = len;
 532        return !len;
 533}
 534
 535static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
 536{
 537        struct commit_list *parent = commit->parents;
 538        int offset;
 539
 540        if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
 541            !parent || !parent->next)
 542                return 0;
 543
 544        offset = sprintf(buf, "Merge:");
 545
 546        while (parent) {
 547                struct commit *p = parent->item;
 548                const char *hex = abbrev
 549                        ? find_unique_abbrev(p->object.sha1, abbrev)
 550                        : sha1_to_hex(p->object.sha1);
 551                const char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
 552                parent = parent->next;
 553
 554                offset += sprintf(buf + offset, " %s%s", hex, dots);
 555        }
 556        buf[offset++] = '\n';
 557        return offset;
 558}
 559
 560unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, unsigned long len, char *buf, unsigned long space, int abbrev, const char *subject, const char *after_subject)
 561{
 562        int hdr = 1, body = 0;
 563        unsigned long offset = 0;
 564        int indent = 4;
 565        int parents_shown = 0;
 566        const char *msg = commit->buffer;
 567        int plain_non_ascii = 0;
 568
 569        if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
 570                indent = 0;
 571
 572        /* After-subject is used to pass in Content-Type: multipart
 573         * MIME header; in that case we do not have to do the
 574         * plaintext content type even if the commit message has
 575         * non 7-bit ASCII character.  Otherwise, check if we need
 576         * to say this is not a 7-bit ASCII.
 577         */
 578        if (fmt == CMIT_FMT_EMAIL && !after_subject) {
 579                int i, ch, in_body;
 580
 581                for (in_body = i = 0; (ch = msg[i]) && i < len; i++) {
 582                        if (!in_body) {
 583                                /* author could be non 7-bit ASCII but
 584                                 * the log may so; skip over the
 585                                 * header part first.
 586                                 */
 587                                if (ch == '\n' &&
 588                                    i + 1 < len && msg[i+1] == '\n')
 589                                        in_body = 1;
 590                        }
 591                        else if (ch & 0x80) {
 592                                plain_non_ascii = 1;
 593                                break;
 594                        }
 595                }
 596        }
 597
 598        for (;;) {
 599                const char *line = msg;
 600                int linelen = get_one_line(msg, len);
 601
 602                if (!linelen)
 603                        break;
 604
 605                /*
 606                 * We want some slop for indentation and a possible
 607                 * final "...". Thus the "+ 20".
 608                 */
 609                if (offset + linelen + 20 > space) {
 610                        memcpy(buf + offset, "    ...\n", 8);
 611                        offset += 8;
 612                        break;
 613                }
 614
 615                msg += linelen;
 616                len -= linelen;
 617                if (hdr) {
 618                        if (linelen == 1) {
 619                                hdr = 0;
 620                                if ((fmt != CMIT_FMT_ONELINE) && !subject)
 621                                        buf[offset++] = '\n';
 622                                continue;
 623                        }
 624                        if (fmt == CMIT_FMT_RAW) {
 625                                memcpy(buf + offset, line, linelen);
 626                                offset += linelen;
 627                                continue;
 628                        }
 629                        if (!memcmp(line, "parent ", 7)) {
 630                                if (linelen != 48)
 631                                        die("bad parent line in commit");
 632                                continue;
 633                        }
 634
 635                        if (!parents_shown) {
 636                                offset += add_merge_info(fmt, buf + offset,
 637                                                         commit, abbrev);
 638                                parents_shown = 1;
 639                                continue;
 640                        }
 641                        /*
 642                         * MEDIUM == DEFAULT shows only author with dates.
 643                         * FULL shows both authors but not dates.
 644                         * FULLER shows both authors and dates.
 645                         */
 646                        if (!memcmp(line, "author ", 7))
 647                                offset += add_user_info("Author", fmt,
 648                                                        buf + offset,
 649                                                        line + 7);
 650                        if (!memcmp(line, "committer ", 10) &&
 651                            (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
 652                                offset += add_user_info("Commit", fmt,
 653                                                        buf + offset,
 654                                                        line + 10);
 655                        continue;
 656                }
 657
 658                if (!subject)
 659                        body = 1;
 660
 661                if (is_empty_line(line, &linelen)) {
 662                        if (!body)
 663                                continue;
 664                        if (subject)
 665                                continue;
 666                        if (fmt == CMIT_FMT_SHORT)
 667                                break;
 668                }
 669
 670                if (subject) {
 671                        int slen = strlen(subject);
 672                        memcpy(buf + offset, subject, slen);
 673                        offset += slen;
 674                        offset += add_rfc2047(buf + offset, line, linelen);
 675                }
 676                else {
 677                        memset(buf + offset, ' ', indent);
 678                        memcpy(buf + offset + indent, line, linelen);
 679                        offset += linelen + indent;
 680                }
 681                buf[offset++] = '\n';
 682                if (fmt == CMIT_FMT_ONELINE)
 683                        break;
 684                if (subject && plain_non_ascii) {
 685                        static const char header[] =
 686                                "Content-Type: text/plain; charset=UTF-8\n"
 687                                "Content-Transfer-Encoding: 8bit\n";
 688                        memcpy(buf + offset, header, sizeof(header)-1);
 689                        offset += sizeof(header)-1;
 690                }
 691                if (after_subject) {
 692                        int slen = strlen(after_subject);
 693                        if (slen > space - offset - 1)
 694                                slen = space - offset - 1;
 695                        memcpy(buf + offset, after_subject, slen);
 696                        offset += slen;
 697                        after_subject = NULL;
 698                }
 699                subject = NULL;
 700        }
 701        while (offset && isspace(buf[offset-1]))
 702                offset--;
 703        /* Make sure there is an EOLN for the non-oneline case */
 704        if (fmt != CMIT_FMT_ONELINE)
 705                buf[offset++] = '\n';
 706        /*
 707         * make sure there is another EOLN to separate the headers from whatever
 708         * body the caller appends if we haven't already written a body
 709         */
 710        if (fmt == CMIT_FMT_EMAIL && !body)
 711                buf[offset++] = '\n';
 712        buf[offset] = '\0';
 713        return offset;
 714}
 715
 716struct commit *pop_commit(struct commit_list **stack)
 717{
 718        struct commit_list *top = *stack;
 719        struct commit *item = top ? top->item : NULL;
 720
 721        if (top) {
 722                *stack = top->next;
 723                free(top);
 724        }
 725        return item;
 726}
 727
 728int count_parents(struct commit * commit)
 729{
 730        int count;
 731        struct commit_list * parents = commit->parents;
 732        for (count = 0; parents; parents = parents->next,count++)
 733                ;
 734        return count;
 735}
 736
 737void topo_sort_default_setter(struct commit *c, void *data)
 738{
 739        c->util = data;
 740}
 741
 742void *topo_sort_default_getter(struct commit *c)
 743{
 744        return c->util;
 745}
 746
 747/*
 748 * Performs an in-place topological sort on the list supplied.
 749 */
 750void sort_in_topological_order(struct commit_list ** list, int lifo)
 751{
 752        sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
 753                                     topo_sort_default_getter);
 754}
 755
 756void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
 757                                  topo_sort_set_fn_t setter,
 758                                  topo_sort_get_fn_t getter)
 759{
 760        struct commit_list * next = *list;
 761        struct commit_list * work = NULL, **insert;
 762        struct commit_list ** pptr = list;
 763        struct sort_node * nodes;
 764        struct sort_node * next_nodes;
 765        int count = 0;
 766
 767        /* determine the size of the list */
 768        while (next) {
 769                next = next->next;
 770                count++;
 771        }
 772        
 773        if (!count)
 774                return;
 775        /* allocate an array to help sort the list */
 776        nodes = xcalloc(count, sizeof(*nodes));
 777        /* link the list to the array */
 778        next_nodes = nodes;
 779        next=*list;
 780        while (next) {
 781                next_nodes->list_item = next;
 782                setter(next->item, next_nodes);
 783                next_nodes++;
 784                next = next->next;
 785        }
 786        /* update the indegree */
 787        next=*list;
 788        while (next) {
 789                struct commit_list * parents = next->item->parents;
 790                while (parents) {
 791                        struct commit * parent=parents->item;
 792                        struct sort_node * pn = (struct sort_node *) getter(parent);
 793
 794                        if (pn)
 795                                pn->indegree++;
 796                        parents=parents->next;
 797                }
 798                next=next->next;
 799        }
 800        /* 
 801         * find the tips
 802         *
 803         * tips are nodes not reachable from any other node in the list 
 804         * 
 805         * the tips serve as a starting set for the work queue.
 806         */
 807        next=*list;
 808        insert = &work;
 809        while (next) {
 810                struct sort_node * node = (struct sort_node *) getter(next->item);
 811
 812                if (node->indegree == 0) {
 813                        insert = &commit_list_insert(next->item, insert)->next;
 814                }
 815                next=next->next;
 816        }
 817
 818        /* process the list in topological order */
 819        if (!lifo)
 820                sort_by_date(&work);
 821        while (work) {
 822                struct commit * work_item = pop_commit(&work);
 823                struct sort_node * work_node = (struct sort_node *) getter(work_item);
 824                struct commit_list * parents = work_item->parents;
 825
 826                while (parents) {
 827                        struct commit * parent=parents->item;
 828                        struct sort_node * pn = (struct sort_node *) getter(parent);
 829
 830                        if (pn) {
 831                                /*
 832                                 * parents are only enqueued for emission 
 833                                 * when all their children have been emitted thereby
 834                                 * guaranteeing topological order.
 835                                 */
 836                                pn->indegree--;
 837                                if (!pn->indegree) {
 838                                        if (!lifo)
 839                                                insert_by_date(parent, &work);
 840                                        else
 841                                                commit_list_insert(parent, &work);
 842                                }
 843                        }
 844                        parents=parents->next;
 845                }
 846                /*
 847                 * work_item is a commit all of whose children
 848                 * have already been emitted. we can emit it now.
 849                 */
 850                *pptr = work_node->list_item;
 851                pptr = &(*pptr)->next;
 852                *pptr = NULL;
 853                setter(work_item, NULL);
 854        }
 855        free(nodes);
 856}
 857
 858/* merge-rebase stuff */
 859
 860/* bits #0..7 in revision.h */
 861#define PARENT1         (1u<< 8)
 862#define PARENT2         (1u<< 9)
 863#define STALE           (1u<<10)
 864#define RESULT          (1u<<11)
 865
 866static struct commit *interesting(struct commit_list *list)
 867{
 868        while (list) {
 869                struct commit *commit = list->item;
 870                list = list->next;
 871                if (commit->object.flags & STALE)
 872                        continue;
 873                return commit;
 874        }
 875        return NULL;
 876}
 877
 878static struct commit_list *merge_bases(struct commit *one, struct commit *two)
 879{
 880        struct commit_list *list = NULL;
 881        struct commit_list *result = NULL;
 882
 883        if (one == two)
 884                /* We do not mark this even with RESULT so we do not
 885                 * have to clean it up.
 886                 */
 887                return commit_list_insert(one, &result);
 888
 889        parse_commit(one);
 890        parse_commit(two);
 891
 892        one->object.flags |= PARENT1;
 893        two->object.flags |= PARENT2;
 894        insert_by_date(one, &list);
 895        insert_by_date(two, &list);
 896
 897        while (interesting(list)) {
 898                struct commit *commit;
 899                struct commit_list *parents;
 900                struct commit_list *n;
 901                int flags;
 902
 903                commit = list->item;
 904                n = list->next;
 905                free(list);
 906                list = n;
 907
 908                flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
 909                if (flags == (PARENT1 | PARENT2)) {
 910                        if (!(commit->object.flags & RESULT)) {
 911                                commit->object.flags |= RESULT;
 912                                insert_by_date(commit, &result);
 913                        }
 914                        /* Mark parents of a found merge stale */
 915                        flags |= STALE;
 916                }
 917                parents = commit->parents;
 918                while (parents) {
 919                        struct commit *p = parents->item;
 920                        parents = parents->next;
 921                        if ((p->object.flags & flags) == flags)
 922                                continue;
 923                        parse_commit(p);
 924                        p->object.flags |= flags;
 925                        insert_by_date(p, &list);
 926                }
 927        }
 928
 929        /* Clean up the result to remove stale ones */
 930        list = result; result = NULL;
 931        while (list) {
 932                struct commit_list *n = list->next;
 933                if (!(list->item->object.flags & STALE))
 934                        insert_by_date(list->item, &result);
 935                free(list);
 936                list = n;
 937        }
 938        return result;
 939}
 940
 941struct commit_list *get_merge_bases(struct commit *one,
 942                                    struct commit *two,
 943                                    int cleanup)
 944{
 945        const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
 946        struct commit_list *list;
 947        struct commit **rslt;
 948        struct commit_list *result;
 949        int cnt, i, j;
 950
 951        result = merge_bases(one, two);
 952        if (one == two)
 953                return result;
 954        if (!result || !result->next) {
 955                if (cleanup) {
 956                        clear_commit_marks(one, all_flags);
 957                        clear_commit_marks(two, all_flags);
 958                }
 959                return result;
 960        }
 961
 962        /* There are more than one */
 963        cnt = 0;
 964        list = result;
 965        while (list) {
 966                list = list->next;
 967                cnt++;
 968        }
 969        rslt = xcalloc(cnt, sizeof(*rslt));
 970        for (list = result, i = 0; list; list = list->next)
 971                rslt[i++] = list->item;
 972        free_commit_list(result);
 973
 974        clear_commit_marks(one, all_flags);
 975        clear_commit_marks(two, all_flags);
 976        for (i = 0; i < cnt - 1; i++) {
 977                for (j = i+1; j < cnt; j++) {
 978                        if (!rslt[i] || !rslt[j])
 979                                continue;
 980                        result = merge_bases(rslt[i], rslt[j]);
 981                        clear_commit_marks(rslt[i], all_flags);
 982                        clear_commit_marks(rslt[j], all_flags);
 983                        for (list = result; list; list = list->next) {
 984                                if (rslt[i] == list->item)
 985                                        rslt[i] = NULL;
 986                                if (rslt[j] == list->item)
 987                                        rslt[j] = NULL;
 988                        }
 989                }
 990        }
 991
 992        /* Surviving ones in rslt[] are the independent results */
 993        result = NULL;
 994        for (i = 0; i < cnt; i++) {
 995                if (rslt[i])
 996                        insert_by_date(rslt[i], &result);
 997        }
 998        free(rslt);
 999        return result;
1000}