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