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