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