revision.con commit Merge branch 'jc/lt-tree-n-cache-tree' into next (5029f64)
   1#include "cache.h"
   2#include "tag.h"
   3#include "blob.h"
   4#include "tree.h"
   5#include "commit.h"
   6#include "diff.h"
   7#include "refs.h"
   8#include "revision.h"
   9
  10static char *path_name(struct name_path *path, const char *name)
  11{
  12        struct name_path *p;
  13        char *n, *m;
  14        int nlen = strlen(name);
  15        int len = nlen + 1;
  16
  17        for (p = path; p; p = p->up) {
  18                if (p->elem_len)
  19                        len += p->elem_len + 1;
  20        }
  21        n = xmalloc(len);
  22        m = n + len - (nlen + 1);
  23        strcpy(m, name);
  24        for (p = path; p; p = p->up) {
  25                if (p->elem_len) {
  26                        m -= p->elem_len + 1;
  27                        memcpy(m, p->elem, p->elem_len);
  28                        m[p->elem_len] = '/';
  29                }
  30        }
  31        return n;
  32}
  33
  34struct object_list **add_object(struct object *obj,
  35                                       struct object_list **p,
  36                                       struct name_path *path,
  37                                       const char *name)
  38{
  39        struct object_list *entry = xmalloc(sizeof(*entry));
  40        entry->item = obj;
  41        entry->next = *p;
  42        entry->name = path_name(path, name);
  43        *p = entry;
  44        return &entry->next;
  45}
  46
  47static void mark_blob_uninteresting(struct blob *blob)
  48{
  49        if (blob->object.flags & UNINTERESTING)
  50                return;
  51        blob->object.flags |= UNINTERESTING;
  52}
  53
  54void mark_tree_uninteresting(struct tree *tree)
  55{
  56        struct tree_desc desc;
  57        struct object *obj = &tree->object;
  58
  59        if (obj->flags & UNINTERESTING)
  60                return;
  61        obj->flags |= UNINTERESTING;
  62        if (!has_sha1_file(obj->sha1))
  63                return;
  64        if (parse_tree(tree) < 0)
  65                die("bad tree %s", sha1_to_hex(obj->sha1));
  66
  67        desc.buf = tree->buffer;
  68        desc.size = tree->size;
  69        while (desc.size) {
  70                unsigned mode;
  71                const char *name;
  72                const unsigned char *sha1;
  73
  74                sha1 = tree_entry_extract(&desc, &name, &mode);
  75                update_tree_entry(&desc);
  76
  77                if (S_ISDIR(mode))
  78                        mark_tree_uninteresting(lookup_tree(sha1));
  79                else
  80                        mark_blob_uninteresting(lookup_blob(sha1));
  81        }
  82
  83        /*
  84         * We don't care about the tree any more
  85         * after it has been marked uninteresting.
  86         */
  87        free(tree->buffer);
  88        tree->buffer = NULL;
  89}
  90
  91void mark_parents_uninteresting(struct commit *commit)
  92{
  93        struct commit_list *parents = commit->parents;
  94
  95        while (parents) {
  96                struct commit *commit = parents->item;
  97                if (!(commit->object.flags & UNINTERESTING)) {
  98                        commit->object.flags |= UNINTERESTING;
  99
 100                        /*
 101                         * Normally we haven't parsed the parent
 102                         * yet, so we won't have a parent of a parent
 103                         * here. However, it may turn out that we've
 104                         * reached this commit some other way (where it
 105                         * wasn't uninteresting), in which case we need
 106                         * to mark its parents recursively too..
 107                         */
 108                        if (commit->parents)
 109                                mark_parents_uninteresting(commit);
 110                }
 111
 112                /*
 113                 * A missing commit is ok iff its parent is marked
 114                 * uninteresting.
 115                 *
 116                 * We just mark such a thing parsed, so that when
 117                 * it is popped next time around, we won't be trying
 118                 * to parse it and get an error.
 119                 */
 120                if (!has_sha1_file(commit->object.sha1))
 121                        commit->object.parsed = 1;
 122                parents = parents->next;
 123        }
 124}
 125
 126static void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
 127{
 128        add_object(obj, &revs->pending_objects, NULL, name);
 129}
 130
 131static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
 132{
 133        struct object *object;
 134
 135        object = parse_object(sha1);
 136        if (!object)
 137                die("bad object %s", name);
 138        object->flags |= flags;
 139        return object;
 140}
 141
 142static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
 143{
 144        unsigned long flags = object->flags;
 145
 146        /*
 147         * Tag object? Look what it points to..
 148         */
 149        while (object->type == tag_type) {
 150                struct tag *tag = (struct tag *) object;
 151                if (revs->tag_objects && !(flags & UNINTERESTING))
 152                        add_pending_object(revs, object, tag->tag);
 153                object = parse_object(tag->tagged->sha1);
 154                if (!object)
 155                        die("bad object %s", sha1_to_hex(tag->tagged->sha1));
 156        }
 157
 158        /*
 159         * Commit object? Just return it, we'll do all the complex
 160         * reachability crud.
 161         */
 162        if (object->type == commit_type) {
 163                struct commit *commit = (struct commit *)object;
 164                if (parse_commit(commit) < 0)
 165                        die("unable to parse commit %s", name);
 166                if (flags & UNINTERESTING) {
 167                        commit->object.flags |= UNINTERESTING;
 168                        mark_parents_uninteresting(commit);
 169                        revs->limited = 1;
 170                }
 171                return commit;
 172        }
 173
 174        /*
 175         * Tree object? Either mark it uniniteresting, or add it
 176         * to the list of objects to look at later..
 177         */
 178        if (object->type == tree_type) {
 179                struct tree *tree = (struct tree *)object;
 180                if (!revs->tree_objects)
 181                        return NULL;
 182                if (flags & UNINTERESTING) {
 183                        mark_tree_uninteresting(tree);
 184                        return NULL;
 185                }
 186                add_pending_object(revs, object, "");
 187                return NULL;
 188        }
 189
 190        /*
 191         * Blob object? You know the drill by now..
 192         */
 193        if (object->type == blob_type) {
 194                struct blob *blob = (struct blob *)object;
 195                if (!revs->blob_objects)
 196                        return NULL;
 197                if (flags & UNINTERESTING) {
 198                        mark_blob_uninteresting(blob);
 199                        return NULL;
 200                }
 201                add_pending_object(revs, object, "");
 202                return NULL;
 203        }
 204        die("%s is unknown object", name);
 205}
 206
 207static int everybody_uninteresting(struct commit_list *orig)
 208{
 209        struct commit_list *list = orig;
 210        while (list) {
 211                struct commit *commit = list->item;
 212                list = list->next;
 213                if (commit->object.flags & UNINTERESTING)
 214                        continue;
 215                return 0;
 216        }
 217        return 1;
 218}
 219
 220static int tree_difference = REV_TREE_SAME;
 221
 222static void file_add_remove(struct diff_options *options,
 223                    int addremove, unsigned mode,
 224                    const unsigned char *sha1,
 225                    const char *base, const char *path)
 226{
 227        int diff = REV_TREE_DIFFERENT;
 228
 229        /*
 230         * Is it an add of a new file? It means that the old tree
 231         * didn't have it at all, so we will turn "REV_TREE_SAME" ->
 232         * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
 233         * (and if it already was "REV_TREE_NEW", we'll keep it
 234         * "REV_TREE_NEW" of course).
 235         */
 236        if (addremove == '+') {
 237                diff = tree_difference;
 238                if (diff != REV_TREE_SAME)
 239                        return;
 240                diff = REV_TREE_NEW;
 241        }
 242        tree_difference = diff;
 243}
 244
 245static void file_change(struct diff_options *options,
 246                 unsigned old_mode, unsigned new_mode,
 247                 const unsigned char *old_sha1,
 248                 const unsigned char *new_sha1,
 249                 const char *base, const char *path)
 250{
 251        tree_difference = REV_TREE_DIFFERENT;
 252}
 253
 254int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
 255{
 256        if (!t1)
 257                return REV_TREE_NEW;
 258        if (!t2)
 259                return REV_TREE_DIFFERENT;
 260        tree_difference = REV_TREE_SAME;
 261        if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
 262                           &revs->pruning) < 0)
 263                return REV_TREE_DIFFERENT;
 264        return tree_difference;
 265}
 266
 267int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
 268{
 269        int retval;
 270        void *tree;
 271        struct tree_desc empty, real;
 272
 273        if (!t1)
 274                return 0;
 275
 276        tree = read_object_with_reference(t1->object.sha1, tree_type, &real.size, NULL);
 277        if (!tree)
 278                return 0;
 279        real.buf = tree;
 280
 281        empty.buf = "";
 282        empty.size = 0;
 283
 284        tree_difference = 0;
 285        retval = diff_tree(&empty, &real, "", &revs->pruning);
 286        free(tree);
 287
 288        return retval >= 0 && !tree_difference;
 289}
 290
 291static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
 292{
 293        struct commit_list **pp, *parent;
 294        int tree_changed = 0;
 295
 296        if (!commit->tree)
 297                return;
 298
 299        if (!commit->parents) {
 300                if (!rev_same_tree_as_empty(revs, commit->tree))
 301                        commit->object.flags |= TREECHANGE;
 302                return;
 303        }
 304
 305        pp = &commit->parents;
 306        while ((parent = *pp) != NULL) {
 307                struct commit *p = parent->item;
 308
 309                parse_commit(p);
 310                switch (rev_compare_tree(revs, p->tree, commit->tree)) {
 311                case REV_TREE_SAME:
 312                        if (p->object.flags & UNINTERESTING) {
 313                                /* Even if a merge with an uninteresting
 314                                 * side branch brought the entire change
 315                                 * we are interested in, we do not want
 316                                 * to lose the other branches of this
 317                                 * merge, so we just keep going.
 318                                 */
 319                                pp = &parent->next;
 320                                continue;
 321                        }
 322                        parent->next = NULL;
 323                        commit->parents = parent;
 324                        return;
 325
 326                case REV_TREE_NEW:
 327                        if (revs->remove_empty_trees &&
 328                            rev_same_tree_as_empty(revs, p->tree)) {
 329                                /* We are adding all the specified
 330                                 * paths from this parent, so the
 331                                 * history beyond this parent is not
 332                                 * interesting.  Remove its parents
 333                                 * (they are grandparents for us).
 334                                 * IOW, we pretend this parent is a
 335                                 * "root" commit.
 336                                 */
 337                                parse_commit(p);
 338                                p->parents = NULL;
 339                        }
 340                /* fallthrough */
 341                case REV_TREE_DIFFERENT:
 342                        tree_changed = 1;
 343                        pp = &parent->next;
 344                        continue;
 345                }
 346                die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
 347        }
 348        if (tree_changed)
 349                commit->object.flags |= TREECHANGE;
 350}
 351
 352static void add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
 353{
 354        struct commit_list *parent = commit->parents;
 355
 356        if (commit->object.flags & ADDED)
 357                return;
 358        commit->object.flags |= ADDED;
 359
 360        /*
 361         * If the commit is uninteresting, don't try to
 362         * prune parents - we want the maximal uninteresting
 363         * set.
 364         *
 365         * Normally we haven't parsed the parent
 366         * yet, so we won't have a parent of a parent
 367         * here. However, it may turn out that we've
 368         * reached this commit some other way (where it
 369         * wasn't uninteresting), in which case we need
 370         * to mark its parents recursively too..
 371         */
 372        if (commit->object.flags & UNINTERESTING) {
 373                while (parent) {
 374                        struct commit *p = parent->item;
 375                        parent = parent->next;
 376                        parse_commit(p);
 377                        p->object.flags |= UNINTERESTING;
 378                        if (p->parents)
 379                                mark_parents_uninteresting(p);
 380                        if (p->object.flags & SEEN)
 381                                continue;
 382                        p->object.flags |= SEEN;
 383                        insert_by_date(p, list);
 384                }
 385                return;
 386        }
 387
 388        /*
 389         * Ok, the commit wasn't uninteresting. Try to
 390         * simplify the commit history and find the parent
 391         * that has no differences in the path set if one exists.
 392         */
 393        if (revs->prune_fn)
 394                revs->prune_fn(revs, commit);
 395
 396        if (revs->no_walk)
 397                return;
 398
 399        parent = commit->parents;
 400        while (parent) {
 401                struct commit *p = parent->item;
 402
 403                parent = parent->next;
 404
 405                parse_commit(p);
 406                if (p->object.flags & SEEN)
 407                        continue;
 408                p->object.flags |= SEEN;
 409                insert_by_date(p, list);
 410        }
 411}
 412
 413static void limit_list(struct rev_info *revs)
 414{
 415        struct commit_list *list = revs->commits;
 416        struct commit_list *newlist = NULL;
 417        struct commit_list **p = &newlist;
 418
 419        while (list) {
 420                struct commit_list *entry = list;
 421                struct commit *commit = list->item;
 422                struct object *obj = &commit->object;
 423
 424                list = list->next;
 425                free(entry);
 426
 427                if (revs->max_age != -1 && (commit->date < revs->max_age))
 428                        obj->flags |= UNINTERESTING;
 429                if (revs->unpacked && has_sha1_pack(obj->sha1))
 430                        obj->flags |= UNINTERESTING;
 431                add_parents_to_list(revs, commit, &list);
 432                if (obj->flags & UNINTERESTING) {
 433                        mark_parents_uninteresting(commit);
 434                        if (everybody_uninteresting(list))
 435                                break;
 436                        continue;
 437                }
 438                if (revs->min_age != -1 && (commit->date > revs->min_age))
 439                        continue;
 440                p = &commit_list_insert(commit, p)->next;
 441        }
 442        if (revs->boundary) {
 443                /* mark the ones that are on the result list first */
 444                for (list = newlist; list; list = list->next) {
 445                        struct commit *commit = list->item;
 446                        commit->object.flags |= TMP_MARK;
 447                }
 448                for (list = newlist; list; list = list->next) {
 449                        struct commit *commit = list->item;
 450                        struct object *obj = &commit->object;
 451                        struct commit_list *parent;
 452                        if (obj->flags & UNINTERESTING)
 453                                continue;
 454                        for (parent = commit->parents;
 455                             parent;
 456                             parent = parent->next) {
 457                                struct commit *pcommit = parent->item;
 458                                if (!(pcommit->object.flags & UNINTERESTING))
 459                                        continue;
 460                                pcommit->object.flags |= BOUNDARY;
 461                                if (pcommit->object.flags & TMP_MARK)
 462                                        continue;
 463                                pcommit->object.flags |= TMP_MARK;
 464                                p = &commit_list_insert(pcommit, p)->next;
 465                        }
 466                }
 467                for (list = newlist; list; list = list->next) {
 468                        struct commit *commit = list->item;
 469                        commit->object.flags &= ~TMP_MARK;
 470                }
 471        }
 472        revs->commits = newlist;
 473}
 474
 475static int all_flags;
 476static struct rev_info *all_revs;
 477
 478static int handle_one_ref(const char *path, const unsigned char *sha1)
 479{
 480        struct object *object = get_reference(all_revs, path, sha1, all_flags);
 481        add_pending_object(all_revs, object, "");
 482        return 0;
 483}
 484
 485static void handle_all(struct rev_info *revs, unsigned flags)
 486{
 487        all_revs = revs;
 488        all_flags = flags;
 489        for_each_ref(handle_one_ref);
 490}
 491
 492static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
 493{
 494        unsigned char sha1[20];
 495        struct object *it;
 496        struct commit *commit;
 497        struct commit_list *parents;
 498
 499        if (*arg == '^') {
 500                flags ^= UNINTERESTING;
 501                arg++;
 502        }
 503        if (get_sha1(arg, sha1))
 504                return 0;
 505        while (1) {
 506                it = get_reference(revs, arg, sha1, 0);
 507                if (strcmp(it->type, tag_type))
 508                        break;
 509                memcpy(sha1, ((struct tag*)it)->tagged->sha1, 20);
 510        }
 511        if (strcmp(it->type, commit_type))
 512                return 0;
 513        commit = (struct commit *)it;
 514        for (parents = commit->parents; parents; parents = parents->next) {
 515                it = &parents->item->object;
 516                it->flags |= flags;
 517                add_pending_object(revs, it, arg);
 518        }
 519        return 1;
 520}
 521
 522void init_revisions(struct rev_info *revs)
 523{
 524        memset(revs, 0, sizeof(*revs));
 525
 526        revs->abbrev = DEFAULT_ABBREV;
 527        revs->ignore_merges = 1;
 528        revs->pruning.recursive = 1;
 529        revs->pruning.add_remove = file_add_remove;
 530        revs->pruning.change = file_change;
 531        revs->lifo = 1;
 532        revs->dense = 1;
 533        revs->prefix = setup_git_directory();
 534        revs->max_age = -1;
 535        revs->min_age = -1;
 536        revs->max_count = -1;
 537
 538        revs->prune_fn = NULL;
 539        revs->prune_data = NULL;
 540
 541        revs->topo_setter = topo_sort_default_setter;
 542        revs->topo_getter = topo_sort_default_getter;
 543
 544        revs->commit_format = CMIT_FMT_DEFAULT;
 545
 546        diff_setup(&revs->diffopt);
 547}
 548
 549/*
 550 * Parse revision information, filling in the "rev_info" structure,
 551 * and removing the used arguments from the argument list.
 552 *
 553 * Returns the number of arguments left that weren't recognized
 554 * (which are also moved to the head of the argument list)
 555 */
 556int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
 557{
 558        int i, flags, seen_dashdash;
 559        const char **unrecognized = argv + 1;
 560        int left = 1;
 561
 562        /* First, search for "--" */
 563        seen_dashdash = 0;
 564        for (i = 1; i < argc; i++) {
 565                const char *arg = argv[i];
 566                if (strcmp(arg, "--"))
 567                        continue;
 568                argv[i] = NULL;
 569                argc = i;
 570                revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
 571                seen_dashdash = 1;
 572                break;
 573        }
 574
 575        flags = 0;
 576        for (i = 1; i < argc; i++) {
 577                struct object *object;
 578                const char *arg = argv[i];
 579                unsigned char sha1[20];
 580                char *dotdot;
 581                int local_flags;
 582
 583                if (*arg == '-') {
 584                        int opts;
 585                        if (!strncmp(arg, "--max-count=", 12)) {
 586                                revs->max_count = atoi(arg + 12);
 587                                continue;
 588                        }
 589                        /* accept -<digit>, like traditional "head" */
 590                        if ((*arg == '-') && isdigit(arg[1])) {
 591                                revs->max_count = atoi(arg + 1);
 592                                continue;
 593                        }
 594                        if (!strcmp(arg, "-n")) {
 595                                if (argc <= i + 1)
 596                                        die("-n requires an argument");
 597                                revs->max_count = atoi(argv[++i]);
 598                                continue;
 599                        }
 600                        if (!strncmp(arg,"-n",2)) {
 601                                revs->max_count = atoi(arg + 2);
 602                                continue;
 603                        }
 604                        if (!strncmp(arg, "--max-age=", 10)) {
 605                                revs->max_age = atoi(arg + 10);
 606                                continue;
 607                        }
 608                        if (!strncmp(arg, "--since=", 8)) {
 609                                revs->max_age = approxidate(arg + 8);
 610                                continue;
 611                        }
 612                        if (!strncmp(arg, "--after=", 8)) {
 613                                revs->max_age = approxidate(arg + 8);
 614                                continue;
 615                        }
 616                        if (!strncmp(arg, "--min-age=", 10)) {
 617                                revs->min_age = atoi(arg + 10);
 618                                continue;
 619                        }
 620                        if (!strncmp(arg, "--before=", 9)) {
 621                                revs->min_age = approxidate(arg + 9);
 622                                continue;
 623                        }
 624                        if (!strncmp(arg, "--until=", 8)) {
 625                                revs->min_age = approxidate(arg + 8);
 626                                continue;
 627                        }
 628                        if (!strcmp(arg, "--all")) {
 629                                handle_all(revs, flags);
 630                                continue;
 631                        }
 632                        if (!strcmp(arg, "--not")) {
 633                                flags ^= UNINTERESTING;
 634                                continue;
 635                        }
 636                        if (!strcmp(arg, "--default")) {
 637                                if (++i >= argc)
 638                                        die("bad --default argument");
 639                                def = argv[i];
 640                                continue;
 641                        }
 642                        if (!strcmp(arg, "--topo-order")) {
 643                                revs->topo_order = 1;
 644                                continue;
 645                        }
 646                        if (!strcmp(arg, "--date-order")) {
 647                                revs->lifo = 0;
 648                                revs->topo_order = 1;
 649                                continue;
 650                        }
 651                        if (!strcmp(arg, "--parents")) {
 652                                revs->parents = 1;
 653                                continue;
 654                        }
 655                        if (!strcmp(arg, "--dense")) {
 656                                revs->dense = 1;
 657                                continue;
 658                        }
 659                        if (!strcmp(arg, "--sparse")) {
 660                                revs->dense = 0;
 661                                continue;
 662                        }
 663                        if (!strcmp(arg, "--remove-empty")) {
 664                                revs->remove_empty_trees = 1;
 665                                continue;
 666                        }
 667                        if (!strcmp(arg, "--no-merges")) {
 668                                revs->no_merges = 1;
 669                                continue;
 670                        }
 671                        if (!strcmp(arg, "--boundary")) {
 672                                revs->boundary = 1;
 673                                continue;
 674                        }
 675                        if (!strcmp(arg, "--objects")) {
 676                                revs->tag_objects = 1;
 677                                revs->tree_objects = 1;
 678                                revs->blob_objects = 1;
 679                                continue;
 680                        }
 681                        if (!strcmp(arg, "--objects-edge")) {
 682                                revs->tag_objects = 1;
 683                                revs->tree_objects = 1;
 684                                revs->blob_objects = 1;
 685                                revs->edge_hint = 1;
 686                                continue;
 687                        }
 688                        if (!strcmp(arg, "--unpacked")) {
 689                                revs->unpacked = 1;
 690                                continue;
 691                        }
 692                        if (!strcmp(arg, "-r")) {
 693                                revs->diff = 1;
 694                                revs->diffopt.recursive = 1;
 695                                continue;
 696                        }
 697                        if (!strcmp(arg, "-t")) {
 698                                revs->diff = 1;
 699                                revs->diffopt.recursive = 1;
 700                                revs->diffopt.tree_in_recursive = 1;
 701                                continue;
 702                        }
 703                        if (!strcmp(arg, "-m")) {
 704                                revs->ignore_merges = 0;
 705                                continue;
 706                        }
 707                        if (!strcmp(arg, "-c")) {
 708                                revs->diff = 1;
 709                                revs->dense_combined_merges = 0;
 710                                revs->combine_merges = 1;
 711                                continue;
 712                        }
 713                        if (!strcmp(arg, "--cc")) {
 714                                revs->diff = 1;
 715                                revs->dense_combined_merges = 1;
 716                                revs->combine_merges = 1;
 717                                continue;
 718                        }
 719                        if (!strcmp(arg, "-v")) {
 720                                revs->verbose_header = 1;
 721                                continue;
 722                        }
 723                        if (!strncmp(arg, "--pretty", 8)) {
 724                                revs->verbose_header = 1;
 725                                revs->commit_format = get_commit_format(arg+8);
 726                                continue;
 727                        }
 728                        if (!strcmp(arg, "--root")) {
 729                                revs->show_root_diff = 1;
 730                                continue;
 731                        }
 732                        if (!strcmp(arg, "--no-commit-id")) {
 733                                revs->no_commit_id = 1;
 734                                continue;
 735                        }
 736                        if (!strcmp(arg, "--always")) {
 737                                revs->always_show_header = 1;
 738                                continue;
 739                        }
 740                        if (!strcmp(arg, "--no-abbrev")) {
 741                                revs->abbrev = 0;
 742                                continue;
 743                        }
 744                        if (!strcmp(arg, "--abbrev")) {
 745                                revs->abbrev = DEFAULT_ABBREV;
 746                                continue;
 747                        }
 748                        if (!strncmp(arg, "--abbrev=", 9)) {
 749                                revs->abbrev = strtoul(arg + 9, NULL, 10);
 750                                if (revs->abbrev < MINIMUM_ABBREV)
 751                                        revs->abbrev = MINIMUM_ABBREV;
 752                                else if (revs->abbrev > 40)
 753                                        revs->abbrev = 40;
 754                                continue;
 755                        }
 756                        if (!strcmp(arg, "--abbrev-commit")) {
 757                                revs->abbrev_commit = 1;
 758                                continue;
 759                        }
 760                        if (!strcmp(arg, "--full-diff")) {
 761                                revs->diff = 1;
 762                                revs->full_diff = 1;
 763                                continue;
 764                        }
 765                        opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
 766                        if (opts > 0) {
 767                                revs->diff = 1;
 768                                i += opts - 1;
 769                                continue;
 770                        }
 771                        *unrecognized++ = arg;
 772                        left++;
 773                        continue;
 774                }
 775                dotdot = strstr(arg, "..");
 776                if (dotdot) {
 777                        unsigned char from_sha1[20];
 778                        const char *next = dotdot + 2;
 779                        const char *this = arg;
 780                        *dotdot = 0;
 781                        if (!*next)
 782                                next = "HEAD";
 783                        if (dotdot == arg)
 784                                this = "HEAD";
 785                        if (!get_sha1(this, from_sha1) &&
 786                            !get_sha1(next, sha1)) {
 787                                struct object *exclude;
 788                                struct object *include;
 789
 790                                exclude = get_reference(revs, this, from_sha1, flags ^ UNINTERESTING);
 791                                include = get_reference(revs, next, sha1, flags);
 792                                if (!exclude || !include)
 793                                        die("Invalid revision range %s..%s", arg, next);
 794
 795                                if (!seen_dashdash) {
 796                                        *dotdot = '.';
 797                                        verify_non_filename(revs->prefix, arg);
 798                                }
 799                                add_pending_object(revs, exclude, this);
 800                                add_pending_object(revs, include, next);
 801                                continue;
 802                        }
 803                        *dotdot = '.';
 804                }
 805                dotdot = strstr(arg, "^@");
 806                if (dotdot && !dotdot[2]) {
 807                        *dotdot = 0;
 808                        if (add_parents_only(revs, arg, flags))
 809                                continue;
 810                        *dotdot = '^';
 811                }
 812                local_flags = 0;
 813                if (*arg == '^') {
 814                        local_flags = UNINTERESTING;
 815                        arg++;
 816                }
 817                if (get_sha1(arg, sha1)) {
 818                        int j;
 819
 820                        if (seen_dashdash || local_flags)
 821                                die("bad revision '%s'", arg);
 822
 823                        /* If we didn't have a "--":
 824                         * (1) all filenames must exist;
 825                         * (2) all rev-args must not be interpretable
 826                         *     as a valid filename.
 827                         * but the latter we have checked in the main loop.
 828                         */
 829                        for (j = i; j < argc; j++)
 830                                verify_filename(revs->prefix, argv[j]);
 831
 832                        revs->prune_data = get_pathspec(revs->prefix, argv + i);
 833                        break;
 834                }
 835                if (!seen_dashdash)
 836                        verify_non_filename(revs->prefix, arg);
 837                object = get_reference(revs, arg, sha1, flags ^ local_flags);
 838                add_pending_object(revs, object, arg);
 839        }
 840        if (def && !revs->pending_objects) {
 841                unsigned char sha1[20];
 842                struct object *object;
 843                if (get_sha1(def, sha1))
 844                        die("bad default revision '%s'", def);
 845                object = get_reference(revs, def, sha1, 0);
 846                add_pending_object(revs, object, def);
 847        }
 848
 849        if (revs->topo_order || revs->unpacked)
 850                revs->limited = 1;
 851
 852        if (revs->prune_data) {
 853                diff_tree_setup_paths(revs->prune_data, &revs->pruning);
 854                revs->prune_fn = try_to_simplify_commit;
 855                if (!revs->full_diff)
 856                        diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
 857        }
 858        if (revs->combine_merges) {
 859                revs->ignore_merges = 0;
 860                if (revs->dense_combined_merges &&
 861                    (revs->diffopt.output_format != DIFF_FORMAT_DIFFSTAT))
 862                        revs->diffopt.output_format = DIFF_FORMAT_PATCH;
 863        }
 864        revs->diffopt.abbrev = revs->abbrev;
 865        diff_setup_done(&revs->diffopt);
 866
 867        return left;
 868}
 869
 870void prepare_revision_walk(struct rev_info *revs)
 871{
 872        struct object_list *list;
 873
 874        list = revs->pending_objects;
 875        revs->pending_objects = NULL;
 876        while (list) {
 877                struct commit *commit = handle_commit(revs, list->item, list->name);
 878                if (commit) {
 879                        if (!(commit->object.flags & SEEN)) {
 880                                commit->object.flags |= SEEN;
 881                                insert_by_date(commit, &revs->commits);
 882                        }
 883                }
 884                list = list->next;
 885        }
 886
 887        if (revs->no_walk)
 888                return;
 889        if (revs->limited)
 890                limit_list(revs);
 891        if (revs->topo_order)
 892                sort_in_topological_order_fn(&revs->commits, revs->lifo,
 893                                             revs->topo_setter,
 894                                             revs->topo_getter);
 895}
 896
 897static int rewrite_one(struct rev_info *revs, struct commit **pp)
 898{
 899        for (;;) {
 900                struct commit *p = *pp;
 901                if (!revs->limited)
 902                        add_parents_to_list(revs, p, &revs->commits);
 903                if (p->object.flags & (TREECHANGE | UNINTERESTING))
 904                        return 0;
 905                if (!p->parents)
 906                        return -1;
 907                *pp = p->parents->item;
 908        }
 909}
 910
 911static void rewrite_parents(struct rev_info *revs, struct commit *commit)
 912{
 913        struct commit_list **pp = &commit->parents;
 914        while (*pp) {
 915                struct commit_list *parent = *pp;
 916                if (rewrite_one(revs, &parent->item) < 0) {
 917                        *pp = parent->next;
 918                        continue;
 919                }
 920                pp = &parent->next;
 921        }
 922}
 923
 924static void mark_boundary_to_show(struct commit *commit)
 925{
 926        struct commit_list *p = commit->parents;
 927        while (p) {
 928                commit = p->item;
 929                p = p->next;
 930                if (commit->object.flags & BOUNDARY)
 931                        commit->object.flags |= BOUNDARY_SHOW;
 932        }
 933}
 934
 935struct commit *get_revision(struct rev_info *revs)
 936{
 937        struct commit_list *list = revs->commits;
 938
 939        if (!list)
 940                return NULL;
 941
 942        /* Check the max_count ... */
 943        switch (revs->max_count) {
 944        case -1:
 945                break;
 946        case 0:
 947                return NULL;
 948        default:
 949                revs->max_count--;
 950        }
 951
 952        do {
 953                struct commit *commit = revs->commits->item;
 954
 955                revs->commits = revs->commits->next;
 956
 957                /*
 958                 * If we haven't done the list limiting, we need to look at
 959                 * the parents here. We also need to do the date-based limiting
 960                 * that we'd otherwise have done in limit_list().
 961                 */
 962                if (!revs->limited) {
 963                        if ((revs->unpacked &&
 964                             has_sha1_pack(commit->object.sha1)) ||
 965                            (revs->max_age != -1 &&
 966                             (commit->date < revs->max_age)))
 967                                continue;
 968                        add_parents_to_list(revs, commit, &revs->commits);
 969                }
 970                if (commit->object.flags & SHOWN)
 971                        continue;
 972
 973                /* We want to show boundary commits only when their
 974                 * children are shown.  When path-limiter is in effect,
 975                 * rewrite_parents() drops some commits from getting shown,
 976                 * and there is no point showing boundary parents that
 977                 * are not shown.  After rewrite_parents() rewrites the
 978                 * parents of a commit that is shown, we mark the boundary
 979                 * parents with BOUNDARY_SHOW.
 980                 */
 981                if (commit->object.flags & BOUNDARY_SHOW) {
 982                        commit->object.flags |= SHOWN;
 983                        return commit;
 984                }
 985                if (commit->object.flags & UNINTERESTING)
 986                        continue;
 987                if (revs->min_age != -1 && (commit->date > revs->min_age))
 988                        continue;
 989                if (revs->no_merges &&
 990                    commit->parents && commit->parents->next)
 991                        continue;
 992                if (revs->prune_fn && revs->dense) {
 993                        if (!(commit->object.flags & TREECHANGE))
 994                                continue;
 995                        if (revs->parents)
 996                                rewrite_parents(revs, commit);
 997                }
 998                if (revs->boundary)
 999                        mark_boundary_to_show(commit);
1000                commit->object.flags |= SHOWN;
1001                return commit;
1002        } while (revs->commits);
1003        return NULL;
1004}