commit.con commit Merge fixes up to GIT 1.2.2 (709a9e5)
   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
  25enum cmit_fmt get_commit_format(const char *arg)
  26{
  27        if (!*arg)
  28                return CMIT_FMT_DEFAULT;
  29        if (!strcmp(arg, "=raw"))
  30                return CMIT_FMT_RAW;
  31        if (!strcmp(arg, "=medium"))
  32                return CMIT_FMT_MEDIUM;
  33        if (!strcmp(arg, "=short"))
  34                return CMIT_FMT_SHORT;
  35        if (!strcmp(arg, "=full"))
  36                return CMIT_FMT_FULL;
  37        if (!strcmp(arg, "=fuller"))
  38                return CMIT_FMT_FULLER;
  39        if (!strcmp(arg, "=oneline"))
  40                return CMIT_FMT_ONELINE;
  41        die("invalid --pretty format");
  42}
  43
  44static struct commit *check_commit(struct object *obj,
  45                                   const unsigned char *sha1,
  46                                   int quiet)
  47{
  48        if (obj->type != commit_type) {
  49                if (!quiet)
  50                        error("Object %s is a %s, not a commit",
  51                              sha1_to_hex(sha1), obj->type);
  52                return NULL;
  53        }
  54        return (struct commit *) obj;
  55}
  56
  57struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
  58                                              int quiet)
  59{
  60        struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
  61
  62        if (!obj)
  63                return NULL;
  64        return check_commit(obj, sha1, quiet);
  65}
  66
  67struct commit *lookup_commit_reference(const unsigned char *sha1)
  68{
  69        return lookup_commit_reference_gently(sha1, 0);
  70}
  71
  72struct commit *lookup_commit(const unsigned char *sha1)
  73{
  74        struct object *obj = lookup_object(sha1);
  75        if (!obj) {
  76                struct commit *ret = xmalloc(sizeof(struct commit));
  77                memset(ret, 0, sizeof(struct commit));
  78                created_object(sha1, &ret->object);
  79                ret->object.type = commit_type;
  80                return ret;
  81        }
  82        if (!obj->type)
  83                obj->type = commit_type;
  84        return check_commit(obj, sha1, 0);
  85}
  86
  87static unsigned long parse_commit_date(const char *buf)
  88{
  89        unsigned long date;
  90
  91        if (memcmp(buf, "author", 6))
  92                return 0;
  93        while (*buf++ != '\n')
  94                /* nada */;
  95        if (memcmp(buf, "committer", 9))
  96                return 0;
  97        while (*buf++ != '>')
  98                /* nada */;
  99        date = strtoul(buf, NULL, 10);
 100        if (date == ULONG_MAX)
 101                date = 0;
 102        return date;
 103}
 104
 105static struct commit_graft {
 106        unsigned char sha1[20];
 107        int nr_parent;
 108        unsigned char parent[0][20]; /* more */
 109} **commit_graft;
 110static int commit_graft_alloc, commit_graft_nr;
 111
 112static int commit_graft_pos(const unsigned char *sha1)
 113{
 114        int lo, hi;
 115        lo = 0;
 116        hi = commit_graft_nr;
 117        while (lo < hi) {
 118                int mi = (lo + hi) / 2;
 119                struct commit_graft *graft = commit_graft[mi];
 120                int cmp = memcmp(sha1, graft->sha1, 20);
 121                if (!cmp)
 122                        return mi;
 123                if (cmp < 0)
 124                        hi = mi;
 125                else
 126                        lo = mi + 1;
 127        }
 128        return -lo - 1;
 129}
 130
 131static void prepare_commit_graft(void)
 132{
 133        char *graft_file = get_graft_file();
 134        FILE *fp = fopen(graft_file, "r");
 135        char buf[1024];
 136        if (!fp) {
 137                commit_graft = (struct commit_graft **) "hack";
 138                return;
 139        }
 140        while (fgets(buf, sizeof(buf), fp)) {
 141                /* The format is just "Commit Parent1 Parent2 ...\n" */
 142                int len = strlen(buf);
 143                int i;
 144                struct commit_graft *graft = NULL;
 145
 146                if (buf[len-1] == '\n')
 147                        buf[--len] = 0;
 148                if (buf[0] == '#')
 149                        continue;
 150                if ((len + 1) % 41) {
 151                bad_graft_data:
 152                        error("bad graft data: %s", buf);
 153                        free(graft);
 154                        continue;
 155                }
 156                i = (len + 1) / 41 - 1;
 157                graft = xmalloc(sizeof(*graft) + 20 * i);
 158                graft->nr_parent = i;
 159                if (get_sha1_hex(buf, graft->sha1))
 160                        goto bad_graft_data;
 161                for (i = 40; i < len; i += 41) {
 162                        if (buf[i] != ' ')
 163                                goto bad_graft_data;
 164                        if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
 165                                goto bad_graft_data;
 166                }
 167                i = commit_graft_pos(graft->sha1);
 168                if (0 <= i) {
 169                        error("duplicate graft data: %s", buf);
 170                        free(graft);
 171                        continue;
 172                }
 173                i = -i - 1;
 174                if (commit_graft_alloc <= ++commit_graft_nr) {
 175                        commit_graft_alloc = alloc_nr(commit_graft_alloc);
 176                        commit_graft = xrealloc(commit_graft,
 177                                                sizeof(*commit_graft) *
 178                                                commit_graft_alloc);
 179                }
 180                if (i < commit_graft_nr)
 181                        memmove(commit_graft + i + 1,
 182                                commit_graft + i,
 183                                (commit_graft_nr - i - 1) *
 184                                sizeof(*commit_graft));
 185                commit_graft[i] = graft;
 186        }
 187        fclose(fp);
 188}
 189
 190static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
 191{
 192        int pos;
 193        if (!commit_graft)
 194                prepare_commit_graft();
 195        pos = commit_graft_pos(sha1);
 196        if (pos < 0)
 197                return NULL;
 198        return commit_graft[pos];
 199}
 200
 201int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
 202{
 203        char *bufptr = buffer;
 204        unsigned char parent[20];
 205        struct commit_list **pptr;
 206        struct commit_graft *graft;
 207        unsigned n_refs = 0;
 208
 209        if (item->object.parsed)
 210                return 0;
 211        item->object.parsed = 1;
 212        if (memcmp(bufptr, "tree ", 5))
 213                return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
 214        if (get_sha1_hex(bufptr + 5, parent) < 0)
 215                return error("bad tree pointer in commit %s\n", sha1_to_hex(item->object.sha1));
 216        item->tree = lookup_tree(parent);
 217        if (item->tree)
 218                n_refs++;
 219        bufptr += 46; /* "tree " + "hex sha1" + "\n" */
 220        pptr = &item->parents;
 221
 222        graft = lookup_commit_graft(item->object.sha1);
 223        while (!memcmp(bufptr, "parent ", 7)) {
 224                struct commit *new_parent;
 225
 226                if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
 227                        return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
 228                bufptr += 48;
 229                if (graft)
 230                        continue;
 231                new_parent = lookup_commit(parent);
 232                if (new_parent) {
 233                        pptr = &commit_list_insert(new_parent, pptr)->next;
 234                        n_refs++;
 235                }
 236        }
 237        if (graft) {
 238                int i;
 239                struct commit *new_parent;
 240                for (i = 0; i < graft->nr_parent; i++) {
 241                        new_parent = lookup_commit(graft->parent[i]);
 242                        if (!new_parent)
 243                                continue;
 244                        pptr = &commit_list_insert(new_parent, pptr)->next;
 245                        n_refs++;
 246                }
 247        }
 248        item->date = parse_commit_date(bufptr);
 249
 250        if (track_object_refs) {
 251                unsigned i = 0;
 252                struct commit_list *p;
 253                struct object_refs *refs = alloc_object_refs(n_refs);
 254                if (item->tree)
 255                        refs->ref[i++] = &item->tree->object;
 256                for (p = item->parents; p; p = p->next)
 257                        refs->ref[i++] = &p->item->object;
 258                set_object_refs(&item->object, refs);
 259        }
 260
 261        return 0;
 262}
 263
 264int parse_commit(struct commit *item)
 265{
 266        char type[20];
 267        void *buffer;
 268        unsigned long size;
 269        int ret;
 270
 271        if (item->object.parsed)
 272                return 0;
 273        buffer = read_sha1_file(item->object.sha1, type, &size);
 274        if (!buffer)
 275                return error("Could not read %s",
 276                             sha1_to_hex(item->object.sha1));
 277        if (strcmp(type, commit_type)) {
 278                free(buffer);
 279                return error("Object %s not a commit",
 280                             sha1_to_hex(item->object.sha1));
 281        }
 282        ret = parse_commit_buffer(item, buffer, size);
 283        if (save_commit_buffer && !ret) {
 284                item->buffer = buffer;
 285                return 0;
 286        }
 287        free(buffer);
 288        return ret;
 289}
 290
 291struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
 292{
 293        struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
 294        new_list->item = item;
 295        new_list->next = *list_p;
 296        *list_p = new_list;
 297        return new_list;
 298}
 299
 300void free_commit_list(struct commit_list *list)
 301{
 302        while (list) {
 303                struct commit_list *temp = list;
 304                list = temp->next;
 305                free(temp);
 306        }
 307}
 308
 309struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
 310{
 311        struct commit_list **pp = list;
 312        struct commit_list *p;
 313        while ((p = *pp) != NULL) {
 314                if (p->item->date < item->date) {
 315                        break;
 316                }
 317                pp = &p->next;
 318        }
 319        return commit_list_insert(item, pp);
 320}
 321
 322        
 323void sort_by_date(struct commit_list **list)
 324{
 325        struct commit_list *ret = NULL;
 326        while (*list) {
 327                insert_by_date((*list)->item, &ret);
 328                *list = (*list)->next;
 329        }
 330        *list = ret;
 331}
 332
 333struct commit *pop_most_recent_commit(struct commit_list **list,
 334                                      unsigned int mark)
 335{
 336        struct commit *ret = (*list)->item;
 337        struct commit_list *parents = ret->parents;
 338        struct commit_list *old = *list;
 339
 340        *list = (*list)->next;
 341        free(old);
 342
 343        while (parents) {
 344                struct commit *commit = parents->item;
 345                parse_commit(commit);
 346                if (!(commit->object.flags & mark)) {
 347                        commit->object.flags |= mark;
 348                        insert_by_date(commit, list);
 349                }
 350                parents = parents->next;
 351        }
 352        return ret;
 353}
 354
 355void clear_commit_marks(struct commit *commit, unsigned int mark)
 356{
 357        struct commit_list *parents;
 358
 359        parents = commit->parents;
 360        commit->object.flags &= ~mark;
 361        while (parents) {
 362                struct commit *parent = parents->item;
 363                if (parent && parent->object.parsed &&
 364                    (parent->object.flags & mark))
 365                        clear_commit_marks(parent, mark);
 366                parents = parents->next;
 367        }
 368}
 369
 370/*
 371 * Generic support for pretty-printing the header
 372 */
 373static int get_one_line(const char *msg, unsigned long len)
 374{
 375        int ret = 0;
 376
 377        while (len--) {
 378                char c = *msg++;
 379                ret++;
 380                if (c == '\n')
 381                        break;
 382                if (!c)
 383                        return 0;
 384        }
 385        return ret;
 386}
 387
 388static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const char *line)
 389{
 390        char *date;
 391        int namelen;
 392        unsigned long time;
 393        int tz, ret;
 394        const char *filler = "    ";
 395
 396        if (fmt == CMIT_FMT_ONELINE)
 397                return 0;
 398        date = strchr(line, '>');
 399        if (!date)
 400                return 0;
 401        namelen = ++date - line;
 402        time = strtoul(date, &date, 10);
 403        tz = strtol(date, NULL, 10);
 404
 405        ret = sprintf(buf, "%s: %.*s%.*s\n", what,
 406                      (fmt == CMIT_FMT_FULLER) ? 4 : 0,
 407                      filler, namelen, line);
 408        switch (fmt) {
 409        case CMIT_FMT_MEDIUM:
 410                ret += sprintf(buf + ret, "Date:   %s\n", show_date(time, tz));
 411                break;
 412        case CMIT_FMT_FULLER:
 413                ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
 414                break;
 415        default:
 416                /* notin' */
 417                break;
 418        }
 419        return ret;
 420}
 421
 422static int is_empty_line(const char *line, int len)
 423{
 424        while (len && isspace(line[len-1]))
 425                len--;
 426        return !len;
 427}
 428
 429static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
 430{
 431        struct commit_list *parent = commit->parents;
 432        int offset;
 433
 434        if ((fmt == CMIT_FMT_ONELINE) || !parent || !parent->next)
 435                return 0;
 436
 437        offset = sprintf(buf, "Merge:");
 438
 439        while (parent) {
 440                struct commit *p = parent->item;
 441                const char *hex = abbrev
 442                        ? find_unique_abbrev(p->object.sha1, abbrev)
 443                        : sha1_to_hex(p->object.sha1);
 444                char *dots = (abbrev && strlen(hex) != 40) ? "..." : "";
 445                parent = parent->next;
 446
 447                offset += sprintf(buf + offset, " %s%s", hex, dots);
 448        }
 449        buf[offset++] = '\n';
 450        return offset;
 451}
 452
 453unsigned long pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, unsigned long len, char *buf, unsigned long space, int abbrev)
 454{
 455        int hdr = 1, body = 0;
 456        unsigned long offset = 0;
 457        int indent = (fmt == CMIT_FMT_ONELINE) ? 0 : 4;
 458        int parents_shown = 0;
 459        const char *msg = commit->buffer;
 460
 461        for (;;) {
 462                const char *line = msg;
 463                int linelen = get_one_line(msg, len);
 464
 465                if (!linelen)
 466                        break;
 467
 468                /*
 469                 * We want some slop for indentation and a possible
 470                 * final "...". Thus the "+ 20".
 471                 */
 472                if (offset + linelen + 20 > space) {
 473                        memcpy(buf + offset, "    ...\n", 8);
 474                        offset += 8;
 475                        break;
 476                }
 477
 478                msg += linelen;
 479                len -= linelen;
 480                if (hdr) {
 481                        if (linelen == 1) {
 482                                hdr = 0;
 483                                if (fmt != CMIT_FMT_ONELINE)
 484                                        buf[offset++] = '\n';
 485                                continue;
 486                        }
 487                        if (fmt == CMIT_FMT_RAW) {
 488                                memcpy(buf + offset, line, linelen);
 489                                offset += linelen;
 490                                continue;
 491                        }
 492                        if (!memcmp(line, "parent ", 7)) {
 493                                if (linelen != 48)
 494                                        die("bad parent line in commit");
 495                                continue;
 496                        }
 497
 498                        if (!parents_shown) {
 499                                offset += add_merge_info(fmt, buf + offset,
 500                                                         commit, abbrev);
 501                                parents_shown = 1;
 502                                continue;
 503                        }
 504                        /*
 505                         * MEDIUM == DEFAULT shows only author with dates.
 506                         * FULL shows both authors but not dates.
 507                         * FULLER shows both authors and dates.
 508                         */
 509                        if (!memcmp(line, "author ", 7))
 510                                offset += add_user_info("Author", fmt,
 511                                                        buf + offset,
 512                                                        line + 7);
 513                        if (!memcmp(line, "committer ", 10) &&
 514                            (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
 515                                offset += add_user_info("Commit", fmt,
 516                                                        buf + offset,
 517                                                        line + 10);
 518                        continue;
 519                }
 520
 521                if (is_empty_line(line, linelen)) {
 522                        if (!body)
 523                                continue;
 524                        if (fmt == CMIT_FMT_SHORT)
 525                                break;
 526                } else {
 527                        body = 1;
 528                }
 529
 530                memset(buf + offset, ' ', indent);
 531                memcpy(buf + offset + indent, line, linelen);
 532                offset += linelen + indent;
 533                if (fmt == CMIT_FMT_ONELINE)
 534                        break;
 535        }
 536        if (fmt == CMIT_FMT_ONELINE) {
 537                /* We do not want the terminating newline */
 538                if (buf[offset - 1] == '\n')
 539                        offset--;
 540        }
 541        else {
 542                /* Make sure there is an EOLN */
 543                if (buf[offset - 1] != '\n')
 544                        buf[offset++] = '\n';
 545        }
 546        buf[offset] = '\0';
 547        return offset;
 548}
 549
 550struct commit *pop_commit(struct commit_list **stack)
 551{
 552        struct commit_list *top = *stack;
 553        struct commit *item = top ? top->item : NULL;
 554
 555        if (top) {
 556                *stack = top->next;
 557                free(top);
 558        }
 559        return item;
 560}
 561
 562int count_parents(struct commit * commit)
 563{
 564        int count = 0;
 565        struct commit_list * parents = commit->parents;
 566        for (count=0;parents; parents=parents->next,count++)
 567          ;
 568        return count;
 569}
 570
 571/*
 572 * Performs an in-place topological sort on the list supplied.
 573 */
 574void sort_in_topological_order(struct commit_list ** list, int lifo)
 575{
 576        struct commit_list * next = *list;
 577        struct commit_list * work = NULL, **insert;
 578        struct commit_list ** pptr = list;
 579        struct sort_node * nodes;
 580        struct sort_node * next_nodes;
 581        int count = 0;
 582
 583        /* determine the size of the list */
 584        while (next) {
 585                next = next->next;
 586                count++;
 587        }
 588        
 589        if (!count)
 590                return;
 591        /* allocate an array to help sort the list */
 592        nodes = xcalloc(count, sizeof(*nodes));
 593        /* link the list to the array */
 594        next_nodes = nodes;
 595        next=*list;
 596        while (next) {
 597                next_nodes->list_item = next;
 598                next->item->object.util = next_nodes;
 599                next_nodes++;
 600                next = next->next;
 601        }
 602        /* update the indegree */
 603        next=*list;
 604        while (next) {
 605                struct commit_list * parents = next->item->parents;
 606                while (parents) {
 607                        struct commit * parent=parents->item;
 608                        struct sort_node * pn = (struct sort_node *)parent->object.util;
 609                        
 610                        if (pn)
 611                                pn->indegree++;
 612                        parents=parents->next;
 613                }
 614                next=next->next;
 615        }
 616        /* 
 617         * find the tips
 618         *
 619         * tips are nodes not reachable from any other node in the list 
 620         * 
 621         * the tips serve as a starting set for the work queue.
 622         */
 623        next=*list;
 624        insert = &work;
 625        while (next) {
 626                struct sort_node * node = (struct sort_node *)next->item->object.util;
 627
 628                if (node->indegree == 0) {
 629                        insert = &commit_list_insert(next->item, insert)->next;
 630                }
 631                next=next->next;
 632        }
 633
 634        /* process the list in topological order */
 635        if (!lifo)
 636                sort_by_date(&work);
 637        while (work) {
 638                struct commit * work_item = pop_commit(&work);
 639                struct sort_node * work_node = (struct sort_node *)work_item->object.util;
 640                struct commit_list * parents = work_item->parents;
 641
 642                while (parents) {
 643                        struct commit * parent=parents->item;
 644                        struct sort_node * pn = (struct sort_node *)parent->object.util;
 645                        
 646                        if (pn) {
 647                                /* 
 648                                 * parents are only enqueued for emission 
 649                                 * when all their children have been emitted thereby
 650                                 * guaranteeing topological order.
 651                                 */
 652                                pn->indegree--;
 653                                if (!pn->indegree) {
 654                                        if (!lifo)
 655                                                insert_by_date(parent, &work);
 656                                        else
 657                                                commit_list_insert(parent, &work);
 658                                }
 659                        }
 660                        parents=parents->next;
 661                }
 662                /*
 663                 * work_item is a commit all of whose children
 664                 * have already been emitted. we can emit it now.
 665                 */
 666                *pptr = work_node->list_item;
 667                pptr = &(*pptr)->next;
 668                *pptr = NULL;
 669                work_item->object.util = NULL;
 670        }
 671        free(nodes);
 672}