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