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