revision.con commit revision: quit pruning diff more quickly when possible (a937b37)
   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#include "line-log.h"
  17#include "mailmap.h"
  18#include "commit-slab.h"
  19#include "dir.h"
  20#include "cache-tree.h"
  21#include "bisect.h"
  22
  23volatile show_early_output_fn_t show_early_output;
  24
  25static const char *term_bad;
  26static const char *term_good;
  27
  28void show_object_with_name(FILE *out, struct object *obj, const char *name)
  29{
  30        const char *p;
  31
  32        fprintf(out, "%s ", oid_to_hex(&obj->oid));
  33        for (p = name; *p && *p != '\n'; p++)
  34                fputc(*p, out);
  35        fputc('\n', out);
  36}
  37
  38static void mark_blob_uninteresting(struct blob *blob)
  39{
  40        if (!blob)
  41                return;
  42        if (blob->object.flags & UNINTERESTING)
  43                return;
  44        blob->object.flags |= UNINTERESTING;
  45}
  46
  47static void mark_tree_contents_uninteresting(struct tree *tree)
  48{
  49        struct tree_desc desc;
  50        struct name_entry entry;
  51        struct object *obj = &tree->object;
  52
  53        if (!has_object_file(&obj->oid))
  54                return;
  55        if (parse_tree(tree) < 0)
  56                die("bad tree %s", oid_to_hex(&obj->oid));
  57
  58        init_tree_desc(&desc, tree->buffer, tree->size);
  59        while (tree_entry(&desc, &entry)) {
  60                switch (object_type(entry.mode)) {
  61                case OBJ_TREE:
  62                        mark_tree_uninteresting(lookup_tree(entry.oid->hash));
  63                        break;
  64                case OBJ_BLOB:
  65                        mark_blob_uninteresting(lookup_blob(entry.oid->hash));
  66                        break;
  67                default:
  68                        /* Subproject commit - not in this repository */
  69                        break;
  70                }
  71        }
  72
  73        /*
  74         * We don't care about the tree any more
  75         * after it has been marked uninteresting.
  76         */
  77        free_tree_buffer(tree);
  78}
  79
  80void mark_tree_uninteresting(struct tree *tree)
  81{
  82        struct object *obj;
  83
  84        if (!tree)
  85                return;
  86
  87        obj = &tree->object;
  88        if (obj->flags & UNINTERESTING)
  89                return;
  90        obj->flags |= UNINTERESTING;
  91        mark_tree_contents_uninteresting(tree);
  92}
  93
  94void mark_parents_uninteresting(struct commit *commit)
  95{
  96        struct commit_list *parents = NULL, *l;
  97
  98        for (l = commit->parents; l; l = l->next)
  99                commit_list_insert(l->item, &parents);
 100
 101        while (parents) {
 102                struct commit *commit = pop_commit(&parents);
 103
 104                while (commit) {
 105                        /*
 106                         * A missing commit is ok iff its parent is marked
 107                         * uninteresting.
 108                         *
 109                         * We just mark such a thing parsed, so that when
 110                         * it is popped next time around, we won't be trying
 111                         * to parse it and get an error.
 112                         */
 113                        if (!has_object_file(&commit->object.oid))
 114                                commit->object.parsed = 1;
 115
 116                        if (commit->object.flags & UNINTERESTING)
 117                                break;
 118
 119                        commit->object.flags |= UNINTERESTING;
 120
 121                        /*
 122                         * Normally we haven't parsed the parent
 123                         * yet, so we won't have a parent of a parent
 124                         * here. However, it may turn out that we've
 125                         * reached this commit some other way (where it
 126                         * wasn't uninteresting), in which case we need
 127                         * to mark its parents recursively too..
 128                         */
 129                        if (!commit->parents)
 130                                break;
 131
 132                        for (l = commit->parents->next; l; l = l->next)
 133                                commit_list_insert(l->item, &parents);
 134                        commit = commit->parents->item;
 135                }
 136        }
 137}
 138
 139static void add_pending_object_with_path(struct rev_info *revs,
 140                                         struct object *obj,
 141                                         const char *name, unsigned mode,
 142                                         const char *path)
 143{
 144        if (!obj)
 145                return;
 146        if (revs->no_walk && (obj->flags & UNINTERESTING))
 147                revs->no_walk = 0;
 148        if (revs->reflog_info && obj->type == OBJ_COMMIT) {
 149                struct strbuf buf = STRBUF_INIT;
 150                int len = interpret_branch_name(name, 0, &buf, 0);
 151                int st;
 152
 153                if (0 < len && name[len] && buf.len)
 154                        strbuf_addstr(&buf, name + len);
 155                st = add_reflog_for_walk(revs->reflog_info,
 156                                         (struct commit *)obj,
 157                                         buf.buf[0] ? buf.buf: name);
 158                strbuf_release(&buf);
 159                if (st)
 160                        return;
 161        }
 162        add_object_array_with_path(obj, name, &revs->pending, mode, path);
 163}
 164
 165static void add_pending_object_with_mode(struct rev_info *revs,
 166                                         struct object *obj,
 167                                         const char *name, unsigned mode)
 168{
 169        add_pending_object_with_path(revs, obj, name, mode, NULL);
 170}
 171
 172void add_pending_object(struct rev_info *revs,
 173                        struct object *obj, const char *name)
 174{
 175        add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
 176}
 177
 178void add_head_to_pending(struct rev_info *revs)
 179{
 180        unsigned char sha1[20];
 181        struct object *obj;
 182        if (get_sha1("HEAD", sha1))
 183                return;
 184        obj = parse_object(sha1);
 185        if (!obj)
 186                return;
 187        add_pending_object(revs, obj, "HEAD");
 188}
 189
 190static struct object *get_reference(struct rev_info *revs, const char *name,
 191                                    const unsigned char *sha1,
 192                                    unsigned int flags)
 193{
 194        struct object *object;
 195
 196        object = parse_object(sha1);
 197        if (!object) {
 198                if (revs->ignore_missing)
 199                        return object;
 200                die("bad object %s", name);
 201        }
 202        object->flags |= flags;
 203        return object;
 204}
 205
 206void add_pending_sha1(struct rev_info *revs, const char *name,
 207                      const unsigned char *sha1, unsigned int flags)
 208{
 209        struct object *object = get_reference(revs, name, sha1, flags);
 210        add_pending_object(revs, object, name);
 211}
 212
 213static struct commit *handle_commit(struct rev_info *revs,
 214                                    struct object_array_entry *entry)
 215{
 216        struct object *object = entry->item;
 217        const char *name = entry->name;
 218        const char *path = entry->path;
 219        unsigned int mode = entry->mode;
 220        unsigned long flags = object->flags;
 221
 222        /*
 223         * Tag object? Look what it points to..
 224         */
 225        while (object->type == OBJ_TAG) {
 226                struct tag *tag = (struct tag *) object;
 227                if (revs->tag_objects && !(flags & UNINTERESTING))
 228                        add_pending_object(revs, object, tag->tag);
 229                if (!tag->tagged)
 230                        die("bad tag");
 231                object = parse_object(tag->tagged->oid.hash);
 232                if (!object) {
 233                        if (flags & UNINTERESTING)
 234                                return NULL;
 235                        die("bad object %s", oid_to_hex(&tag->tagged->oid));
 236                }
 237                object->flags |= flags;
 238                /*
 239                 * We'll handle the tagged object by looping or dropping
 240                 * through to the non-tag handlers below. Do not
 241                 * propagate path data from the tag's pending entry.
 242                 */
 243                path = NULL;
 244                mode = 0;
 245        }
 246
 247        /*
 248         * Commit object? Just return it, we'll do all the complex
 249         * reachability crud.
 250         */
 251        if (object->type == OBJ_COMMIT) {
 252                struct commit *commit = (struct commit *)object;
 253                if (parse_commit(commit) < 0)
 254                        die("unable to parse commit %s", name);
 255                if (flags & UNINTERESTING) {
 256                        mark_parents_uninteresting(commit);
 257                        revs->limited = 1;
 258                }
 259                if (revs->show_source && !commit->util)
 260                        commit->util = xstrdup(name);
 261                return commit;
 262        }
 263
 264        /*
 265         * Tree object? Either mark it uninteresting, or add it
 266         * to the list of objects to look at later..
 267         */
 268        if (object->type == OBJ_TREE) {
 269                struct tree *tree = (struct tree *)object;
 270                if (!revs->tree_objects)
 271                        return NULL;
 272                if (flags & UNINTERESTING) {
 273                        mark_tree_contents_uninteresting(tree);
 274                        return NULL;
 275                }
 276                add_pending_object_with_path(revs, object, name, mode, path);
 277                return NULL;
 278        }
 279
 280        /*
 281         * Blob object? You know the drill by now..
 282         */
 283        if (object->type == OBJ_BLOB) {
 284                if (!revs->blob_objects)
 285                        return NULL;
 286                if (flags & UNINTERESTING)
 287                        return NULL;
 288                add_pending_object_with_path(revs, object, name, mode, path);
 289                return NULL;
 290        }
 291        die("%s is unknown object", name);
 292}
 293
 294static int everybody_uninteresting(struct commit_list *orig,
 295                                   struct commit **interesting_cache)
 296{
 297        struct commit_list *list = orig;
 298
 299        if (*interesting_cache) {
 300                struct commit *commit = *interesting_cache;
 301                if (!(commit->object.flags & UNINTERESTING))
 302                        return 0;
 303        }
 304
 305        while (list) {
 306                struct commit *commit = list->item;
 307                list = list->next;
 308                if (commit->object.flags & UNINTERESTING)
 309                        continue;
 310
 311                *interesting_cache = commit;
 312                return 0;
 313        }
 314        return 1;
 315}
 316
 317/*
 318 * A definition of "relevant" commit that we can use to simplify limited graphs
 319 * by eliminating side branches.
 320 *
 321 * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
 322 * in our list), or that is a specified BOTTOM commit. Then after computing
 323 * a limited list, during processing we can generally ignore boundary merges
 324 * coming from outside the graph, (ie from irrelevant parents), and treat
 325 * those merges as if they were single-parent. TREESAME is defined to consider
 326 * only relevant parents, if any. If we are TREESAME to our on-graph parents,
 327 * we don't care if we were !TREESAME to non-graph parents.
 328 *
 329 * Treating bottom commits as relevant ensures that a limited graph's
 330 * connection to the actual bottom commit is not viewed as a side branch, but
 331 * treated as part of the graph. For example:
 332 *
 333 *   ....Z...A---X---o---o---B
 334 *        .     /
 335 *         W---Y
 336 *
 337 * When computing "A..B", the A-X connection is at least as important as
 338 * Y-X, despite A being flagged UNINTERESTING.
 339 *
 340 * And when computing --ancestry-path "A..B", the A-X connection is more
 341 * important than Y-X, despite both A and Y being flagged UNINTERESTING.
 342 */
 343static inline int relevant_commit(struct commit *commit)
 344{
 345        return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
 346}
 347
 348/*
 349 * Return a single relevant commit from a parent list. If we are a TREESAME
 350 * commit, and this selects one of our parents, then we can safely simplify to
 351 * that parent.
 352 */
 353static struct commit *one_relevant_parent(const struct rev_info *revs,
 354                                          struct commit_list *orig)
 355{
 356        struct commit_list *list = orig;
 357        struct commit *relevant = NULL;
 358
 359        if (!orig)
 360                return NULL;
 361
 362        /*
 363         * For 1-parent commits, or if first-parent-only, then return that
 364         * first parent (even if not "relevant" by the above definition).
 365         * TREESAME will have been set purely on that parent.
 366         */
 367        if (revs->first_parent_only || !orig->next)
 368                return orig->item;
 369
 370        /*
 371         * For multi-parent commits, identify a sole relevant parent, if any.
 372         * If we have only one relevant parent, then TREESAME will be set purely
 373         * with regard to that parent, and we can simplify accordingly.
 374         *
 375         * If we have more than one relevant parent, or no relevant parents
 376         * (and multiple irrelevant ones), then we can't select a parent here
 377         * and return NULL.
 378         */
 379        while (list) {
 380                struct commit *commit = list->item;
 381                list = list->next;
 382                if (relevant_commit(commit)) {
 383                        if (relevant)
 384                                return NULL;
 385                        relevant = commit;
 386                }
 387        }
 388        return relevant;
 389}
 390
 391/*
 392 * The goal is to get REV_TREE_NEW as the result only if the
 393 * diff consists of all '+' (and no other changes), REV_TREE_OLD
 394 * if the whole diff is removal of old data, and otherwise
 395 * REV_TREE_DIFFERENT (of course if the trees are the same we
 396 * want REV_TREE_SAME).
 397 *
 398 * The only time we care about the distinction is when
 399 * remove_empty_trees is in effect, in which case we care only about
 400 * whether the whole change is REV_TREE_NEW, or if there's another type
 401 * of change. Which means we can stop the diff early in either of these
 402 * cases:
 403 *
 404 *   1. We're not using remove_empty_trees at all.
 405 *
 406 *   2. We saw anything except REV_TREE_NEW.
 407 */
 408static int tree_difference = REV_TREE_SAME;
 409
 410static void file_add_remove(struct diff_options *options,
 411                    int addremove, unsigned mode,
 412                    const unsigned char *sha1,
 413                    int sha1_valid,
 414                    const char *fullpath, unsigned dirty_submodule)
 415{
 416        int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
 417        struct rev_info *revs = options->change_fn_data;
 418
 419        tree_difference |= diff;
 420        if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
 421                DIFF_OPT_SET(options, HAS_CHANGES);
 422}
 423
 424static void file_change(struct diff_options *options,
 425                 unsigned old_mode, unsigned new_mode,
 426                 const unsigned char *old_sha1,
 427                 const unsigned char *new_sha1,
 428                 int old_sha1_valid, int new_sha1_valid,
 429                 const char *fullpath,
 430                 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
 431{
 432        tree_difference = REV_TREE_DIFFERENT;
 433        DIFF_OPT_SET(options, HAS_CHANGES);
 434}
 435
 436static int rev_compare_tree(struct rev_info *revs,
 437                            struct commit *parent, struct commit *commit)
 438{
 439        struct tree *t1 = parent->tree;
 440        struct tree *t2 = commit->tree;
 441
 442        if (!t1)
 443                return REV_TREE_NEW;
 444        if (!t2)
 445                return REV_TREE_OLD;
 446
 447        if (revs->simplify_by_decoration) {
 448                /*
 449                 * If we are simplifying by decoration, then the commit
 450                 * is worth showing if it has a tag pointing at it.
 451                 */
 452                if (get_name_decoration(&commit->object))
 453                        return REV_TREE_DIFFERENT;
 454                /*
 455                 * A commit that is not pointed by a tag is uninteresting
 456                 * if we are not limited by path.  This means that you will
 457                 * see the usual "commits that touch the paths" plus any
 458                 * tagged commit by specifying both --simplify-by-decoration
 459                 * and pathspec.
 460                 */
 461                if (!revs->prune_data.nr)
 462                        return REV_TREE_SAME;
 463        }
 464
 465        tree_difference = REV_TREE_SAME;
 466        DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
 467        if (diff_tree_sha1(t1->object.oid.hash, t2->object.oid.hash, "",
 468                           &revs->pruning) < 0)
 469                return REV_TREE_DIFFERENT;
 470        return tree_difference;
 471}
 472
 473static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
 474{
 475        int retval;
 476        struct tree *t1 = commit->tree;
 477
 478        if (!t1)
 479                return 0;
 480
 481        tree_difference = REV_TREE_SAME;
 482        DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
 483        retval = diff_tree_sha1(NULL, t1->object.oid.hash, "", &revs->pruning);
 484
 485        return retval >= 0 && (tree_difference == REV_TREE_SAME);
 486}
 487
 488struct treesame_state {
 489        unsigned int nparents;
 490        unsigned char treesame[FLEX_ARRAY];
 491};
 492
 493static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
 494{
 495        unsigned n = commit_list_count(commit->parents);
 496        struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
 497        st->nparents = n;
 498        add_decoration(&revs->treesame, &commit->object, st);
 499        return st;
 500}
 501
 502/*
 503 * Must be called immediately after removing the nth_parent from a commit's
 504 * parent list, if we are maintaining the per-parent treesame[] decoration.
 505 * This does not recalculate the master TREESAME flag - update_treesame()
 506 * should be called to update it after a sequence of treesame[] modifications
 507 * that may have affected it.
 508 */
 509static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
 510{
 511        struct treesame_state *st;
 512        int old_same;
 513
 514        if (!commit->parents) {
 515                /*
 516                 * Have just removed the only parent from a non-merge.
 517                 * Different handling, as we lack decoration.
 518                 */
 519                if (nth_parent != 0)
 520                        die("compact_treesame %u", nth_parent);
 521                old_same = !!(commit->object.flags & TREESAME);
 522                if (rev_same_tree_as_empty(revs, commit))
 523                        commit->object.flags |= TREESAME;
 524                else
 525                        commit->object.flags &= ~TREESAME;
 526                return old_same;
 527        }
 528
 529        st = lookup_decoration(&revs->treesame, &commit->object);
 530        if (!st || nth_parent >= st->nparents)
 531                die("compact_treesame %u", nth_parent);
 532
 533        old_same = st->treesame[nth_parent];
 534        memmove(st->treesame + nth_parent,
 535                st->treesame + nth_parent + 1,
 536                st->nparents - nth_parent - 1);
 537
 538        /*
 539         * If we've just become a non-merge commit, update TREESAME
 540         * immediately, and remove the no-longer-needed decoration.
 541         * If still a merge, defer update until update_treesame().
 542         */
 543        if (--st->nparents == 1) {
 544                if (commit->parents->next)
 545                        die("compact_treesame parents mismatch");
 546                if (st->treesame[0] && revs->dense)
 547                        commit->object.flags |= TREESAME;
 548                else
 549                        commit->object.flags &= ~TREESAME;
 550                free(add_decoration(&revs->treesame, &commit->object, NULL));
 551        }
 552
 553        return old_same;
 554}
 555
 556static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
 557{
 558        if (commit->parents && commit->parents->next) {
 559                unsigned n;
 560                struct treesame_state *st;
 561                struct commit_list *p;
 562                unsigned relevant_parents;
 563                unsigned relevant_change, irrelevant_change;
 564
 565                st = lookup_decoration(&revs->treesame, &commit->object);
 566                if (!st)
 567                        die("update_treesame %s", oid_to_hex(&commit->object.oid));
 568                relevant_parents = 0;
 569                relevant_change = irrelevant_change = 0;
 570                for (p = commit->parents, n = 0; p; n++, p = p->next) {
 571                        if (relevant_commit(p->item)) {
 572                                relevant_change |= !st->treesame[n];
 573                                relevant_parents++;
 574                        } else
 575                                irrelevant_change |= !st->treesame[n];
 576                }
 577                if (relevant_parents ? relevant_change : irrelevant_change)
 578                        commit->object.flags &= ~TREESAME;
 579                else
 580                        commit->object.flags |= TREESAME;
 581        }
 582
 583        return commit->object.flags & TREESAME;
 584}
 585
 586static inline int limiting_can_increase_treesame(const struct rev_info *revs)
 587{
 588        /*
 589         * TREESAME is irrelevant unless prune && dense;
 590         * if simplify_history is set, we can't have a mixture of TREESAME and
 591         *    !TREESAME INTERESTING parents (and we don't have treesame[]
 592         *    decoration anyway);
 593         * if first_parent_only is set, then the TREESAME flag is locked
 594         *    against the first parent (and again we lack treesame[] decoration).
 595         */
 596        return revs->prune && revs->dense &&
 597               !revs->simplify_history &&
 598               !revs->first_parent_only;
 599}
 600
 601static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
 602{
 603        struct commit_list **pp, *parent;
 604        struct treesame_state *ts = NULL;
 605        int relevant_change = 0, irrelevant_change = 0;
 606        int relevant_parents, nth_parent;
 607
 608        /*
 609         * If we don't do pruning, everything is interesting
 610         */
 611        if (!revs->prune)
 612                return;
 613
 614        if (!commit->tree)
 615                return;
 616
 617        if (!commit->parents) {
 618                if (rev_same_tree_as_empty(revs, commit))
 619                        commit->object.flags |= TREESAME;
 620                return;
 621        }
 622
 623        /*
 624         * Normal non-merge commit? If we don't want to make the
 625         * history dense, we consider it always to be a change..
 626         */
 627        if (!revs->dense && !commit->parents->next)
 628                return;
 629
 630        for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
 631             (parent = *pp) != NULL;
 632             pp = &parent->next, nth_parent++) {
 633                struct commit *p = parent->item;
 634                if (relevant_commit(p))
 635                        relevant_parents++;
 636
 637                if (nth_parent == 1) {
 638                        /*
 639                         * This our second loop iteration - so we now know
 640                         * we're dealing with a merge.
 641                         *
 642                         * Do not compare with later parents when we care only about
 643                         * the first parent chain, in order to avoid derailing the
 644                         * traversal to follow a side branch that brought everything
 645                         * in the path we are limited to by the pathspec.
 646                         */
 647                        if (revs->first_parent_only)
 648                                break;
 649                        /*
 650                         * If this will remain a potentially-simplifiable
 651                         * merge, remember per-parent treesame if needed.
 652                         * Initialise the array with the comparison from our
 653                         * first iteration.
 654                         */
 655                        if (revs->treesame.name &&
 656                            !revs->simplify_history &&
 657                            !(commit->object.flags & UNINTERESTING)) {
 658                                ts = initialise_treesame(revs, commit);
 659                                if (!(irrelevant_change || relevant_change))
 660                                        ts->treesame[0] = 1;
 661                        }
 662                }
 663                if (parse_commit(p) < 0)
 664                        die("cannot simplify commit %s (because of %s)",
 665                            oid_to_hex(&commit->object.oid),
 666                            oid_to_hex(&p->object.oid));
 667                switch (rev_compare_tree(revs, p, commit)) {
 668                case REV_TREE_SAME:
 669                        if (!revs->simplify_history || !relevant_commit(p)) {
 670                                /* Even if a merge with an uninteresting
 671                                 * side branch brought the entire change
 672                                 * we are interested in, we do not want
 673                                 * to lose the other branches of this
 674                                 * merge, so we just keep going.
 675                                 */
 676                                if (ts)
 677                                        ts->treesame[nth_parent] = 1;
 678                                continue;
 679                        }
 680                        parent->next = NULL;
 681                        commit->parents = parent;
 682                        commit->object.flags |= TREESAME;
 683                        return;
 684
 685                case REV_TREE_NEW:
 686                        if (revs->remove_empty_trees &&
 687                            rev_same_tree_as_empty(revs, p)) {
 688                                /* We are adding all the specified
 689                                 * paths from this parent, so the
 690                                 * history beyond this parent is not
 691                                 * interesting.  Remove its parents
 692                                 * (they are grandparents for us).
 693                                 * IOW, we pretend this parent is a
 694                                 * "root" commit.
 695                                 */
 696                                if (parse_commit(p) < 0)
 697                                        die("cannot simplify commit %s (invalid %s)",
 698                                            oid_to_hex(&commit->object.oid),
 699                                            oid_to_hex(&p->object.oid));
 700                                p->parents = NULL;
 701                        }
 702                /* fallthrough */
 703                case REV_TREE_OLD:
 704                case REV_TREE_DIFFERENT:
 705                        if (relevant_commit(p))
 706                                relevant_change = 1;
 707                        else
 708                                irrelevant_change = 1;
 709                        continue;
 710                }
 711                die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
 712        }
 713
 714        /*
 715         * TREESAME is straightforward for single-parent commits. For merge
 716         * commits, it is most useful to define it so that "irrelevant"
 717         * parents cannot make us !TREESAME - if we have any relevant
 718         * parents, then we only consider TREESAMEness with respect to them,
 719         * allowing irrelevant merges from uninteresting branches to be
 720         * simplified away. Only if we have only irrelevant parents do we
 721         * base TREESAME on them. Note that this logic is replicated in
 722         * update_treesame, which should be kept in sync.
 723         */
 724        if (relevant_parents ? !relevant_change : !irrelevant_change)
 725                commit->object.flags |= TREESAME;
 726}
 727
 728static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
 729                    struct commit_list *cached_base, struct commit_list **cache)
 730{
 731        struct commit_list *new_entry;
 732
 733        if (cached_base && p->date < cached_base->item->date)
 734                new_entry = commit_list_insert_by_date(p, &cached_base->next);
 735        else
 736                new_entry = commit_list_insert_by_date(p, head);
 737
 738        if (cache && (!*cache || p->date < (*cache)->item->date))
 739                *cache = new_entry;
 740}
 741
 742static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
 743                    struct commit_list **list, struct commit_list **cache_ptr)
 744{
 745        struct commit_list *parent = commit->parents;
 746        unsigned left_flag;
 747        struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
 748
 749        if (commit->object.flags & ADDED)
 750                return 0;
 751        commit->object.flags |= ADDED;
 752
 753        if (revs->include_check &&
 754            !revs->include_check(commit, revs->include_check_data))
 755                return 0;
 756
 757        /*
 758         * If the commit is uninteresting, don't try to
 759         * prune parents - we want the maximal uninteresting
 760         * set.
 761         *
 762         * Normally we haven't parsed the parent
 763         * yet, so we won't have a parent of a parent
 764         * here. However, it may turn out that we've
 765         * reached this commit some other way (where it
 766         * wasn't uninteresting), in which case we need
 767         * to mark its parents recursively too..
 768         */
 769        if (commit->object.flags & UNINTERESTING) {
 770                while (parent) {
 771                        struct commit *p = parent->item;
 772                        parent = parent->next;
 773                        if (p)
 774                                p->object.flags |= UNINTERESTING;
 775                        if (parse_commit_gently(p, 1) < 0)
 776                                continue;
 777                        if (p->parents)
 778                                mark_parents_uninteresting(p);
 779                        if (p->object.flags & SEEN)
 780                                continue;
 781                        p->object.flags |= SEEN;
 782                        commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
 783                }
 784                return 0;
 785        }
 786
 787        /*
 788         * Ok, the commit wasn't uninteresting. Try to
 789         * simplify the commit history and find the parent
 790         * that has no differences in the path set if one exists.
 791         */
 792        try_to_simplify_commit(revs, commit);
 793
 794        if (revs->no_walk)
 795                return 0;
 796
 797        left_flag = (commit->object.flags & SYMMETRIC_LEFT);
 798
 799        for (parent = commit->parents; parent; parent = parent->next) {
 800                struct commit *p = parent->item;
 801
 802                if (parse_commit_gently(p, revs->ignore_missing_links) < 0)
 803                        return -1;
 804                if (revs->show_source && !p->util)
 805                        p->util = commit->util;
 806                p->object.flags |= left_flag;
 807                if (!(p->object.flags & SEEN)) {
 808                        p->object.flags |= SEEN;
 809                        commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
 810                }
 811                if (revs->first_parent_only)
 812                        break;
 813        }
 814        return 0;
 815}
 816
 817static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
 818{
 819        struct commit_list *p;
 820        int left_count = 0, right_count = 0;
 821        int left_first;
 822        struct patch_ids ids;
 823        unsigned cherry_flag;
 824
 825        /* First count the commits on the left and on the right */
 826        for (p = list; p; p = p->next) {
 827                struct commit *commit = p->item;
 828                unsigned flags = commit->object.flags;
 829                if (flags & BOUNDARY)
 830                        ;
 831                else if (flags & SYMMETRIC_LEFT)
 832                        left_count++;
 833                else
 834                        right_count++;
 835        }
 836
 837        if (!left_count || !right_count)
 838                return;
 839
 840        left_first = left_count < right_count;
 841        init_patch_ids(&ids);
 842        ids.diffopts.pathspec = revs->diffopt.pathspec;
 843
 844        /* Compute patch-ids for one side */
 845        for (p = list; p; p = p->next) {
 846                struct commit *commit = p->item;
 847                unsigned flags = commit->object.flags;
 848
 849                if (flags & BOUNDARY)
 850                        continue;
 851                /*
 852                 * If we have fewer left, left_first is set and we omit
 853                 * commits on the right branch in this loop.  If we have
 854                 * fewer right, we skip the left ones.
 855                 */
 856                if (left_first != !!(flags & SYMMETRIC_LEFT))
 857                        continue;
 858                add_commit_patch_id(commit, &ids);
 859        }
 860
 861        /* either cherry_mark or cherry_pick are true */
 862        cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
 863
 864        /* Check the other side */
 865        for (p = list; p; p = p->next) {
 866                struct commit *commit = p->item;
 867                struct patch_id *id;
 868                unsigned flags = commit->object.flags;
 869
 870                if (flags & BOUNDARY)
 871                        continue;
 872                /*
 873                 * If we have fewer left, left_first is set and we omit
 874                 * commits on the left branch in this loop.
 875                 */
 876                if (left_first == !!(flags & SYMMETRIC_LEFT))
 877                        continue;
 878
 879                /*
 880                 * Have we seen the same patch id?
 881                 */
 882                id = has_commit_patch_id(commit, &ids);
 883                if (!id)
 884                        continue;
 885
 886                commit->object.flags |= cherry_flag;
 887                id->commit->object.flags |= cherry_flag;
 888        }
 889
 890        free_patch_ids(&ids);
 891}
 892
 893/* How many extra uninteresting commits we want to see.. */
 894#define SLOP 5
 895
 896static int still_interesting(struct commit_list *src, unsigned long date, int slop,
 897                             struct commit **interesting_cache)
 898{
 899        /*
 900         * No source list at all? We're definitely done..
 901         */
 902        if (!src)
 903                return 0;
 904
 905        /*
 906         * Does the destination list contain entries with a date
 907         * before the source list? Definitely _not_ done.
 908         */
 909        if (date <= src->item->date)
 910                return SLOP;
 911
 912        /*
 913         * Does the source list still have interesting commits in
 914         * it? Definitely not done..
 915         */
 916        if (!everybody_uninteresting(src, interesting_cache))
 917                return SLOP;
 918
 919        /* Ok, we're closing in.. */
 920        return slop-1;
 921}
 922
 923/*
 924 * "rev-list --ancestry-path A..B" computes commits that are ancestors
 925 * of B but not ancestors of A but further limits the result to those
 926 * that are descendants of A.  This takes the list of bottom commits and
 927 * the result of "A..B" without --ancestry-path, and limits the latter
 928 * further to the ones that can reach one of the commits in "bottom".
 929 */
 930static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
 931{
 932        struct commit_list *p;
 933        struct commit_list *rlist = NULL;
 934        int made_progress;
 935
 936        /*
 937         * Reverse the list so that it will be likely that we would
 938         * process parents before children.
 939         */
 940        for (p = list; p; p = p->next)
 941                commit_list_insert(p->item, &rlist);
 942
 943        for (p = bottom; p; p = p->next)
 944                p->item->object.flags |= TMP_MARK;
 945
 946        /*
 947         * Mark the ones that can reach bottom commits in "list",
 948         * in a bottom-up fashion.
 949         */
 950        do {
 951                made_progress = 0;
 952                for (p = rlist; p; p = p->next) {
 953                        struct commit *c = p->item;
 954                        struct commit_list *parents;
 955                        if (c->object.flags & (TMP_MARK | UNINTERESTING))
 956                                continue;
 957                        for (parents = c->parents;
 958                             parents;
 959                             parents = parents->next) {
 960                                if (!(parents->item->object.flags & TMP_MARK))
 961                                        continue;
 962                                c->object.flags |= TMP_MARK;
 963                                made_progress = 1;
 964                                break;
 965                        }
 966                }
 967        } while (made_progress);
 968
 969        /*
 970         * NEEDSWORK: decide if we want to remove parents that are
 971         * not marked with TMP_MARK from commit->parents for commits
 972         * in the resulting list.  We may not want to do that, though.
 973         */
 974
 975        /*
 976         * The ones that are not marked with TMP_MARK are uninteresting
 977         */
 978        for (p = list; p; p = p->next) {
 979                struct commit *c = p->item;
 980                if (c->object.flags & TMP_MARK)
 981                        continue;
 982                c->object.flags |= UNINTERESTING;
 983        }
 984
 985        /* We are done with the TMP_MARK */
 986        for (p = list; p; p = p->next)
 987                p->item->object.flags &= ~TMP_MARK;
 988        for (p = bottom; p; p = p->next)
 989                p->item->object.flags &= ~TMP_MARK;
 990        free_commit_list(rlist);
 991}
 992
 993/*
 994 * Before walking the history, keep the set of "negative" refs the
 995 * caller has asked to exclude.
 996 *
 997 * This is used to compute "rev-list --ancestry-path A..B", as we need
 998 * to filter the result of "A..B" further to the ones that can actually
 999 * reach A.
1000 */
1001static struct commit_list *collect_bottom_commits(struct commit_list *list)
1002{
1003        struct commit_list *elem, *bottom = NULL;
1004        for (elem = list; elem; elem = elem->next)
1005                if (elem->item->object.flags & BOTTOM)
1006                        commit_list_insert(elem->item, &bottom);
1007        return bottom;
1008}
1009
1010/* Assumes either left_only or right_only is set */
1011static void limit_left_right(struct commit_list *list, struct rev_info *revs)
1012{
1013        struct commit_list *p;
1014
1015        for (p = list; p; p = p->next) {
1016                struct commit *commit = p->item;
1017
1018                if (revs->right_only) {
1019                        if (commit->object.flags & SYMMETRIC_LEFT)
1020                                commit->object.flags |= SHOWN;
1021                } else  /* revs->left_only is set */
1022                        if (!(commit->object.flags & SYMMETRIC_LEFT))
1023                                commit->object.flags |= SHOWN;
1024        }
1025}
1026
1027static int limit_list(struct rev_info *revs)
1028{
1029        int slop = SLOP;
1030        unsigned long date = ~0ul;
1031        struct commit_list *list = revs->commits;
1032        struct commit_list *newlist = NULL;
1033        struct commit_list **p = &newlist;
1034        struct commit_list *bottom = NULL;
1035        struct commit *interesting_cache = NULL;
1036
1037        if (revs->ancestry_path) {
1038                bottom = collect_bottom_commits(list);
1039                if (!bottom)
1040                        die("--ancestry-path given but there are no bottom commits");
1041        }
1042
1043        while (list) {
1044                struct commit *commit = pop_commit(&list);
1045                struct object *obj = &commit->object;
1046                show_early_output_fn_t show;
1047
1048                if (commit == interesting_cache)
1049                        interesting_cache = NULL;
1050
1051                if (revs->max_age != -1 && (commit->date < revs->max_age))
1052                        obj->flags |= UNINTERESTING;
1053                if (add_parents_to_list(revs, commit, &list, NULL) < 0)
1054                        return -1;
1055                if (obj->flags & UNINTERESTING) {
1056                        mark_parents_uninteresting(commit);
1057                        if (revs->show_all)
1058                                p = &commit_list_insert(commit, p)->next;
1059                        slop = still_interesting(list, date, slop, &interesting_cache);
1060                        if (slop)
1061                                continue;
1062                        /* If showing all, add the whole pending list to the end */
1063                        if (revs->show_all)
1064                                *p = list;
1065                        break;
1066                }
1067                if (revs->min_age != -1 && (commit->date > revs->min_age))
1068                        continue;
1069                date = commit->date;
1070                p = &commit_list_insert(commit, p)->next;
1071
1072                show = show_early_output;
1073                if (!show)
1074                        continue;
1075
1076                show(revs, newlist);
1077                show_early_output = NULL;
1078        }
1079        if (revs->cherry_pick || revs->cherry_mark)
1080                cherry_pick_list(newlist, revs);
1081
1082        if (revs->left_only || revs->right_only)
1083                limit_left_right(newlist, revs);
1084
1085        if (bottom) {
1086                limit_to_ancestry(bottom, newlist);
1087                free_commit_list(bottom);
1088        }
1089
1090        /*
1091         * Check if any commits have become TREESAME by some of their parents
1092         * becoming UNINTERESTING.
1093         */
1094        if (limiting_can_increase_treesame(revs))
1095                for (list = newlist; list; list = list->next) {
1096                        struct commit *c = list->item;
1097                        if (c->object.flags & (UNINTERESTING | TREESAME))
1098                                continue;
1099                        update_treesame(revs, c);
1100                }
1101
1102        revs->commits = newlist;
1103        return 0;
1104}
1105
1106/*
1107 * Add an entry to refs->cmdline with the specified information.
1108 * *name is copied.
1109 */
1110static void add_rev_cmdline(struct rev_info *revs,
1111                            struct object *item,
1112                            const char *name,
1113                            int whence,
1114                            unsigned flags)
1115{
1116        struct rev_cmdline_info *info = &revs->cmdline;
1117        int nr = info->nr;
1118
1119        ALLOC_GROW(info->rev, nr + 1, info->alloc);
1120        info->rev[nr].item = item;
1121        info->rev[nr].name = xstrdup(name);
1122        info->rev[nr].whence = whence;
1123        info->rev[nr].flags = flags;
1124        info->nr++;
1125}
1126
1127static void add_rev_cmdline_list(struct rev_info *revs,
1128                                 struct commit_list *commit_list,
1129                                 int whence,
1130                                 unsigned flags)
1131{
1132        while (commit_list) {
1133                struct object *object = &commit_list->item->object;
1134                add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
1135                                whence, flags);
1136                commit_list = commit_list->next;
1137        }
1138}
1139
1140struct all_refs_cb {
1141        int all_flags;
1142        int warned_bad_reflog;
1143        struct rev_info *all_revs;
1144        const char *name_for_errormsg;
1145};
1146
1147int ref_excluded(struct string_list *ref_excludes, const char *path)
1148{
1149        struct string_list_item *item;
1150
1151        if (!ref_excludes)
1152                return 0;
1153        for_each_string_list_item(item, ref_excludes) {
1154                if (!wildmatch(item->string, path, 0, NULL))
1155                        return 1;
1156        }
1157        return 0;
1158}
1159
1160static int handle_one_ref(const char *path, const struct object_id *oid,
1161                          int flag, void *cb_data)
1162{
1163        struct all_refs_cb *cb = cb_data;
1164        struct object *object;
1165
1166        if (ref_excluded(cb->all_revs->ref_excludes, path))
1167            return 0;
1168
1169        object = get_reference(cb->all_revs, path, oid->hash, cb->all_flags);
1170        add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1171        add_pending_sha1(cb->all_revs, path, oid->hash, cb->all_flags);
1172        return 0;
1173}
1174
1175static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1176        unsigned flags)
1177{
1178        cb->all_revs = revs;
1179        cb->all_flags = flags;
1180}
1181
1182void clear_ref_exclusion(struct string_list **ref_excludes_p)
1183{
1184        if (*ref_excludes_p) {
1185                string_list_clear(*ref_excludes_p, 0);
1186                free(*ref_excludes_p);
1187        }
1188        *ref_excludes_p = NULL;
1189}
1190
1191void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1192{
1193        if (!*ref_excludes_p) {
1194                *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
1195                (*ref_excludes_p)->strdup_strings = 1;
1196        }
1197        string_list_append(*ref_excludes_p, exclude);
1198}
1199
1200static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
1201                int (*for_each)(const char *, each_ref_fn, void *))
1202{
1203        struct all_refs_cb cb;
1204        init_all_refs_cb(&cb, revs, flags);
1205        for_each(submodule, handle_one_ref, &cb);
1206}
1207
1208static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
1209{
1210        struct all_refs_cb *cb = cb_data;
1211        if (!is_null_sha1(sha1)) {
1212                struct object *o = parse_object(sha1);
1213                if (o) {
1214                        o->flags |= cb->all_flags;
1215                        /* ??? CMDLINEFLAGS ??? */
1216                        add_pending_object(cb->all_revs, o, "");
1217                }
1218                else if (!cb->warned_bad_reflog) {
1219                        warning("reflog of '%s' references pruned commits",
1220                                cb->name_for_errormsg);
1221                        cb->warned_bad_reflog = 1;
1222                }
1223        }
1224}
1225
1226static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
1227                const char *email, unsigned long timestamp, int tz,
1228                const char *message, void *cb_data)
1229{
1230        handle_one_reflog_commit(osha1, cb_data);
1231        handle_one_reflog_commit(nsha1, cb_data);
1232        return 0;
1233}
1234
1235static int handle_one_reflog(const char *path, const struct object_id *oid,
1236                             int flag, void *cb_data)
1237{
1238        struct all_refs_cb *cb = cb_data;
1239        cb->warned_bad_reflog = 0;
1240        cb->name_for_errormsg = path;
1241        for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
1242        return 0;
1243}
1244
1245void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
1246{
1247        struct all_refs_cb cb;
1248
1249        cb.all_revs = revs;
1250        cb.all_flags = flags;
1251        for_each_reflog(handle_one_reflog, &cb);
1252}
1253
1254static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
1255                           struct strbuf *path)
1256{
1257        size_t baselen = path->len;
1258        int i;
1259
1260        if (it->entry_count >= 0) {
1261                struct tree *tree = lookup_tree(it->sha1);
1262                add_pending_object_with_path(revs, &tree->object, "",
1263                                             040000, path->buf);
1264        }
1265
1266        for (i = 0; i < it->subtree_nr; i++) {
1267                struct cache_tree_sub *sub = it->down[i];
1268                strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
1269                add_cache_tree(sub->cache_tree, revs, path);
1270                strbuf_setlen(path, baselen);
1271        }
1272
1273}
1274
1275void add_index_objects_to_pending(struct rev_info *revs, unsigned flags)
1276{
1277        int i;
1278
1279        read_cache();
1280        for (i = 0; i < active_nr; i++) {
1281                struct cache_entry *ce = active_cache[i];
1282                struct blob *blob;
1283
1284                if (S_ISGITLINK(ce->ce_mode))
1285                        continue;
1286
1287                blob = lookup_blob(ce->oid.hash);
1288                if (!blob)
1289                        die("unable to add index blob to traversal");
1290                add_pending_object_with_path(revs, &blob->object, "",
1291                                             ce->ce_mode, ce->name);
1292        }
1293
1294        if (active_cache_tree) {
1295                struct strbuf path = STRBUF_INIT;
1296                add_cache_tree(active_cache_tree, revs, &path);
1297                strbuf_release(&path);
1298        }
1299}
1300
1301static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
1302                            int exclude_parent)
1303{
1304        unsigned char sha1[20];
1305        struct object *it;
1306        struct commit *commit;
1307        struct commit_list *parents;
1308        int parent_number;
1309        const char *arg = arg_;
1310
1311        if (*arg == '^') {
1312                flags ^= UNINTERESTING | BOTTOM;
1313                arg++;
1314        }
1315        if (get_sha1_committish(arg, sha1))
1316                return 0;
1317        while (1) {
1318                it = get_reference(revs, arg, sha1, 0);
1319                if (!it && revs->ignore_missing)
1320                        return 0;
1321                if (it->type != OBJ_TAG)
1322                        break;
1323                if (!((struct tag*)it)->tagged)
1324                        return 0;
1325                hashcpy(sha1, ((struct tag*)it)->tagged->oid.hash);
1326        }
1327        if (it->type != OBJ_COMMIT)
1328                return 0;
1329        commit = (struct commit *)it;
1330        if (exclude_parent &&
1331            exclude_parent > commit_list_count(commit->parents))
1332                return 0;
1333        for (parents = commit->parents, parent_number = 1;
1334             parents;
1335             parents = parents->next, parent_number++) {
1336                if (exclude_parent && parent_number != exclude_parent)
1337                        continue;
1338
1339                it = &parents->item->object;
1340                it->flags |= flags;
1341                add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
1342                add_pending_object(revs, it, arg);
1343        }
1344        return 1;
1345}
1346
1347void init_revisions(struct rev_info *revs, const char *prefix)
1348{
1349        memset(revs, 0, sizeof(*revs));
1350
1351        revs->abbrev = DEFAULT_ABBREV;
1352        revs->ignore_merges = 1;
1353        revs->simplify_history = 1;
1354        DIFF_OPT_SET(&revs->pruning, RECURSIVE);
1355        DIFF_OPT_SET(&revs->pruning, QUICK);
1356        revs->pruning.add_remove = file_add_remove;
1357        revs->pruning.change = file_change;
1358        revs->pruning.change_fn_data = revs;
1359        revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1360        revs->dense = 1;
1361        revs->prefix = prefix;
1362        revs->max_age = -1;
1363        revs->min_age = -1;
1364        revs->skip_count = -1;
1365        revs->max_count = -1;
1366        revs->max_parents = -1;
1367        revs->expand_tabs_in_log = -1;
1368
1369        revs->commit_format = CMIT_FMT_DEFAULT;
1370        revs->expand_tabs_in_log_default = 8;
1371
1372        init_grep_defaults();
1373        grep_init(&revs->grep_filter, prefix);
1374        revs->grep_filter.status_only = 1;
1375        revs->grep_filter.regflags = REG_NEWLINE;
1376
1377        diff_setup(&revs->diffopt);
1378        if (prefix && !revs->diffopt.prefix) {
1379                revs->diffopt.prefix = prefix;
1380                revs->diffopt.prefix_length = strlen(prefix);
1381        }
1382
1383        revs->notes_opt.use_default_notes = -1;
1384}
1385
1386static void add_pending_commit_list(struct rev_info *revs,
1387                                    struct commit_list *commit_list,
1388                                    unsigned int flags)
1389{
1390        while (commit_list) {
1391                struct object *object = &commit_list->item->object;
1392                object->flags |= flags;
1393                add_pending_object(revs, object, oid_to_hex(&object->oid));
1394                commit_list = commit_list->next;
1395        }
1396}
1397
1398static void prepare_show_merge(struct rev_info *revs)
1399{
1400        struct commit_list *bases;
1401        struct commit *head, *other;
1402        unsigned char sha1[20];
1403        const char **prune = NULL;
1404        int i, prune_num = 1; /* counting terminating NULL */
1405
1406        if (get_sha1("HEAD", sha1))
1407                die("--merge without HEAD?");
1408        head = lookup_commit_or_die(sha1, "HEAD");
1409        if (get_sha1("MERGE_HEAD", sha1))
1410                die("--merge without MERGE_HEAD?");
1411        other = lookup_commit_or_die(sha1, "MERGE_HEAD");
1412        add_pending_object(revs, &head->object, "HEAD");
1413        add_pending_object(revs, &other->object, "MERGE_HEAD");
1414        bases = get_merge_bases(head, other);
1415        add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
1416        add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
1417        free_commit_list(bases);
1418        head->object.flags |= SYMMETRIC_LEFT;
1419
1420        if (!active_nr)
1421                read_cache();
1422        for (i = 0; i < active_nr; i++) {
1423                const struct cache_entry *ce = active_cache[i];
1424                if (!ce_stage(ce))
1425                        continue;
1426                if (ce_path_match(ce, &revs->prune_data, NULL)) {
1427                        prune_num++;
1428                        REALLOC_ARRAY(prune, prune_num);
1429                        prune[prune_num-2] = ce->name;
1430                        prune[prune_num-1] = NULL;
1431                }
1432                while ((i+1 < active_nr) &&
1433                       ce_same_name(ce, active_cache[i+1]))
1434                        i++;
1435        }
1436        clear_pathspec(&revs->prune_data);
1437        parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
1438                       PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
1439        revs->limited = 1;
1440}
1441
1442int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
1443{
1444        struct object_context oc;
1445        char *dotdot;
1446        struct object *object;
1447        unsigned char sha1[20];
1448        int local_flags;
1449        const char *arg = arg_;
1450        int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
1451        unsigned get_sha1_flags = 0;
1452
1453        flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
1454
1455        dotdot = strstr(arg, "..");
1456        if (dotdot) {
1457                unsigned char from_sha1[20];
1458                const char *next = dotdot + 2;
1459                const char *this = arg;
1460                int symmetric = *next == '.';
1461                unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
1462                static const char head_by_default[] = "HEAD";
1463                unsigned int a_flags;
1464
1465                *dotdot = 0;
1466                next += symmetric;
1467
1468                if (!*next)
1469                        next = head_by_default;
1470                if (dotdot == arg)
1471                        this = head_by_default;
1472                if (this == head_by_default && next == head_by_default &&
1473                    !symmetric) {
1474                        /*
1475                         * Just ".."?  That is not a range but the
1476                         * pathspec for the parent directory.
1477                         */
1478                        if (!cant_be_filename) {
1479                                *dotdot = '.';
1480                                return -1;
1481                        }
1482                }
1483                if (!get_sha1_committish(this, from_sha1) &&
1484                    !get_sha1_committish(next, sha1)) {
1485                        struct object *a_obj, *b_obj;
1486
1487                        if (!cant_be_filename) {
1488                                *dotdot = '.';
1489                                verify_non_filename(revs->prefix, arg);
1490                        }
1491
1492                        a_obj = parse_object(from_sha1);
1493                        b_obj = parse_object(sha1);
1494                        if (!a_obj || !b_obj) {
1495                        missing:
1496                                if (revs->ignore_missing)
1497                                        return 0;
1498                                die(symmetric
1499                                    ? "Invalid symmetric difference expression %s"
1500                                    : "Invalid revision range %s", arg);
1501                        }
1502
1503                        if (!symmetric) {
1504                                /* just A..B */
1505                                a_flags = flags_exclude;
1506                        } else {
1507                                /* A...B -- find merge bases between the two */
1508                                struct commit *a, *b;
1509                                struct commit_list *exclude;
1510
1511                                a = (a_obj->type == OBJ_COMMIT
1512                                     ? (struct commit *)a_obj
1513                                     : lookup_commit_reference(a_obj->oid.hash));
1514                                b = (b_obj->type == OBJ_COMMIT
1515                                     ? (struct commit *)b_obj
1516                                     : lookup_commit_reference(b_obj->oid.hash));
1517                                if (!a || !b)
1518                                        goto missing;
1519                                exclude = get_merge_bases(a, b);
1520                                add_rev_cmdline_list(revs, exclude,
1521                                                     REV_CMD_MERGE_BASE,
1522                                                     flags_exclude);
1523                                add_pending_commit_list(revs, exclude,
1524                                                        flags_exclude);
1525                                free_commit_list(exclude);
1526
1527                                a_flags = flags | SYMMETRIC_LEFT;
1528                        }
1529
1530                        a_obj->flags |= a_flags;
1531                        b_obj->flags |= flags;
1532                        add_rev_cmdline(revs, a_obj, this,
1533                                        REV_CMD_LEFT, a_flags);
1534                        add_rev_cmdline(revs, b_obj, next,
1535                                        REV_CMD_RIGHT, flags);
1536                        add_pending_object(revs, a_obj, this);
1537                        add_pending_object(revs, b_obj, next);
1538                        return 0;
1539                }
1540                *dotdot = '.';
1541        }
1542
1543        dotdot = strstr(arg, "^@");
1544        if (dotdot && !dotdot[2]) {
1545                *dotdot = 0;
1546                if (add_parents_only(revs, arg, flags, 0))
1547                        return 0;
1548                *dotdot = '^';
1549        }
1550        dotdot = strstr(arg, "^!");
1551        if (dotdot && !dotdot[2]) {
1552                *dotdot = 0;
1553                if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
1554                        *dotdot = '^';
1555        }
1556        dotdot = strstr(arg, "^-");
1557        if (dotdot) {
1558                int exclude_parent = 1;
1559
1560                if (dotdot[2]) {
1561                        char *end;
1562                        exclude_parent = strtoul(dotdot + 2, &end, 10);
1563                        if (*end != '\0' || !exclude_parent)
1564                                return -1;
1565                }
1566
1567                *dotdot = 0;
1568                if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
1569                        *dotdot = '^';
1570        }
1571
1572        local_flags = 0;
1573        if (*arg == '^') {
1574                local_flags = UNINTERESTING | BOTTOM;
1575                arg++;
1576        }
1577
1578        if (revarg_opt & REVARG_COMMITTISH)
1579                get_sha1_flags = GET_SHA1_COMMITTISH;
1580
1581        if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
1582                return revs->ignore_missing ? 0 : -1;
1583        if (!cant_be_filename)
1584                verify_non_filename(revs->prefix, arg);
1585        object = get_reference(revs, arg, sha1, flags ^ local_flags);
1586        add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
1587        add_pending_object_with_mode(revs, object, arg, oc.mode);
1588        return 0;
1589}
1590
1591struct cmdline_pathspec {
1592        int alloc;
1593        int nr;
1594        const char **path;
1595};
1596
1597static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
1598{
1599        while (*av) {
1600                ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1601                prune->path[prune->nr++] = *(av++);
1602        }
1603}
1604
1605static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
1606                                     struct cmdline_pathspec *prune)
1607{
1608        while (strbuf_getline(sb, stdin) != EOF) {
1609                ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
1610                prune->path[prune->nr++] = xstrdup(sb->buf);
1611        }
1612}
1613
1614static void read_revisions_from_stdin(struct rev_info *revs,
1615                                      struct cmdline_pathspec *prune)
1616{
1617        struct strbuf sb;
1618        int seen_dashdash = 0;
1619        int save_warning;
1620
1621        save_warning = warn_on_object_refname_ambiguity;
1622        warn_on_object_refname_ambiguity = 0;
1623
1624        strbuf_init(&sb, 1000);
1625        while (strbuf_getline(&sb, stdin) != EOF) {
1626                int len = sb.len;
1627                if (!len)
1628                        break;
1629                if (sb.buf[0] == '-') {
1630                        if (len == 2 && sb.buf[1] == '-') {
1631                                seen_dashdash = 1;
1632                                break;
1633                        }
1634                        die("options not supported in --stdin mode");
1635                }
1636                if (handle_revision_arg(sb.buf, revs, 0,
1637                                        REVARG_CANNOT_BE_FILENAME))
1638                        die("bad revision '%s'", sb.buf);
1639        }
1640        if (seen_dashdash)
1641                read_pathspec_from_stdin(revs, &sb, prune);
1642
1643        strbuf_release(&sb);
1644        warn_on_object_refname_ambiguity = save_warning;
1645}
1646
1647static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
1648{
1649        append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
1650}
1651
1652static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
1653{
1654        append_header_grep_pattern(&revs->grep_filter, field, pattern);
1655}
1656
1657static void add_message_grep(struct rev_info *revs, const char *pattern)
1658{
1659        add_grep(revs, pattern, GREP_PATTERN_BODY);
1660}
1661
1662static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
1663                               int *unkc, const char **unkv)
1664{
1665        const char *arg = argv[0];
1666        const char *optarg;
1667        int argcount;
1668
1669        /* pseudo revision arguments */
1670        if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
1671            !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
1672            !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
1673            !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1674            !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
1675            !strcmp(arg, "--indexed-objects") ||
1676            starts_with(arg, "--exclude=") ||
1677            starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
1678            starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
1679        {
1680                unkv[(*unkc)++] = arg;
1681                return 1;
1682        }
1683
1684        if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
1685                revs->max_count = atoi(optarg);
1686                revs->no_walk = 0;
1687                return argcount;
1688        } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
1689                revs->skip_count = atoi(optarg);
1690                return argcount;
1691        } else if ((*arg == '-') && isdigit(arg[1])) {
1692                /* accept -<digit>, like traditional "head" */
1693                if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
1694                    revs->max_count < 0)
1695                        die("'%s': not a non-negative integer", arg + 1);
1696                revs->no_walk = 0;
1697        } else if (!strcmp(arg, "-n")) {
1698                if (argc <= 1)
1699                        return error("-n requires an argument");
1700                revs->max_count = atoi(argv[1]);
1701                revs->no_walk = 0;
1702                return 2;
1703        } else if (starts_with(arg, "-n")) {
1704                revs->max_count = atoi(arg + 2);
1705                revs->no_walk = 0;
1706        } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
1707                revs->max_age = atoi(optarg);
1708                return argcount;
1709        } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
1710                revs->max_age = approxidate(optarg);
1711                return argcount;
1712        } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
1713                revs->max_age = approxidate(optarg);
1714                return argcount;
1715        } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
1716                revs->min_age = atoi(optarg);
1717                return argcount;
1718        } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
1719                revs->min_age = approxidate(optarg);
1720                return argcount;
1721        } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
1722                revs->min_age = approxidate(optarg);
1723                return argcount;
1724        } else if (!strcmp(arg, "--first-parent")) {
1725                revs->first_parent_only = 1;
1726        } else if (!strcmp(arg, "--ancestry-path")) {
1727                revs->ancestry_path = 1;
1728                revs->simplify_history = 0;
1729                revs->limited = 1;
1730        } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
1731                init_reflog_walk(&revs->reflog_info);
1732        } else if (!strcmp(arg, "--default")) {
1733                if (argc <= 1)
1734                        return error("bad --default argument");
1735                revs->def = argv[1];
1736                return 2;
1737        } else if (!strcmp(arg, "--merge")) {
1738                revs->show_merge = 1;
1739        } else if (!strcmp(arg, "--topo-order")) {
1740                revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
1741                revs->topo_order = 1;
1742        } else if (!strcmp(arg, "--simplify-merges")) {
1743                revs->simplify_merges = 1;
1744                revs->topo_order = 1;
1745                revs->rewrite_parents = 1;
1746                revs->simplify_history = 0;
1747                revs->limited = 1;
1748        } else if (!strcmp(arg, "--simplify-by-decoration")) {
1749                revs->simplify_merges = 1;
1750                revs->topo_order = 1;
1751                revs->rewrite_parents = 1;
1752                revs->simplify_history = 0;
1753                revs->simplify_by_decoration = 1;
1754                revs->limited = 1;
1755                revs->prune = 1;
1756                load_ref_decorations(DECORATE_SHORT_REFS);
1757        } else if (!strcmp(arg, "--date-order")) {
1758                revs->sort_order = REV_SORT_BY_COMMIT_DATE;
1759                revs->topo_order = 1;
1760        } else if (!strcmp(arg, "--author-date-order")) {
1761                revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
1762                revs->topo_order = 1;
1763        } else if (starts_with(arg, "--early-output")) {
1764                int count = 100;
1765                switch (arg[14]) {
1766                case '=':
1767                        count = atoi(arg+15);
1768                        /* Fallthrough */
1769                case 0:
1770                        revs->topo_order = 1;
1771                       revs->early_output = count;
1772                }
1773        } else if (!strcmp(arg, "--parents")) {
1774                revs->rewrite_parents = 1;
1775                revs->print_parents = 1;
1776        } else if (!strcmp(arg, "--dense")) {
1777                revs->dense = 1;
1778        } else if (!strcmp(arg, "--sparse")) {
1779                revs->dense = 0;
1780        } else if (!strcmp(arg, "--show-all")) {
1781                revs->show_all = 1;
1782        } else if (!strcmp(arg, "--remove-empty")) {
1783                revs->remove_empty_trees = 1;
1784        } else if (!strcmp(arg, "--merges")) {
1785                revs->min_parents = 2;
1786        } else if (!strcmp(arg, "--no-merges")) {
1787                revs->max_parents = 1;
1788        } else if (starts_with(arg, "--min-parents=")) {
1789                revs->min_parents = atoi(arg+14);
1790        } else if (starts_with(arg, "--no-min-parents")) {
1791                revs->min_parents = 0;
1792        } else if (starts_with(arg, "--max-parents=")) {
1793                revs->max_parents = atoi(arg+14);
1794        } else if (starts_with(arg, "--no-max-parents")) {
1795                revs->max_parents = -1;
1796        } else if (!strcmp(arg, "--boundary")) {
1797                revs->boundary = 1;
1798        } else if (!strcmp(arg, "--left-right")) {
1799                revs->left_right = 1;
1800        } else if (!strcmp(arg, "--left-only")) {
1801                if (revs->right_only)
1802                        die("--left-only is incompatible with --right-only"
1803                            " or --cherry");
1804                revs->left_only = 1;
1805        } else if (!strcmp(arg, "--right-only")) {
1806                if (revs->left_only)
1807                        die("--right-only is incompatible with --left-only");
1808                revs->right_only = 1;
1809        } else if (!strcmp(arg, "--cherry")) {
1810                if (revs->left_only)
1811                        die("--cherry is incompatible with --left-only");
1812                revs->cherry_mark = 1;
1813                revs->right_only = 1;
1814                revs->max_parents = 1;
1815                revs->limited = 1;
1816        } else if (!strcmp(arg, "--count")) {
1817                revs->count = 1;
1818        } else if (!strcmp(arg, "--cherry-mark")) {
1819                if (revs->cherry_pick)
1820                        die("--cherry-mark is incompatible with --cherry-pick");
1821                revs->cherry_mark = 1;
1822                revs->limited = 1; /* needs limit_list() */
1823        } else if (!strcmp(arg, "--cherry-pick")) {
1824                if (revs->cherry_mark)
1825                        die("--cherry-pick is incompatible with --cherry-mark");
1826                revs->cherry_pick = 1;
1827                revs->limited = 1;
1828        } else if (!strcmp(arg, "--objects")) {
1829                revs->tag_objects = 1;
1830                revs->tree_objects = 1;
1831                revs->blob_objects = 1;
1832        } else if (!strcmp(arg, "--objects-edge")) {
1833                revs->tag_objects = 1;
1834                revs->tree_objects = 1;
1835                revs->blob_objects = 1;
1836                revs->edge_hint = 1;
1837        } else if (!strcmp(arg, "--objects-edge-aggressive")) {
1838                revs->tag_objects = 1;
1839                revs->tree_objects = 1;
1840                revs->blob_objects = 1;
1841                revs->edge_hint = 1;
1842                revs->edge_hint_aggressive = 1;
1843        } else if (!strcmp(arg, "--verify-objects")) {
1844                revs->tag_objects = 1;
1845                revs->tree_objects = 1;
1846                revs->blob_objects = 1;
1847                revs->verify_objects = 1;
1848        } else if (!strcmp(arg, "--unpacked")) {
1849                revs->unpacked = 1;
1850        } else if (starts_with(arg, "--unpacked=")) {
1851                die("--unpacked=<packfile> no longer supported.");
1852        } else if (!strcmp(arg, "-r")) {
1853                revs->diff = 1;
1854                DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1855        } else if (!strcmp(arg, "-t")) {
1856                revs->diff = 1;
1857                DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
1858                DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
1859        } else if (!strcmp(arg, "-m")) {
1860                revs->ignore_merges = 0;
1861        } else if (!strcmp(arg, "-c")) {
1862                revs->diff = 1;
1863                revs->dense_combined_merges = 0;
1864                revs->combine_merges = 1;
1865        } else if (!strcmp(arg, "--cc")) {
1866                revs->diff = 1;
1867                revs->dense_combined_merges = 1;
1868                revs->combine_merges = 1;
1869        } else if (!strcmp(arg, "-v")) {
1870                revs->verbose_header = 1;
1871        } else if (!strcmp(arg, "--pretty")) {
1872                revs->verbose_header = 1;
1873                revs->pretty_given = 1;
1874                get_commit_format(NULL, revs);
1875        } else if (starts_with(arg, "--pretty=") || starts_with(arg, "--format=")) {
1876                /*
1877                 * Detached form ("--pretty X" as opposed to "--pretty=X")
1878                 * not allowed, since the argument is optional.
1879                 */
1880                revs->verbose_header = 1;
1881                revs->pretty_given = 1;
1882                get_commit_format(arg+9, revs);
1883        } else if (!strcmp(arg, "--expand-tabs")) {
1884                revs->expand_tabs_in_log = 8;
1885        } else if (!strcmp(arg, "--no-expand-tabs")) {
1886                revs->expand_tabs_in_log = 0;
1887        } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
1888                int val;
1889                if (strtol_i(arg, 10, &val) < 0 || val < 0)
1890                        die("'%s': not a non-negative integer", arg);
1891                revs->expand_tabs_in_log = val;
1892        } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
1893                revs->show_notes = 1;
1894                revs->show_notes_given = 1;
1895                revs->notes_opt.use_default_notes = 1;
1896        } else if (!strcmp(arg, "--show-signature")) {
1897                revs->show_signature = 1;
1898        } else if (!strcmp(arg, "--no-show-signature")) {
1899                revs->show_signature = 0;
1900        } else if (!strcmp(arg, "--show-linear-break") ||
1901                   starts_with(arg, "--show-linear-break=")) {
1902                if (starts_with(arg, "--show-linear-break="))
1903                        revs->break_bar = xstrdup(arg + 20);
1904                else
1905                        revs->break_bar = "                    ..........";
1906                revs->track_linear = 1;
1907                revs->track_first_time = 1;
1908        } else if (starts_with(arg, "--show-notes=") ||
1909                   starts_with(arg, "--notes=")) {
1910                struct strbuf buf = STRBUF_INIT;
1911                revs->show_notes = 1;
1912                revs->show_notes_given = 1;
1913                if (starts_with(arg, "--show-notes")) {
1914                        if (revs->notes_opt.use_default_notes < 0)
1915                                revs->notes_opt.use_default_notes = 1;
1916                        strbuf_addstr(&buf, arg+13);
1917                }
1918                else
1919                        strbuf_addstr(&buf, arg+8);
1920                expand_notes_ref(&buf);
1921                string_list_append(&revs->notes_opt.extra_notes_refs,
1922                                   strbuf_detach(&buf, NULL));
1923        } else if (!strcmp(arg, "--no-notes")) {
1924                revs->show_notes = 0;
1925                revs->show_notes_given = 1;
1926                revs->notes_opt.use_default_notes = -1;
1927                /* we have been strdup'ing ourselves, so trick
1928                 * string_list into free()ing strings */
1929                revs->notes_opt.extra_notes_refs.strdup_strings = 1;
1930                string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
1931                revs->notes_opt.extra_notes_refs.strdup_strings = 0;
1932        } else if (!strcmp(arg, "--standard-notes")) {
1933                revs->show_notes_given = 1;
1934                revs->notes_opt.use_default_notes = 1;
1935        } else if (!strcmp(arg, "--no-standard-notes")) {
1936                revs->notes_opt.use_default_notes = 0;
1937        } else if (!strcmp(arg, "--oneline")) {
1938                revs->verbose_header = 1;
1939                get_commit_format("oneline", revs);
1940                revs->pretty_given = 1;
1941                revs->abbrev_commit = 1;
1942        } else if (!strcmp(arg, "--graph")) {
1943                revs->topo_order = 1;
1944                revs->rewrite_parents = 1;
1945                revs->graph = graph_init(revs);
1946        } else if (!strcmp(arg, "--root")) {
1947                revs->show_root_diff = 1;
1948        } else if (!strcmp(arg, "--no-commit-id")) {
1949                revs->no_commit_id = 1;
1950        } else if (!strcmp(arg, "--always")) {
1951                revs->always_show_header = 1;
1952        } else if (!strcmp(arg, "--no-abbrev")) {
1953                revs->abbrev = 0;
1954        } else if (!strcmp(arg, "--abbrev")) {
1955                revs->abbrev = DEFAULT_ABBREV;
1956        } else if (starts_with(arg, "--abbrev=")) {
1957                revs->abbrev = strtoul(arg + 9, NULL, 10);
1958                if (revs->abbrev < MINIMUM_ABBREV)
1959                        revs->abbrev = MINIMUM_ABBREV;
1960                else if (revs->abbrev > 40)
1961                        revs->abbrev = 40;
1962        } else if (!strcmp(arg, "--abbrev-commit")) {
1963                revs->abbrev_commit = 1;
1964                revs->abbrev_commit_given = 1;
1965        } else if (!strcmp(arg, "--no-abbrev-commit")) {
1966                revs->abbrev_commit = 0;
1967        } else if (!strcmp(arg, "--full-diff")) {
1968                revs->diff = 1;
1969                revs->full_diff = 1;
1970        } else if (!strcmp(arg, "--full-history")) {
1971                revs->simplify_history = 0;
1972        } else if (!strcmp(arg, "--relative-date")) {
1973                revs->date_mode.type = DATE_RELATIVE;
1974                revs->date_mode_explicit = 1;
1975        } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
1976                parse_date_format(optarg, &revs->date_mode);
1977                revs->date_mode_explicit = 1;
1978                return argcount;
1979        } else if (!strcmp(arg, "--log-size")) {
1980                revs->show_log_size = 1;
1981        }
1982        /*
1983         * Grepping the commit log
1984         */
1985        else if ((argcount = parse_long_opt("author", argv, &optarg))) {
1986                add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
1987                return argcount;
1988        } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
1989                add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
1990                return argcount;
1991        } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
1992                add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
1993                return argcount;
1994        } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
1995                add_message_grep(revs, optarg);
1996                return argcount;
1997        } else if (!strcmp(arg, "--grep-debug")) {
1998                revs->grep_filter.debug = 1;
1999        } else if (!strcmp(arg, "--basic-regexp")) {
2000                revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
2001        } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
2002                revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
2003        } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
2004                revs->grep_filter.regflags |= REG_ICASE;
2005                DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
2006        } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
2007                revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
2008        } else if (!strcmp(arg, "--perl-regexp")) {
2009                revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
2010        } else if (!strcmp(arg, "--all-match")) {
2011                revs->grep_filter.all_match = 1;
2012        } else if (!strcmp(arg, "--invert-grep")) {
2013                revs->invert_grep = 1;
2014        } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
2015                if (strcmp(optarg, "none"))
2016                        git_log_output_encoding = xstrdup(optarg);
2017                else
2018                        git_log_output_encoding = "";
2019                return argcount;
2020        } else if (!strcmp(arg, "--reverse")) {
2021                revs->reverse ^= 1;
2022        } else if (!strcmp(arg, "--children")) {
2023                revs->children.name = "children";
2024                revs->limited = 1;
2025        } else if (!strcmp(arg, "--ignore-missing")) {
2026                revs->ignore_missing = 1;
2027        } else {
2028                int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
2029                if (!opts)
2030                        unkv[(*unkc)++] = arg;
2031                return opts;
2032        }
2033        if (revs->graph && revs->track_linear)
2034                die("--show-linear-break and --graph are incompatible");
2035
2036        return 1;
2037}
2038
2039void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
2040                        const struct option *options,
2041                        const char * const usagestr[])
2042{
2043        int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
2044                                    &ctx->cpidx, ctx->out);
2045        if (n <= 0) {
2046                error("unknown option `%s'", ctx->argv[0]);
2047                usage_with_options(usagestr, options);
2048        }
2049        ctx->argv += n;
2050        ctx->argc -= n;
2051}
2052
2053static int for_each_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data, const char *term) {
2054        struct strbuf bisect_refs = STRBUF_INIT;
2055        int status;
2056        strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
2057        status = for_each_ref_in_submodule(submodule, bisect_refs.buf, fn, cb_data);
2058        strbuf_release(&bisect_refs);
2059        return status;
2060}
2061
2062static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2063{
2064        return for_each_bisect_ref(submodule, fn, cb_data, term_bad);
2065}
2066
2067static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
2068{
2069        return for_each_bisect_ref(submodule, fn, cb_data, term_good);
2070}
2071
2072static int handle_revision_pseudo_opt(const char *submodule,
2073                                struct rev_info *revs,
2074                                int argc, const char **argv, int *flags)
2075{
2076        const char *arg = argv[0];
2077        const char *optarg;
2078        int argcount;
2079
2080        /*
2081         * NOTE!
2082         *
2083         * Commands like "git shortlog" will not accept the options below
2084         * unless parse_revision_opt queues them (as opposed to erroring
2085         * out).
2086         *
2087         * When implementing your new pseudo-option, remember to
2088         * register it in the list at the top of handle_revision_opt.
2089         */
2090        if (!strcmp(arg, "--all")) {
2091                handle_refs(submodule, revs, *flags, for_each_ref_submodule);
2092                handle_refs(submodule, revs, *flags, head_ref_submodule);
2093                clear_ref_exclusion(&revs->ref_excludes);
2094        } else if (!strcmp(arg, "--branches")) {
2095                handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
2096                clear_ref_exclusion(&revs->ref_excludes);
2097        } else if (!strcmp(arg, "--bisect")) {
2098                read_bisect_terms(&term_bad, &term_good);
2099                handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
2100                handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
2101                revs->bisect = 1;
2102        } else if (!strcmp(arg, "--tags")) {
2103                handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
2104                clear_ref_exclusion(&revs->ref_excludes);
2105        } else if (!strcmp(arg, "--remotes")) {
2106                handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
2107                clear_ref_exclusion(&revs->ref_excludes);
2108        } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2109                struct all_refs_cb cb;
2110                init_all_refs_cb(&cb, revs, *flags);
2111                for_each_glob_ref(handle_one_ref, optarg, &cb);
2112                clear_ref_exclusion(&revs->ref_excludes);
2113                return argcount;
2114        } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2115                add_ref_exclusion(&revs->ref_excludes, optarg);
2116                return argcount;
2117        } else if (starts_with(arg, "--branches=")) {
2118                struct all_refs_cb cb;
2119                init_all_refs_cb(&cb, revs, *flags);
2120                for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
2121                clear_ref_exclusion(&revs->ref_excludes);
2122        } else if (starts_with(arg, "--tags=")) {
2123                struct all_refs_cb cb;
2124                init_all_refs_cb(&cb, revs, *flags);
2125                for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
2126                clear_ref_exclusion(&revs->ref_excludes);
2127        } else if (starts_with(arg, "--remotes=")) {
2128                struct all_refs_cb cb;
2129                init_all_refs_cb(&cb, revs, *flags);
2130                for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
2131                clear_ref_exclusion(&revs->ref_excludes);
2132        } else if (!strcmp(arg, "--reflog")) {
2133                add_reflogs_to_pending(revs, *flags);
2134        } else if (!strcmp(arg, "--indexed-objects")) {
2135                add_index_objects_to_pending(revs, *flags);
2136        } else if (!strcmp(arg, "--not")) {
2137                *flags ^= UNINTERESTING | BOTTOM;
2138        } else if (!strcmp(arg, "--no-walk")) {
2139                revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2140        } else if (starts_with(arg, "--no-walk=")) {
2141                /*
2142                 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
2143                 * not allowed, since the argument is optional.
2144                 */
2145                if (!strcmp(arg + 10, "sorted"))
2146                        revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
2147                else if (!strcmp(arg + 10, "unsorted"))
2148                        revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
2149                else
2150                        return error("invalid argument to --no-walk");
2151        } else if (!strcmp(arg, "--do-walk")) {
2152                revs->no_walk = 0;
2153        } else {
2154                return 0;
2155        }
2156
2157        return 1;
2158}
2159
2160static void NORETURN diagnose_missing_default(const char *def)
2161{
2162        unsigned char sha1[20];
2163        int flags;
2164        const char *refname;
2165
2166        refname = resolve_ref_unsafe(def, 0, sha1, &flags);
2167        if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2168                die(_("your current branch appears to be broken"));
2169
2170        skip_prefix(refname, "refs/heads/", &refname);
2171        die(_("your current branch '%s' does not have any commits yet"),
2172            refname);
2173}
2174
2175/*
2176 * Parse revision information, filling in the "rev_info" structure,
2177 * and removing the used arguments from the argument list.
2178 *
2179 * Returns the number of arguments left that weren't recognized
2180 * (which are also moved to the head of the argument list)
2181 */
2182int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
2183{
2184        int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
2185        struct cmdline_pathspec prune_data;
2186        const char *submodule = NULL;
2187
2188        memset(&prune_data, 0, sizeof(prune_data));
2189        if (opt)
2190                submodule = opt->submodule;
2191
2192        /* First, search for "--" */
2193        if (opt && opt->assume_dashdash) {
2194                seen_dashdash = 1;
2195        } else {
2196                seen_dashdash = 0;
2197                for (i = 1; i < argc; i++) {
2198                        const char *arg = argv[i];
2199                        if (strcmp(arg, "--"))
2200                                continue;
2201                        argv[i] = NULL;
2202                        argc = i;
2203                        if (argv[i + 1])
2204                                append_prune_data(&prune_data, argv + i + 1);
2205                        seen_dashdash = 1;
2206                        break;
2207                }
2208        }
2209
2210        /* Second, deal with arguments and options */
2211        flags = 0;
2212        revarg_opt = opt ? opt->revarg_opt : 0;
2213        if (seen_dashdash)
2214                revarg_opt |= REVARG_CANNOT_BE_FILENAME;
2215        read_from_stdin = 0;
2216        for (left = i = 1; i < argc; i++) {
2217                const char *arg = argv[i];
2218                if (*arg == '-') {
2219                        int opts;
2220
2221                        opts = handle_revision_pseudo_opt(submodule,
2222                                                revs, argc - i, argv + i,
2223                                                &flags);
2224                        if (opts > 0) {
2225                                i += opts - 1;
2226                                continue;
2227                        }
2228
2229                        if (!strcmp(arg, "--stdin")) {
2230                                if (revs->disable_stdin) {
2231                                        argv[left++] = arg;
2232                                        continue;
2233                                }
2234                                if (read_from_stdin++)
2235                                        die("--stdin given twice?");
2236                                read_revisions_from_stdin(revs, &prune_data);
2237                                continue;
2238                        }
2239
2240                        opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
2241                        if (opts > 0) {
2242                                i += opts - 1;
2243                                continue;
2244                        }
2245                        if (opts < 0)
2246                                exit(128);
2247                        continue;
2248                }
2249
2250
2251                if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
2252                        int j;
2253                        if (seen_dashdash || *arg == '^')
2254                                die("bad revision '%s'", arg);
2255
2256                        /* If we didn't have a "--":
2257                         * (1) all filenames must exist;
2258                         * (2) all rev-args must not be interpretable
2259                         *     as a valid filename.
2260                         * but the latter we have checked in the main loop.
2261                         */
2262                        for (j = i; j < argc; j++)
2263                                verify_filename(revs->prefix, argv[j], j == i);
2264
2265                        append_prune_data(&prune_data, argv + i);
2266                        break;
2267                }
2268                else
2269                        got_rev_arg = 1;
2270        }
2271
2272        if (prune_data.nr) {
2273                /*
2274                 * If we need to introduce the magic "a lone ':' means no
2275                 * pathspec whatsoever", here is the place to do so.
2276                 *
2277                 * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
2278                 *      prune_data.nr = 0;
2279                 *      prune_data.alloc = 0;
2280                 *      free(prune_data.path);
2281                 *      prune_data.path = NULL;
2282                 * } else {
2283                 *      terminate prune_data.alloc with NULL and
2284                 *      call init_pathspec() to set revs->prune_data here.
2285                 * }
2286                 */
2287                ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc);
2288                prune_data.path[prune_data.nr++] = NULL;
2289                parse_pathspec(&revs->prune_data, 0, 0,
2290                               revs->prefix, prune_data.path);
2291        }
2292
2293        if (revs->def == NULL)
2294                revs->def = opt ? opt->def : NULL;
2295        if (opt && opt->tweak)
2296                opt->tweak(revs, opt);
2297        if (revs->show_merge)
2298                prepare_show_merge(revs);
2299        if (revs->def && !revs->pending.nr && !got_rev_arg) {
2300                unsigned char sha1[20];
2301                struct object *object;
2302                struct object_context oc;
2303                if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2304                        diagnose_missing_default(revs->def);
2305                object = get_reference(revs, revs->def, sha1, 0);
2306                add_pending_object_with_mode(revs, object, revs->def, oc.mode);
2307        }
2308
2309        /* Did the user ask for any diff output? Run the diff! */
2310        if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
2311                revs->diff = 1;
2312
2313        /* Pickaxe, diff-filter and rename following need diffs */
2314        if (revs->diffopt.pickaxe ||
2315            revs->diffopt.filter ||
2316            DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2317                revs->diff = 1;
2318
2319        if (revs->topo_order)
2320                revs->limited = 1;
2321
2322        if (revs->prune_data.nr) {
2323                copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
2324                /* Can't prune commits with rename following: the paths change.. */
2325                if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
2326                        revs->prune = 1;
2327                if (!revs->full_diff)
2328                        copy_pathspec(&revs->diffopt.pathspec,
2329                                      &revs->prune_data);
2330        }
2331        if (revs->combine_merges)
2332                revs->ignore_merges = 0;
2333        revs->diffopt.abbrev = revs->abbrev;
2334
2335        if (revs->line_level_traverse) {
2336                revs->limited = 1;
2337                revs->topo_order = 1;
2338        }
2339
2340        diff_setup_done(&revs->diffopt);
2341
2342        grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
2343                                 &revs->grep_filter);
2344        compile_grep_patterns(&revs->grep_filter);
2345
2346        if (revs->reverse && revs->reflog_info)
2347                die("cannot combine --reverse with --walk-reflogs");
2348        if (revs->rewrite_parents && revs->children.name)
2349                die("cannot combine --parents and --children");
2350
2351        /*
2352         * Limitations on the graph functionality
2353         */
2354        if (revs->reverse && revs->graph)
2355                die("cannot combine --reverse with --graph");
2356
2357        if (revs->reflog_info && revs->graph)
2358                die("cannot combine --walk-reflogs with --graph");
2359        if (revs->no_walk && revs->graph)
2360                die("cannot combine --no-walk with --graph");
2361        if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
2362                die("cannot use --grep-reflog without --walk-reflogs");
2363
2364        if (revs->first_parent_only && revs->bisect)
2365                die(_("--first-parent is incompatible with --bisect"));
2366
2367        if (revs->expand_tabs_in_log < 0)
2368                revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
2369
2370        return left;
2371}
2372
2373static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
2374{
2375        struct commit_list *l = xcalloc(1, sizeof(*l));
2376
2377        l->item = child;
2378        l->next = add_decoration(&revs->children, &parent->object, l);
2379}
2380
2381static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
2382{
2383        struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2384        struct commit_list **pp, *p;
2385        int surviving_parents;
2386
2387        /* Examine existing parents while marking ones we have seen... */
2388        pp = &commit->parents;
2389        surviving_parents = 0;
2390        while ((p = *pp) != NULL) {
2391                struct commit *parent = p->item;
2392                if (parent->object.flags & TMP_MARK) {
2393                        *pp = p->next;
2394                        if (ts)
2395                                compact_treesame(revs, commit, surviving_parents);
2396                        continue;
2397                }
2398                parent->object.flags |= TMP_MARK;
2399                surviving_parents++;
2400                pp = &p->next;
2401        }
2402        /* clear the temporary mark */
2403        for (p = commit->parents; p; p = p->next) {
2404                p->item->object.flags &= ~TMP_MARK;
2405        }
2406        /* no update_treesame() - removing duplicates can't affect TREESAME */
2407        return surviving_parents;
2408}
2409
2410struct merge_simplify_state {
2411        struct commit *simplified;
2412};
2413
2414static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
2415{
2416        struct merge_simplify_state *st;
2417
2418        st = lookup_decoration(&revs->merge_simplification, &commit->object);
2419        if (!st) {
2420                st = xcalloc(1, sizeof(*st));
2421                add_decoration(&revs->merge_simplification, &commit->object, st);
2422        }
2423        return st;
2424}
2425
2426static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
2427{
2428        struct commit_list *h = reduce_heads(commit->parents);
2429        int i = 0, marked = 0;
2430        struct commit_list *po, *pn;
2431
2432        /* Want these for sanity-checking only */
2433        int orig_cnt = commit_list_count(commit->parents);
2434        int cnt = commit_list_count(h);
2435
2436        /*
2437         * Not ready to remove items yet, just mark them for now, based
2438         * on the output of reduce_heads(). reduce_heads outputs the reduced
2439         * set in its original order, so this isn't too hard.
2440         */
2441        po = commit->parents;
2442        pn = h;
2443        while (po) {
2444                if (pn && po->item == pn->item) {
2445                        pn = pn->next;
2446                        i++;
2447                } else {
2448                        po->item->object.flags |= TMP_MARK;
2449                        marked++;
2450                }
2451                po=po->next;
2452        }
2453
2454        if (i != cnt || cnt+marked != orig_cnt)
2455                die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
2456
2457        free_commit_list(h);
2458
2459        return marked;
2460}
2461
2462static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
2463{
2464        struct commit_list *p;
2465        int marked = 0;
2466
2467        for (p = commit->parents; p; p = p->next) {
2468                struct commit *parent = p->item;
2469                if (!parent->parents && (parent->object.flags & TREESAME)) {
2470                        parent->object.flags |= TMP_MARK;
2471                        marked++;
2472                }
2473        }
2474
2475        return marked;
2476}
2477
2478/*
2479 * Awkward naming - this means one parent we are TREESAME to.
2480 * cf mark_treesame_root_parents: root parents that are TREESAME (to an
2481 * empty tree). Better name suggestions?
2482 */
2483static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
2484{
2485        struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
2486        struct commit *unmarked = NULL, *marked = NULL;
2487        struct commit_list *p;
2488        unsigned n;
2489
2490        for (p = commit->parents, n = 0; p; p = p->next, n++) {
2491                if (ts->treesame[n]) {
2492                        if (p->item->object.flags & TMP_MARK) {
2493                                if (!marked)
2494                                        marked = p->item;
2495                        } else {
2496                                if (!unmarked) {
2497                                        unmarked = p->item;
2498                                        break;
2499                                }
2500                        }
2501                }
2502        }
2503
2504        /*
2505         * If we are TREESAME to a marked-for-deletion parent, but not to any
2506         * unmarked parents, unmark the first TREESAME parent. This is the
2507         * parent that the default simplify_history==1 scan would have followed,
2508         * and it doesn't make sense to omit that path when asking for a
2509         * simplified full history. Retaining it improves the chances of
2510         * understanding odd missed merges that took an old version of a file.
2511         *
2512         * Example:
2513         *
2514         *   I--------*X       A modified the file, but mainline merge X used
2515         *    \       /        "-s ours", so took the version from I. X is
2516         *     `-*A--'         TREESAME to I and !TREESAME to A.
2517         *
2518         * Default log from X would produce "I". Without this check,
2519         * --full-history --simplify-merges would produce "I-A-X", showing
2520         * the merge commit X and that it changed A, but not making clear that
2521         * it had just taken the I version. With this check, the topology above
2522         * is retained.
2523         *
2524         * Note that it is possible that the simplification chooses a different
2525         * TREESAME parent from the default, in which case this test doesn't
2526         * activate, and we _do_ drop the default parent. Example:
2527         *
2528         *   I------X         A modified the file, but it was reverted in B,
2529         *    \    /          meaning mainline merge X is TREESAME to both
2530         *    *A-*B           parents.
2531         *
2532         * Default log would produce "I" by following the first parent;
2533         * --full-history --simplify-merges will produce "I-A-B". But this is a
2534         * reasonable result - it presents a logical full history leading from
2535         * I to X, and X is not an important merge.
2536         */
2537        if (!unmarked && marked) {
2538                marked->object.flags &= ~TMP_MARK;
2539                return 1;
2540        }
2541
2542        return 0;
2543}
2544
2545static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
2546{
2547        struct commit_list **pp, *p;
2548        int nth_parent, removed = 0;
2549
2550        pp = &commit->parents;
2551        nth_parent = 0;
2552        while ((p = *pp) != NULL) {
2553                struct commit *parent = p->item;
2554                if (parent->object.flags & TMP_MARK) {
2555                        parent->object.flags &= ~TMP_MARK;
2556                        *pp = p->next;
2557                        free(p);
2558                        removed++;
2559                        compact_treesame(revs, commit, nth_parent);
2560                        continue;
2561                }
2562                pp = &p->next;
2563                nth_parent++;
2564        }
2565
2566        /* Removing parents can only increase TREESAMEness */
2567        if (removed && !(commit->object.flags & TREESAME))
2568                update_treesame(revs, commit);
2569
2570        return nth_parent;
2571}
2572
2573static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
2574{
2575        struct commit_list *p;
2576        struct commit *parent;
2577        struct merge_simplify_state *st, *pst;
2578        int cnt;
2579
2580        st = locate_simplify_state(revs, commit);
2581
2582        /*
2583         * Have we handled this one?
2584         */
2585        if (st->simplified)
2586                return tail;
2587
2588        /*
2589         * An UNINTERESTING commit simplifies to itself, so does a
2590         * root commit.  We do not rewrite parents of such commit
2591         * anyway.
2592         */
2593        if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
2594                st->simplified = commit;
2595                return tail;
2596        }
2597
2598        /*
2599         * Do we know what commit all of our parents that matter
2600         * should be rewritten to?  Otherwise we are not ready to
2601         * rewrite this one yet.
2602         */
2603        for (cnt = 0, p = commit->parents; p; p = p->next) {
2604                pst = locate_simplify_state(revs, p->item);
2605                if (!pst->simplified) {
2606                        tail = &commit_list_insert(p->item, tail)->next;
2607                        cnt++;
2608                }
2609                if (revs->first_parent_only)
2610                        break;
2611        }
2612        if (cnt) {
2613                tail = &commit_list_insert(commit, tail)->next;
2614                return tail;
2615        }
2616
2617        /*
2618         * Rewrite our list of parents. Note that this cannot
2619         * affect our TREESAME flags in any way - a commit is
2620         * always TREESAME to its simplification.
2621         */
2622        for (p = commit->parents; p; p = p->next) {
2623                pst = locate_simplify_state(revs, p->item);
2624                p->item = pst->simplified;
2625                if (revs->first_parent_only)
2626                        break;
2627        }
2628
2629        if (revs->first_parent_only)
2630                cnt = 1;
2631        else
2632                cnt = remove_duplicate_parents(revs, commit);
2633
2634        /*
2635         * It is possible that we are a merge and one side branch
2636         * does not have any commit that touches the given paths;
2637         * in such a case, the immediate parent from that branch
2638         * will be rewritten to be the merge base.
2639         *
2640         *      o----X          X: the commit we are looking at;
2641         *     /    /           o: a commit that touches the paths;
2642         * ---o----'
2643         *
2644         * Further, a merge of an independent branch that doesn't
2645         * touch the path will reduce to a treesame root parent:
2646         *
2647         *  ----o----X          X: the commit we are looking at;
2648         *          /           o: a commit that touches the paths;
2649         *         r            r: a root commit not touching the paths
2650         *
2651         * Detect and simplify both cases.
2652         */
2653        if (1 < cnt) {
2654                int marked = mark_redundant_parents(revs, commit);
2655                marked += mark_treesame_root_parents(revs, commit);
2656                if (marked)
2657                        marked -= leave_one_treesame_to_parent(revs, commit);
2658                if (marked)
2659                        cnt = remove_marked_parents(revs, commit);
2660        }
2661
2662        /*
2663         * A commit simplifies to itself if it is a root, if it is
2664         * UNINTERESTING, if it touches the given paths, or if it is a
2665         * merge and its parents don't simplify to one relevant commit
2666         * (the first two cases are already handled at the beginning of
2667         * this function).
2668         *
2669         * Otherwise, it simplifies to what its sole relevant parent
2670         * simplifies to.
2671         */
2672        if (!cnt ||
2673            (commit->object.flags & UNINTERESTING) ||
2674            !(commit->object.flags & TREESAME) ||
2675            (parent = one_relevant_parent(revs, commit->parents)) == NULL)
2676                st->simplified = commit;
2677        else {
2678                pst = locate_simplify_state(revs, parent);
2679                st->simplified = pst->simplified;
2680        }
2681        return tail;
2682}
2683
2684static void simplify_merges(struct rev_info *revs)
2685{
2686        struct commit_list *list, *next;
2687        struct commit_list *yet_to_do, **tail;
2688        struct commit *commit;
2689
2690        if (!revs->prune)
2691                return;
2692
2693        /* feed the list reversed */
2694        yet_to_do = NULL;
2695        for (list = revs->commits; list; list = next) {
2696                commit = list->item;
2697                next = list->next;
2698                /*
2699                 * Do not free(list) here yet; the original list
2700                 * is used later in this function.
2701                 */
2702                commit_list_insert(commit, &yet_to_do);
2703        }
2704        while (yet_to_do) {
2705                list = yet_to_do;
2706                yet_to_do = NULL;
2707                tail = &yet_to_do;
2708                while (list) {
2709                        commit = pop_commit(&list);
2710                        tail = simplify_one(revs, commit, tail);
2711                }
2712        }
2713
2714        /* clean up the result, removing the simplified ones */
2715        list = revs->commits;
2716        revs->commits = NULL;
2717        tail = &revs->commits;
2718        while (list) {
2719                struct merge_simplify_state *st;
2720
2721                commit = pop_commit(&list);
2722                st = locate_simplify_state(revs, commit);
2723                if (st->simplified == commit)
2724                        tail = &commit_list_insert(commit, tail)->next;
2725        }
2726}
2727
2728static void set_children(struct rev_info *revs)
2729{
2730        struct commit_list *l;
2731        for (l = revs->commits; l; l = l->next) {
2732                struct commit *commit = l->item;
2733                struct commit_list *p;
2734
2735                for (p = commit->parents; p; p = p->next)
2736                        add_child(revs, p->item, commit);
2737        }
2738}
2739
2740void reset_revision_walk(void)
2741{
2742        clear_object_flags(SEEN | ADDED | SHOWN);
2743}
2744
2745int prepare_revision_walk(struct rev_info *revs)
2746{
2747        int i;
2748        struct object_array old_pending;
2749        struct commit_list **next = &revs->commits;
2750
2751        memcpy(&old_pending, &revs->pending, sizeof(old_pending));
2752        revs->pending.nr = 0;
2753        revs->pending.alloc = 0;
2754        revs->pending.objects = NULL;
2755        for (i = 0; i < old_pending.nr; i++) {
2756                struct object_array_entry *e = old_pending.objects + i;
2757                struct commit *commit = handle_commit(revs, e);
2758                if (commit) {
2759                        if (!(commit->object.flags & SEEN)) {
2760                                commit->object.flags |= SEEN;
2761                                next = commit_list_append(commit, next);
2762                        }
2763                }
2764        }
2765        if (!revs->leak_pending)
2766                object_array_clear(&old_pending);
2767
2768        /* Signal whether we need per-parent treesame decoration */
2769        if (revs->simplify_merges ||
2770            (revs->limited && limiting_can_increase_treesame(revs)))
2771                revs->treesame.name = "treesame";
2772
2773        if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
2774                commit_list_sort_by_date(&revs->commits);
2775        if (revs->no_walk)
2776                return 0;
2777        if (revs->limited)
2778                if (limit_list(revs) < 0)
2779                        return -1;
2780        if (revs->topo_order)
2781                sort_in_topological_order(&revs->commits, revs->sort_order);
2782        if (revs->line_level_traverse)
2783                line_log_filter(revs);
2784        if (revs->simplify_merges)
2785                simplify_merges(revs);
2786        if (revs->children.name)
2787                set_children(revs);
2788        return 0;
2789}
2790
2791static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
2792{
2793        struct commit_list *cache = NULL;
2794
2795        for (;;) {
2796                struct commit *p = *pp;
2797                if (!revs->limited)
2798                        if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
2799                                return rewrite_one_error;
2800                if (p->object.flags & UNINTERESTING)
2801                        return rewrite_one_ok;
2802                if (!(p->object.flags & TREESAME))
2803                        return rewrite_one_ok;
2804                if (!p->parents)
2805                        return rewrite_one_noparents;
2806                if ((p = one_relevant_parent(revs, p->parents)) == NULL)
2807                        return rewrite_one_ok;
2808                *pp = p;
2809        }
2810}
2811
2812int rewrite_parents(struct rev_info *revs, struct commit *commit,
2813        rewrite_parent_fn_t rewrite_parent)
2814{
2815        struct commit_list **pp = &commit->parents;
2816        while (*pp) {
2817                struct commit_list *parent = *pp;
2818                switch (rewrite_parent(revs, &parent->item)) {
2819                case rewrite_one_ok:
2820                        break;
2821                case rewrite_one_noparents:
2822                        *pp = parent->next;
2823                        continue;
2824                case rewrite_one_error:
2825                        return -1;
2826                }
2827                pp = &parent->next;
2828        }
2829        remove_duplicate_parents(revs, commit);
2830        return 0;
2831}
2832
2833static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2834{
2835        char *person, *endp;
2836        size_t len, namelen, maillen;
2837        const char *name;
2838        const char *mail;
2839        struct ident_split ident;
2840
2841        person = strstr(buf->buf, what);
2842        if (!person)
2843                return 0;
2844
2845        person += strlen(what);
2846        endp = strchr(person, '\n');
2847        if (!endp)
2848                return 0;
2849
2850        len = endp - person;
2851
2852        if (split_ident_line(&ident, person, len))
2853                return 0;
2854
2855        mail = ident.mail_begin;
2856        maillen = ident.mail_end - ident.mail_begin;
2857        name = ident.name_begin;
2858        namelen = ident.name_end - ident.name_begin;
2859
2860        if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2861                struct strbuf namemail = STRBUF_INIT;
2862
2863                strbuf_addf(&namemail, "%.*s <%.*s>",
2864                            (int)namelen, name, (int)maillen, mail);
2865
2866                strbuf_splice(buf, ident.name_begin - buf->buf,
2867                              ident.mail_end - ident.name_begin + 1,
2868                              namemail.buf, namemail.len);
2869
2870                strbuf_release(&namemail);
2871
2872                return 1;
2873        }
2874
2875        return 0;
2876}
2877
2878static int commit_match(struct commit *commit, struct rev_info *opt)
2879{
2880        int retval;
2881        const char *encoding;
2882        const char *message;
2883        struct strbuf buf = STRBUF_INIT;
2884
2885        if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
2886                return 1;
2887
2888        /* Prepend "fake" headers as needed */
2889        if (opt->grep_filter.use_reflog_filter) {
2890                strbuf_addstr(&buf, "reflog ");
2891                get_reflog_message(&buf, opt->reflog_info);
2892                strbuf_addch(&buf, '\n');
2893        }
2894
2895        /*
2896         * We grep in the user's output encoding, under the assumption that it
2897         * is the encoding they are most likely to write their grep pattern
2898         * for. In addition, it means we will match the "notes" encoding below,
2899         * so we will not end up with a buffer that has two different encodings
2900         * in it.
2901         */
2902        encoding = get_log_output_encoding();
2903        message = logmsg_reencode(commit, NULL, encoding);
2904
2905        /* Copy the commit to temporary if we are using "fake" headers */
2906        if (buf.len)
2907                strbuf_addstr(&buf, message);
2908
2909        if (opt->grep_filter.header_list && opt->mailmap) {
2910                if (!buf.len)
2911                        strbuf_addstr(&buf, message);
2912
2913                commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2914                commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2915        }
2916
2917        /* Append "fake" message parts as needed */
2918        if (opt->show_notes) {
2919                if (!buf.len)
2920                        strbuf_addstr(&buf, message);
2921                format_display_notes(commit->object.oid.hash, &buf, encoding, 1);
2922        }
2923
2924        /*
2925         * Find either in the original commit message, or in the temporary.
2926         * Note that we cast away the constness of "message" here. It is
2927         * const because it may come from the cached commit buffer. That's OK,
2928         * because we know that it is modifiable heap memory, and that while
2929         * grep_buffer may modify it for speed, it will restore any
2930         * changes before returning.
2931         */
2932        if (buf.len)
2933                retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
2934        else
2935                retval = grep_buffer(&opt->grep_filter,
2936                                     (char *)message, strlen(message));
2937        strbuf_release(&buf);
2938        unuse_commit_buffer(commit, message);
2939        return opt->invert_grep ? !retval : retval;
2940}
2941
2942static inline int want_ancestry(const struct rev_info *revs)
2943{
2944        return (revs->rewrite_parents || revs->children.name);
2945}
2946
2947enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
2948{
2949        if (commit->object.flags & SHOWN)
2950                return commit_ignore;
2951        if (revs->unpacked && has_sha1_pack(commit->object.oid.hash))
2952                return commit_ignore;
2953        if (revs->show_all)
2954                return commit_show;
2955        if (commit->object.flags & UNINTERESTING)
2956                return commit_ignore;
2957        if (revs->min_age != -1 && (commit->date > revs->min_age))
2958                return commit_ignore;
2959        if (revs->min_parents || (revs->max_parents >= 0)) {
2960                int n = commit_list_count(commit->parents);
2961                if ((n < revs->min_parents) ||
2962                    ((revs->max_parents >= 0) && (n > revs->max_parents)))
2963                        return commit_ignore;
2964        }
2965        if (!commit_match(commit, revs))
2966                return commit_ignore;
2967        if (revs->prune && revs->dense) {
2968                /* Commit without changes? */
2969                if (commit->object.flags & TREESAME) {
2970                        int n;
2971                        struct commit_list *p;
2972                        /* drop merges unless we want parenthood */
2973                        if (!want_ancestry(revs))
2974                                return commit_ignore;
2975                        /*
2976                         * If we want ancestry, then need to keep any merges
2977                         * between relevant commits to tie together topology.
2978                         * For consistency with TREESAME and simplification
2979                         * use "relevant" here rather than just INTERESTING,
2980                         * to treat bottom commit(s) as part of the topology.
2981                         */
2982                        for (n = 0, p = commit->parents; p; p = p->next)
2983                                if (relevant_commit(p->item))
2984                                        if (++n >= 2)
2985                                                return commit_show;
2986                        return commit_ignore;
2987                }
2988        }
2989        return commit_show;
2990}
2991
2992define_commit_slab(saved_parents, struct commit_list *);
2993
2994#define EMPTY_PARENT_LIST ((struct commit_list *)-1)
2995
2996/*
2997 * You may only call save_parents() once per commit (this is checked
2998 * for non-root commits).
2999 */
3000static void save_parents(struct rev_info *revs, struct commit *commit)
3001{
3002        struct commit_list **pp;
3003
3004        if (!revs->saved_parents_slab) {
3005                revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
3006                init_saved_parents(revs->saved_parents_slab);
3007        }
3008
3009        pp = saved_parents_at(revs->saved_parents_slab, commit);
3010
3011        /*
3012         * When walking with reflogs, we may visit the same commit
3013         * several times: once for each appearance in the reflog.
3014         *
3015         * In this case, save_parents() will be called multiple times.
3016         * We want to keep only the first set of parents.  We need to
3017         * store a sentinel value for an empty (i.e., NULL) parent
3018         * list to distinguish it from a not-yet-saved list, however.
3019         */
3020        if (*pp)
3021                return;
3022        if (commit->parents)
3023                *pp = copy_commit_list(commit->parents);
3024        else
3025                *pp = EMPTY_PARENT_LIST;
3026}
3027
3028static void free_saved_parents(struct rev_info *revs)
3029{
3030        if (revs->saved_parents_slab)
3031                clear_saved_parents(revs->saved_parents_slab);
3032}
3033
3034struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
3035{
3036        struct commit_list *parents;
3037
3038        if (!revs->saved_parents_slab)
3039                return commit->parents;
3040
3041        parents = *saved_parents_at(revs->saved_parents_slab, commit);
3042        if (parents == EMPTY_PARENT_LIST)
3043                return NULL;
3044        return parents;
3045}
3046
3047enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
3048{
3049        enum commit_action action = get_commit_action(revs, commit);
3050
3051        if (action == commit_show &&
3052            !revs->show_all &&
3053            revs->prune && revs->dense && want_ancestry(revs)) {
3054                /*
3055                 * --full-diff on simplified parents is no good: it
3056                 * will show spurious changes from the commits that
3057                 * were elided.  So we save the parents on the side
3058                 * when --full-diff is in effect.
3059                 */
3060                if (revs->full_diff)
3061                        save_parents(revs, commit);
3062                if (rewrite_parents(revs, commit, rewrite_one) < 0)
3063                        return commit_error;
3064        }
3065        return action;
3066}
3067
3068static void track_linear(struct rev_info *revs, struct commit *commit)
3069{
3070        if (revs->track_first_time) {
3071                revs->linear = 1;
3072                revs->track_first_time = 0;
3073        } else {
3074                struct commit_list *p;
3075                for (p = revs->previous_parents; p; p = p->next)
3076                        if (p->item == NULL || /* first commit */
3077                            !oidcmp(&p->item->object.oid, &commit->object.oid))
3078                                break;
3079                revs->linear = p != NULL;
3080        }
3081        if (revs->reverse) {
3082                if (revs->linear)
3083                        commit->object.flags |= TRACK_LINEAR;
3084        }
3085        free_commit_list(revs->previous_parents);
3086        revs->previous_parents = copy_commit_list(commit->parents);
3087}
3088
3089static struct commit *get_revision_1(struct rev_info *revs)
3090{
3091        if (!revs->commits)
3092                return NULL;
3093
3094        do {
3095                struct commit *commit = pop_commit(&revs->commits);
3096
3097                if (revs->reflog_info) {
3098                        save_parents(revs, commit);
3099                        fake_reflog_parent(revs->reflog_info, commit);
3100                        commit->object.flags &= ~(ADDED | SEEN | SHOWN);
3101                }
3102
3103                /*
3104                 * If we haven't done the list limiting, we need to look at
3105                 * the parents here. We also need to do the date-based limiting
3106                 * that we'd otherwise have done in limit_list().
3107                 */
3108                if (!revs->limited) {
3109                        if (revs->max_age != -1 &&
3110                            (commit->date < revs->max_age))
3111                                continue;
3112                        if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
3113                                if (!revs->ignore_missing_links)
3114                                        die("Failed to traverse parents of commit %s",
3115                                                oid_to_hex(&commit->object.oid));
3116                        }
3117                }
3118
3119                switch (simplify_commit(revs, commit)) {
3120                case commit_ignore:
3121                        continue;
3122                case commit_error:
3123                        die("Failed to simplify parents of commit %s",
3124                            oid_to_hex(&commit->object.oid));
3125                default:
3126                        if (revs->track_linear)
3127                                track_linear(revs, commit);
3128                        return commit;
3129                }
3130        } while (revs->commits);
3131        return NULL;
3132}
3133
3134/*
3135 * Return true for entries that have not yet been shown.  (This is an
3136 * object_array_each_func_t.)
3137 */
3138static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
3139{
3140        return !(entry->item->flags & SHOWN);
3141}
3142
3143/*
3144 * If array is on the verge of a realloc, garbage-collect any entries
3145 * that have already been shown to try to free up some space.
3146 */
3147static void gc_boundary(struct object_array *array)
3148{
3149        if (array->nr == array->alloc)
3150                object_array_filter(array, entry_unshown, NULL);
3151}
3152
3153static void create_boundary_commit_list(struct rev_info *revs)
3154{
3155        unsigned i;
3156        struct commit *c;
3157        struct object_array *array = &revs->boundary_commits;
3158        struct object_array_entry *objects = array->objects;
3159
3160        /*
3161         * If revs->commits is non-NULL at this point, an error occurred in
3162         * get_revision_1().  Ignore the error and continue printing the
3163         * boundary commits anyway.  (This is what the code has always
3164         * done.)
3165         */
3166        if (revs->commits) {
3167                free_commit_list(revs->commits);
3168                revs->commits = NULL;
3169        }
3170
3171        /*
3172         * Put all of the actual boundary commits from revs->boundary_commits
3173         * into revs->commits
3174         */
3175        for (i = 0; i < array->nr; i++) {
3176                c = (struct commit *)(objects[i].item);
3177                if (!c)
3178                        continue;
3179                if (!(c->object.flags & CHILD_SHOWN))
3180                        continue;
3181                if (c->object.flags & (SHOWN | BOUNDARY))
3182                        continue;
3183                c->object.flags |= BOUNDARY;
3184                commit_list_insert(c, &revs->commits);
3185        }
3186
3187        /*
3188         * If revs->topo_order is set, sort the boundary commits
3189         * in topological order
3190         */
3191        sort_in_topological_order(&revs->commits, revs->sort_order);
3192}
3193
3194static struct commit *get_revision_internal(struct rev_info *revs)
3195{
3196        struct commit *c = NULL;
3197        struct commit_list *l;
3198
3199        if (revs->boundary == 2) {
3200                /*
3201                 * All of the normal commits have already been returned,
3202                 * and we are now returning boundary commits.
3203                 * create_boundary_commit_list() has populated
3204                 * revs->commits with the remaining commits to return.
3205                 */
3206                c = pop_commit(&revs->commits);
3207                if (c)
3208                        c->object.flags |= SHOWN;
3209                return c;
3210        }
3211
3212        /*
3213         * If our max_count counter has reached zero, then we are done. We
3214         * don't simply return NULL because we still might need to show
3215         * boundary commits. But we want to avoid calling get_revision_1, which
3216         * might do a considerable amount of work finding the next commit only
3217         * for us to throw it away.
3218         *
3219         * If it is non-zero, then either we don't have a max_count at all
3220         * (-1), or it is still counting, in which case we decrement.
3221         */
3222        if (revs->max_count) {
3223                c = get_revision_1(revs);
3224                if (c) {
3225                        while (revs->skip_count > 0) {
3226                                revs->skip_count--;
3227                                c = get_revision_1(revs);
3228                                if (!c)
3229                                        break;
3230                        }
3231                }
3232
3233                if (revs->max_count > 0)
3234                        revs->max_count--;
3235        }
3236
3237        if (c)
3238                c->object.flags |= SHOWN;
3239
3240        if (!revs->boundary)
3241                return c;
3242
3243        if (!c) {
3244                /*
3245                 * get_revision_1() runs out the commits, and
3246                 * we are done computing the boundaries.
3247                 * switch to boundary commits output mode.
3248                 */
3249                revs->boundary = 2;
3250
3251                /*
3252                 * Update revs->commits to contain the list of
3253                 * boundary commits.
3254                 */
3255                create_boundary_commit_list(revs);
3256
3257                return get_revision_internal(revs);
3258        }
3259
3260        /*
3261         * boundary commits are the commits that are parents of the
3262         * ones we got from get_revision_1() but they themselves are
3263         * not returned from get_revision_1().  Before returning
3264         * 'c', we need to mark its parents that they could be boundaries.
3265         */
3266
3267        for (l = c->parents; l; l = l->next) {
3268                struct object *p;
3269                p = &(l->item->object);
3270                if (p->flags & (CHILD_SHOWN | SHOWN))
3271                        continue;
3272                p->flags |= CHILD_SHOWN;
3273                gc_boundary(&revs->boundary_commits);
3274                add_object_array(p, NULL, &revs->boundary_commits);
3275        }
3276
3277        return c;
3278}
3279
3280struct commit *get_revision(struct rev_info *revs)
3281{
3282        struct commit *c;
3283        struct commit_list *reversed;
3284
3285        if (revs->reverse) {
3286                reversed = NULL;
3287                while ((c = get_revision_internal(revs)))
3288                        commit_list_insert(c, &reversed);
3289                revs->commits = reversed;
3290                revs->reverse = 0;
3291                revs->reverse_output_stage = 1;
3292        }
3293
3294        if (revs->reverse_output_stage) {
3295                c = pop_commit(&revs->commits);
3296                if (revs->track_linear)
3297                        revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
3298                return c;
3299        }
3300
3301        c = get_revision_internal(revs);
3302        if (c && revs->graph)
3303                graph_update(revs->graph, c);
3304        if (!c) {
3305                free_saved_parents(revs);
3306                if (revs->previous_parents) {
3307                        free_commit_list(revs->previous_parents);
3308                        revs->previous_parents = NULL;
3309                }
3310        }
3311        return c;
3312}
3313
3314char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
3315{
3316        if (commit->object.flags & BOUNDARY)
3317                return "-";
3318        else if (commit->object.flags & UNINTERESTING)
3319                return "^";
3320        else if (commit->object.flags & PATCHSAME)
3321                return "=";
3322        else if (!revs || revs->left_right) {
3323                if (commit->object.flags & SYMMETRIC_LEFT)
3324                        return "<";
3325                else
3326                        return ">";
3327        } else if (revs->graph)
3328                return "*";
3329        else if (revs->cherry_mark)
3330                return "+";
3331        return "";
3332}
3333
3334void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
3335{
3336        char *mark = get_revision_mark(revs, commit);
3337        if (!strlen(mark))
3338                return;
3339        fputs(mark, stdout);
3340        putchar(' ');
3341}