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