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