revision.con commit Merge branch 'jn/no-g-plus-s-on-bsd' into maint (6e89b37)
   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 "graph.h"
  10#include "grep.h"
  11#include "reflog-walk.h"
  12#include "patch-ids.h"
  13#include "decorate.h"
  14#include "log-tree.h"
  15#include "string-list.h"
  16
  17volatile show_early_output_fn_t show_early_output;
  18
  19char *path_name(const struct name_path *path, const char *name)
  20{
  21        const struct name_path *p;
  22        char *n, *m;
  23        int nlen = strlen(name);
  24        int len = nlen + 1;
  25
  26        for (p = path; p; p = p->up) {
  27                if (p->elem_len)
  28                        len += p->elem_len + 1;
  29        }
  30        n = xmalloc(len);
  31        m = n + len - (nlen + 1);
  32        strcpy(m, name);
  33        for (p = path; p; p = p->up) {
  34                if (p->elem_len) {
  35                        m -= p->elem_len + 1;
  36                        memcpy(m, p->elem, p->elem_len);
  37                        m[p->elem_len] = '/';
  38                }
  39        }
  40        return n;
  41}
  42
  43void add_object(struct object *obj,
  44                struct object_array *p,
  45                struct name_path *path,
  46                const char *name)
  47{
  48        add_object_array(obj, path_name(path, name), p);
  49}
  50
  51static void mark_blob_uninteresting(struct blob *blob)
  52{
  53        if (!blob)
  54                return;
  55        if (blob->object.flags & UNINTERESTING)
  56                return;
  57        blob->object.flags |= UNINTERESTING;
  58}
  59
  60void mark_tree_uninteresting(struct tree *tree)
  61{
  62        struct tree_desc desc;
  63        struct name_entry entry;
  64        struct object *obj = &tree->object;
  65
  66        if (!tree)
  67                return;
  68        if (obj->flags & UNINTERESTING)
  69                return;
  70        obj->flags |= UNINTERESTING;
  71        if (!has_sha1_file(obj->sha1))
  72                return;
  73        if (parse_tree(tree) < 0)
  74                die("bad tree %s", sha1_to_hex(obj->sha1));
  75
  76        init_tree_desc(&desc, tree->buffer, tree->size);
  77        while (tree_entry(&desc, &entry)) {
  78                switch (object_type(entry.mode)) {
  79                case OBJ_TREE:
  80                        mark_tree_uninteresting(lookup_tree(entry.sha1));
  81                        break;
  82                case OBJ_BLOB:
  83                        mark_blob_uninteresting(lookup_blob(entry.sha1));
  84                        break;
  85                default:
  86                        /* Subproject commit - not in this repository */
  87                        break;
  88                }
  89        }
  90
  91        /*
  92         * We don't care about the tree any more
  93         * after it has been marked uninteresting.
  94         */
  95        free(tree->buffer);
  96        tree->buffer = NULL;
  97}
  98
  99void mark_parents_uninteresting(struct commit *commit)
 100{
 101        struct commit_list *parents = commit->parents;
 102
 103        while (parents) {
 104                struct commit *commit = parents->item;
 105                if (!(commit->object.flags & UNINTERESTING)) {
 106                        commit->object.flags |= UNINTERESTING;
 107
 108                        /*
 109                         * Normally we haven't parsed the parent
 110                         * yet, so we won't have a parent of a parent
 111                         * here. However, it may turn out that we've
 112                         * reached this commit some other way (where it
 113                         * wasn't uninteresting), in which case we need
 114                         * to mark its parents recursively too..
 115                         */
 116                        if (commit->parents)
 117                                mark_parents_uninteresting(commit);
 118                }
 119
 120                /*
 121                 * A missing commit is ok iff its parent is marked
 122                 * uninteresting.
 123                 *
 124                 * We just mark such a thing parsed, so that when
 125                 * it is popped next time around, we won't be trying
 126                 * to parse it and get an error.
 127                 */
 128                if (!has_sha1_file(commit->object.sha1))
 129                        commit->object.parsed = 1;
 130                parents = parents->next;
 131        }
 132}
 133
 134static void add_pending_object_with_mode(struct rev_info *revs, struct object *obj, const char *name, unsigned mode)
 135{
 136        if (!obj)
 137                return;
 138        if (revs->no_walk && (obj->flags & UNINTERESTING))
 139                revs->no_walk = 0;
 140        if (revs->reflog_info && obj->type == OBJ_COMMIT) {
 141                struct strbuf buf = STRBUF_INIT;
 142                int len = interpret_branch_name(name, &buf);
 143                int st;
 144
 145                if (0 < len && name[len] && buf.len)
 146                        strbuf_addstr(&buf, name + len);
 147                st = add_reflog_for_walk(revs->reflog_info,
 148                                         (struct commit *)obj,
 149                                         buf.buf[0] ? buf.buf: name);
 150                strbuf_release(&buf);
 151                if (st)
 152                        return;
 153        }
 154        add_object_array_with_mode(obj, name, &revs->pending, mode);
 155}
 156
 157void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
 158{
 159        add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
 160}
 161
 162void add_head_to_pending(struct rev_info *revs)
 163{
 164        unsigned char sha1[20];
 165        struct object *obj;
 166        if (get_sha1("HEAD", sha1))
 167                return;
 168        obj = parse_object(sha1);
 169        if (!obj)
 170                return;
 171        add_pending_object(revs, obj, "HEAD");
 172}
 173
 174static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
 175{
 176        struct object *object;
 177
 178        object = parse_object(sha1);
 179        if (!object) {
 180                if (revs->ignore_missing)
 181                        return object;
 182                die("bad object %s", name);
 183        }
 184        object->flags |= flags;
 185        return object;
 186}
 187
 188static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
 189{
 190        unsigned long flags = object->flags;
 191
 192        /*
 193         * Tag object? Look what it points to..
 194         */
 195        while (object->type == OBJ_TAG) {
 196                struct tag *tag = (struct tag *) object;
 197                if (revs->tag_objects && !(flags & UNINTERESTING))
 198                        add_pending_object(revs, object, tag->tag);
 199                if (!tag->tagged)
 200                        die("bad tag");
 201                object = parse_object(tag->tagged->sha1);
 202                if (!object) {
 203                        if (flags & UNINTERESTING)
 204                                return NULL;
 205                        die("bad object %s", sha1_to_hex(tag->tagged->sha1));
 206                }
 207        }
 208
 209        /*
 210         * Commit object? Just return it, we'll do all the complex
 211         * reachability crud.
 212         */
 213        if (object->type == OBJ_COMMIT) {
 214                struct commit *commit = (struct commit *)object;
 215                if (parse_commit(commit) < 0)
 216                        die("unable to parse commit %s", name);
 217                if (flags & UNINTERESTING) {
 218                        commit->object.flags |= UNINTERESTING;
 219                        mark_parents_uninteresting(commit);
 220                        revs->limited = 1;
 221                }
 222                if (revs->show_source && !commit->util)
 223                        commit->util = (void *) name;
 224                return commit;
 225        }
 226
 227        /*
 228         * Tree object? Either mark it uninteresting, or add it
 229         * to the list of objects to look at later..
 230         */
 231        if (object->type == OBJ_TREE) {
 232                struct tree *tree = (struct tree *)object;
 233                if (!revs->tree_objects)
 234                        return NULL;
 235                if (flags & UNINTERESTING) {
 236                        mark_tree_uninteresting(tree);
 237                        return NULL;
 238                }
 239                add_pending_object(revs, object, "");
 240                return NULL;
 241        }
 242
 243        /*
 244         * Blob object? You know the drill by now..
 245         */
 246        if (object->type == OBJ_BLOB) {
 247                struct blob *blob = (struct blob *)object;
 248                if (!revs->blob_objects)
 249                        return NULL;
 250                if (flags & UNINTERESTING) {
 251                        mark_blob_uninteresting(blob);
 252                        return NULL;
 253                }
 254                add_pending_object(revs, object, "");
 255                return NULL;
 256        }
 257        die("%s is unknown object", name);
 258}
 259
 260static int everybody_uninteresting(struct commit_list *orig)
 261{
 262        struct commit_list *list = orig;
 263        while (list) {
 264                struct commit *commit = list->item;
 265                list = list->next;
 266                if (commit->object.flags & UNINTERESTING)
 267                        continue;
 268                return 0;
 269        }
 270        return 1;
 271}
 272
 273/*
 274 * The goal is to get REV_TREE_NEW as the result only if the
 275 * diff consists of all '+' (and no other changes), REV_TREE_OLD
 276 * if the whole diff is removal of old data, and otherwise
 277 * REV_TREE_DIFFERENT (of course if the trees are the same we
 278 * want REV_TREE_SAME).
 279 * That means that once we get to REV_TREE_DIFFERENT, we do not
 280 * have to look any further.
 281 */
 282static int tree_difference = REV_TREE_SAME;
 283
 284static void file_add_remove(struct diff_options *options,
 285                    int addremove, unsigned mode,
 286                    const unsigned char *sha1,
 287                    const char *fullpath, unsigned dirty_submodule)
 288{
 289        int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
 290
 291        tree_difference |= diff;
 292        if (tree_difference == REV_TREE_DIFFERENT)
 293                DIFF_OPT_SET(options, HAS_CHANGES);
 294}
 295
 296static void file_change(struct diff_options *options,
 297                 unsigned old_mode, unsigned new_mode,
 298                 const unsigned char *old_sha1,
 299                 const unsigned char *new_sha1,
 300                 const char *fullpath,
 301                 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
 302{
 303        tree_difference = REV_TREE_DIFFERENT;
 304        DIFF_OPT_SET(options, HAS_CHANGES);
 305}
 306
 307static int rev_compare_tree(struct rev_info *revs, struct commit *parent, struct commit *commit)
 308{
 309        struct tree *t1 = parent->tree;
 310        struct tree *t2 = commit->tree;
 311
 312        if (!t1)
 313                return REV_TREE_NEW;
 314        if (!t2)
 315                return REV_TREE_OLD;
 316
 317        if (revs->simplify_by_decoration) {
 318                /*
 319                 * If we are simplifying by decoration, then the commit
 320                 * is worth showing if it has a tag pointing at it.
 321                 */
 322                if (lookup_decoration(&name_decoration, &commit->object))
 323                        return REV_TREE_DIFFERENT;
 324                /*
 325                 * A commit that is not pointed by a tag is uninteresting
 326                 * if we are not limited by path.  This means that you will
 327                 * see the usual "commits that touch the paths" plus any
 328                 * tagged commit by specifying both --simplify-by-decoration
 329                 * and pathspec.
 330                 */
 331                if (!revs->prune_data.nr)
 332                        return REV_TREE_SAME;
 333        }
 334
 335        tree_difference = REV_TREE_SAME;
 336        DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
 337        if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
 338                           &revs->pruning) < 0)
 339                return REV_TREE_DIFFERENT;
 340        return tree_difference;
 341}
 342
 343static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
 344{
 345        int retval;
 346        void *tree;
 347        unsigned long size;
 348        struct tree_desc empty, real;
 349        struct tree *t1 = commit->tree;
 350
 351        if (!t1)
 352                return 0;
 353
 354        tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
 355        if (!tree)
 356                return 0;
 357        init_tree_desc(&real, tree, size);
 358        init_tree_desc(&empty, "", 0);
 359
 360        tree_difference = REV_TREE_SAME;
 361        DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
 362        retval = diff_tree(&empty, &real, "", &revs->pruning);
 363        free(tree);
 364
 365        return retval >= 0 && (tree_difference == REV_TREE_SAME);
 366}
 367
 368static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
 369{
 370        struct commit_list **pp, *parent;
 371        int tree_changed = 0, tree_same = 0;
 372
 373        /*
 374         * If we don't do pruning, everything is interesting
 375         */
 376        if (!revs->prune)
 377                return;
 378
 379        if (!commit->tree)
 380                return;
 381
 382        if (!commit->parents) {
 383                if (rev_same_tree_as_empty(revs, commit))
 384                        commit->object.flags |= TREESAME;
 385                return;
 386        }
 387
 388        /*
 389         * Normal non-merge commit? If we don't want to make the
 390         * history dense, we consider it always to be a change..
 391         */
 392        if (!revs->dense && !commit->parents->next)
 393                return;
 394
 395        pp = &commit->parents;
 396        while ((parent = *pp) != NULL) {
 397                struct commit *p = parent->item;
 398
 399                if (parse_commit(p) < 0)
 400                        die("cannot simplify commit %s (because of %s)",
 401                            sha1_to_hex(commit->object.sha1),
 402                            sha1_to_hex(p->object.sha1));
 403                switch (rev_compare_tree(revs, p, commit)) {
 404                case REV_TREE_SAME:
 405                        tree_same = 1;
 406                        if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
 407                                /* Even if a merge with an uninteresting
 408                                 * side branch brought the entire change
 409                                 * we are interested in, we do not want
 410                                 * to lose the other branches of this
 411                                 * merge, so we just keep going.
 412                                 */
 413                                pp = &parent->next;
 414                                continue;
 415                        }
 416                        parent->next = NULL;
 417                        commit->parents = parent;
 418                        commit->object.flags |= TREESAME;
 419                        return;
 420
 421                case REV_TREE_NEW:
 422                        if (revs->remove_empty_trees &&
 423                            rev_same_tree_as_empty(revs, p)) {
 424                                /* We are adding all the specified
 425                                 * paths from this parent, so the
 426                                 * history beyond this parent is not
 427                                 * interesting.  Remove its parents
 428                                 * (they are grandparents for us).
 429                                 * IOW, we pretend this parent is a
 430                                 * "root" commit.
 431                                 */
 432                                if (parse_commit(p) < 0)
 433                                        die("cannot simplify commit %s (invalid %s)",
 434                                            sha1_to_hex(commit->object.sha1),
 435                                            sha1_to_hex(p->object.sha1));
 436                                p->parents = NULL;
 437                        }
 438                /* fallthrough */
 439                case REV_TREE_OLD:
 440                case REV_TREE_DIFFERENT:
 441                        tree_changed = 1;
 442                        pp = &parent->next;
 443                        continue;
 444                }
 445                die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
 446        }
 447        if (tree_changed && !tree_same)
 448                return;
 449        commit->object.flags |= TREESAME;
 450}
 451
 452static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
 453                    struct commit_list *cached_base, struct commit_list **cache)
 454{
 455        struct commit_list *new_entry;
 456
 457        if (cached_base && p->date < cached_base->item->date)
 458                new_entry = commit_list_insert_by_date(p, &cached_base->next);
 459        else
 460                new_entry = commit_list_insert_by_date(p, head);
 461
 462        if (cache && (!*cache || p->date < (*cache)->item->date))
 463                *cache = new_entry;
 464}
 465
 466static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
 467                    struct commit_list **list, struct commit_list **cache_ptr)
 468{
 469        struct commit_list *parent = commit->parents;
 470        unsigned left_flag;
 471        struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
 472
 473        if (commit->object.flags & ADDED)
 474                return 0;
 475        commit->object.flags |= ADDED;
 476
 477        /*
 478         * If the commit is uninteresting, don't try to
 479         * prune parents - we want the maximal uninteresting
 480         * set.
 481         *
 482         * Normally we haven't parsed the parent
 483         * yet, so we won't have a parent of a parent
 484         * here. However, it may turn out that we've
 485         * reached this commit some other way (where it
 486         * wasn't uninteresting), in which case we need
 487         * to mark its parents recursively too..
 488         */
 489        if (commit->object.flags & UNINTERESTING) {
 490                while (parent) {
 491                        struct commit *p = parent->item;
 492                        parent = parent->next;
 493                        if (p)
 494                                p->object.flags |= UNINTERESTING;
 495                        if (parse_commit(p) < 0)
 496                                continue;
 497                        if (p->parents)
 498                                mark_parents_uninteresting(p);
 499                        if (p->object.flags & SEEN)
 500                                continue;
 501                        p->object.flags |= SEEN;
 502                        commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
 503                }
 504                return 0;
 505        }
 506
 507        /*
 508         * Ok, the commit wasn't uninteresting. Try to
 509         * simplify the commit history and find the parent
 510         * that has no differences in the path set if one exists.
 511         */
 512        try_to_simplify_commit(revs, commit);
 513
 514        if (revs->no_walk)
 515                return 0;
 516
 517        left_flag = (commit->object.flags & SYMMETRIC_LEFT);
 518
 519        for (parent = commit->parents; parent; parent = parent->next) {
 520                struct commit *p = parent->item;
 521
 522                if (parse_commit(p) < 0)
 523                        return -1;
 524                if (revs->show_source && !p->util)
 525                        p->util = commit->util;
 526                p->object.flags |= left_flag;
 527                if (!(p->object.flags & SEEN)) {
 528                        p->object.flags |= SEEN;
 529                        commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
 530                }
 531                if (revs->first_parent_only)
 532                        break;
 533        }
 534        return 0;
 535}
 536
 537static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
 538{
 539        struct commit_list *p;
 540        int left_count = 0, right_count = 0;
 541        int left_first;
 542        struct patch_ids ids;
 543        unsigned cherry_flag;
 544
 545        /* First count the commits on the left and on the right */
 546        for (p = list; p; p = p->next) {
 547                struct commit *commit = p->item;
 548                unsigned flags = commit->object.flags;
 549                if (flags & BOUNDARY)
 550                        ;
 551                else if (flags & SYMMETRIC_LEFT)
 552                        left_count++;
 553                else
 554                        right_count++;
 555        }
 556
 557        if (!left_count || !right_count)
 558                return;
 559
 560        left_first = left_count < right_count;
 561        init_patch_ids(&ids);
 562        ids.diffopts.pathspec = revs->diffopt.pathspec;
 563
 564        /* Compute patch-ids for one side */
 565        for (p = list; p; p = p->next) {
 566                struct commit *commit = p->item;
 567                unsigned flags = commit->object.flags;
 568
 569                if (flags & BOUNDARY)
 570                        continue;
 571                /*
 572                 * If we have fewer left, left_first is set and we omit
 573                 * commits on the right branch in this loop.  If we have
 574                 * fewer right, we skip the left ones.
 575                 */
 576                if (left_first != !!(flags & SYMMETRIC_LEFT))
 577                        continue;
 578                commit->util = add_commit_patch_id(commit, &ids);
 579        }
 580
 581        /* either cherry_mark or cherry_pick are true */
 582        cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
 583
 584        /* Check the other side */
 585        for (p = list; p; p = p->next) {
 586                struct commit *commit = p->item;
 587                struct patch_id *id;
 588                unsigned flags = commit->object.flags;
 589
 590                if (flags & BOUNDARY)
 591                        continue;
 592                /*
 593                 * If we have fewer left, left_first is set and we omit
 594                 * commits on the left branch in this loop.
 595                 */
 596                if (left_first == !!(flags & SYMMETRIC_LEFT))
 597                        continue;
 598
 599                /*
 600                 * Have we seen the same patch id?
 601                 */
 602                id = has_commit_patch_id(commit, &ids);
 603                if (!id)
 604                        continue;
 605                id->seen = 1;
 606                commit->object.flags |= cherry_flag;
 607        }
 608
 609        /* Now check the original side for seen ones */
 610        for (p = list; p; p = p->next) {
 611                struct commit *commit = p->item;
 612                struct patch_id *ent;
 613
 614                ent = commit->util;
 615                if (!ent)
 616                        continue;
 617                if (ent->seen)
 618                        commit->object.flags |= cherry_flag;
 619                commit->util = NULL;
 620        }
 621
 622        free_patch_ids(&ids);
 623}
 624
 625/* How many extra uninteresting commits we want to see.. */
 626#define SLOP 5
 627
 628static int still_interesting(struct commit_list *src, unsigned long date, int slop)
 629{
 630        /*
 631         * No source list at all? We're definitely done..
 632         */
 633        if (!src)
 634                return 0;
 635
 636        /*
 637         * Does the destination list contain entries with a date
 638         * before the source list? Definitely _not_ done.
 639         */
 640        if (date < src->item->date)
 641                return SLOP;
 642
 643        /*
 644         * Does the source list still have interesting commits in
 645         * it? Definitely not done..
 646         */
 647        if (!everybody_uninteresting(src))
 648                return SLOP;
 649
 650        /* Ok, we're closing in.. */
 651        return slop-1;
 652}
 653
 654/*
 655 * "rev-list --ancestry-path A..B" computes commits that are ancestors
 656 * of B but not ancestors of A but further limits the result to those
 657 * that are descendants of A.  This takes the list of bottom commits and
 658 * the result of "A..B" without --ancestry-path, and limits the latter
 659 * further to the ones that can reach one of the commits in "bottom".
 660 */
 661static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
 662{
 663        struct commit_list *p;
 664        struct commit_list *rlist = NULL;
 665        int made_progress;
 666
 667        /*
 668         * Reverse the list so that it will be likely that we would
 669         * process parents before children.
 670         */
 671        for (p = list; p; p = p->next)
 672                commit_list_insert(p->item, &rlist);
 673
 674        for (p = bottom; p; p = p->next)
 675                p->item->object.flags |= TMP_MARK;
 676
 677        /*
 678         * Mark the ones that can reach bottom commits in "list",
 679         * in a bottom-up fashion.
 680         */
 681        do {
 682                made_progress = 0;
 683                for (p = rlist; p; p = p->next) {
 684                        struct commit *c = p->item;
 685                        struct commit_list *parents;
 686                        if (c->object.flags & (TMP_MARK | UNINTERESTING))
 687                                continue;
 688                        for (parents = c->parents;
 689                             parents;
 690                             parents = parents->next) {
 691                                if (!(parents->item->object.flags & TMP_MARK))
 692                                        continue;
 693                                c->object.flags |= TMP_MARK;
 694                                made_progress = 1;
 695                                break;
 696                        }
 697                }
 698        } while (made_progress);
 699
 700        /*
 701         * NEEDSWORK: decide if we want to remove parents that are
 702         * not marked with TMP_MARK from commit->parents for commits
 703         * in the resulting list.  We may not want to do that, though.
 704         */
 705
 706        /*
 707         * The ones that are not marked with TMP_MARK are uninteresting
 708         */
 709        for (p = list; p; p = p->next) {
 710                struct commit *c = p->item;
 711                if (c->object.flags & TMP_MARK)
 712                        continue;
 713                c->object.flags |= UNINTERESTING;
 714        }
 715
 716        /* We are done with the TMP_MARK */
 717        for (p = list; p; p = p->next)
 718                p->item->object.flags &= ~TMP_MARK;
 719        for (p = bottom; p; p = p->next)
 720                p->item->object.flags &= ~TMP_MARK;
 721        free_commit_list(rlist);
 722}
 723
 724/*
 725 * Before walking the history, keep the set of "negative" refs the
 726 * caller has asked to exclude.
 727 *
 728 * This is used to compute "rev-list --ancestry-path A..B", as we need
 729 * to filter the result of "A..B" further to the ones that can actually
 730 * reach A.
 731 */
 732static struct commit_list *collect_bottom_commits(struct rev_info *revs)
 733{
 734        struct commit_list *bottom = NULL;
 735        int i;
 736        for (i = 0; i < revs->cmdline.nr; i++) {
 737                struct rev_cmdline_entry *elem = &revs->cmdline.rev[i];
 738                if ((elem->flags & UNINTERESTING) &&
 739                    elem->item->type == OBJ_COMMIT)
 740                        commit_list_insert((struct commit *)elem->item, &bottom);
 741        }
 742        return bottom;
 743}
 744
 745/* Assumes either left_only or right_only is set */
 746static void limit_left_right(struct commit_list *list, struct rev_info *revs)
 747{
 748        struct commit_list *p;
 749
 750        for (p = list; p; p = p->next) {
 751                struct commit *commit = p->item;
 752
 753                if (revs->right_only) {
 754                        if (commit->object.flags & SYMMETRIC_LEFT)
 755                                commit->object.flags |= SHOWN;
 756                } else  /* revs->left_only is set */
 757                        if (!(commit->object.flags & SYMMETRIC_LEFT))
 758                                commit->object.flags |= SHOWN;
 759        }
 760}
 761
 762static int limit_list(struct rev_info *revs)
 763{
 764        int slop = SLOP;
 765        unsigned long date = ~0ul;
 766        struct commit_list *list = revs->commits;
 767        struct commit_list *newlist = NULL;
 768        struct commit_list **p = &newlist;
 769        struct commit_list *bottom = NULL;
 770
 771        if (revs->ancestry_path) {
 772                bottom = collect_bottom_commits(revs);
 773                if (!bottom)
 774                        die("--ancestry-path given but there are no bottom commits");
 775        }
 776
 777        while (list) {
 778                struct commit_list *entry = list;
 779                struct commit *commit = list->item;
 780                struct object *obj = &commit->object;
 781                show_early_output_fn_t show;
 782
 783                list = list->next;
 784                free(entry);
 785
 786                if (revs->max_age != -1 && (commit->date < revs->max_age))
 787                        obj->flags |= UNINTERESTING;
 788                if (add_parents_to_list(revs, commit, &list, NULL) < 0)
 789                        return -1;
 790                if (obj->flags & UNINTERESTING) {
 791                        mark_parents_uninteresting(commit);
 792                        if (revs->show_all)
 793                                p = &commit_list_insert(commit, p)->next;
 794                        slop = still_interesting(list, date, slop);
 795                        if (slop)
 796                                continue;
 797                        /* If showing all, add the whole pending list to the end */
 798                        if (revs->show_all)
 799                                *p = list;
 800                        break;
 801                }
 802                if (revs->min_age != -1 && (commit->date > revs->min_age))
 803                        continue;
 804                date = commit->date;
 805                p = &commit_list_insert(commit, p)->next;
 806
 807                show = show_early_output;
 808                if (!show)
 809                        continue;
 810
 811                show(revs, newlist);
 812                show_early_output = NULL;
 813        }
 814        if (revs->cherry_pick || revs->cherry_mark)
 815                cherry_pick_list(newlist, revs);
 816
 817        if (revs->left_only || revs->right_only)
 818                limit_left_right(newlist, revs);
 819
 820        if (bottom) {
 821                limit_to_ancestry(bottom, newlist);
 822                free_commit_list(bottom);
 823        }
 824
 825        revs->commits = newlist;
 826        return 0;
 827}
 828
 829static void add_rev_cmdline(struct rev_info *revs,
 830                            struct object *item,
 831                            const char *name,
 832                            int whence,
 833                            unsigned flags)
 834{
 835        struct rev_cmdline_info *info = &revs->cmdline;
 836        int nr = info->nr;
 837
 838        ALLOC_GROW(info->rev, nr + 1, info->alloc);
 839        info->rev[nr].item = item;
 840        info->rev[nr].name = name;
 841        info->rev[nr].whence = whence;
 842        info->rev[nr].flags = flags;
 843        info->nr++;
 844}
 845
 846struct all_refs_cb {
 847        int all_flags;
 848        int warned_bad_reflog;
 849        struct rev_info *all_revs;
 850        const char *name_for_errormsg;
 851};
 852
 853static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 854{
 855        struct all_refs_cb *cb = cb_data;
 856        struct object *object = get_reference(cb->all_revs, path, sha1,
 857                                              cb->all_flags);
 858        add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
 859        add_pending_object(cb->all_revs, object, path);
 860        return 0;
 861}
 862
 863static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
 864        unsigned flags)
 865{
 866        cb->all_revs = revs;
 867        cb->all_flags = flags;
 868}
 869
 870static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
 871                int (*for_each)(const char *, each_ref_fn, void *))
 872{
 873        struct all_refs_cb cb;
 874        init_all_refs_cb(&cb, revs, flags);
 875        for_each(submodule, handle_one_ref, &cb);
 876}
 877
 878static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
 879{
 880        struct all_refs_cb *cb = cb_data;
 881        if (!is_null_sha1(sha1)) {
 882                struct object *o = parse_object(sha1);
 883                if (o) {
 884                        o->flags |= cb->all_flags;
 885                        /* ??? CMDLINEFLAGS ??? */
 886                        add_pending_object(cb->all_revs, o, "");
 887                }
 888                else if (!cb->warned_bad_reflog) {
 889                        warning("reflog of '%s' references pruned commits",
 890                                cb->name_for_errormsg);
 891                        cb->warned_bad_reflog = 1;
 892                }
 893        }
 894}
 895
 896static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 897                const char *email, unsigned long timestamp, int tz,
 898                const char *message, void *cb_data)
 899{
 900        handle_one_reflog_commit(osha1, cb_data);
 901        handle_one_reflog_commit(nsha1, cb_data);
 902        return 0;
 903}
 904
 905static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 906{
 907        struct all_refs_cb *cb = cb_data;
 908        cb->warned_bad_reflog = 0;
 909        cb->name_for_errormsg = path;
 910        for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
 911        return 0;
 912}
 913
 914static void handle_reflog(struct rev_info *revs, unsigned flags)
 915{
 916        struct all_refs_cb cb;
 917        cb.all_revs = revs;
 918        cb.all_flags = flags;
 919        for_each_reflog(handle_one_reflog, &cb);
 920}
 921
 922static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
 923{
 924        unsigned char sha1[20];
 925        struct object *it;
 926        struct commit *commit;
 927        struct commit_list *parents;
 928        const char *arg = arg_;
 929
 930        if (*arg == '^') {
 931                flags ^= UNINTERESTING;
 932                arg++;
 933        }
 934        if (get_sha1(arg, sha1))
 935                return 0;
 936        while (1) {
 937                it = get_reference(revs, arg, sha1, 0);
 938                if (!it && revs->ignore_missing)
 939                        return 0;
 940                if (it->type != OBJ_TAG)
 941                        break;
 942                if (!((struct tag*)it)->tagged)
 943                        return 0;
 944                hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
 945        }
 946        if (it->type != OBJ_COMMIT)
 947                return 0;
 948        commit = (struct commit *)it;
 949        for (parents = commit->parents; parents; parents = parents->next) {
 950                it = &parents->item->object;
 951                it->flags |= flags;
 952                add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
 953                add_pending_object(revs, it, arg);
 954        }
 955        return 1;
 956}
 957
 958void init_revisions(struct rev_info *revs, const char *prefix)
 959{
 960        memset(revs, 0, sizeof(*revs));
 961
 962        revs->abbrev = DEFAULT_ABBREV;
 963        revs->ignore_merges = 1;
 964        revs->simplify_history = 1;
 965        DIFF_OPT_SET(&revs->pruning, RECURSIVE);
 966        DIFF_OPT_SET(&revs->pruning, QUICK);
 967        revs->pruning.add_remove = file_add_remove;
 968        revs->pruning.change = file_change;
 969        revs->lifo = 1;
 970        revs->dense = 1;
 971        revs->prefix = prefix;
 972        revs->max_age = -1;
 973        revs->min_age = -1;
 974        revs->skip_count = -1;
 975        revs->max_count = -1;
 976        revs->max_parents = -1;
 977
 978        revs->commit_format = CMIT_FMT_DEFAULT;
 979
 980        revs->grep_filter.status_only = 1;
 981        revs->grep_filter.pattern_tail = &(revs->grep_filter.pattern_list);
 982        revs->grep_filter.header_tail = &(revs->grep_filter.header_list);
 983        revs->grep_filter.regflags = REG_NEWLINE;
 984
 985        diff_setup(&revs->diffopt);
 986        if (prefix && !revs->diffopt.prefix) {
 987                revs->diffopt.prefix = prefix;
 988                revs->diffopt.prefix_length = strlen(prefix);
 989        }
 990
 991        revs->notes_opt.use_default_notes = -1;
 992}
 993
 994static void add_pending_commit_list(struct rev_info *revs,
 995                                    struct commit_list *commit_list,
 996                                    unsigned int flags)
 997{
 998        while (commit_list) {
 999                struct object *object = &commit_list->item->object;
1000                object->flags |= flags;
1001                add_pending_object(revs, object, sha1_to_hex(object->sha1));
1002                commit_list = commit_list->next;
1003        }
1004}
1005
1006static void prepare_show_merge(struct rev_info *revs)
1007{
1008        struct commit_list *bases;
1009        struct commit *head, *other;
1010        unsigned char sha1[20];
1011        const char **prune = NULL;
1012        int i, prune_num = 1; /* counting terminating NULL */
1013
1014        if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
1015                die("--merge without HEAD?");
1016        if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
1017                die("--merge without MERGE_HEAD?");
1018        add_pending_object(revs, &head->object, "HEAD");
1019        add_pending_object(revs, &other->object, "MERGE_HEAD");
1020        bases = get_merge_bases(head, other, 1);
1021        add_pending_commit_list(revs, bases, UNINTERESTING);
1022        free_commit_list(bases);
1023        head->object.flags |= SYMMETRIC_LEFT;
1024
1025        if (!active_nr)
1026                read_cache();
1027        for (i = 0; i < active_nr; i++) {
1028                struct cache_entry *ce = active_cache[i];
1029                if (!ce_stage(ce))
1030                        continue;
1031                if (ce_path_match(ce, &revs->prune_data)) {
1032                        prune_num++;
1033                        prune = xrealloc(prune, sizeof(*prune) * prune_num);
1034                        prune[prune_num-2] = ce->name;
1035                        prune[prune_num-1] = NULL;
1036                }
1037                while ((i+1 < active_nr) &&
1038                       ce_same_name(ce, active_cache[i+1]))
1039                        i++;
1040        }
1041        free_pathspec(&revs->prune_data);
1042        init_pathspec(&revs->prune_data, prune);
1043        revs->limited = 1;
1044}
1045
1046int handle_revision_arg(const char *arg_, struct rev_info *revs,
1047                        int flags,
1048                        int cant_be_filename)
1049{
1050        unsigned mode;
1051        char *dotdot;
1052        struct object *object;
1053        unsigned char sha1[20];
1054        int local_flags;
1055        const char *arg = arg_;
1056
1057        dotdot = strstr(arg, "..");
1058        if (dotdot) {
1059                unsigned char from_sha1[20];
1060                const char *next = dotdot + 2;
1061                const char *this = arg;
1062                int symmetric = *next == '.';
1063                unsigned int flags_exclude = flags ^ UNINTERESTING;
1064                unsigned int a_flags;
1065
1066                *dotdot = 0;
1067                next += symmetric;
1068
1069                if (!*next)
1070                        next = "HEAD";
1071                if (dotdot == arg)
1072                        this = "HEAD";
1073                if (!get_sha1(this, from_sha1) &&
1074                    !get_sha1(next, sha1)) {
1075                        struct commit *a, *b;
1076                        struct commit_list *exclude;
1077
1078                        a = lookup_commit_reference(from_sha1);
1079                        b = lookup_commit_reference(sha1);
1080                        if (!a || !b) {
1081                                if (revs->ignore_missing)
1082                                        return 0;
1083                                die(symmetric ?
1084                                    "Invalid symmetric difference expression %s...%s" :
1085                                    "Invalid revision range %s..%s",
1086                                    arg, next);
1087                        }
1088
1089                        if (!cant_be_filename) {
1090                                *dotdot = '.';
1091                                verify_non_filename(revs->prefix, arg);
1092                        }
1093
1094                        if (symmetric) {
1095                                exclude = get_merge_bases(a, b, 1);
1096                                add_pending_commit_list(revs, exclude,
1097                                                        flags_exclude);
1098                                free_commit_list(exclude);
1099                                a_flags = flags | SYMMETRIC_LEFT;
1100                        } else
1101                                a_flags = flags_exclude;
1102                        a->object.flags |= a_flags;
1103                        b->object.flags |= flags;
1104                        add_rev_cmdline(revs, &a->object, this,
1105                                        REV_CMD_LEFT, a_flags);
1106                        add_rev_cmdline(revs, &b->object, next,
1107                                        REV_CMD_RIGHT, flags);
1108                        add_pending_object(revs, &a->object, this);
1109                        add_pending_object(revs, &b->object, next);
1110                        return 0;
1111                }
1112                *dotdot = '.';
1113        }
1114        dotdot = strstr(arg, "^@");
1115        if (dotdot && !dotdot[2]) {
1116                *dotdot = 0;
1117                if (add_parents_only(revs, arg, flags))
1118                        return 0;
1119                *dotdot = '^';
1120        }
1121        dotdot = strstr(arg, "^!");
1122        if (dotdot && !dotdot[2]) {
1123                *dotdot = 0;
1124                if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
1125                        *dotdot = '^';
1126        }
1127
1128        local_flags = 0;
1129        if (*arg == '^') {
1130                local_flags = UNINTERESTING;
1131                arg++;
1132        }
1133        if (get_sha1_with_mode(arg, sha1, &mode))
1134                return revs->ignore_missing ? 0 : -1;
1135        if (!cant_be_filename)
1136                verify_non_filename(revs->prefix, arg);
1137        object = get_reference(revs, arg, sha1, flags ^ local_flags);
1138        add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1139        add_pending_object_with_mode(revs, object, arg, mode);
1140        return 0;
1141}
1142
1143struct cmdline_pathspec {
1144        int alloc;
1145        int nr;
1146        const char **path;
1147};
1148
1149static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1150{
1151        while (*av) {
1152                ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1153                prune->path[prune->nr++] = *(av++);
1154        }
1155}
1156
1157static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1158                                     struct cmdline_pathspec *prune)
1159{
1160        while (strbuf_getwholeline(sb, stdin, '\n') != EOF) {
1161                int len = sb->len;
1162                if (len && sb->buf[len - 1] == '\n')
1163                        sb->buf[--len] = '\0';
1164                ALLOC_GROW(prune->path, prune->nr+1, prune->alloc);
1165                prune->path[prune->nr++] = xstrdup(sb->buf);
1166        }
1167}
1168
1169static void read_revisions_from_stdin(struct rev_info *revs,
1170                                      struct cmdline_pathspec *prune)
1171{
1172        struct strbuf sb;
1173        int seen_dashdash = 0;
1174
1175        strbuf_init(&sb, 1000);
1176        while (strbuf_getwholeline(&sb, stdin, '\n') != EOF) {
1177                int len = sb.len;
1178                if (len && sb.buf[len - 1] == '\n')
1179                        sb.buf[--len] = '\0';
1180                if (!len)
1181                        break;
1182                if (sb.buf[0] == '-') {
1183                        if (len == 2 && sb.buf[1] == '-') {
1184                                seen_dashdash = 1;
1185                                break;
1186                        }
1187                        die("options not supported in --stdin mode");
1188                }
1189                if (handle_revision_arg(sb.buf, revs, 0, 1))
1190                        die("bad revision '%s'", sb.buf);
1191        }
1192        if (seen_dashdash)
1193                read_pathspec_from_stdin(revs, &sb, prune);
1194        strbuf_release(&sb);
1195}
1196
1197static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1198{
1199        append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1200}
1201
1202static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1203{
1204        append_header_grep_pattern(&revs->grep_filter, field, pattern);
1205}
1206
1207static void add_message_grep(struct rev_info *revs, const char *pattern)
1208{
1209        add_grep(revs, pattern, GREP_PATTERN_BODY);
1210}
1211
1212static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1213                               int *unkc, const char **unkv)
1214{
1215        const char *arg = argv[0];
1216        const char *optarg;
1217        int argcount;
1218
1219        /* pseudo revision arguments */
1220        if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1221            !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1222            !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1223            !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1224            !strcmp(arg, "--bisect") || !prefixcmp(arg, "--glob=") ||
1225            !prefixcmp(arg, "--branches=") || !prefixcmp(arg, "--tags=") ||
1226            !prefixcmp(arg, "--remotes="))
1227        {
1228                unkv[(*unkc)++] = arg;
1229                return 1;
1230        }
1231
1232        if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1233                revs->max_count = atoi(optarg);
1234                revs->no_walk = 0;
1235                return argcount;
1236        } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1237                revs->skip_count = atoi(optarg);
1238                return argcount;
1239        } else if ((*arg == '-') && isdigit(arg[1])) {
1240        /* accept -<digit>, like traditional "head" */
1241                revs->max_count = atoi(arg + 1);
1242                revs->no_walk = 0;
1243        } else if (!strcmp(arg, "-n")) {
1244                if (argc <= 1)
1245                        return error("-n requires an argument");
1246                revs->max_count = atoi(argv[1]);
1247                revs->no_walk = 0;
1248                return 2;
1249        } else if (!prefixcmp(arg, "-n")) {
1250                revs->max_count = atoi(arg + 2);
1251                revs->no_walk = 0;
1252        } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1253                revs->max_age = atoi(optarg);
1254                return argcount;
1255        } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1256                revs->max_age = approxidate(optarg);
1257                return argcount;
1258        } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1259                revs->max_age = approxidate(optarg);
1260                return argcount;
1261        } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1262                revs->min_age = atoi(optarg);
1263                return argcount;
1264        } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1265                revs->min_age = approxidate(optarg);
1266                return argcount;
1267        } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1268                revs->min_age = approxidate(optarg);
1269                return argcount;
1270        } else if (!strcmp(arg, "--first-parent")) {
1271                revs->first_parent_only = 1;
1272        } else if (!strcmp(arg, "--ancestry-path")) {
1273                revs->ancestry_path = 1;
1274                revs->simplify_history = 0;
1275                revs->limited = 1;
1276        } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1277                init_reflog_walk(&revs->reflog_info);
1278        } else if (!strcmp(arg, "--default")) {
1279                if (argc <= 1)
1280                        return error("bad --default argument");
1281                revs->def = argv[1];
1282                return 2;
1283        } else if (!strcmp(arg, "--merge")) {
1284                revs->show_merge = 1;
1285        } else if (!strcmp(arg, "--topo-order")) {
1286                revs->lifo = 1;
1287                revs->topo_order = 1;
1288        } else if (!strcmp(arg, "--simplify-merges")) {
1289                revs->simplify_merges = 1;
1290                revs->rewrite_parents = 1;
1291                revs->simplify_history = 0;
1292                revs->limited = 1;
1293        } else if (!strcmp(arg, "--simplify-by-decoration")) {
1294                revs->simplify_merges = 1;
1295                revs->rewrite_parents = 1;
1296                revs->simplify_history = 0;
1297                revs->simplify_by_decoration = 1;
1298                revs->limited = 1;
1299                revs->prune = 1;
1300                load_ref_decorations(DECORATE_SHORT_REFS);
1301        } else if (!strcmp(arg, "--date-order")) {
1302                revs->lifo = 0;
1303                revs->topo_order = 1;
1304        } else if (!prefixcmp(arg, "--early-output")) {
1305                int count = 100;
1306                switch (arg[14]) {
1307                case '=':
1308                        count = atoi(arg+15);
1309                        /* Fallthrough */
1310                case 0:
1311                        revs->topo_order = 1;
1312                       revs->early_output = count;
1313                }
1314        } else if (!strcmp(arg, "--parents")) {
1315                revs->rewrite_parents = 1;
1316                revs->print_parents = 1;
1317        } else if (!strcmp(arg, "--dense")) {
1318                revs->dense = 1;
1319        } else if (!strcmp(arg, "--sparse")) {
1320                revs->dense = 0;
1321        } else if (!strcmp(arg, "--show-all")) {
1322                revs->show_all = 1;
1323        } else if (!strcmp(arg, "--remove-empty")) {
1324                revs->remove_empty_trees = 1;
1325        } else if (!strcmp(arg, "--merges")) {
1326                revs->min_parents = 2;
1327        } else if (!strcmp(arg, "--no-merges")) {
1328                revs->max_parents = 1;
1329        } else if (!prefixcmp(arg, "--min-parents=")) {
1330                revs->min_parents = atoi(arg+14);
1331        } else if (!prefixcmp(arg, "--no-min-parents")) {
1332                revs->min_parents = 0;
1333        } else if (!prefixcmp(arg, "--max-parents=")) {
1334                revs->max_parents = atoi(arg+14);
1335        } else if (!prefixcmp(arg, "--no-max-parents")) {
1336                revs->max_parents = -1;
1337        } else if (!strcmp(arg, "--boundary")) {
1338                revs->boundary = 1;
1339        } else if (!strcmp(arg, "--left-right")) {
1340                revs->left_right = 1;
1341        } else if (!strcmp(arg, "--left-only")) {
1342                if (revs->right_only)
1343                        die("--left-only is incompatible with --right-only"
1344                            " or --cherry");
1345                revs->left_only = 1;
1346        } else if (!strcmp(arg, "--right-only")) {
1347                if (revs->left_only)
1348                        die("--right-only is incompatible with --left-only");
1349                revs->right_only = 1;
1350        } else if (!strcmp(arg, "--cherry")) {
1351                if (revs->left_only)
1352                        die("--cherry is incompatible with --left-only");
1353                revs->cherry_mark = 1;
1354                revs->right_only = 1;
1355                revs->max_parents = 1;
1356                revs->limited = 1;
1357        } else if (!strcmp(arg, "--count")) {
1358                revs->count = 1;
1359        } else if (!strcmp(arg, "--cherry-mark")) {
1360                if (revs->cherry_pick)
1361                        die("--cherry-mark is incompatible with --cherry-pick");
1362                revs->cherry_mark = 1;
1363                revs->limited = 1; /* needs limit_list() */
1364        } else if (!strcmp(arg, "--cherry-pick")) {
1365                if (revs->cherry_mark)
1366                        die("--cherry-pick is incompatible with --cherry-mark");
1367                revs->cherry_pick = 1;
1368                revs->limited = 1;
1369        } else if (!strcmp(arg, "--objects")) {
1370                revs->tag_objects = 1;
1371                revs->tree_objects = 1;
1372                revs->blob_objects = 1;
1373        } else if (!strcmp(arg, "--objects-edge")) {
1374                revs->tag_objects = 1;
1375                revs->tree_objects = 1;
1376                revs->blob_objects = 1;
1377                revs->edge_hint = 1;
1378        } else if (!strcmp(arg, "--unpacked")) {
1379                revs->unpacked = 1;
1380        } else if (!prefixcmp(arg, "--unpacked=")) {
1381                die("--unpacked=<packfile> no longer supported.");
1382        } else if (!strcmp(arg, "-r")) {
1383                revs->diff = 1;
1384                DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1385        } else if (!strcmp(arg, "-t")) {
1386                revs->diff = 1;
1387                DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1388                DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1389        } else if (!strcmp(arg, "-m")) {
1390                revs->ignore_merges = 0;
1391        } else if (!strcmp(arg, "-c")) {
1392                revs->diff = 1;
1393                revs->dense_combined_merges = 0;
1394                revs->combine_merges = 1;
1395        } else if (!strcmp(arg, "--cc")) {
1396                revs->diff = 1;
1397                revs->dense_combined_merges = 1;
1398                revs->combine_merges = 1;
1399        } else if (!strcmp(arg, "-v")) {
1400                revs->verbose_header = 1;
1401        } else if (!strcmp(arg, "--pretty")) {
1402                revs->verbose_header = 1;
1403                revs->pretty_given = 1;
1404                get_commit_format(arg+8, revs);
1405        } else if (!prefixcmp(arg, "--pretty=") || !prefixcmp(arg, "--format=")) {
1406                /*
1407                 * Detached form ("--pretty X" as opposed to "--pretty=X")
1408                 * not allowed, since the argument is optional.
1409                 */
1410                revs->verbose_header = 1;
1411                revs->pretty_given = 1;
1412                get_commit_format(arg+9, revs);
1413        } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1414                revs->show_notes = 1;
1415                revs->show_notes_given = 1;
1416                revs->notes_opt.use_default_notes = 1;
1417        } else if (!prefixcmp(arg, "--show-notes=") ||
1418                   !prefixcmp(arg, "--notes=")) {
1419                struct strbuf buf = STRBUF_INIT;
1420                revs->show_notes = 1;
1421                revs->show_notes_given = 1;
1422                if (!prefixcmp(arg, "--show-notes")) {
1423                        if (revs->notes_opt.use_default_notes < 0)
1424                                revs->notes_opt.use_default_notes = 1;
1425                        strbuf_addstr(&buf, arg+13);
1426                }
1427                else
1428                        strbuf_addstr(&buf, arg+8);
1429                expand_notes_ref(&buf);
1430                string_list_append(&revs->notes_opt.extra_notes_refs,
1431                                   strbuf_detach(&buf, NULL));
1432        } else if (!strcmp(arg, "--no-notes")) {
1433                revs->show_notes = 0;
1434                revs->show_notes_given = 1;
1435                revs->notes_opt.use_default_notes = -1;
1436                /* we have been strdup'ing ourselves, so trick
1437                 * string_list into free()ing strings */
1438                revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1439                string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1440                revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1441        } else if (!strcmp(arg, "--standard-notes")) {
1442                revs->show_notes_given = 1;
1443                revs->notes_opt.use_default_notes = 1;
1444        } else if (!strcmp(arg, "--no-standard-notes")) {
1445                revs->notes_opt.use_default_notes = 0;
1446        } else if (!strcmp(arg, "--oneline")) {
1447                revs->verbose_header = 1;
1448                get_commit_format("oneline", revs);
1449                revs->pretty_given = 1;
1450                revs->abbrev_commit = 1;
1451        } else if (!strcmp(arg, "--graph")) {
1452                revs->topo_order = 1;
1453                revs->rewrite_parents = 1;
1454                revs->graph = graph_init(revs);
1455        } else if (!strcmp(arg, "--root")) {
1456                revs->show_root_diff = 1;
1457        } else if (!strcmp(arg, "--no-commit-id")) {
1458                revs->no_commit_id = 1;
1459        } else if (!strcmp(arg, "--always")) {
1460                revs->always_show_header = 1;
1461        } else if (!strcmp(arg, "--no-abbrev")) {
1462                revs->abbrev = 0;
1463        } else if (!strcmp(arg, "--abbrev")) {
1464                revs->abbrev = DEFAULT_ABBREV;
1465        } else if (!prefixcmp(arg, "--abbrev=")) {
1466                revs->abbrev = strtoul(arg + 9, NULL, 10);
1467                if (revs->abbrev < MINIMUM_ABBREV)
1468                        revs->abbrev = MINIMUM_ABBREV;
1469                else if (revs->abbrev > 40)
1470                        revs->abbrev = 40;
1471        } else if (!strcmp(arg, "--abbrev-commit")) {
1472                revs->abbrev_commit = 1;
1473                revs->abbrev_commit_given = 1;
1474        } else if (!strcmp(arg, "--no-abbrev-commit")) {
1475                revs->abbrev_commit = 0;
1476        } else if (!strcmp(arg, "--full-diff")) {
1477                revs->diff = 1;
1478                revs->full_diff = 1;
1479        } else if (!strcmp(arg, "--full-history")) {
1480                revs->simplify_history = 0;
1481        } else if (!strcmp(arg, "--relative-date")) {
1482                revs->date_mode = DATE_RELATIVE;
1483                revs->date_mode_explicit = 1;
1484        } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1485                revs->date_mode = parse_date_format(optarg);
1486                revs->date_mode_explicit = 1;
1487                return argcount;
1488        } else if (!strcmp(arg, "--log-size")) {
1489                revs->show_log_size = 1;
1490        }
1491        /*
1492         * Grepping the commit log
1493         */
1494        else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1495                add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1496                return argcount;
1497        } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1498                add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1499                return argcount;
1500        } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1501                add_message_grep(revs, optarg);
1502                return argcount;
1503        } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
1504                revs->grep_filter.regflags |= REG_EXTENDED;
1505        } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
1506                revs->grep_filter.regflags |= REG_ICASE;
1507        } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
1508                revs->grep_filter.fixed = 1;
1509        } else if (!strcmp(arg, "--all-match")) {
1510                revs->grep_filter.all_match = 1;
1511        } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
1512                if (strcmp(optarg, "none"))
1513                        git_log_output_encoding = xstrdup(optarg);
1514                else
1515                        git_log_output_encoding = "";
1516                return argcount;
1517        } else if (!strcmp(arg, "--reverse")) {
1518                revs->reverse ^= 1;
1519        } else if (!strcmp(arg, "--children")) {
1520                revs->children.name = "children";
1521                revs->limited = 1;
1522        } else if (!strcmp(arg, "--ignore-missing")) {
1523                revs->ignore_missing = 1;
1524        } else {
1525                int opts = diff_opt_parse(&revs->diffopt, argv, argc);
1526                if (!opts)
1527                        unkv[(*unkc)++] = arg;
1528                return opts;
1529        }
1530
1531        return 1;
1532}
1533
1534void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
1535                        const struct option *options,
1536                        const char * const usagestr[])
1537{
1538        int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
1539                                    &ctx->cpidx, ctx->out);
1540        if (n <= 0) {
1541                error("unknown option `%s'", ctx->argv[0]);
1542                usage_with_options(usagestr, options);
1543        }
1544        ctx->argv += n;
1545        ctx->argc -= n;
1546}
1547
1548static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1549{
1550        return for_each_ref_in_submodule(submodule, "refs/bisect/bad", fn, cb_data);
1551}
1552
1553static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
1554{
1555        return for_each_ref_in_submodule(submodule, "refs/bisect/good", fn, cb_data);
1556}
1557
1558static int handle_revision_pseudo_opt(const char *submodule,
1559                                struct rev_info *revs,
1560                                int argc, const char **argv, int *flags)
1561{
1562        const char *arg = argv[0];
1563        const char *optarg;
1564        int argcount;
1565
1566        /*
1567         * NOTE!
1568         *
1569         * Commands like "git shortlog" will not accept the options below
1570         * unless parse_revision_opt queues them (as opposed to erroring
1571         * out).
1572         *
1573         * When implementing your new pseudo-option, remember to
1574         * register it in the list at the top of handle_revision_opt.
1575         */
1576        if (!strcmp(arg, "--all")) {
1577                handle_refs(submodule, revs, *flags, for_each_ref_submodule);
1578                handle_refs(submodule, revs, *flags, head_ref_submodule);
1579        } else if (!strcmp(arg, "--branches")) {
1580                handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
1581        } else if (!strcmp(arg, "--bisect")) {
1582                handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
1583                handle_refs(submodule, revs, *flags ^ UNINTERESTING, for_each_good_bisect_ref);
1584                revs->bisect = 1;
1585        } else if (!strcmp(arg, "--tags")) {
1586                handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
1587        } else if (!strcmp(arg, "--remotes")) {
1588                handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
1589        } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
1590                struct all_refs_cb cb;
1591                init_all_refs_cb(&cb, revs, *flags);
1592                for_each_glob_ref(handle_one_ref, optarg, &cb);
1593                return argcount;
1594        } else if (!prefixcmp(arg, "--branches=")) {
1595                struct all_refs_cb cb;
1596                init_all_refs_cb(&cb, revs, *flags);
1597                for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1598        } else if (!prefixcmp(arg, "--tags=")) {
1599                struct all_refs_cb cb;
1600                init_all_refs_cb(&cb, revs, *flags);
1601                for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1602        } else if (!prefixcmp(arg, "--remotes=")) {
1603                struct all_refs_cb cb;
1604                init_all_refs_cb(&cb, revs, *flags);
1605                for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1606        } else if (!strcmp(arg, "--reflog")) {
1607                handle_reflog(revs, *flags);
1608        } else if (!strcmp(arg, "--not")) {
1609                *flags ^= UNINTERESTING;
1610        } else if (!strcmp(arg, "--no-walk")) {
1611                revs->no_walk = 1;
1612        } else if (!strcmp(arg, "--do-walk")) {
1613                revs->no_walk = 0;
1614        } else {
1615                return 0;
1616        }
1617
1618        return 1;
1619}
1620
1621/*
1622 * Parse revision information, filling in the "rev_info" structure,
1623 * and removing the used arguments from the argument list.
1624 *
1625 * Returns the number of arguments left that weren't recognized
1626 * (which are also moved to the head of the argument list)
1627 */
1628int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
1629{
1630        int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0;
1631        struct cmdline_pathspec prune_data;
1632        const char *submodule = NULL;
1633
1634        memset(&prune_data, 0, sizeof(prune_data));
1635        if (opt)
1636                submodule = opt->submodule;
1637
1638        /* First, search for "--" */
1639        seen_dashdash = 0;
1640        for (i = 1; i < argc; i++) {
1641                const char *arg = argv[i];
1642                if (strcmp(arg, "--"))
1643                        continue;
1644                argv[i] = NULL;
1645                argc = i;
1646                if (argv[i + 1])
1647                        append_prune_data(&prune_data, argv + i + 1);
1648                seen_dashdash = 1;
1649                break;
1650        }
1651
1652        /* Second, deal with arguments and options */
1653        flags = 0;
1654        read_from_stdin = 0;
1655        for (left = i = 1; i < argc; i++) {
1656                const char *arg = argv[i];
1657                if (*arg == '-') {
1658                        int opts;
1659
1660                        opts = handle_revision_pseudo_opt(submodule,
1661                                                revs, argc - i, argv + i,
1662                                                &flags);
1663                        if (opts > 0) {
1664                                i += opts - 1;
1665                                continue;
1666                        }
1667
1668                        if (!strcmp(arg, "--stdin")) {
1669                                if (revs->disable_stdin) {
1670                                        argv[left++] = arg;
1671                                        continue;
1672                                }
1673                                if (read_from_stdin++)
1674                                        die("--stdin given twice?");
1675                                read_revisions_from_stdin(revs, &prune_data);
1676                                continue;
1677                        }
1678
1679                        opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
1680                        if (opts > 0) {
1681                                i += opts - 1;
1682                                continue;
1683                        }
1684                        if (opts < 0)
1685                                exit(128);
1686                        continue;
1687                }
1688
1689                if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1690                        int j;
1691                        if (seen_dashdash || *arg == '^')
1692                                die("bad revision '%s'", arg);
1693
1694                        /* If we didn't have a "--":
1695                         * (1) all filenames must exist;
1696                         * (2) all rev-args must not be interpretable
1697                         *     as a valid filename.
1698                         * but the latter we have checked in the main loop.
1699                         */
1700                        for (j = i; j < argc; j++)
1701                                verify_filename(revs->prefix, argv[j]);
1702
1703                        append_prune_data(&prune_data, argv + i);
1704                        break;
1705                }
1706                else
1707                        got_rev_arg = 1;
1708        }
1709
1710        if (prune_data.nr) {
1711                /*
1712                 * If we need to introduce the magic "a lone ':' means no
1713                 * pathspec whatsoever", here is the place to do so.
1714                 *
1715                 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
1716                 *      prune_data.nr = 0;
1717                 *      prune_data.alloc = 0;
1718                 *      free(prune_data.path);
1719                 *      prune_data.path = NULL;
1720                 * } else {
1721                 *      terminate prune_data.alloc with NULL and
1722                 *      call init_pathspec() to set revs->prune_data here.
1723                 * }
1724                 */
1725                ALLOC_GROW(prune_data.path, prune_data.nr+1, prune_data.alloc);
1726                prune_data.path[prune_data.nr++] = NULL;
1727                init_pathspec(&revs->prune_data,
1728                              get_pathspec(revs->prefix, prune_data.path));
1729        }
1730
1731        if (revs->def == NULL)
1732                revs->def = opt ? opt->def : NULL;
1733        if (opt && opt->tweak)
1734                opt->tweak(revs, opt);
1735        if (revs->show_merge)
1736                prepare_show_merge(revs);
1737        if (revs->def && !revs->pending.nr && !got_rev_arg) {
1738                unsigned char sha1[20];
1739                struct object *object;
1740                unsigned mode;
1741                if (get_sha1_with_mode(revs->def, sha1, &mode))
1742                        die("bad default revision '%s'", revs->def);
1743                object = get_reference(revs, revs->def, sha1, 0);
1744                add_pending_object_with_mode(revs, object, revs->def, mode);
1745        }
1746
1747        /* Did the user ask for any diff output? Run the diff! */
1748        if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
1749                revs->diff = 1;
1750
1751        /* Pickaxe, diff-filter and rename following need diffs */
1752        if (revs->diffopt.pickaxe ||
1753            revs->diffopt.filter ||
1754            DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1755                revs->diff = 1;
1756
1757        if (revs->topo_order)
1758                revs->limited = 1;
1759
1760        if (revs->prune_data.nr) {
1761                diff_tree_setup_paths(revs->prune_data.raw, &revs->pruning);
1762                /* Can't prune commits with rename following: the paths change.. */
1763                if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
1764                        revs->prune = 1;
1765                if (!revs->full_diff)
1766                        diff_tree_setup_paths(revs->prune_data.raw, &revs->diffopt);
1767        }
1768        if (revs->combine_merges)
1769                revs->ignore_merges = 0;
1770        revs->diffopt.abbrev = revs->abbrev;
1771        if (diff_setup_done(&revs->diffopt) < 0)
1772                die("diff_setup_done failed");
1773
1774        compile_grep_patterns(&revs->grep_filter);
1775
1776        if (revs->reverse && revs->reflog_info)
1777                die("cannot combine --reverse with --walk-reflogs");
1778        if (revs->rewrite_parents && revs->children.name)
1779                die("cannot combine --parents and --children");
1780
1781        /*
1782         * Limitations on the graph functionality
1783         */
1784        if (revs->reverse && revs->graph)
1785                die("cannot combine --reverse with --graph");
1786
1787        if (revs->reflog_info && revs->graph)
1788                die("cannot combine --walk-reflogs with --graph");
1789
1790        return left;
1791}
1792
1793static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
1794{
1795        struct commit_list *l = xcalloc(1, sizeof(*l));
1796
1797        l->item = child;
1798        l->next = add_decoration(&revs->children, &parent->object, l);
1799}
1800
1801static int remove_duplicate_parents(struct commit *commit)
1802{
1803        struct commit_list **pp, *p;
1804        int surviving_parents;
1805
1806        /* Examine existing parents while marking ones we have seen... */
1807        pp = &commit->parents;
1808        while ((p = *pp) != NULL) {
1809                struct commit *parent = p->item;
1810                if (parent->object.flags & TMP_MARK) {
1811                        *pp = p->next;
1812                        continue;
1813                }
1814                parent->object.flags |= TMP_MARK;
1815                pp = &p->next;
1816        }
1817        /* count them while clearing the temporary mark */
1818        surviving_parents = 0;
1819        for (p = commit->parents; p; p = p->next) {
1820                p->item->object.flags &= ~TMP_MARK;
1821                surviving_parents++;
1822        }
1823        return surviving_parents;
1824}
1825
1826struct merge_simplify_state {
1827        struct commit *simplified;
1828};
1829
1830static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
1831{
1832        struct merge_simplify_state *st;
1833
1834        st = lookup_decoration(&revs->merge_simplification, &commit->object);
1835        if (!st) {
1836                st = xcalloc(1, sizeof(*st));
1837                add_decoration(&revs->merge_simplification, &commit->object, st);
1838        }
1839        return st;
1840}
1841
1842static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
1843{
1844        struct commit_list *p;
1845        struct merge_simplify_state *st, *pst;
1846        int cnt;
1847
1848        st = locate_simplify_state(revs, commit);
1849
1850        /*
1851         * Have we handled this one?
1852         */
1853        if (st->simplified)
1854                return tail;
1855
1856        /*
1857         * An UNINTERESTING commit simplifies to itself, so does a
1858         * root commit.  We do not rewrite parents of such commit
1859         * anyway.
1860         */
1861        if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
1862                st->simplified = commit;
1863                return tail;
1864        }
1865
1866        /*
1867         * Do we know what commit all of our parents should be rewritten to?
1868         * Otherwise we are not ready to rewrite this one yet.
1869         */
1870        for (cnt = 0, p = commit->parents; p; p = p->next) {
1871                pst = locate_simplify_state(revs, p->item);
1872                if (!pst->simplified) {
1873                        tail = &commit_list_insert(p->item, tail)->next;
1874                        cnt++;
1875                }
1876        }
1877        if (cnt) {
1878                tail = &commit_list_insert(commit, tail)->next;
1879                return tail;
1880        }
1881
1882        /*
1883         * Rewrite our list of parents.
1884         */
1885        for (p = commit->parents; p; p = p->next) {
1886                pst = locate_simplify_state(revs, p->item);
1887                p->item = pst->simplified;
1888        }
1889        cnt = remove_duplicate_parents(commit);
1890
1891        /*
1892         * It is possible that we are a merge and one side branch
1893         * does not have any commit that touches the given paths;
1894         * in such a case, the immediate parents will be rewritten
1895         * to different commits.
1896         *
1897         *      o----X          X: the commit we are looking at;
1898         *     /    /           o: a commit that touches the paths;
1899         * ---o----'
1900         *
1901         * Further reduce the parents by removing redundant parents.
1902         */
1903        if (1 < cnt) {
1904                struct commit_list *h = reduce_heads(commit->parents);
1905                cnt = commit_list_count(h);
1906                free_commit_list(commit->parents);
1907                commit->parents = h;
1908        }
1909
1910        /*
1911         * A commit simplifies to itself if it is a root, if it is
1912         * UNINTERESTING, if it touches the given paths, or if it is a
1913         * merge and its parents simplifies to more than one commits
1914         * (the first two cases are already handled at the beginning of
1915         * this function).
1916         *
1917         * Otherwise, it simplifies to what its sole parent simplifies to.
1918         */
1919        if (!cnt ||
1920            (commit->object.flags & UNINTERESTING) ||
1921            !(commit->object.flags & TREESAME) ||
1922            (1 < cnt))
1923                st->simplified = commit;
1924        else {
1925                pst = locate_simplify_state(revs, commit->parents->item);
1926                st->simplified = pst->simplified;
1927        }
1928        return tail;
1929}
1930
1931static void simplify_merges(struct rev_info *revs)
1932{
1933        struct commit_list *list;
1934        struct commit_list *yet_to_do, **tail;
1935
1936        if (!revs->topo_order)
1937                sort_in_topological_order(&revs->commits, revs->lifo);
1938        if (!revs->prune)
1939                return;
1940
1941        /* feed the list reversed */
1942        yet_to_do = NULL;
1943        for (list = revs->commits; list; list = list->next)
1944                commit_list_insert(list->item, &yet_to_do);
1945        while (yet_to_do) {
1946                list = yet_to_do;
1947                yet_to_do = NULL;
1948                tail = &yet_to_do;
1949                while (list) {
1950                        struct commit *commit = list->item;
1951                        struct commit_list *next = list->next;
1952                        free(list);
1953                        list = next;
1954                        tail = simplify_one(revs, commit, tail);
1955                }
1956        }
1957
1958        /* clean up the result, removing the simplified ones */
1959        list = revs->commits;
1960        revs->commits = NULL;
1961        tail = &revs->commits;
1962        while (list) {
1963                struct commit *commit = list->item;
1964                struct commit_list *next = list->next;
1965                struct merge_simplify_state *st;
1966                free(list);
1967                list = next;
1968                st = locate_simplify_state(revs, commit);
1969                if (st->simplified == commit)
1970                        tail = &commit_list_insert(commit, tail)->next;
1971        }
1972}
1973
1974static void set_children(struct rev_info *revs)
1975{
1976        struct commit_list *l;
1977        for (l = revs->commits; l; l = l->next) {
1978                struct commit *commit = l->item;
1979                struct commit_list *p;
1980
1981                for (p = commit->parents; p; p = p->next)
1982                        add_child(revs, p->item, commit);
1983        }
1984}
1985
1986int prepare_revision_walk(struct rev_info *revs)
1987{
1988        int nr = revs->pending.nr;
1989        struct object_array_entry *e, *list;
1990
1991        e = list = revs->pending.objects;
1992        revs->pending.nr = 0;
1993        revs->pending.alloc = 0;
1994        revs->pending.objects = NULL;
1995        while (--nr >= 0) {
1996                struct commit *commit = handle_commit(revs, e->item, e->name);
1997                if (commit) {
1998                        if (!(commit->object.flags & SEEN)) {
1999                                commit->object.flags |= SEEN;
2000                                commit_list_insert_by_date(commit, &revs->commits);
2001                        }
2002                }
2003                e++;
2004        }
2005        free(list);
2006
2007        if (revs->no_walk)
2008                return 0;
2009        if (revs->limited)
2010                if (limit_list(revs) < 0)
2011                        return -1;
2012        if (revs->topo_order)
2013                sort_in_topological_order(&revs->commits, revs->lifo);
2014        if (revs->simplify_merges)
2015                simplify_merges(revs);
2016        if (revs->children.name)
2017                set_children(revs);
2018        return 0;
2019}
2020
2021enum rewrite_result {
2022        rewrite_one_ok,
2023        rewrite_one_noparents,
2024        rewrite_one_error
2025};
2026
2027static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2028{
2029        struct commit_list *cache = NULL;
2030
2031        for (;;) {
2032                struct commit *p = *pp;
2033                if (!revs->limited)
2034                        if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2035                                return rewrite_one_error;
2036                if (p->parents && p->parents->next)
2037                        return rewrite_one_ok;
2038                if (p->object.flags & UNINTERESTING)
2039                        return rewrite_one_ok;
2040                if (!(p->object.flags & TREESAME))
2041                        return rewrite_one_ok;
2042                if (!p->parents)
2043                        return rewrite_one_noparents;
2044                *pp = p->parents->item;
2045        }
2046}
2047
2048static int rewrite_parents(struct rev_info *revs, struct commit *commit)
2049{
2050        struct commit_list **pp = &commit->parents;
2051        while (*pp) {
2052                struct commit_list *parent = *pp;
2053                switch (rewrite_one(revs, &parent->item)) {
2054                case rewrite_one_ok:
2055                        break;
2056                case rewrite_one_noparents:
2057                        *pp = parent->next;
2058                        continue;
2059                case rewrite_one_error:
2060                        return -1;
2061                }
2062                pp = &parent->next;
2063        }
2064        remove_duplicate_parents(commit);
2065        return 0;
2066}
2067
2068static int commit_match(struct commit *commit, struct rev_info *opt)
2069{
2070        if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2071                return 1;
2072        return grep_buffer(&opt->grep_filter,
2073                           NULL, /* we say nothing, not even filename */
2074                           commit->buffer, strlen(commit->buffer));
2075}
2076
2077static inline int want_ancestry(struct rev_info *revs)
2078{
2079        return (revs->rewrite_parents || revs->children.name);
2080}
2081
2082enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2083{
2084        if (commit->object.flags & SHOWN)
2085                return commit_ignore;
2086        if (revs->unpacked && has_sha1_pack(commit->object.sha1))
2087                return commit_ignore;
2088        if (revs->show_all)
2089                return commit_show;
2090        if (commit->object.flags & UNINTERESTING)
2091                return commit_ignore;
2092        if (revs->min_age != -1 && (commit->date > revs->min_age))
2093                return commit_ignore;
2094        if (revs->min_parents || (revs->max_parents >= 0)) {
2095                int n = 0;
2096                struct commit_list *p;
2097                for (p = commit->parents; p; p = p->next)
2098                        n++;
2099                if ((n < revs->min_parents) ||
2100                    ((revs->max_parents >= 0) && (n > revs->max_parents)))
2101                        return commit_ignore;
2102        }
2103        if (!commit_match(commit, revs))
2104                return commit_ignore;
2105        if (revs->prune && revs->dense) {
2106                /* Commit without changes? */
2107                if (commit->object.flags & TREESAME) {
2108                        /* drop merges unless we want parenthood */
2109                        if (!want_ancestry(revs))
2110                                return commit_ignore;
2111                        /* non-merge - always ignore it */
2112                        if (!commit->parents || !commit->parents->next)
2113                                return commit_ignore;
2114                }
2115        }
2116        return commit_show;
2117}
2118
2119enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
2120{
2121        enum commit_action action = get_commit_action(revs, commit);
2122
2123        if (action == commit_show &&
2124            !revs->show_all &&
2125            revs->prune && revs->dense && want_ancestry(revs)) {
2126                if (rewrite_parents(revs, commit) < 0)
2127                        return commit_error;
2128        }
2129        return action;
2130}
2131
2132static struct commit *get_revision_1(struct rev_info *revs)
2133{
2134        if (!revs->commits)
2135                return NULL;
2136
2137        do {
2138                struct commit_list *entry = revs->commits;
2139                struct commit *commit = entry->item;
2140
2141                revs->commits = entry->next;
2142                free(entry);
2143
2144                if (revs->reflog_info) {
2145                        fake_reflog_parent(revs->reflog_info, commit);
2146                        commit->object.flags &= ~(ADDED | SEEN | SHOWN);
2147                }
2148
2149                /*
2150                 * If we haven't done the list limiting, we need to look at
2151                 * the parents here. We also need to do the date-based limiting
2152                 * that we'd otherwise have done in limit_list().
2153                 */
2154                if (!revs->limited) {
2155                        if (revs->max_age != -1 &&
2156                            (commit->date < revs->max_age))
2157                                continue;
2158                        if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0)
2159                                die("Failed to traverse parents of commit %s",
2160                                    sha1_to_hex(commit->object.sha1));
2161                }
2162
2163                switch (simplify_commit(revs, commit)) {
2164                case commit_ignore:
2165                        continue;
2166                case commit_error:
2167                        die("Failed to simplify parents of commit %s",
2168                            sha1_to_hex(commit->object.sha1));
2169                default:
2170                        return commit;
2171                }
2172        } while (revs->commits);
2173        return NULL;
2174}
2175
2176static void gc_boundary(struct object_array *array)
2177{
2178        unsigned nr = array->nr;
2179        unsigned alloc = array->alloc;
2180        struct object_array_entry *objects = array->objects;
2181
2182        if (alloc <= nr) {
2183                unsigned i, j;
2184                for (i = j = 0; i < nr; i++) {
2185                        if (objects[i].item->flags & SHOWN)
2186                                continue;
2187                        if (i != j)
2188                                objects[j] = objects[i];
2189                        j++;
2190                }
2191                for (i = j; i < nr; i++)
2192                        objects[i].item = NULL;
2193                array->nr = j;
2194        }
2195}
2196
2197static void create_boundary_commit_list(struct rev_info *revs)
2198{
2199        unsigned i;
2200        struct commit *c;
2201        struct object_array *array = &revs->boundary_commits;
2202        struct object_array_entry *objects = array->objects;
2203
2204        /*
2205         * If revs->commits is non-NULL at this point, an error occurred in
2206         * get_revision_1().  Ignore the error and continue printing the
2207         * boundary commits anyway.  (This is what the code has always
2208         * done.)
2209         */
2210        if (revs->commits) {
2211                free_commit_list(revs->commits);
2212                revs->commits = NULL;
2213        }
2214
2215        /*
2216         * Put all of the actual boundary commits from revs->boundary_commits
2217         * into revs->commits
2218         */
2219        for (i = 0; i < array->nr; i++) {
2220                c = (struct commit *)(objects[i].item);
2221                if (!c)
2222                        continue;
2223                if (!(c->object.flags & CHILD_SHOWN))
2224                        continue;
2225                if (c->object.flags & (SHOWN | BOUNDARY))
2226                        continue;
2227                c->object.flags |= BOUNDARY;
2228                commit_list_insert(c, &revs->commits);
2229        }
2230
2231        /*
2232         * If revs->topo_order is set, sort the boundary commits
2233         * in topological order
2234         */
2235        sort_in_topological_order(&revs->commits, revs->lifo);
2236}
2237
2238static struct commit *get_revision_internal(struct rev_info *revs)
2239{
2240        struct commit *c = NULL;
2241        struct commit_list *l;
2242
2243        if (revs->boundary == 2) {
2244                /*
2245                 * All of the normal commits have already been returned,
2246                 * and we are now returning boundary commits.
2247                 * create_boundary_commit_list() has populated
2248                 * revs->commits with the remaining commits to return.
2249                 */
2250                c = pop_commit(&revs->commits);
2251                if (c)
2252                        c->object.flags |= SHOWN;
2253                return c;
2254        }
2255
2256        /*
2257         * Now pick up what they want to give us
2258         */
2259        c = get_revision_1(revs);
2260        if (c) {
2261                while (0 < revs->skip_count) {
2262                        revs->skip_count--;
2263                        c = get_revision_1(revs);
2264                        if (!c)
2265                                break;
2266                }
2267        }
2268
2269        /*
2270         * Check the max_count.
2271         */
2272        switch (revs->max_count) {
2273        case -1:
2274                break;
2275        case 0:
2276                c = NULL;
2277                break;
2278        default:
2279                revs->max_count--;
2280        }
2281
2282        if (c)
2283                c->object.flags |= SHOWN;
2284
2285        if (!revs->boundary) {
2286                return c;
2287        }
2288
2289        if (!c) {
2290                /*
2291                 * get_revision_1() runs out the commits, and
2292                 * we are done computing the boundaries.
2293                 * switch to boundary commits output mode.
2294                 */
2295                revs->boundary = 2;
2296
2297                /*
2298                 * Update revs->commits to contain the list of
2299                 * boundary commits.
2300                 */
2301                create_boundary_commit_list(revs);
2302
2303                return get_revision_internal(revs);
2304        }
2305
2306        /*
2307         * boundary commits are the commits that are parents of the
2308         * ones we got from get_revision_1() but they themselves are
2309         * not returned from get_revision_1().  Before returning
2310         * 'c', we need to mark its parents that they could be boundaries.
2311         */
2312
2313        for (l = c->parents; l; l = l->next) {
2314                struct object *p;
2315                p = &(l->item->object);
2316                if (p->flags & (CHILD_SHOWN | SHOWN))
2317                        continue;
2318                p->flags |= CHILD_SHOWN;
2319                gc_boundary(&revs->boundary_commits);
2320                add_object_array(p, NULL, &revs->boundary_commits);
2321        }
2322
2323        return c;
2324}
2325
2326struct commit *get_revision(struct rev_info *revs)
2327{
2328        struct commit *c;
2329        struct commit_list *reversed;
2330
2331        if (revs->reverse) {
2332                reversed = NULL;
2333                while ((c = get_revision_internal(revs))) {
2334                        commit_list_insert(c, &reversed);
2335                }
2336                revs->commits = reversed;
2337                revs->reverse = 0;
2338                revs->reverse_output_stage = 1;
2339        }
2340
2341        if (revs->reverse_output_stage)
2342                return pop_commit(&revs->commits);
2343
2344        c = get_revision_internal(revs);
2345        if (c && revs->graph)
2346                graph_update(revs->graph, c);
2347        return c;
2348}
2349
2350char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
2351{
2352        if (commit->object.flags & BOUNDARY)
2353                return "-";
2354        else if (commit->object.flags & UNINTERESTING)
2355                return "^";
2356        else if (commit->object.flags & PATCHSAME)
2357                return "=";
2358        else if (!revs || revs->left_right) {
2359                if (commit->object.flags & SYMMETRIC_LEFT)
2360                        return "<";
2361                else
2362                        return ">";
2363        } else if (revs->graph)
2364                return "*";
2365        else if (revs->cherry_mark)
2366                return "+";
2367        return "";
2368}
2369
2370void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
2371{
2372        char *mark = get_revision_mark(revs, commit);
2373        if (!strlen(mark))
2374                return;
2375        fputs(mark, stdout);
2376        putchar(' ');
2377}