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