revision.con commit archive - leakfix for format_subst() (20fbfd8)
   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#include "grep.h"
  10#include "reflog-walk.h"
  11#include "patch-ids.h"
  12
  13static char *path_name(struct name_path *path, const char *name)
  14{
  15        struct name_path *p;
  16        char *n, *m;
  17        int nlen = strlen(name);
  18        int len = nlen + 1;
  19
  20        for (p = path; p; p = p->up) {
  21                if (p->elem_len)
  22                        len += p->elem_len + 1;
  23        }
  24        n = xmalloc(len);
  25        m = n + len - (nlen + 1);
  26        strcpy(m, name);
  27        for (p = path; p; p = p->up) {
  28                if (p->elem_len) {
  29                        m -= p->elem_len + 1;
  30                        memcpy(m, p->elem, p->elem_len);
  31                        m[p->elem_len] = '/';
  32                }
  33        }
  34        return n;
  35}
  36
  37void add_object(struct object *obj,
  38                struct object_array *p,
  39                struct name_path *path,
  40                const char *name)
  41{
  42        add_object_array(obj, path_name(path, name), p);
  43}
  44
  45static void mark_blob_uninteresting(struct blob *blob)
  46{
  47        if (blob->object.flags & UNINTERESTING)
  48                return;
  49        blob->object.flags |= UNINTERESTING;
  50}
  51
  52void mark_tree_uninteresting(struct tree *tree)
  53{
  54        struct tree_desc desc;
  55        struct name_entry entry;
  56        struct object *obj = &tree->object;
  57
  58        if (obj->flags & UNINTERESTING)
  59                return;
  60        obj->flags |= UNINTERESTING;
  61        if (!has_sha1_file(obj->sha1))
  62                return;
  63        if (parse_tree(tree) < 0)
  64                die("bad tree %s", sha1_to_hex(obj->sha1));
  65
  66        init_tree_desc(&desc, tree->buffer, tree->size);
  67        while (tree_entry(&desc, &entry)) {
  68                if (S_ISDIR(entry.mode))
  69                        mark_tree_uninteresting(lookup_tree(entry.sha1));
  70                else
  71                        mark_blob_uninteresting(lookup_blob(entry.sha1));
  72        }
  73
  74        /*
  75         * We don't care about the tree any more
  76         * after it has been marked uninteresting.
  77         */
  78        free(tree->buffer);
  79        tree->buffer = NULL;
  80}
  81
  82void mark_parents_uninteresting(struct commit *commit)
  83{
  84        struct commit_list *parents = commit->parents;
  85
  86        while (parents) {
  87                struct commit *commit = parents->item;
  88                if (!(commit->object.flags & UNINTERESTING)) {
  89                        commit->object.flags |= UNINTERESTING;
  90
  91                        /*
  92                         * Normally we haven't parsed the parent
  93                         * yet, so we won't have a parent of a parent
  94                         * here. However, it may turn out that we've
  95                         * reached this commit some other way (where it
  96                         * wasn't uninteresting), in which case we need
  97                         * to mark its parents recursively too..
  98                         */
  99                        if (commit->parents)
 100                                mark_parents_uninteresting(commit);
 101                }
 102
 103                /*
 104                 * A missing commit is ok iff its parent is marked
 105                 * uninteresting.
 106                 *
 107                 * We just mark such a thing parsed, so that when
 108                 * it is popped next time around, we won't be trying
 109                 * to parse it and get an error.
 110                 */
 111                if (!has_sha1_file(commit->object.sha1))
 112                        commit->object.parsed = 1;
 113                parents = parents->next;
 114        }
 115}
 116
 117static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
 118{
 119        if (revs->no_walk && (obj->flags & UNINTERESTING))
 120                die("object ranges do not make sense when not walking revisions");
 121        if (revs->reflog_info && obj->type == OBJ_COMMIT &&
 122                        add_reflog_for_walk(revs->reflog_info,
 123                                (struct commit *)obj, name))
 124                return;
 125        add_object_array_with_mode(obj, name, &revs->pending, mode);
 126}
 127
 128void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
 129{
 130        add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
 131}
 132
 133static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
 134{
 135        struct object *object;
 136
 137        object = parse_object(sha1);
 138        if (!object)
 139                die("bad object %s", name);
 140        object->flags |= flags;
 141        return object;
 142}
 143
 144static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
 145{
 146        unsigned long flags = object->flags;
 147
 148        /*
 149         * Tag object? Look what it points to..
 150         */
 151        while (object->type == OBJ_TAG) {
 152                struct tag *tag = (struct tag *) object;
 153                if (revs->tag_objects && !(flags & UNINTERESTING))
 154                        add_pending_object(revs, object, tag->tag);
 155                object = parse_object(tag->tagged->sha1);
 156                if (!object)
 157                        die("bad object %s", sha1_to_hex(tag->tagged->sha1));
 158        }
 159
 160        /*
 161         * Commit object? Just return it, we'll do all the complex
 162         * reachability crud.
 163         */
 164        if (object->type == OBJ_COMMIT) {
 165                struct commit *commit = (struct commit *)object;
 166                if (parse_commit(commit) < 0)
 167                        die("unable to parse commit %s", name);
 168                if (flags & UNINTERESTING) {
 169                        commit->object.flags |= UNINTERESTING;
 170                        mark_parents_uninteresting(commit);
 171                        revs->limited = 1;
 172                }
 173                return commit;
 174        }
 175
 176        /*
 177         * Tree object? Either mark it uniniteresting, or add it
 178         * to the list of objects to look at later..
 179         */
 180        if (object->type == OBJ_TREE) {
 181                struct tree *tree = (struct tree *)object;
 182                if (!revs->tree_objects)
 183                        return NULL;
 184                if (flags & UNINTERESTING) {
 185                        mark_tree_uninteresting(tree);
 186                        return NULL;
 187                }
 188                add_pending_object(revs, object, "");
 189                return NULL;
 190        }
 191
 192        /*
 193         * Blob object? You know the drill by now..
 194         */
 195        if (object->type == OBJ_BLOB) {
 196                struct blob *blob = (struct blob *)object;
 197                if (!revs->blob_objects)
 198                        return NULL;
 199                if (flags & UNINTERESTING) {
 200                        mark_blob_uninteresting(blob);
 201                        return NULL;
 202                }
 203                add_pending_object(revs, object, "");
 204                return NULL;
 205        }
 206        die("%s is unknown object", name);
 207}
 208
 209static int everybody_uninteresting(struct commit_list *orig)
 210{
 211        struct commit_list *list = orig;
 212        while (list) {
 213                struct commit *commit = list->item;
 214                list = list->next;
 215                if (commit->object.flags & UNINTERESTING)
 216                        continue;
 217                return 0;
 218        }
 219        return 1;
 220}
 221
 222/*
 223 * The goal is to get REV_TREE_NEW as the result only if the
 224 * diff consists of all '+' (and no other changes), and
 225 * REV_TREE_DIFFERENT otherwise (of course if the trees are
 226 * the same we want REV_TREE_SAME).  That means that once we
 227 * get to REV_TREE_DIFFERENT, we do not have to look any further.
 228 */
 229static int tree_difference = REV_TREE_SAME;
 230
 231static void file_add_remove(struct diff_options *options,
 232                    int addremove, unsigned mode,
 233                    const unsigned char *sha1,
 234                    const char *base, const char *path)
 235{
 236        int diff = REV_TREE_DIFFERENT;
 237
 238        /*
 239         * Is it an add of a new file? It means that the old tree
 240         * didn't have it at all, so we will turn "REV_TREE_SAME" ->
 241         * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
 242         * (and if it already was "REV_TREE_NEW", we'll keep it
 243         * "REV_TREE_NEW" of course).
 244         */
 245        if (addremove == '+') {
 246                diff = tree_difference;
 247                if (diff != REV_TREE_SAME)
 248                        return;
 249                diff = REV_TREE_NEW;
 250        }
 251        tree_difference = diff;
 252        if (tree_difference == REV_TREE_DIFFERENT)
 253                options->has_changes = 1;
 254}
 255
 256static void file_change(struct diff_options *options,
 257                 unsigned old_mode, unsigned new_mode,
 258                 const unsigned char *old_sha1,
 259                 const unsigned char *new_sha1,
 260                 const char *base, const char *path)
 261{
 262        tree_difference = REV_TREE_DIFFERENT;
 263        options->has_changes = 1;
 264}
 265
 266static int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
 267{
 268        if (!t1)
 269                return REV_TREE_NEW;
 270        if (!t2)
 271                return REV_TREE_DIFFERENT;
 272        tree_difference = REV_TREE_SAME;
 273        revs->pruning.has_changes = 0;
 274        if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
 275                           &revs->pruning) < 0)
 276                return REV_TREE_DIFFERENT;
 277        return tree_difference;
 278}
 279
 280static int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
 281{
 282        int retval;
 283        void *tree;
 284        unsigned long size;
 285        struct tree_desc empty, real;
 286
 287        if (!t1)
 288                return 0;
 289
 290        tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
 291        if (!tree)
 292                return 0;
 293        init_tree_desc(&real, tree, size);
 294        init_tree_desc(&empty, "", 0);
 295
 296        tree_difference = REV_TREE_SAME;
 297        revs->pruning.has_changes = 0;
 298        retval = diff_tree(&empty, &real, "", &revs->pruning);
 299        free(tree);
 300
 301        return retval >= 0 && (tree_difference == REV_TREE_SAME);
 302}
 303
 304static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
 305{
 306        struct commit_list **pp, *parent;
 307        int tree_changed = 0, tree_same = 0;
 308
 309        if (!commit->tree)
 310                return;
 311
 312        if (!commit->parents) {
 313                if (!rev_same_tree_as_empty(revs, commit->tree))
 314                        commit->object.flags |= TREECHANGE;
 315                return;
 316        }
 317
 318        pp = &commit->parents;
 319        while ((parent = *pp) != NULL) {
 320                struct commit *p = parent->item;
 321
 322                if (parse_commit(p) < 0)
 323                        die("cannot simplify commit %s (because of %s)",
 324                            sha1_to_hex(commit->object.sha1),
 325                            sha1_to_hex(p->object.sha1));
 326                switch (rev_compare_tree(revs, p->tree, commit->tree)) {
 327                case REV_TREE_SAME:
 328                        tree_same = 1;
 329                        if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
 330                                /* Even if a merge with an uninteresting
 331                                 * side branch brought the entire change
 332                                 * we are interested in, we do not want
 333                                 * to lose the other branches of this
 334                                 * merge, so we just keep going.
 335                                 */
 336                                pp = &parent->next;
 337                                continue;
 338                        }
 339                        parent->next = NULL;
 340                        commit->parents = parent;
 341                        return;
 342
 343                case REV_TREE_NEW:
 344                        if (revs->remove_empty_trees &&
 345                            rev_same_tree_as_empty(revs, p->tree)) {
 346                                /* We are adding all the specified
 347                                 * paths from this parent, so the
 348                                 * history beyond this parent is not
 349                                 * interesting.  Remove its parents
 350                                 * (they are grandparents for us).
 351                                 * IOW, we pretend this parent is a
 352                                 * "root" commit.
 353                                 */
 354                                if (parse_commit(p) < 0)
 355                                        die("cannot simplify commit %s (invalid %s)",
 356                                            sha1_to_hex(commit->object.sha1),
 357                                            sha1_to_hex(p->object.sha1));
 358                                p->parents = NULL;
 359                        }
 360                /* fallthrough */
 361                case REV_TREE_DIFFERENT:
 362                        tree_changed = 1;
 363                        pp = &parent->next;
 364                        continue;
 365                }
 366                die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
 367        }
 368        if (tree_changed && !tree_same)
 369                commit->object.flags |= TREECHANGE;
 370}
 371
 372static int add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
 373{
 374        struct commit_list *parent = commit->parents;
 375        unsigned left_flag;
 376        int add, rest;
 377
 378        if (commit->object.flags & ADDED)
 379                return 0;
 380        commit->object.flags |= ADDED;
 381
 382        /*
 383         * If the commit is uninteresting, don't try to
 384         * prune parents - we want the maximal uninteresting
 385         * set.
 386         *
 387         * Normally we haven't parsed the parent
 388         * yet, so we won't have a parent of a parent
 389         * here. However, it may turn out that we've
 390         * reached this commit some other way (where it
 391         * wasn't uninteresting), in which case we need
 392         * to mark its parents recursively too..
 393         */
 394        if (commit->object.flags & UNINTERESTING) {
 395                while (parent) {
 396                        struct commit *p = parent->item;
 397                        parent = parent->next;
 398                        if (parse_commit(p) < 0)
 399                                return -1;
 400                        p->object.flags |= UNINTERESTING;
 401                        if (p->parents)
 402                                mark_parents_uninteresting(p);
 403                        if (p->object.flags & SEEN)
 404                                continue;
 405                        p->object.flags |= SEEN;
 406                        insert_by_date(p, list);
 407                }
 408                return 0;
 409        }
 410
 411        /*
 412         * Ok, the commit wasn't uninteresting. Try to
 413         * simplify the commit history and find the parent
 414         * that has no differences in the path set if one exists.
 415         */
 416        if (revs->prune_fn)
 417                revs->prune_fn(revs, commit);
 418
 419        if (revs->no_walk)
 420                return 0;
 421
 422        left_flag = (commit->object.flags & SYMMETRIC_LEFT);
 423
 424        rest = !revs->first_parent_only;
 425        for (parent = commit->parents, add = 1; parent; add = rest) {
 426                struct commit *p = parent->item;
 427
 428                parent = parent->next;
 429                if (parse_commit(p) < 0)
 430                        return -1;
 431                p->object.flags |= left_flag;
 432                if (p->object.flags & SEEN)
 433                        continue;
 434                p->object.flags |= SEEN;
 435                if (add)
 436                        insert_by_date(p, list);
 437        }
 438        return 0;
 439}
 440
 441static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
 442{
 443        struct commit_list *p;
 444        int left_count = 0, right_count = 0;
 445        int left_first;
 446        struct patch_ids ids;
 447
 448        /* First count the commits on the left and on the right */
 449        for (p = list; p; p = p->next) {
 450                struct commit *commit = p->item;
 451                unsigned flags = commit->object.flags;
 452                if (flags & BOUNDARY)
 453                        ;
 454                else if (flags & SYMMETRIC_LEFT)
 455                        left_count++;
 456                else
 457                        right_count++;
 458        }
 459
 460        left_first = left_count < right_count;
 461        init_patch_ids(&ids);
 462        if (revs->diffopt.nr_paths) {
 463                ids.diffopts.nr_paths = revs->diffopt.nr_paths;
 464                ids.diffopts.paths = revs->diffopt.paths;
 465                ids.diffopts.pathlens = revs->diffopt.pathlens;
 466        }
 467
 468        /* Compute patch-ids for one side */
 469        for (p = list; p; p = p->next) {
 470                struct commit *commit = p->item;
 471                unsigned flags = commit->object.flags;
 472
 473                if (flags & BOUNDARY)
 474                        continue;
 475                /*
 476                 * If we have fewer left, left_first is set and we omit
 477                 * commits on the right branch in this loop.  If we have
 478                 * fewer right, we skip the left ones.
 479                 */
 480                if (left_first != !!(flags & SYMMETRIC_LEFT))
 481                        continue;
 482                commit->util = add_commit_patch_id(commit, &ids);
 483        }
 484
 485        /* Check the other side */
 486        for (p = list; p; p = p->next) {
 487                struct commit *commit = p->item;
 488                struct patch_id *id;
 489                unsigned flags = commit->object.flags;
 490
 491                if (flags & BOUNDARY)
 492                        continue;
 493                /*
 494                 * If we have fewer left, left_first is set and we omit
 495                 * commits on the left branch in this loop.
 496                 */
 497                if (left_first == !!(flags & SYMMETRIC_LEFT))
 498                        continue;
 499
 500                /*
 501                 * Have we seen the same patch id?
 502                 */
 503                id = has_commit_patch_id(commit, &ids);
 504                if (!id)
 505                        continue;
 506                id->seen = 1;
 507                commit->object.flags |= SHOWN;
 508        }
 509
 510        /* Now check the original side for seen ones */
 511        for (p = list; p; p = p->next) {
 512                struct commit *commit = p->item;
 513                struct patch_id *ent;
 514
 515                ent = commit->util;
 516                if (!ent)
 517                        continue;
 518                if (ent->seen)
 519                        commit->object.flags |= SHOWN;
 520                commit->util = NULL;
 521        }
 522
 523        free_patch_ids(&ids);
 524}
 525
 526static int limit_list(struct rev_info *revs)
 527{
 528        struct commit_list *list = revs->commits;
 529        struct commit_list *newlist = NULL;
 530        struct commit_list **p = &newlist;
 531
 532        while (list) {
 533                struct commit_list *entry = list;
 534                struct commit *commit = list->item;
 535                struct object *obj = &commit->object;
 536
 537                list = list->next;
 538                free(entry);
 539
 540                if (revs->max_age != -1 && (commit->date < revs->max_age))
 541                        obj->flags |= UNINTERESTING;
 542                if (add_parents_to_list(revs, commit, &list) < 0)
 543                        return -1;
 544                if (obj->flags & UNINTERESTING) {
 545                        mark_parents_uninteresting(commit);
 546                        if (everybody_uninteresting(list))
 547                                break;
 548                        continue;
 549                }
 550                if (revs->min_age != -1 && (commit->date > revs->min_age))
 551                        continue;
 552                p = &commit_list_insert(commit, p)->next;
 553        }
 554        if (revs->cherry_pick)
 555                cherry_pick_list(newlist, revs);
 556
 557        revs->commits = newlist;
 558        return 0;
 559}
 560
 561struct all_refs_cb {
 562        int all_flags;
 563        int warned_bad_reflog;
 564        struct rev_info *all_revs;
 565        const char *name_for_errormsg;
 566};
 567
 568static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 569{
 570        struct all_refs_cb *cb = cb_data;
 571        struct object *object = get_reference(cb->all_revs, path, sha1,
 572                                              cb->all_flags);
 573        add_pending_object(cb->all_revs, object, path);
 574        return 0;
 575}
 576
 577static void handle_all(struct rev_info *revs, unsigned flags)
 578{
 579        struct all_refs_cb cb;
 580        cb.all_revs = revs;
 581        cb.all_flags = flags;
 582        for_each_ref(handle_one_ref, &cb);
 583}
 584
 585static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
 586{
 587        struct all_refs_cb *cb = cb_data;
 588        if (!is_null_sha1(sha1)) {
 589                struct object *o = parse_object(sha1);
 590                if (o) {
 591                        o->flags |= cb->all_flags;
 592                        add_pending_object(cb->all_revs, o, "");
 593                }
 594                else if (!cb->warned_bad_reflog) {
 595                        warning("reflog of '%s' references pruned commits",
 596                                cb->name_for_errormsg);
 597                        cb->warned_bad_reflog = 1;
 598                }
 599        }
 600}
 601
 602static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 603                const char *email, unsigned long timestamp, int tz,
 604                const char *message, void *cb_data)
 605{
 606        handle_one_reflog_commit(osha1, cb_data);
 607        handle_one_reflog_commit(nsha1, cb_data);
 608        return 0;
 609}
 610
 611static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 612{
 613        struct all_refs_cb *cb = cb_data;
 614        cb->warned_bad_reflog = 0;
 615        cb->name_for_errormsg = path;
 616        for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
 617        return 0;
 618}
 619
 620static void handle_reflog(struct rev_info *revs, unsigned flags)
 621{
 622        struct all_refs_cb cb;
 623        cb.all_revs = revs;
 624        cb.all_flags = flags;
 625        for_each_reflog(handle_one_reflog, &cb);
 626}
 627
 628static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
 629{
 630        unsigned char sha1[20];
 631        struct object *it;
 632        struct commit *commit;
 633        struct commit_list *parents;
 634
 635        if (*arg == '^') {
 636                flags ^= UNINTERESTING;
 637                arg++;
 638        }
 639        if (get_sha1(arg, sha1))
 640                return 0;
 641        while (1) {
 642                it = get_reference(revs, arg, sha1, 0);
 643                if (it->type != OBJ_TAG)
 644                        break;
 645                hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
 646        }
 647        if (it->type != OBJ_COMMIT)
 648                return 0;
 649        commit = (struct commit *)it;
 650        for (parents = commit->parents; parents; parents = parents->next) {
 651                it = &parents->item->object;
 652                it->flags |= flags;
 653                add_pending_object(revs, it, arg);
 654        }
 655        return 1;
 656}
 657
 658void init_revisions(struct rev_info *revs, const char *prefix)
 659{
 660        memset(revs, 0, sizeof(*revs));
 661
 662        revs->abbrev = DEFAULT_ABBREV;
 663        revs->ignore_merges = 1;
 664        revs->simplify_history = 1;
 665        revs->pruning.recursive = 1;
 666        revs->pruning.quiet = 1;
 667        revs->pruning.add_remove = file_add_remove;
 668        revs->pruning.change = file_change;
 669        revs->lifo = 1;
 670        revs->dense = 1;
 671        revs->prefix = prefix;
 672        revs->max_age = -1;
 673        revs->min_age = -1;
 674        revs->skip_count = -1;
 675        revs->max_count = -1;
 676
 677        revs->prune_fn = NULL;
 678        revs->prune_data = NULL;
 679
 680        revs->topo_setter = topo_sort_default_setter;
 681        revs->topo_getter = topo_sort_default_getter;
 682
 683        revs->commit_format = CMIT_FMT_DEFAULT;
 684
 685        diff_setup(&revs->diffopt);
 686}
 687
 688static void add_pending_commit_list(struct rev_info *revs,
 689                                    struct commit_list *commit_list,
 690                                    unsigned int flags)
 691{
 692        while (commit_list) {
 693                struct object *object = &commit_list->item->object;
 694                object->flags |= flags;
 695                add_pending_object(revs, object, sha1_to_hex(object->sha1));
 696                commit_list = commit_list->next;
 697        }
 698}
 699
 700static void prepare_show_merge(struct rev_info *revs)
 701{
 702        struct commit_list *bases;
 703        struct commit *head, *other;
 704        unsigned char sha1[20];
 705        const char **prune = NULL;
 706        int i, prune_num = 1; /* counting terminating NULL */
 707
 708        if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
 709                die("--merge without HEAD?");
 710        if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
 711                die("--merge without MERGE_HEAD?");
 712        add_pending_object(revs, &head->object, "HEAD");
 713        add_pending_object(revs, &other->object, "MERGE_HEAD");
 714        bases = get_merge_bases(head, other, 1);
 715        while (bases) {
 716                struct commit *it = bases->item;
 717                struct commit_list *n = bases->next;
 718                free(bases);
 719                bases = n;
 720                it->object.flags |= UNINTERESTING;
 721                add_pending_object(revs, &it->object, "(merge-base)");
 722        }
 723
 724        if (!active_nr)
 725                read_cache();
 726        for (i = 0; i < active_nr; i++) {
 727                struct cache_entry *ce = active_cache[i];
 728                if (!ce_stage(ce))
 729                        continue;
 730                if (ce_path_match(ce, revs->prune_data)) {
 731                        prune_num++;
 732                        prune = xrealloc(prune, sizeof(*prune) * prune_num);
 733                        prune[prune_num-2] = ce->name;
 734                        prune[prune_num-1] = NULL;
 735                }
 736                while ((i+1 < active_nr) &&
 737                       ce_same_name(ce, active_cache[i+1]))
 738                        i++;
 739        }
 740        revs->prune_data = prune;
 741}
 742
 743int handle_revision_arg(const char *arg, struct rev_info *revs,
 744                        int flags,
 745                        int cant_be_filename)
 746{
 747        unsigned mode;
 748        char *dotdot;
 749        struct object *object;
 750        unsigned char sha1[20];
 751        int local_flags;
 752
 753        dotdot = strstr(arg, "..");
 754        if (dotdot) {
 755                unsigned char from_sha1[20];
 756                const char *next = dotdot + 2;
 757                const char *this = arg;
 758                int symmetric = *next == '.';
 759                unsigned int flags_exclude = flags ^ UNINTERESTING;
 760
 761                *dotdot = 0;
 762                next += symmetric;
 763
 764                if (!*next)
 765                        next = "HEAD";
 766                if (dotdot == arg)
 767                        this = "HEAD";
 768                if (!get_sha1(this, from_sha1) &&
 769                    !get_sha1(next, sha1)) {
 770                        struct commit *a, *b;
 771                        struct commit_list *exclude;
 772
 773                        a = lookup_commit_reference(from_sha1);
 774                        b = lookup_commit_reference(sha1);
 775                        if (!a || !b) {
 776                                die(symmetric ?
 777                                    "Invalid symmetric difference expression %s...%s" :
 778                                    "Invalid revision range %s..%s",
 779                                    arg, next);
 780                        }
 781
 782                        if (!cant_be_filename) {
 783                                *dotdot = '.';
 784                                verify_non_filename(revs->prefix, arg);
 785                        }
 786
 787                        if (symmetric) {
 788                                exclude = get_merge_bases(a, b, 1);
 789                                add_pending_commit_list(revs, exclude,
 790                                                        flags_exclude);
 791                                free_commit_list(exclude);
 792                                a->object.flags |= flags | SYMMETRIC_LEFT;
 793                        } else
 794                                a->object.flags |= flags_exclude;
 795                        b->object.flags |= flags;
 796                        add_pending_object(revs, &a->object, this);
 797                        add_pending_object(revs, &b->object, next);
 798                        return 0;
 799                }
 800                *dotdot = '.';
 801        }
 802        dotdot = strstr(arg, "^@");
 803        if (dotdot && !dotdot[2]) {
 804                *dotdot = 0;
 805                if (add_parents_only(revs, arg, flags))
 806                        return 0;
 807                *dotdot = '^';
 808        }
 809        dotdot = strstr(arg, "^!");
 810        if (dotdot && !dotdot[2]) {
 811                *dotdot = 0;
 812                if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
 813                        *dotdot = '^';
 814        }
 815
 816        local_flags = 0;
 817        if (*arg == '^') {
 818                local_flags = UNINTERESTING;
 819                arg++;
 820        }
 821        if (get_sha1_with_mode(arg, sha1, &mode))
 822                return -1;
 823        if (!cant_be_filename)
 824                verify_non_filename(revs->prefix, arg);
 825        object = get_reference(revs, arg, sha1, flags ^ local_flags);
 826        add_pending_object_with_mode(revs, object, arg, mode);
 827        return 0;
 828}
 829
 830static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
 831{
 832        if (!revs->grep_filter) {
 833                struct grep_opt *opt = xcalloc(1, sizeof(*opt));
 834                opt->status_only = 1;
 835                opt->pattern_tail = &(opt->pattern_list);
 836                opt->regflags = REG_NEWLINE;
 837                revs->grep_filter = opt;
 838        }
 839        append_grep_pattern(revs->grep_filter, ptn,
 840                            "command line", 0, what);
 841}
 842
 843static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern)
 844{
 845        char *pat;
 846        const char *prefix;
 847        int patlen, fldlen;
 848
 849        fldlen = strlen(field);
 850        patlen = strlen(pattern);
 851        pat = xmalloc(patlen + fldlen + 10);
 852        prefix = ".*";
 853        if (*pattern == '^') {
 854                prefix = "";
 855                pattern++;
 856        }
 857        sprintf(pat, "^%s %s%s", field, prefix, pattern);
 858        add_grep(revs, pat, GREP_PATTERN_HEAD);
 859}
 860
 861static void add_message_grep(struct rev_info *revs, const char *pattern)
 862{
 863        add_grep(revs, pattern, GREP_PATTERN_BODY);
 864}
 865
 866static void add_ignore_packed(struct rev_info *revs, const char *name)
 867{
 868        int num = ++revs->num_ignore_packed;
 869
 870        revs->ignore_packed = xrealloc(revs->ignore_packed,
 871                                       sizeof(const char **) * (num + 1));
 872        revs->ignore_packed[num-1] = name;
 873        revs->ignore_packed[num] = NULL;
 874}
 875
 876/*
 877 * Parse revision information, filling in the "rev_info" structure,
 878 * and removing the used arguments from the argument list.
 879 *
 880 * Returns the number of arguments left that weren't recognized
 881 * (which are also moved to the head of the argument list)
 882 */
 883int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
 884{
 885        int i, flags, seen_dashdash, show_merge;
 886        const char **unrecognized = argv + 1;
 887        int left = 1;
 888        int all_match = 0;
 889        int regflags = 0;
 890
 891        /* First, search for "--" */
 892        seen_dashdash = 0;
 893        for (i = 1; i < argc; i++) {
 894                const char *arg = argv[i];
 895                if (strcmp(arg, "--"))
 896                        continue;
 897                argv[i] = NULL;
 898                argc = i;
 899                if (argv[i + 1])
 900                        revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
 901                seen_dashdash = 1;
 902                break;
 903        }
 904
 905        flags = show_merge = 0;
 906        for (i = 1; i < argc; i++) {
 907                const char *arg = argv[i];
 908                if (*arg == '-') {
 909                        int opts;
 910                        if (!prefixcmp(arg, "--max-count=")) {
 911                                revs->max_count = atoi(arg + 12);
 912                                continue;
 913                        }
 914                        if (!prefixcmp(arg, "--skip=")) {
 915                                revs->skip_count = atoi(arg + 7);
 916                                continue;
 917                        }
 918                        /* accept -<digit>, like traditional "head" */
 919                        if ((*arg == '-') && isdigit(arg[1])) {
 920                                revs->max_count = atoi(arg + 1);
 921                                continue;
 922                        }
 923                        if (!strcmp(arg, "-n")) {
 924                                if (argc <= i + 1)
 925                                        die("-n requires an argument");
 926                                revs->max_count = atoi(argv[++i]);
 927                                continue;
 928                        }
 929                        if (!prefixcmp(arg, "-n")) {
 930                                revs->max_count = atoi(arg + 2);
 931                                continue;
 932                        }
 933                        if (!prefixcmp(arg, "--max-age=")) {
 934                                revs->max_age = atoi(arg + 10);
 935                                continue;
 936                        }
 937                        if (!prefixcmp(arg, "--since=")) {
 938                                revs->max_age = approxidate(arg + 8);
 939                                continue;
 940                        }
 941                        if (!prefixcmp(arg, "--after=")) {
 942                                revs->max_age = approxidate(arg + 8);
 943                                continue;
 944                        }
 945                        if (!prefixcmp(arg, "--min-age=")) {
 946                                revs->min_age = atoi(arg + 10);
 947                                continue;
 948                        }
 949                        if (!prefixcmp(arg, "--before=")) {
 950                                revs->min_age = approxidate(arg + 9);
 951                                continue;
 952                        }
 953                        if (!prefixcmp(arg, "--until=")) {
 954                                revs->min_age = approxidate(arg + 8);
 955                                continue;
 956                        }
 957                        if (!strcmp(arg, "--all")) {
 958                                handle_all(revs, flags);
 959                                continue;
 960                        }
 961                        if (!strcmp(arg, "--first-parent")) {
 962                                revs->first_parent_only = 1;
 963                                continue;
 964                        }
 965                        if (!strcmp(arg, "--reflog")) {
 966                                handle_reflog(revs, flags);
 967                                continue;
 968                        }
 969                        if (!strcmp(arg, "-g") ||
 970                                        !strcmp(arg, "--walk-reflogs")) {
 971                                init_reflog_walk(&revs->reflog_info);
 972                                continue;
 973                        }
 974                        if (!strcmp(arg, "--not")) {
 975                                flags ^= UNINTERESTING;
 976                                continue;
 977                        }
 978                        if (!strcmp(arg, "--default")) {
 979                                if (++i >= argc)
 980                                        die("bad --default argument");
 981                                def = argv[i];
 982                                continue;
 983                        }
 984                        if (!strcmp(arg, "--merge")) {
 985                                show_merge = 1;
 986                                continue;
 987                        }
 988                        if (!strcmp(arg, "--topo-order")) {
 989                                revs->topo_order = 1;
 990                                continue;
 991                        }
 992                        if (!strcmp(arg, "--date-order")) {
 993                                revs->lifo = 0;
 994                                revs->topo_order = 1;
 995                                continue;
 996                        }
 997                        if (!strcmp(arg, "--parents")) {
 998                                revs->parents = 1;
 999                                continue;
1000                        }
1001                        if (!strcmp(arg, "--dense")) {
1002                                revs->dense = 1;
1003                                continue;
1004                        }
1005                        if (!strcmp(arg, "--sparse")) {
1006                                revs->dense = 0;
1007                                continue;
1008                        }
1009                        if (!strcmp(arg, "--remove-empty")) {
1010                                revs->remove_empty_trees = 1;
1011                                continue;
1012                        }
1013                        if (!strcmp(arg, "--no-merges")) {
1014                                revs->no_merges = 1;
1015                                continue;
1016                        }
1017                        if (!strcmp(arg, "--boundary")) {
1018                                revs->boundary = 1;
1019                                continue;
1020                        }
1021                        if (!strcmp(arg, "--left-right")) {
1022                                revs->left_right = 1;
1023                                continue;
1024                        }
1025                        if (!strcmp(arg, "--cherry-pick")) {
1026                                revs->cherry_pick = 1;
1027                                continue;
1028                        }
1029                        if (!strcmp(arg, "--objects")) {
1030                                revs->tag_objects = 1;
1031                                revs->tree_objects = 1;
1032                                revs->blob_objects = 1;
1033                                continue;
1034                        }
1035                        if (!strcmp(arg, "--objects-edge")) {
1036                                revs->tag_objects = 1;
1037                                revs->tree_objects = 1;
1038                                revs->blob_objects = 1;
1039                                revs->edge_hint = 1;
1040                                continue;
1041                        }
1042                        if (!strcmp(arg, "--unpacked")) {
1043                                revs->unpacked = 1;
1044                                free(revs->ignore_packed);
1045                                revs->ignore_packed = NULL;
1046                                revs->num_ignore_packed = 0;
1047                                continue;
1048                        }
1049                        if (!prefixcmp(arg, "--unpacked=")) {
1050                                revs->unpacked = 1;
1051                                add_ignore_packed(revs, arg+11);
1052                                continue;
1053                        }
1054                        if (!strcmp(arg, "-r")) {
1055                                revs->diff = 1;
1056                                revs->diffopt.recursive = 1;
1057                                continue;
1058                        }
1059                        if (!strcmp(arg, "-t")) {
1060                                revs->diff = 1;
1061                                revs->diffopt.recursive = 1;
1062                                revs->diffopt.tree_in_recursive = 1;
1063                                continue;
1064                        }
1065                        if (!strcmp(arg, "-m")) {
1066                                revs->ignore_merges = 0;
1067                                continue;
1068                        }
1069                        if (!strcmp(arg, "-c")) {
1070                                revs->diff = 1;
1071                                revs->dense_combined_merges = 0;
1072                                revs->combine_merges = 1;
1073                                continue;
1074                        }
1075                        if (!strcmp(arg, "--cc")) {
1076                                revs->diff = 1;
1077                                revs->dense_combined_merges = 1;
1078                                revs->combine_merges = 1;
1079                                continue;
1080                        }
1081                        if (!strcmp(arg, "-v")) {
1082                                revs->verbose_header = 1;
1083                                continue;
1084                        }
1085                        if (!prefixcmp(arg, "--pretty")) {
1086                                revs->verbose_header = 1;
1087                                revs->commit_format = get_commit_format(arg+8);
1088                                continue;
1089                        }
1090                        if (!strcmp(arg, "--root")) {
1091                                revs->show_root_diff = 1;
1092                                continue;
1093                        }
1094                        if (!strcmp(arg, "--no-commit-id")) {
1095                                revs->no_commit_id = 1;
1096                                continue;
1097                        }
1098                        if (!strcmp(arg, "--always")) {
1099                                revs->always_show_header = 1;
1100                                continue;
1101                        }
1102                        if (!strcmp(arg, "--no-abbrev")) {
1103                                revs->abbrev = 0;
1104                                continue;
1105                        }
1106                        if (!strcmp(arg, "--abbrev")) {
1107                                revs->abbrev = DEFAULT_ABBREV;
1108                                continue;
1109                        }
1110                        if (!prefixcmp(arg, "--abbrev=")) {
1111                                revs->abbrev = strtoul(arg + 9, NULL, 10);
1112                                if (revs->abbrev < MINIMUM_ABBREV)
1113                                        revs->abbrev = MINIMUM_ABBREV;
1114                                else if (revs->abbrev > 40)
1115                                        revs->abbrev = 40;
1116                                continue;
1117                        }
1118                        if (!strcmp(arg, "--abbrev-commit")) {
1119                                revs->abbrev_commit = 1;
1120                                continue;
1121                        }
1122                        if (!strcmp(arg, "--full-diff")) {
1123                                revs->diff = 1;
1124                                revs->full_diff = 1;
1125                                continue;
1126                        }
1127                        if (!strcmp(arg, "--full-history")) {
1128                                revs->simplify_history = 0;
1129                                continue;
1130                        }
1131                        if (!strcmp(arg, "--relative-date")) {
1132                                revs->date_mode = DATE_RELATIVE;
1133                                continue;
1134                        }
1135                        if (!strncmp(arg, "--date=", 7)) {
1136                                if (!strcmp(arg + 7, "relative"))
1137                                        revs->date_mode = DATE_RELATIVE;
1138                                else if (!strcmp(arg + 7, "iso8601") ||
1139                                         !strcmp(arg + 7, "iso"))
1140                                        revs->date_mode = DATE_ISO8601;
1141                                else if (!strcmp(arg + 7, "rfc2822") ||
1142                                         !strcmp(arg + 7, "rfc"))
1143                                        revs->date_mode = DATE_RFC2822;
1144                                else if (!strcmp(arg + 7, "short"))
1145                                        revs->date_mode = DATE_SHORT;
1146                                else if (!strcmp(arg + 7, "local"))
1147                                        revs->date_mode = DATE_LOCAL;
1148                                else if (!strcmp(arg + 7, "default"))
1149                                        revs->date_mode = DATE_NORMAL;
1150                                else
1151                                        die("unknown date format %s", arg);
1152                                continue;
1153                        }
1154                        if (!strcmp(arg, "--log-size")) {
1155                                revs->show_log_size = 1;
1156                                continue;
1157                        }
1158
1159                        /*
1160                         * Grepping the commit log
1161                         */
1162                        if (!prefixcmp(arg, "--author=")) {
1163                                add_header_grep(revs, "author", arg+9);
1164                                continue;
1165                        }
1166                        if (!prefixcmp(arg, "--committer=")) {
1167                                add_header_grep(revs, "committer", arg+12);
1168                                continue;
1169                        }
1170                        if (!prefixcmp(arg, "--grep=")) {
1171                                add_message_grep(revs, arg+7);
1172                                continue;
1173                        }
1174                        if (!strcmp(arg, "--extended-regexp") ||
1175                            !strcmp(arg, "-E")) {
1176                                regflags |= REG_EXTENDED;
1177                                continue;
1178                        }
1179                        if (!strcmp(arg, "--regexp-ignore-case") ||
1180                            !strcmp(arg, "-i")) {
1181                                regflags |= REG_ICASE;
1182                                continue;
1183                        }
1184                        if (!strcmp(arg, "--all-match")) {
1185                                all_match = 1;
1186                                continue;
1187                        }
1188                        if (!prefixcmp(arg, "--encoding=")) {
1189                                arg += 11;
1190                                if (strcmp(arg, "none"))
1191                                        git_log_output_encoding = xstrdup(arg);
1192                                else
1193                                        git_log_output_encoding = "";
1194                                continue;
1195                        }
1196                        if (!strcmp(arg, "--reverse")) {
1197                                revs->reverse ^= 1;
1198                                continue;
1199                        }
1200                        if (!strcmp(arg, "--no-walk")) {
1201                                revs->no_walk = 1;
1202                                continue;
1203                        }
1204                        if (!strcmp(arg, "--do-walk")) {
1205                                revs->no_walk = 0;
1206                                continue;
1207                        }
1208
1209                        opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
1210                        if (opts > 0) {
1211                                if (strcmp(argv[i], "-z"))
1212                                        revs->diff = 1;
1213                                i += opts - 1;
1214                                continue;
1215                        }
1216                        *unrecognized++ = arg;
1217                        left++;
1218                        continue;
1219                }
1220
1221                if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1222                        int j;
1223                        if (seen_dashdash || *arg == '^')
1224                                die("bad revision '%s'", arg);
1225
1226                        /* If we didn't have a "--":
1227                         * (1) all filenames must exist;
1228                         * (2) all rev-args must not be interpretable
1229                         *     as a valid filename.
1230                         * but the latter we have checked in the main loop.
1231                         */
1232                        for (j = i; j < argc; j++)
1233                                verify_filename(revs->prefix, argv[j]);
1234
1235                        revs->prune_data = get_pathspec(revs->prefix,
1236                                                        argv + i);
1237                        break;
1238                }
1239        }
1240
1241        if (revs->grep_filter)
1242                revs->grep_filter->regflags |= regflags;
1243
1244        if (show_merge)
1245                prepare_show_merge(revs);
1246        if (def && !revs->pending.nr) {
1247                unsigned char sha1[20];
1248                struct object *object;
1249                unsigned mode;
1250                if (get_sha1_with_mode(def, sha1, &mode))
1251                        die("bad default revision '%s'", def);
1252                object = get_reference(revs, def, sha1, 0);
1253                add_pending_object_with_mode(revs, object, def, mode);
1254        }
1255
1256        if (revs->topo_order)
1257                revs->limited = 1;
1258
1259        if (revs->prune_data) {
1260                diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1261                /* Can't prune commits with rename following: the paths change.. */
1262                if (!revs->diffopt.follow_renames)
1263                        revs->prune_fn = try_to_simplify_commit;
1264                if (!revs->full_diff)
1265                        diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1266        }
1267        if (revs->combine_merges) {
1268                revs->ignore_merges = 0;
1269                if (revs->dense_combined_merges && !revs->diffopt.output_format)
1270                        revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1271        }
1272        revs->diffopt.abbrev = revs->abbrev;
1273        if (diff_setup_done(&revs->diffopt) < 0)
1274                die("diff_setup_done failed");
1275
1276        if (revs->grep_filter) {
1277                revs->grep_filter->all_match = all_match;
1278                compile_grep_patterns(revs->grep_filter);
1279        }
1280
1281        if (revs->reverse && revs->reflog_info)
1282                die("cannot combine --reverse with --walk-reflogs");
1283
1284        return left;
1285}
1286
1287int prepare_revision_walk(struct rev_info *revs)
1288{
1289        int nr = revs->pending.nr;
1290        struct object_array_entry *e, *list;
1291
1292        e = list = revs->pending.objects;
1293        revs->pending.nr = 0;
1294        revs->pending.alloc = 0;
1295        revs->pending.objects = NULL;
1296        while (--nr >= 0) {
1297                struct commit *commit = handle_commit(revs, e->item, e->name);
1298                if (commit) {
1299                        if (!(commit->object.flags & SEEN)) {
1300                                commit->object.flags |= SEEN;
1301                                insert_by_date(commit, &revs->commits);
1302                        }
1303                }
1304                e++;
1305        }
1306        free(list);
1307
1308        if (revs->no_walk)
1309                return 0;
1310        if (revs->limited)
1311                if (limit_list(revs) < 0)
1312                        return -1;
1313        if (revs->topo_order)
1314                sort_in_topological_order_fn(&revs->commits, revs->lifo,
1315                                             revs->topo_setter,
1316                                             revs->topo_getter);
1317        return 0;
1318}
1319
1320enum rewrite_result {
1321        rewrite_one_ok,
1322        rewrite_one_noparents,
1323        rewrite_one_error,
1324};
1325
1326static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
1327{
1328        for (;;) {
1329                struct commit *p = *pp;
1330                if (!revs->limited)
1331                        if (add_parents_to_list(revs, p, &revs->commits) < 0)
1332                                return rewrite_one_error;
1333                if (p->parents && p->parents->next)
1334                        return rewrite_one_ok;
1335                if (p->object.flags & (TREECHANGE | UNINTERESTING))
1336                        return rewrite_one_ok;
1337                if (!p->parents)
1338                        return rewrite_one_noparents;
1339                *pp = p->parents->item;
1340        }
1341}
1342
1343static void remove_duplicate_parents(struct commit *commit)
1344{
1345        struct commit_list **pp, *p;
1346
1347        /* Examine existing parents while marking ones we have seen... */
1348        pp = &commit->parents;
1349        while ((p = *pp) != NULL) {
1350                struct commit *parent = p->item;
1351                if (parent->object.flags & TMP_MARK) {
1352                        *pp = p->next;
1353                        continue;
1354                }
1355                parent->object.flags |= TMP_MARK;
1356                pp = &p->next;
1357        }
1358        /* ... and clear the temporary mark */
1359        for (p = commit->parents; p; p = p->next)
1360                p->item->object.flags &= ~TMP_MARK;
1361}
1362
1363static int rewrite_parents(struct rev_info *revs, struct commit *commit)
1364{
1365        struct commit_list **pp = &commit->parents;
1366        while (*pp) {
1367                struct commit_list *parent = *pp;
1368                switch (rewrite_one(revs, &parent->item)) {
1369                case rewrite_one_ok:
1370                        break;
1371                case rewrite_one_noparents:
1372                        *pp = parent->next;
1373                        continue;
1374                case rewrite_one_error:
1375                        return -1;
1376                }
1377                pp = &parent->next;
1378        }
1379        remove_duplicate_parents(commit);
1380        return 0;
1381}
1382
1383static int commit_match(struct commit *commit, struct rev_info *opt)
1384{
1385        if (!opt->grep_filter)
1386                return 1;
1387        return grep_buffer(opt->grep_filter,
1388                           NULL, /* we say nothing, not even filename */
1389                           commit->buffer, strlen(commit->buffer));
1390}
1391
1392static struct commit *get_revision_1(struct rev_info *revs)
1393{
1394        if (!revs->commits)
1395                return NULL;
1396
1397        do {
1398                struct commit_list *entry = revs->commits;
1399                struct commit *commit = entry->item;
1400
1401                revs->commits = entry->next;
1402                free(entry);
1403
1404                if (revs->reflog_info)
1405                        fake_reflog_parent(revs->reflog_info, commit);
1406
1407                /*
1408                 * If we haven't done the list limiting, we need to look at
1409                 * the parents here. We also need to do the date-based limiting
1410                 * that we'd otherwise have done in limit_list().
1411                 */
1412                if (!revs->limited) {
1413                        if (revs->max_age != -1 &&
1414                            (commit->date < revs->max_age))
1415                                continue;
1416                        if (add_parents_to_list(revs, commit, &revs->commits) < 0)
1417                                return NULL;
1418                }
1419                if (commit->object.flags & SHOWN)
1420                        continue;
1421
1422                if (revs->unpacked && has_sha1_pack(commit->object.sha1,
1423                                                    revs->ignore_packed))
1424                    continue;
1425
1426                if (commit->object.flags & UNINTERESTING)
1427                        continue;
1428                if (revs->min_age != -1 && (commit->date > revs->min_age))
1429                        continue;
1430                if (revs->no_merges &&
1431                    commit->parents && commit->parents->next)
1432                        continue;
1433                if (!commit_match(commit, revs))
1434                        continue;
1435                if (revs->prune_fn && revs->dense) {
1436                        /* Commit without changes? */
1437                        if (!(commit->object.flags & TREECHANGE)) {
1438                                /* drop merges unless we want parenthood */
1439                                if (!revs->parents)
1440                                        continue;
1441                                /* non-merge - always ignore it */
1442                                if (!commit->parents || !commit->parents->next)
1443                                        continue;
1444                        }
1445                        if (revs->parents && rewrite_parents(revs, commit) < 0)
1446                                return NULL;
1447                }
1448                return commit;
1449        } while (revs->commits);
1450        return NULL;
1451}
1452
1453static void gc_boundary(struct object_array *array)
1454{
1455        unsigned nr = array->nr;
1456        unsigned alloc = array->alloc;
1457        struct object_array_entry *objects = array->objects;
1458
1459        if (alloc <= nr) {
1460                unsigned i, j;
1461                for (i = j = 0; i < nr; i++) {
1462                        if (objects[i].item->flags & SHOWN)
1463                                continue;
1464                        if (i != j)
1465                                objects[j] = objects[i];
1466                        j++;
1467                }
1468                for (i = j; i < nr; i++)
1469                        objects[i].item = NULL;
1470                array->nr = j;
1471        }
1472}
1473
1474struct commit *get_revision(struct rev_info *revs)
1475{
1476        struct commit *c = NULL;
1477        struct commit_list *l;
1478
1479        if (revs->boundary == 2) {
1480                unsigned i;
1481                struct object_array *array = &revs->boundary_commits;
1482                struct object_array_entry *objects = array->objects;
1483                for (i = 0; i < array->nr; i++) {
1484                        c = (struct commit *)(objects[i].item);
1485                        if (!c)
1486                                continue;
1487                        if (!(c->object.flags & CHILD_SHOWN))
1488                                continue;
1489                        if (!(c->object.flags & SHOWN))
1490                                break;
1491                }
1492                if (array->nr <= i)
1493                        return NULL;
1494
1495                c->object.flags |= SHOWN | BOUNDARY;
1496                return c;
1497        }
1498
1499        if (revs->reverse) {
1500                int limit = -1;
1501
1502                if (0 <= revs->max_count) {
1503                        limit = revs->max_count;
1504                        if (0 < revs->skip_count)
1505                                limit += revs->skip_count;
1506                }
1507                l = NULL;
1508                while ((c = get_revision_1(revs))) {
1509                        commit_list_insert(c, &l);
1510                        if ((0 < limit) && !--limit)
1511                                break;
1512                }
1513                revs->commits = l;
1514                revs->reverse = 0;
1515                revs->max_count = -1;
1516                c = NULL;
1517        }
1518
1519        /*
1520         * Now pick up what they want to give us
1521         */
1522        c = get_revision_1(revs);
1523        if (c) {
1524                while (0 < revs->skip_count) {
1525                        revs->skip_count--;
1526                        c = get_revision_1(revs);
1527                        if (!c)
1528                                break;
1529                }
1530        }
1531
1532        /*
1533         * Check the max_count.
1534         */
1535        switch (revs->max_count) {
1536        case -1:
1537                break;
1538        case 0:
1539                c = NULL;
1540                break;
1541        default:
1542                revs->max_count--;
1543        }
1544
1545        if (c)
1546                c->object.flags |= SHOWN;
1547
1548        if (!revs->boundary) {
1549                return c;
1550        }
1551
1552        if (!c) {
1553                /*
1554                 * get_revision_1() runs out the commits, and
1555                 * we are done computing the boundaries.
1556                 * switch to boundary commits output mode.
1557                 */
1558                revs->boundary = 2;
1559                return get_revision(revs);
1560        }
1561
1562        /*
1563         * boundary commits are the commits that are parents of the
1564         * ones we got from get_revision_1() but they themselves are
1565         * not returned from get_revision_1().  Before returning
1566         * 'c', we need to mark its parents that they could be boundaries.
1567         */
1568
1569        for (l = c->parents; l; l = l->next) {
1570                struct object *p;
1571                p = &(l->item->object);
1572                if (p->flags & (CHILD_SHOWN | SHOWN))
1573                        continue;
1574                p->flags |= CHILD_SHOWN;
1575                gc_boundary(&revs->boundary_commits);
1576                add_object_array(p, NULL, &revs->boundary_commits);
1577        }
1578
1579        return c;
1580}