commit.con commit Fix lseek(2) calls with args 2 and 3 swapped (b5da246)
   1#include "cache.h"
   2#include "tag.h"
   3#include "commit.h"
   4#include "pkt-line.h"
   5#include "utf8.h"
   6#include "interpolate.h"
   7
   8int save_commit_buffer = 1;
   9
  10struct sort_node
  11{
  12        /*
  13         * the number of children of the associated commit
  14         * that also occur in the list being sorted.
  15         */
  16        unsigned int indegree;
  17
  18        /*
  19         * reference to original list item that we will re-use
  20         * on output.
  21         */
  22        struct commit_list * list_item;
  23
  24};
  25
  26const char *commit_type = "commit";
  27
  28struct cmt_fmt_map {
  29        const char *n;
  30        size_t cmp_len;
  31        enum cmit_fmt v;
  32} cmt_fmts[] = {
  33        { "raw",        1,      CMIT_FMT_RAW },
  34        { "medium",     1,      CMIT_FMT_MEDIUM },
  35        { "short",      1,      CMIT_FMT_SHORT },
  36        { "email",      1,      CMIT_FMT_EMAIL },
  37        { "full",       5,      CMIT_FMT_FULL },
  38        { "fuller",     5,      CMIT_FMT_FULLER },
  39        { "oneline",    1,      CMIT_FMT_ONELINE },
  40        { "format:",    7,      CMIT_FMT_USERFORMAT},
  41};
  42
  43static char *user_format;
  44
  45enum cmit_fmt get_commit_format(const char *arg)
  46{
  47        int i;
  48
  49        if (!arg || !*arg)
  50                return CMIT_FMT_DEFAULT;
  51        if (*arg == '=')
  52                arg++;
  53        if (!prefixcmp(arg, "format:")) {
  54                if (user_format)
  55                        free(user_format);
  56                user_format = xstrdup(arg + 7);
  57                return CMIT_FMT_USERFORMAT;
  58        }
  59        for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
  60                if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) &&
  61                    !strncmp(arg, cmt_fmts[i].n, strlen(arg)))
  62                        return cmt_fmts[i].v;
  63        }
  64
  65        die("invalid --pretty format: %s", arg);
  66}
  67
  68static struct commit *check_commit(struct object *obj,
  69                                   const unsigned char *sha1,
  70                                   int quiet)
  71{
  72        if (obj->type != OBJ_COMMIT) {
  73                if (!quiet)
  74                        error("Object %s is a %s, not a commit",
  75                              sha1_to_hex(sha1), typename(obj->type));
  76                return NULL;
  77        }
  78        return (struct commit *) obj;
  79}
  80
  81struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
  82                                              int quiet)
  83{
  84        struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
  85
  86        if (!obj)
  87                return NULL;
  88        return check_commit(obj, sha1, quiet);
  89}
  90
  91struct commit *lookup_commit_reference(const unsigned char *sha1)
  92{
  93        return lookup_commit_reference_gently(sha1, 0);
  94}
  95
  96struct commit *lookup_commit(const unsigned char *sha1)
  97{
  98        struct object *obj = lookup_object(sha1);
  99        if (!obj) {
 100                struct commit *ret = alloc_commit_node();
 101                created_object(sha1, &ret->object);
 102                ret->object.type = OBJ_COMMIT;
 103                return ret;
 104        }
 105        if (!obj->type)
 106                obj->type = OBJ_COMMIT;
 107        return check_commit(obj, sha1, 0);
 108}
 109
 110static unsigned long parse_commit_date(const char *buf)
 111{
 112        unsigned long date;
 113
 114        if (memcmp(buf, "author", 6))
 115                return 0;
 116        while (*buf++ != '\n')
 117                /* nada */;
 118        if (memcmp(buf, "committer", 9))
 119                return 0;
 120        while (*buf++ != '>')
 121                /* nada */;
 122        date = strtoul(buf, NULL, 10);
 123        if (date == ULONG_MAX)
 124                date = 0;
 125        return date;
 126}
 127
 128static struct commit_graft **commit_graft;
 129static int commit_graft_alloc, commit_graft_nr;
 130
 131static int commit_graft_pos(const unsigned char *sha1)
 132{
 133        int lo, hi;
 134        lo = 0;
 135        hi = commit_graft_nr;
 136        while (lo < hi) {
 137                int mi = (lo + hi) / 2;
 138                struct commit_graft *graft = commit_graft[mi];
 139                int cmp = hashcmp(sha1, graft->sha1);
 140                if (!cmp)
 141                        return mi;
 142                if (cmp < 0)
 143                        hi = mi;
 144                else
 145                        lo = mi + 1;
 146        }
 147        return -lo - 1;
 148}
 149
 150int register_commit_graft(struct commit_graft *graft, int ignore_dups)
 151{
 152        int pos = commit_graft_pos(graft->sha1);
 153        
 154        if (0 <= pos) {
 155                if (ignore_dups)
 156                        free(graft);
 157                else {
 158                        free(commit_graft[pos]);
 159                        commit_graft[pos] = graft;
 160                }
 161                return 1;
 162        }
 163        pos = -pos - 1;
 164        if (commit_graft_alloc <= ++commit_graft_nr) {
 165                commit_graft_alloc = alloc_nr(commit_graft_alloc);
 166                commit_graft = xrealloc(commit_graft,
 167                                        sizeof(*commit_graft) *
 168                                        commit_graft_alloc);
 169        }
 170        if (pos < commit_graft_nr)
 171                memmove(commit_graft + pos + 1,
 172                        commit_graft + pos,
 173                        (commit_graft_nr - pos - 1) *
 174                        sizeof(*commit_graft));
 175        commit_graft[pos] = graft;
 176        return 0;
 177}
 178
 179struct commit_graft *read_graft_line(char *buf, int len)
 180{
 181        /* The format is just "Commit Parent1 Parent2 ...\n" */
 182        int i;
 183        struct commit_graft *graft = NULL;
 184
 185        if (buf[len-1] == '\n')
 186                buf[--len] = 0;
 187        if (buf[0] == '#' || buf[0] == '\0')
 188                return NULL;
 189        if ((len + 1) % 41) {
 190        bad_graft_data:
 191                error("bad graft data: %s", buf);
 192                free(graft);
 193                return NULL;
 194        }
 195        i = (len + 1) / 41 - 1;
 196        graft = xmalloc(sizeof(*graft) + 20 * i);
 197        graft->nr_parent = i;
 198        if (get_sha1_hex(buf, graft->sha1))
 199                goto bad_graft_data;
 200        for (i = 40; i < len; i += 41) {
 201                if (buf[i] != ' ')
 202                        goto bad_graft_data;
 203                if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
 204                        goto bad_graft_data;
 205        }
 206        return graft;
 207}
 208
 209int read_graft_file(const char *graft_file)
 210{
 211        FILE *fp = fopen(graft_file, "r");
 212        char buf[1024];
 213        if (!fp)
 214                return -1;
 215        while (fgets(buf, sizeof(buf), fp)) {
 216                /* The format is just "Commit Parent1 Parent2 ...\n" */
 217                int len = strlen(buf);
 218                struct commit_graft *graft = read_graft_line(buf, len);
 219                if (!graft)
 220                        continue;
 221                if (register_commit_graft(graft, 1))
 222                        error("duplicate graft data: %s", buf);
 223        }
 224        fclose(fp);
 225        return 0;
 226}
 227
 228static void prepare_commit_graft(void)
 229{
 230        static int commit_graft_prepared;
 231        char *graft_file;
 232
 233        if (commit_graft_prepared)
 234                return;
 235        graft_file = get_graft_file();
 236        read_graft_file(graft_file);
 237        /* make sure shallows are read */
 238        is_repository_shallow();
 239        commit_graft_prepared = 1;
 240}
 241
 242static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
 243{
 244        int pos;
 245        prepare_commit_graft();
 246        pos = commit_graft_pos(sha1);
 247        if (pos < 0)
 248                return NULL;
 249        return commit_graft[pos];
 250}
 251
 252int write_shallow_commits(int fd, int use_pack_protocol)
 253{
 254        int i, count = 0;
 255        for (i = 0; i < commit_graft_nr; i++)
 256                if (commit_graft[i]->nr_parent < 0) {
 257                        const char *hex =
 258                                sha1_to_hex(commit_graft[i]->sha1);
 259                        count++;
 260                        if (use_pack_protocol)
 261                                packet_write(fd, "shallow %s", hex);
 262                        else {
 263                                if (write_in_full(fd, hex,  40) != 40)
 264                                        break;
 265                                if (write_in_full(fd, "\n", 1) != 1)
 266                                        break;
 267                        }
 268                }
 269        return count;
 270}
 271
 272int unregister_shallow(const unsigned char *sha1)
 273{
 274        int pos = commit_graft_pos(sha1);
 275        if (pos < 0)
 276                return -1;
 277        if (pos + 1 < commit_graft_nr)
 278                memcpy(commit_graft + pos, commit_graft + pos + 1,
 279                                sizeof(struct commit_graft *)
 280                                * (commit_graft_nr - pos - 1));
 281        commit_graft_nr--;
 282        return 0;
 283}
 284
 285int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
 286{
 287        char *tail = buffer;
 288        char *bufptr = buffer;
 289        unsigned char parent[20];
 290        struct commit_list **pptr;
 291        struct commit_graft *graft;
 292        unsigned n_refs = 0;
 293
 294        if (item->object.parsed)
 295                return 0;
 296        item->object.parsed = 1;
 297        tail += size;
 298        if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
 299                return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
 300        if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
 301                return error("bad tree pointer in commit %s",
 302                             sha1_to_hex(item->object.sha1));
 303        item->tree = lookup_tree(parent);
 304        if (item->tree)
 305                n_refs++;
 306        bufptr += 46; /* "tree " + "hex sha1" + "\n" */
 307        pptr = &item->parents;
 308
 309        graft = lookup_commit_graft(item->object.sha1);
 310        while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
 311                struct commit *new_parent;
 312
 313                if (tail <= bufptr + 48 ||
 314                    get_sha1_hex(bufptr + 7, parent) ||
 315                    bufptr[47] != '\n')
 316                        return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
 317                bufptr += 48;
 318                if (graft)
 319                        continue;
 320                new_parent = lookup_commit(parent);
 321                if (new_parent) {
 322                        pptr = &commit_list_insert(new_parent, pptr)->next;
 323                        n_refs++;
 324                }
 325        }
 326        if (graft) {
 327                int i;
 328                struct commit *new_parent;
 329                for (i = 0; i < graft->nr_parent; i++) {
 330                        new_parent = lookup_commit(graft->parent[i]);
 331                        if (!new_parent)
 332                                continue;
 333                        pptr = &commit_list_insert(new_parent, pptr)->next;
 334                        n_refs++;
 335                }
 336        }
 337        item->date = parse_commit_date(bufptr);
 338
 339        if (track_object_refs) {
 340                unsigned i = 0;
 341                struct commit_list *p;
 342                struct object_refs *refs = alloc_object_refs(n_refs);
 343                if (item->tree)
 344                        refs->ref[i++] = &item->tree->object;
 345                for (p = item->parents; p; p = p->next)
 346                        refs->ref[i++] = &p->item->object;
 347                set_object_refs(&item->object, refs);
 348        }
 349
 350        return 0;
 351}
 352
 353int parse_commit(struct commit *item)
 354{
 355        enum object_type type;
 356        void *buffer;
 357        unsigned long size;
 358        int ret;
 359
 360        if (item->object.parsed)
 361                return 0;
 362        buffer = read_sha1_file(item->object.sha1, &type, &size);
 363        if (!buffer)
 364                return error("Could not read %s",
 365                             sha1_to_hex(item->object.sha1));
 366        if (type != OBJ_COMMIT) {
 367                free(buffer);
 368                return error("Object %s not a commit",
 369                             sha1_to_hex(item->object.sha1));
 370        }
 371        ret = parse_commit_buffer(item, buffer, size);
 372        if (save_commit_buffer && !ret) {
 373                item->buffer = buffer;
 374                return 0;
 375        }
 376        free(buffer);
 377        return ret;
 378}
 379
 380struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
 381{
 382        struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
 383        new_list->item = item;
 384        new_list->next = *list_p;
 385        *list_p = new_list;
 386        return new_list;
 387}
 388
 389void free_commit_list(struct commit_list *list)
 390{
 391        while (list) {
 392                struct commit_list *temp = list;
 393                list = temp->next;
 394                free(temp);
 395        }
 396}
 397
 398struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
 399{
 400        struct commit_list **pp = list;
 401        struct commit_list *p;
 402        while ((p = *pp) != NULL) {
 403                if (p->item->date < item->date) {
 404                        break;
 405                }
 406                pp = &p->next;
 407        }
 408        return commit_list_insert(item, pp);
 409}
 410
 411        
 412void sort_by_date(struct commit_list **list)
 413{
 414        struct commit_list *ret = NULL;
 415        while (*list) {
 416                insert_by_date((*list)->item, &ret);
 417                *list = (*list)->next;
 418        }
 419        *list = ret;
 420}
 421
 422struct commit *pop_most_recent_commit(struct commit_list **list,
 423                                      unsigned int mark)
 424{
 425        struct commit *ret = (*list)->item;
 426        struct commit_list *parents = ret->parents;
 427        struct commit_list *old = *list;
 428
 429        *list = (*list)->next;
 430        free(old);
 431
 432        while (parents) {
 433                struct commit *commit = parents->item;
 434                parse_commit(commit);
 435                if (!(commit->object.flags & mark)) {
 436                        commit->object.flags |= mark;
 437                        insert_by_date(commit, list);
 438                }
 439                parents = parents->next;
 440        }
 441        return ret;
 442}
 443
 444void clear_commit_marks(struct commit *commit, unsigned int mark)
 445{
 446        struct commit_list *parents;
 447
 448        commit->object.flags &= ~mark;
 449        parents = commit->parents;
 450        while (parents) {
 451                struct commit *parent = parents->item;
 452
 453                /* Have we already cleared this? */
 454                if (mark & parent->object.flags)
 455                        clear_commit_marks(parent, mark);
 456                parents = parents->next;
 457        }
 458}
 459
 460/*
 461 * Generic support for pretty-printing the header
 462 */
 463static int get_one_line(const char *msg, unsigned long len)
 464{
 465        int ret = 0;
 466
 467        while (len--) {
 468                char c = *msg++;
 469                if (!c)
 470                        break;
 471                ret++;
 472                if (c == '\n')
 473                        break;
 474        }
 475        return ret;
 476}
 477
 478/* High bit set, or ISO-2022-INT */
 479static int non_ascii(int ch)
 480{
 481        ch = (ch & 0xff);
 482        return ((ch & 0x80) || (ch == 0x1b));
 483}
 484
 485static int is_rfc2047_special(char ch)
 486{
 487        return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_'));
 488}
 489
 490static int add_rfc2047(char *buf, const char *line, int len,
 491                       const char *encoding)
 492{
 493        char *bp = buf;
 494        int i, needquote;
 495        char q_encoding[128];
 496        const char *q_encoding_fmt = "=?%s?q?";
 497
 498        for (i = needquote = 0; !needquote && i < len; i++) {
 499                int ch = line[i];
 500                if (non_ascii(ch))
 501                        needquote++;
 502                if ((i + 1 < len) &&
 503                    (ch == '=' && line[i+1] == '?'))
 504                        needquote++;
 505        }
 506        if (!needquote)
 507                return sprintf(buf, "%.*s", len, line);
 508
 509        i = snprintf(q_encoding, sizeof(q_encoding), q_encoding_fmt, encoding);
 510        if (sizeof(q_encoding) < i)
 511                die("Insanely long encoding name %s", encoding);
 512        memcpy(bp, q_encoding, i);
 513        bp += i;
 514        for (i = 0; i < len; i++) {
 515                unsigned ch = line[i] & 0xFF;
 516                if (is_rfc2047_special(ch)) {
 517                        sprintf(bp, "=%02X", ch);
 518                        bp += 3;
 519                }
 520                else if (ch == ' ')
 521                        *bp++ = '_';
 522                else
 523                        *bp++ = ch;
 524        }
 525        memcpy(bp, "?=", 2);
 526        bp += 2;
 527        return bp - buf;
 528}
 529
 530static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
 531                         const char *line, int relative_date,
 532                         const char *encoding)
 533{
 534        char *date;
 535        int namelen;
 536        unsigned long time;
 537        int tz, ret;
 538        const char *filler = "    ";
 539
 540        if (fmt == CMIT_FMT_ONELINE)
 541                return 0;
 542        date = strchr(line, '>');
 543        if (!date)
 544                return 0;
 545        namelen = ++date - line;
 546        time = strtoul(date, &date, 10);
 547        tz = strtol(date, NULL, 10);
 548
 549        if (fmt == CMIT_FMT_EMAIL) {
 550                char *name_tail = strchr(line, '<');
 551                int display_name_length;
 552                if (!name_tail)
 553                        return 0;
 554                while (line < name_tail && isspace(name_tail[-1]))
 555                        name_tail--;
 556                display_name_length = name_tail - line;
 557                filler = "";
 558                strcpy(buf, "From: ");
 559                ret = strlen(buf);
 560                ret += add_rfc2047(buf + ret, line, display_name_length,
 561                                   encoding);
 562                memcpy(buf + ret, name_tail, namelen - display_name_length);
 563                ret += namelen - display_name_length;
 564                buf[ret++] = '\n';
 565        }
 566        else {
 567                ret = sprintf(buf, "%s: %.*s%.*s\n", what,
 568                              (fmt == CMIT_FMT_FULLER) ? 4 : 0,
 569                              filler, namelen, line);
 570        }
 571        switch (fmt) {
 572        case CMIT_FMT_MEDIUM:
 573                ret += sprintf(buf + ret, "Date:   %s\n",
 574                               show_date(time, tz, relative_date));
 575                break;
 576        case CMIT_FMT_EMAIL:
 577                ret += sprintf(buf + ret, "Date: %s\n",
 578                               show_rfc2822_date(time, tz));
 579                break;
 580        case CMIT_FMT_FULLER:
 581                ret += sprintf(buf + ret, "%sDate: %s\n", what,
 582                               show_date(time, tz, relative_date));
 583                break;
 584        default:
 585                /* notin' */
 586                break;
 587        }
 588        return ret;
 589}
 590
 591static int is_empty_line(const char *line, int *len_p)
 592{
 593        int len = *len_p;
 594        while (len && isspace(line[len-1]))
 595                len--;
 596        *len_p = len;
 597        return !len;
 598}
 599
 600static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
 601{
 602        struct commit_list *parent = commit->parents;
 603        int offset;
 604
 605        if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
 606            !parent || !parent->next)
 607                return 0;
 608
 609        offset = sprintf(buf, "Merge:");
 610
 611        while (parent) {
 612                struct commit *p = parent->item;
 613                const char *hex = NULL;
 614                const char *dots;
 615                if (abbrev)
 616                        hex = find_unique_abbrev(p->object.sha1, abbrev);
 617                if (!hex)
 618                        hex = sha1_to_hex(p->object.sha1);
 619                dots = (abbrev && strlen(hex) != 40) ?  "..." : "";
 620                parent = parent->next;
 621
 622                offset += sprintf(buf + offset, " %s%s", hex, dots);
 623        }
 624        buf[offset++] = '\n';
 625        return offset;
 626}
 627
 628static char *get_header(const struct commit *commit, const char *key)
 629{
 630        int key_len = strlen(key);
 631        const char *line = commit->buffer;
 632
 633        for (;;) {
 634                const char *eol = strchr(line, '\n'), *next;
 635
 636                if (line == eol)
 637                        return NULL;
 638                if (!eol) {
 639                        eol = line + strlen(line);
 640                        next = NULL;
 641                } else
 642                        next = eol + 1;
 643                if (!strncmp(line, key, key_len) && line[key_len] == ' ') {
 644                        int len = eol - line - key_len;
 645                        char *ret = xmalloc(len);
 646                        memcpy(ret, line + key_len + 1, len - 1);
 647                        ret[len - 1] = '\0';
 648                        return ret;
 649                }
 650                line = next;
 651        }
 652}
 653
 654static char *replace_encoding_header(char *buf, const char *encoding)
 655{
 656        char *encoding_header = strstr(buf, "\nencoding ");
 657        char *header_end = strstr(buf, "\n\n");
 658        char *end_of_encoding_header;
 659        int encoding_header_pos;
 660        int encoding_header_len;
 661        int new_len;
 662        int need_len;
 663        int buflen = strlen(buf) + 1;
 664
 665        if (!header_end)
 666                header_end = buf + buflen;
 667        if (!encoding_header || encoding_header >= header_end)
 668                return buf;
 669        encoding_header++;
 670        end_of_encoding_header = strchr(encoding_header, '\n');
 671        if (!end_of_encoding_header)
 672                return buf; /* should not happen but be defensive */
 673        end_of_encoding_header++;
 674
 675        encoding_header_len = end_of_encoding_header - encoding_header;
 676        encoding_header_pos = encoding_header - buf;
 677
 678        if (is_encoding_utf8(encoding)) {
 679                /* we have re-coded to UTF-8; drop the header */
 680                memmove(encoding_header, end_of_encoding_header,
 681                        buflen - (encoding_header_pos + encoding_header_len));
 682                return buf;
 683        }
 684        new_len = strlen(encoding);
 685        need_len = new_len + strlen("encoding \n");
 686        if (encoding_header_len < need_len) {
 687                buf = xrealloc(buf, buflen + (need_len - encoding_header_len));
 688                encoding_header = buf + encoding_header_pos;
 689                end_of_encoding_header = encoding_header + encoding_header_len;
 690        }
 691        memmove(end_of_encoding_header + (need_len - encoding_header_len),
 692                end_of_encoding_header,
 693                buflen - (encoding_header_pos + encoding_header_len));
 694        memcpy(encoding_header + 9, encoding, strlen(encoding));
 695        encoding_header[9 + new_len] = '\n';
 696        return buf;
 697}
 698
 699static char *logmsg_reencode(const struct commit *commit,
 700                             const char *output_encoding)
 701{
 702        static const char *utf8 = "utf-8";
 703        const char *use_encoding;
 704        char *encoding;
 705        char *out;
 706
 707        if (!*output_encoding)
 708                return NULL;
 709        encoding = get_header(commit, "encoding");
 710        use_encoding = encoding ? encoding : utf8;
 711        if (!strcmp(use_encoding, output_encoding))
 712                out = xstrdup(commit->buffer);
 713        else
 714                out = reencode_string(commit->buffer,
 715                                      output_encoding, use_encoding);
 716        if (out)
 717                out = replace_encoding_header(out, output_encoding);
 718
 719        free(encoding);
 720        return out;
 721}
 722
 723static char *xstrndup(const char *text, int len)
 724{
 725        char *result = xmalloc(len + 1);
 726        memcpy(result, text, len);
 727        result[len] = '\0';
 728        return result;
 729}
 730
 731static void fill_person(struct interp *table, const char *msg, int len)
 732{
 733        int start, end, tz = 0;
 734        unsigned long date;
 735        char *ep;
 736
 737        /* parse name */
 738        for (end = 0; end < len && msg[end] != '<'; end++)
 739                ; /* do nothing */
 740        start = end + 1;
 741        while (end > 0 && isspace(msg[end - 1]))
 742                end--;
 743        table[0].value = xstrndup(msg, end);
 744
 745        if (start >= len)
 746                return;
 747
 748        /* parse email */
 749        for (end = start + 1; end < len && msg[end] != '>'; end++)
 750                ; /* do nothing */
 751
 752        if (end >= len)
 753                return;
 754
 755        table[1].value = xstrndup(msg + start, end - start);
 756
 757        /* parse date */
 758        for (start = end + 1; start < len && isspace(msg[start]); start++)
 759                ; /* do nothing */
 760        if (start >= len)
 761                return;
 762        date = strtoul(msg + start, &ep, 10);
 763        if (msg + start == ep)
 764                return;
 765
 766        table[5].value = xstrndup(msg + start, ep - (msg + start));
 767
 768        /* parse tz */
 769        for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
 770                ; /* do nothing */
 771        if (start + 1 < len) {
 772                tz = strtoul(msg + start + 1, NULL, 10);
 773                if (msg[start] == '-')
 774                        tz = -tz;
 775        }
 776
 777        interp_set_entry(table, 2, show_date(date, tz, 0));
 778        interp_set_entry(table, 3, show_rfc2822_date(date, tz));
 779        interp_set_entry(table, 4, show_date(date, tz, 1));
 780}
 781
 782static long format_commit_message(const struct commit *commit,
 783                const char *msg, char *buf, unsigned long space)
 784{
 785        struct interp table[] = {
 786                { "%H" },       /* commit hash */
 787                { "%h" },       /* abbreviated commit hash */
 788                { "%T" },       /* tree hash */
 789                { "%t" },       /* abbreviated tree hash */
 790                { "%P" },       /* parent hashes */
 791                { "%p" },       /* abbreviated parent hashes */
 792                { "%an" },      /* author name */
 793                { "%ae" },      /* author email */
 794                { "%ad" },      /* author date */
 795                { "%aD" },      /* author date, RFC2822 style */
 796                { "%ar" },      /* author date, relative */
 797                { "%at" },      /* author date, UNIX timestamp */
 798                { "%cn" },      /* committer name */
 799                { "%ce" },      /* committer email */
 800                { "%cd" },      /* committer date */
 801                { "%cD" },      /* committer date, RFC2822 style */
 802                { "%cr" },      /* committer date, relative */
 803                { "%ct" },      /* committer date, UNIX timestamp */
 804                { "%e" },       /* encoding */
 805                { "%s" },       /* subject */
 806                { "%b" },       /* body */
 807                { "%Cred" },    /* red */
 808                { "%Cgreen" },  /* green */
 809                { "%Cblue" },   /* blue */
 810                { "%Creset" },  /* reset color */
 811                { "%n" }        /* newline */
 812        };
 813        enum interp_index {
 814                IHASH = 0, IHASH_ABBREV,
 815                ITREE, ITREE_ABBREV,
 816                IPARENTS, IPARENTS_ABBREV,
 817                IAUTHOR_NAME, IAUTHOR_EMAIL,
 818                IAUTHOR_DATE, IAUTHOR_DATE_RFC2822, IAUTHOR_DATE_RELATIVE,
 819                IAUTHOR_TIMESTAMP,
 820                ICOMMITTER_NAME, ICOMMITTER_EMAIL,
 821                ICOMMITTER_DATE, ICOMMITTER_DATE_RFC2822,
 822                ICOMMITTER_DATE_RELATIVE, ICOMMITTER_TIMESTAMP,
 823                IENCODING,
 824                ISUBJECT,
 825                IBODY,
 826                IRED, IGREEN, IBLUE, IRESET_COLOR,
 827                INEWLINE
 828        };
 829        struct commit_list *p;
 830        char parents[1024];
 831        int i;
 832        enum { HEADER, SUBJECT, BODY } state;
 833
 834        if (INEWLINE + 1 != ARRAY_SIZE(table))
 835                die("invalid interp table!");
 836
 837        /* these are independent of the commit */
 838        interp_set_entry(table, IRED, "\033[31m");
 839        interp_set_entry(table, IGREEN, "\033[32m");
 840        interp_set_entry(table, IBLUE, "\033[34m");
 841        interp_set_entry(table, IRESET_COLOR, "\033[m");
 842        interp_set_entry(table, INEWLINE, "\n");
 843
 844        /* these depend on the commit */
 845        if (!commit->object.parsed)
 846                parse_object(commit->object.sha1);
 847        interp_set_entry(table, IHASH, sha1_to_hex(commit->object.sha1));
 848        interp_set_entry(table, IHASH_ABBREV,
 849                        find_unique_abbrev(commit->object.sha1,
 850                                DEFAULT_ABBREV));
 851        interp_set_entry(table, ITREE, sha1_to_hex(commit->tree->object.sha1));
 852        interp_set_entry(table, ITREE_ABBREV,
 853                        find_unique_abbrev(commit->tree->object.sha1,
 854                                DEFAULT_ABBREV));
 855
 856        parents[1] = 0;
 857        for (i = 0, p = commit->parents;
 858                        p && i < sizeof(parents) - 1;
 859                        p = p->next)
 860                i += snprintf(parents + i, sizeof(parents) - i - 1, " %s",
 861                        sha1_to_hex(p->item->object.sha1));
 862        interp_set_entry(table, IPARENTS, parents + 1);
 863
 864        parents[1] = 0;
 865        for (i = 0, p = commit->parents;
 866                        p && i < sizeof(parents) - 1;
 867                        p = p->next)
 868                i += snprintf(parents + i, sizeof(parents) - i - 1, " %s",
 869                        find_unique_abbrev(p->item->object.sha1,
 870                                DEFAULT_ABBREV));
 871        interp_set_entry(table, IPARENTS_ABBREV, parents + 1);
 872
 873        for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
 874                int eol;
 875                for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
 876                        ; /* do nothing */
 877
 878                if (state == SUBJECT) {
 879                        table[ISUBJECT].value = xstrndup(msg + i, eol - i);
 880                        i = eol;
 881                }
 882                if (i == eol) {
 883                        state++;
 884                        /* strip empty lines */
 885                        while (msg[eol + 1] == '\n')
 886                                eol++;
 887                } else if (!prefixcmp(msg + i, "author "))
 888                        fill_person(table + IAUTHOR_NAME,
 889                                        msg + i + 7, eol - i - 7);
 890                else if (!prefixcmp(msg + i, "committer "))
 891                        fill_person(table + ICOMMITTER_NAME,
 892                                        msg + i + 10, eol - i - 10);
 893                else if (!prefixcmp(msg + i, "encoding "))
 894                        table[IENCODING].value =
 895                                xstrndup(msg + i + 9, eol - i - 9);
 896                i = eol;
 897        }
 898        if (msg[i])
 899                table[IBODY].value = xstrdup(msg + i);
 900        for (i = 0; i < ARRAY_SIZE(table); i++)
 901                if (!table[i].value)
 902                        interp_set_entry(table, i, "<unknown>");
 903
 904        interpolate(buf, space, user_format, table, ARRAY_SIZE(table));
 905        interp_clear_table(table, ARRAY_SIZE(table));
 906
 907        return strlen(buf);
 908}
 909
 910unsigned long pretty_print_commit(enum cmit_fmt fmt,
 911                                  const struct commit *commit,
 912                                  unsigned long len,
 913                                  char *buf, unsigned long space,
 914                                  int abbrev, const char *subject,
 915                                  const char *after_subject,
 916                                  int relative_date)
 917{
 918        int hdr = 1, body = 0, seen_title = 0;
 919        unsigned long offset = 0;
 920        int indent = 4;
 921        int parents_shown = 0;
 922        const char *msg = commit->buffer;
 923        int plain_non_ascii = 0;
 924        char *reencoded;
 925        const char *encoding;
 926
 927        if (fmt == CMIT_FMT_USERFORMAT)
 928                return format_commit_message(commit, msg, buf, space);
 929
 930        encoding = (git_log_output_encoding
 931                    ? git_log_output_encoding
 932                    : git_commit_encoding);
 933        if (!encoding)
 934                encoding = "utf-8";
 935        reencoded = logmsg_reencode(commit, encoding);
 936        if (reencoded)
 937                msg = reencoded;
 938
 939        if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
 940                indent = 0;
 941
 942        /* After-subject is used to pass in Content-Type: multipart
 943         * MIME header; in that case we do not have to do the
 944         * plaintext content type even if the commit message has
 945         * non 7-bit ASCII character.  Otherwise, check if we need
 946         * to say this is not a 7-bit ASCII.
 947         */
 948        if (fmt == CMIT_FMT_EMAIL && !after_subject) {
 949                int i, ch, in_body;
 950
 951                for (in_body = i = 0; (ch = msg[i]) && i < len; i++) {
 952                        if (!in_body) {
 953                                /* author could be non 7-bit ASCII but
 954                                 * the log may be so; skip over the
 955                                 * header part first.
 956                                 */
 957                                if (ch == '\n' &&
 958                                    i + 1 < len && msg[i+1] == '\n')
 959                                        in_body = 1;
 960                        }
 961                        else if (non_ascii(ch)) {
 962                                plain_non_ascii = 1;
 963                                break;
 964                        }
 965                }
 966        }
 967
 968        for (;;) {
 969                const char *line = msg;
 970                int linelen = get_one_line(msg, len);
 971
 972                if (!linelen)
 973                        break;
 974
 975                /*
 976                 * We want some slop for indentation and a possible
 977                 * final "...". Thus the "+ 20".
 978                 */
 979                if (offset + linelen + 20 > space) {
 980                        memcpy(buf + offset, "    ...\n", 8);
 981                        offset += 8;
 982                        break;
 983                }
 984
 985                msg += linelen;
 986                len -= linelen;
 987                if (hdr) {
 988                        if (linelen == 1) {
 989                                hdr = 0;
 990                                if ((fmt != CMIT_FMT_ONELINE) && !subject)
 991                                        buf[offset++] = '\n';
 992                                continue;
 993                        }
 994                        if (fmt == CMIT_FMT_RAW) {
 995                                memcpy(buf + offset, line, linelen);
 996                                offset += linelen;
 997                                continue;
 998                        }
 999                        if (!memcmp(line, "parent ", 7)) {
1000                                if (linelen != 48)
1001                                        die("bad parent line in commit");
1002                                continue;
1003                        }
1004
1005                        if (!parents_shown) {
1006                                offset += add_merge_info(fmt, buf + offset,
1007                                                         commit, abbrev);
1008                                parents_shown = 1;
1009                                continue;
1010                        }
1011                        /*
1012                         * MEDIUM == DEFAULT shows only author with dates.
1013                         * FULL shows both authors but not dates.
1014                         * FULLER shows both authors and dates.
1015                         */
1016                        if (!memcmp(line, "author ", 7))
1017                                offset += add_user_info("Author", fmt,
1018                                                        buf + offset,
1019                                                        line + 7,
1020                                                        relative_date,
1021                                                        encoding);
1022                        if (!memcmp(line, "committer ", 10) &&
1023                            (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
1024                                offset += add_user_info("Commit", fmt,
1025                                                        buf + offset,
1026                                                        line + 10,
1027                                                        relative_date,
1028                                                        encoding);
1029                        continue;
1030                }
1031
1032                if (!subject)
1033                        body = 1;
1034
1035                if (is_empty_line(line, &linelen)) {
1036                        if (!seen_title)
1037                                continue;
1038                        if (!body)
1039                                continue;
1040                        if (subject)
1041                                continue;
1042                        if (fmt == CMIT_FMT_SHORT)
1043                                break;
1044                }
1045
1046                seen_title = 1;
1047                if (subject) {
1048                        int slen = strlen(subject);
1049                        memcpy(buf + offset, subject, slen);
1050                        offset += slen;
1051                        offset += add_rfc2047(buf + offset, line, linelen,
1052                                              encoding);
1053                }
1054                else {
1055                        memset(buf + offset, ' ', indent);
1056                        memcpy(buf + offset + indent, line, linelen);
1057                        offset += linelen + indent;
1058                }
1059                buf[offset++] = '\n';
1060                if (fmt == CMIT_FMT_ONELINE)
1061                        break;
1062                if (subject && plain_non_ascii) {
1063                        int sz;
1064                        char header[512];
1065                        const char *header_fmt =
1066                                "Content-Type: text/plain; charset=%s\n"
1067                                "Content-Transfer-Encoding: 8bit\n";
1068                        sz = snprintf(header, sizeof(header), header_fmt,
1069                                      encoding);
1070                        if (sizeof(header) < sz)
1071                                die("Encoding name %s too long", encoding);
1072                        memcpy(buf + offset, header, sz);
1073                        offset += sz;
1074                }
1075                if (after_subject) {
1076                        int slen = strlen(after_subject);
1077                        if (slen > space - offset - 1)
1078                                slen = space - offset - 1;
1079                        memcpy(buf + offset, after_subject, slen);
1080                        offset += slen;
1081                        after_subject = NULL;
1082                }
1083                subject = NULL;
1084        }
1085        while (offset && isspace(buf[offset-1]))
1086                offset--;
1087        /* Make sure there is an EOLN for the non-oneline case */
1088        if (fmt != CMIT_FMT_ONELINE)
1089                buf[offset++] = '\n';
1090        /*
1091         * make sure there is another EOLN to separate the headers from whatever
1092         * body the caller appends if we haven't already written a body
1093         */
1094        if (fmt == CMIT_FMT_EMAIL && !body)
1095                buf[offset++] = '\n';
1096        buf[offset] = '\0';
1097
1098        free(reencoded);
1099        return offset;
1100}
1101
1102struct commit *pop_commit(struct commit_list **stack)
1103{
1104        struct commit_list *top = *stack;
1105        struct commit *item = top ? top->item : NULL;
1106
1107        if (top) {
1108                *stack = top->next;
1109                free(top);
1110        }
1111        return item;
1112}
1113
1114int count_parents(struct commit * commit)
1115{
1116        int count;
1117        struct commit_list * parents = commit->parents;
1118        for (count = 0; parents; parents = parents->next,count++)
1119                ;
1120        return count;
1121}
1122
1123void topo_sort_default_setter(struct commit *c, void *data)
1124{
1125        c->util = data;
1126}
1127
1128void *topo_sort_default_getter(struct commit *c)
1129{
1130        return c->util;
1131}
1132
1133/*
1134 * Performs an in-place topological sort on the list supplied.
1135 */
1136void sort_in_topological_order(struct commit_list ** list, int lifo)
1137{
1138        sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
1139                                     topo_sort_default_getter);
1140}
1141
1142void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
1143                                  topo_sort_set_fn_t setter,
1144                                  topo_sort_get_fn_t getter)
1145{
1146        struct commit_list * next = *list;
1147        struct commit_list * work = NULL, **insert;
1148        struct commit_list ** pptr = list;
1149        struct sort_node * nodes;
1150        struct sort_node * next_nodes;
1151        int count = 0;
1152
1153        /* determine the size of the list */
1154        while (next) {
1155                next = next->next;
1156                count++;
1157        }
1158        
1159        if (!count)
1160                return;
1161        /* allocate an array to help sort the list */
1162        nodes = xcalloc(count, sizeof(*nodes));
1163        /* link the list to the array */
1164        next_nodes = nodes;
1165        next=*list;
1166        while (next) {
1167                next_nodes->list_item = next;
1168                setter(next->item, next_nodes);
1169                next_nodes++;
1170                next = next->next;
1171        }
1172        /* update the indegree */
1173        next=*list;
1174        while (next) {
1175                struct commit_list * parents = next->item->parents;
1176                while (parents) {
1177                        struct commit * parent=parents->item;
1178                        struct sort_node * pn = (struct sort_node *) getter(parent);
1179
1180                        if (pn)
1181                                pn->indegree++;
1182                        parents=parents->next;
1183                }
1184                next=next->next;
1185        }
1186        /* 
1187         * find the tips
1188         *
1189         * tips are nodes not reachable from any other node in the list 
1190         * 
1191         * the tips serve as a starting set for the work queue.
1192         */
1193        next=*list;
1194        insert = &work;
1195        while (next) {
1196                struct sort_node * node = (struct sort_node *) getter(next->item);
1197
1198                if (node->indegree == 0) {
1199                        insert = &commit_list_insert(next->item, insert)->next;
1200                }
1201                next=next->next;
1202        }
1203
1204        /* process the list in topological order */
1205        if (!lifo)
1206                sort_by_date(&work);
1207        while (work) {
1208                struct commit * work_item = pop_commit(&work);
1209                struct sort_node * work_node = (struct sort_node *) getter(work_item);
1210                struct commit_list * parents = work_item->parents;
1211
1212                while (parents) {
1213                        struct commit * parent=parents->item;
1214                        struct sort_node * pn = (struct sort_node *) getter(parent);
1215
1216                        if (pn) {
1217                                /*
1218                                 * parents are only enqueued for emission 
1219                                 * when all their children have been emitted thereby
1220                                 * guaranteeing topological order.
1221                                 */
1222                                pn->indegree--;
1223                                if (!pn->indegree) {
1224                                        if (!lifo)
1225                                                insert_by_date(parent, &work);
1226                                        else
1227                                                commit_list_insert(parent, &work);
1228                                }
1229                        }
1230                        parents=parents->next;
1231                }
1232                /*
1233                 * work_item is a commit all of whose children
1234                 * have already been emitted. we can emit it now.
1235                 */
1236                *pptr = work_node->list_item;
1237                pptr = &(*pptr)->next;
1238                *pptr = NULL;
1239                setter(work_item, NULL);
1240        }
1241        free(nodes);
1242}
1243
1244/* merge-base stuff */
1245
1246/* bits #0..15 in revision.h */
1247#define PARENT1         (1u<<16)
1248#define PARENT2         (1u<<17)
1249#define STALE           (1u<<18)
1250#define RESULT          (1u<<19)
1251
1252static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
1253
1254static struct commit *interesting(struct commit_list *list)
1255{
1256        while (list) {
1257                struct commit *commit = list->item;
1258                list = list->next;
1259                if (commit->object.flags & STALE)
1260                        continue;
1261                return commit;
1262        }
1263        return NULL;
1264}
1265
1266static struct commit_list *merge_bases(struct commit *one, struct commit *two)
1267{
1268        struct commit_list *list = NULL;
1269        struct commit_list *result = NULL;
1270
1271        if (one == two)
1272                /* We do not mark this even with RESULT so we do not
1273                 * have to clean it up.
1274                 */
1275                return commit_list_insert(one, &result);
1276
1277        parse_commit(one);
1278        parse_commit(two);
1279
1280        one->object.flags |= PARENT1;
1281        two->object.flags |= PARENT2;
1282        insert_by_date(one, &list);
1283        insert_by_date(two, &list);
1284
1285        while (interesting(list)) {
1286                struct commit *commit;
1287                struct commit_list *parents;
1288                struct commit_list *n;
1289                int flags;
1290
1291                commit = list->item;
1292                n = list->next;
1293                free(list);
1294                list = n;
1295
1296                flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
1297                if (flags == (PARENT1 | PARENT2)) {
1298                        if (!(commit->object.flags & RESULT)) {
1299                                commit->object.flags |= RESULT;
1300                                insert_by_date(commit, &result);
1301                        }
1302                        /* Mark parents of a found merge stale */
1303                        flags |= STALE;
1304                }
1305                parents = commit->parents;
1306                while (parents) {
1307                        struct commit *p = parents->item;
1308                        parents = parents->next;
1309                        if ((p->object.flags & flags) == flags)
1310                                continue;
1311                        parse_commit(p);
1312                        p->object.flags |= flags;
1313                        insert_by_date(p, &list);
1314                }
1315        }
1316
1317        /* Clean up the result to remove stale ones */
1318        free_commit_list(list);
1319        list = result; result = NULL;
1320        while (list) {
1321                struct commit_list *n = list->next;
1322                if (!(list->item->object.flags & STALE))
1323                        insert_by_date(list->item, &result);
1324                free(list);
1325                list = n;
1326        }
1327        return result;
1328}
1329
1330struct commit_list *get_merge_bases(struct commit *one,
1331                                    struct commit *two,
1332                                    int cleanup)
1333{
1334        struct commit_list *list;
1335        struct commit **rslt;
1336        struct commit_list *result;
1337        int cnt, i, j;
1338
1339        result = merge_bases(one, two);
1340        if (one == two)
1341                return result;
1342        if (!result || !result->next) {
1343                if (cleanup) {
1344                        clear_commit_marks(one, all_flags);
1345                        clear_commit_marks(two, all_flags);
1346                }
1347                return result;
1348        }
1349
1350        /* There are more than one */
1351        cnt = 0;
1352        list = result;
1353        while (list) {
1354                list = list->next;
1355                cnt++;
1356        }
1357        rslt = xcalloc(cnt, sizeof(*rslt));
1358        for (list = result, i = 0; list; list = list->next)
1359                rslt[i++] = list->item;
1360        free_commit_list(result);
1361
1362        clear_commit_marks(one, all_flags);
1363        clear_commit_marks(two, all_flags);
1364        for (i = 0; i < cnt - 1; i++) {
1365                for (j = i+1; j < cnt; j++) {
1366                        if (!rslt[i] || !rslt[j])
1367                                continue;
1368                        result = merge_bases(rslt[i], rslt[j]);
1369                        clear_commit_marks(rslt[i], all_flags);
1370                        clear_commit_marks(rslt[j], all_flags);
1371                        for (list = result; list; list = list->next) {
1372                                if (rslt[i] == list->item)
1373                                        rslt[i] = NULL;
1374                                if (rslt[j] == list->item)
1375                                        rslt[j] = NULL;
1376                        }
1377                }
1378        }
1379
1380        /* Surviving ones in rslt[] are the independent results */
1381        result = NULL;
1382        for (i = 0; i < cnt; i++) {
1383                if (rslt[i])
1384                        insert_by_date(rslt[i], &result);
1385        }
1386        free(rslt);
1387        return result;
1388}
1389
1390int in_merge_bases(struct commit *commit, struct commit **reference, int num)
1391{
1392        struct commit_list *bases, *b;
1393        int ret = 0;
1394
1395        if (num == 1)
1396                bases = get_merge_bases(commit, *reference, 1);
1397        else
1398                die("not yet");
1399        for (b = bases; b; b = b->next) {
1400                if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
1401                        ret = 1;
1402                        break;
1403                }
1404        }
1405
1406        free_commit_list(bases);
1407        return ret;
1408}