revision.con commit cvsserver: Introduce new state variable 'method' (80573ba)
   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 "grep.h"
  10#include "reflog-walk.h"
  11
  12static char *path_name(struct name_path *path, const char *name)
  13{
  14        struct name_path *p;
  15        char *n, *m;
  16        int nlen = strlen(name);
  17        int len = nlen + 1;
  18
  19        for (p = path; p; p = p->up) {
  20                if (p->elem_len)
  21                        len += p->elem_len + 1;
  22        }
  23        n = xmalloc(len);
  24        m = n + len - (nlen + 1);
  25        strcpy(m, name);
  26        for (p = path; p; p = p->up) {
  27                if (p->elem_len) {
  28                        m -= p->elem_len + 1;
  29                        memcpy(m, p->elem, p->elem_len);
  30                        m[p->elem_len] = '/';
  31                }
  32        }
  33        return n;
  34}
  35
  36void add_object(struct object *obj,
  37                struct object_array *p,
  38                struct name_path *path,
  39                const char *name)
  40{
  41        add_object_array(obj, path_name(path, name), p);
  42}
  43
  44static void mark_blob_uninteresting(struct blob *blob)
  45{
  46        if (blob->object.flags & UNINTERESTING)
  47                return;
  48        blob->object.flags |= UNINTERESTING;
  49}
  50
  51void mark_tree_uninteresting(struct tree *tree)
  52{
  53        struct tree_desc desc;
  54        struct name_entry entry;
  55        struct object *obj = &tree->object;
  56
  57        if (obj->flags & UNINTERESTING)
  58                return;
  59        obj->flags |= UNINTERESTING;
  60        if (!has_sha1_file(obj->sha1))
  61                return;
  62        if (parse_tree(tree) < 0)
  63                die("bad tree %s", sha1_to_hex(obj->sha1));
  64
  65        init_tree_desc(&desc, tree->buffer, tree->size);
  66        while (tree_entry(&desc, &entry)) {
  67                if (S_ISDIR(entry.mode))
  68                        mark_tree_uninteresting(lookup_tree(entry.sha1));
  69                else
  70                        mark_blob_uninteresting(lookup_blob(entry.sha1));
  71        }
  72
  73        /*
  74         * We don't care about the tree any more
  75         * after it has been marked uninteresting.
  76         */
  77        free(tree->buffer);
  78        tree->buffer = NULL;
  79}
  80
  81void mark_parents_uninteresting(struct commit *commit)
  82{
  83        struct commit_list *parents = commit->parents;
  84
  85        while (parents) {
  86                struct commit *commit = parents->item;
  87                if (!(commit->object.flags & UNINTERESTING)) {
  88                        commit->object.flags |= UNINTERESTING;
  89
  90                        /*
  91                         * Normally we haven't parsed the parent
  92                         * yet, so we won't have a parent of a parent
  93                         * here. However, it may turn out that we've
  94                         * reached this commit some other way (where it
  95                         * wasn't uninteresting), in which case we need
  96                         * to mark its parents recursively too..
  97                         */
  98                        if (commit->parents)
  99                                mark_parents_uninteresting(commit);
 100                }
 101
 102                /*
 103                 * A missing commit is ok iff its parent is marked
 104                 * uninteresting.
 105                 *
 106                 * We just mark such a thing parsed, so that when
 107                 * it is popped next time around, we won't be trying
 108                 * to parse it and get an error.
 109                 */
 110                if (!has_sha1_file(commit->object.sha1))
 111                        commit->object.parsed = 1;
 112                parents = parents->next;
 113        }
 114}
 115
 116void add_pending_object(struct rev_info *revs, struct object *obj, const char *name)
 117{
 118        if (revs->no_walk && (obj->flags & UNINTERESTING))
 119                die("object ranges do not make sense when not walking revisions");
 120        add_object_array(obj, name, &revs->pending);
 121        if (revs->reflog_info && obj->type == OBJ_COMMIT)
 122                add_reflog_for_walk(revs->reflog_info,
 123                                (struct commit *)obj, name);
 124}
 125
 126static struct object *get_reference(struct rev_info *revs, const char *name, const unsigned char *sha1, unsigned int flags)
 127{
 128        struct object *object;
 129
 130        object = parse_object(sha1);
 131        if (!object)
 132                die("bad object %s", name);
 133        object->flags |= flags;
 134        return object;
 135}
 136
 137static struct commit *handle_commit(struct rev_info *revs, struct object *object, const char *name)
 138{
 139        unsigned long flags = object->flags;
 140
 141        /*
 142         * Tag object? Look what it points to..
 143         */
 144        while (object->type == OBJ_TAG) {
 145                struct tag *tag = (struct tag *) object;
 146                if (revs->tag_objects && !(flags & UNINTERESTING))
 147                        add_pending_object(revs, object, tag->tag);
 148                object = parse_object(tag->tagged->sha1);
 149                if (!object)
 150                        die("bad object %s", sha1_to_hex(tag->tagged->sha1));
 151        }
 152
 153        /*
 154         * Commit object? Just return it, we'll do all the complex
 155         * reachability crud.
 156         */
 157        if (object->type == OBJ_COMMIT) {
 158                struct commit *commit = (struct commit *)object;
 159                if (parse_commit(commit) < 0)
 160                        die("unable to parse commit %s", name);
 161                if (flags & UNINTERESTING) {
 162                        commit->object.flags |= UNINTERESTING;
 163                        mark_parents_uninteresting(commit);
 164                        revs->limited = 1;
 165                }
 166                return commit;
 167        }
 168
 169        /*
 170         * Tree object? Either mark it uniniteresting, or add it
 171         * to the list of objects to look at later..
 172         */
 173        if (object->type == OBJ_TREE) {
 174                struct tree *tree = (struct tree *)object;
 175                if (!revs->tree_objects)
 176                        return NULL;
 177                if (flags & UNINTERESTING) {
 178                        mark_tree_uninteresting(tree);
 179                        return NULL;
 180                }
 181                add_pending_object(revs, object, "");
 182                return NULL;
 183        }
 184
 185        /*
 186         * Blob object? You know the drill by now..
 187         */
 188        if (object->type == OBJ_BLOB) {
 189                struct blob *blob = (struct blob *)object;
 190                if (!revs->blob_objects)
 191                        return NULL;
 192                if (flags & UNINTERESTING) {
 193                        mark_blob_uninteresting(blob);
 194                        return NULL;
 195                }
 196                add_pending_object(revs, object, "");
 197                return NULL;
 198        }
 199        die("%s is unknown object", name);
 200}
 201
 202static int everybody_uninteresting(struct commit_list *orig)
 203{
 204        struct commit_list *list = orig;
 205        while (list) {
 206                struct commit *commit = list->item;
 207                list = list->next;
 208                if (commit->object.flags & UNINTERESTING)
 209                        continue;
 210                return 0;
 211        }
 212        return 1;
 213}
 214
 215/*
 216 * The goal is to get REV_TREE_NEW as the result only if the
 217 * diff consists of all '+' (and no other changes), and
 218 * REV_TREE_DIFFERENT otherwise (of course if the trees are
 219 * the same we want REV_TREE_SAME).  That means that once we
 220 * get to REV_TREE_DIFFERENT, we do not have to look any further.
 221 */
 222static int tree_difference = REV_TREE_SAME;
 223
 224static void file_add_remove(struct diff_options *options,
 225                    int addremove, unsigned mode,
 226                    const unsigned char *sha1,
 227                    const char *base, const char *path)
 228{
 229        int diff = REV_TREE_DIFFERENT;
 230
 231        /*
 232         * Is it an add of a new file? It means that the old tree
 233         * didn't have it at all, so we will turn "REV_TREE_SAME" ->
 234         * "REV_TREE_NEW", but leave any "REV_TREE_DIFFERENT" alone
 235         * (and if it already was "REV_TREE_NEW", we'll keep it
 236         * "REV_TREE_NEW" of course).
 237         */
 238        if (addremove == '+') {
 239                diff = tree_difference;
 240                if (diff != REV_TREE_SAME)
 241                        return;
 242                diff = REV_TREE_NEW;
 243        }
 244        tree_difference = diff;
 245        if (tree_difference == REV_TREE_DIFFERENT)
 246                options->has_changes = 1;
 247}
 248
 249static void file_change(struct diff_options *options,
 250                 unsigned old_mode, unsigned new_mode,
 251                 const unsigned char *old_sha1,
 252                 const unsigned char *new_sha1,
 253                 const char *base, const char *path)
 254{
 255        tree_difference = REV_TREE_DIFFERENT;
 256        options->has_changes = 1;
 257}
 258
 259int rev_compare_tree(struct rev_info *revs, struct tree *t1, struct tree *t2)
 260{
 261        if (!t1)
 262                return REV_TREE_NEW;
 263        if (!t2)
 264                return REV_TREE_DIFFERENT;
 265        tree_difference = REV_TREE_SAME;
 266        revs->pruning.has_changes = 0;
 267        if (diff_tree_sha1(t1->object.sha1, t2->object.sha1, "",
 268                           &revs->pruning) < 0)
 269                return REV_TREE_DIFFERENT;
 270        return tree_difference;
 271}
 272
 273int rev_same_tree_as_empty(struct rev_info *revs, struct tree *t1)
 274{
 275        int retval;
 276        void *tree;
 277        unsigned long size;
 278        struct tree_desc empty, real;
 279
 280        if (!t1)
 281                return 0;
 282
 283        tree = read_object_with_reference(t1->object.sha1, tree_type, &size, NULL);
 284        if (!tree)
 285                return 0;
 286        init_tree_desc(&real, tree, size);
 287        init_tree_desc(&empty, "", 0);
 288
 289        tree_difference = REV_TREE_SAME;
 290        revs->pruning.has_changes = 0;
 291        retval = diff_tree(&empty, &real, "", &revs->pruning);
 292        free(tree);
 293
 294        return retval >= 0 && (tree_difference == REV_TREE_SAME);
 295}
 296
 297static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
 298{
 299        struct commit_list **pp, *parent;
 300        int tree_changed = 0, tree_same = 0;
 301
 302        if (!commit->tree)
 303                return;
 304
 305        if (!commit->parents) {
 306                if (!rev_same_tree_as_empty(revs, commit->tree))
 307                        commit->object.flags |= TREECHANGE;
 308                return;
 309        }
 310
 311        pp = &commit->parents;
 312        while ((parent = *pp) != NULL) {
 313                struct commit *p = parent->item;
 314
 315                parse_commit(p);
 316                switch (rev_compare_tree(revs, p->tree, commit->tree)) {
 317                case REV_TREE_SAME:
 318                        tree_same = 1;
 319                        if (!revs->simplify_history || (p->object.flags & UNINTERESTING)) {
 320                                /* Even if a merge with an uninteresting
 321                                 * side branch brought the entire change
 322                                 * we are interested in, we do not want
 323                                 * to lose the other branches of this
 324                                 * merge, so we just keep going.
 325                                 */
 326                                pp = &parent->next;
 327                                continue;
 328                        }
 329                        parent->next = NULL;
 330                        commit->parents = parent;
 331                        return;
 332
 333                case REV_TREE_NEW:
 334                        if (revs->remove_empty_trees &&
 335                            rev_same_tree_as_empty(revs, p->tree)) {
 336                                /* We are adding all the specified
 337                                 * paths from this parent, so the
 338                                 * history beyond this parent is not
 339                                 * interesting.  Remove its parents
 340                                 * (they are grandparents for us).
 341                                 * IOW, we pretend this parent is a
 342                                 * "root" commit.
 343                                 */
 344                                parse_commit(p);
 345                                p->parents = NULL;
 346                        }
 347                /* fallthrough */
 348                case REV_TREE_DIFFERENT:
 349                        tree_changed = 1;
 350                        pp = &parent->next;
 351                        continue;
 352                }
 353                die("bad tree compare for commit %s", sha1_to_hex(commit->object.sha1));
 354        }
 355        if (tree_changed && !tree_same)
 356                commit->object.flags |= TREECHANGE;
 357}
 358
 359static void add_parents_to_list(struct rev_info *revs, struct commit *commit, struct commit_list **list)
 360{
 361        struct commit_list *parent = commit->parents;
 362        unsigned left_flag;
 363
 364        if (commit->object.flags & ADDED)
 365                return;
 366        commit->object.flags |= ADDED;
 367
 368        /*
 369         * If the commit is uninteresting, don't try to
 370         * prune parents - we want the maximal uninteresting
 371         * set.
 372         *
 373         * Normally we haven't parsed the parent
 374         * yet, so we won't have a parent of a parent
 375         * here. However, it may turn out that we've
 376         * reached this commit some other way (where it
 377         * wasn't uninteresting), in which case we need
 378         * to mark its parents recursively too..
 379         */
 380        if (commit->object.flags & UNINTERESTING) {
 381                while (parent) {
 382                        struct commit *p = parent->item;
 383                        parent = parent->next;
 384                        parse_commit(p);
 385                        p->object.flags |= UNINTERESTING;
 386                        if (p->parents)
 387                                mark_parents_uninteresting(p);
 388                        if (p->object.flags & SEEN)
 389                                continue;
 390                        p->object.flags |= SEEN;
 391                        insert_by_date(p, list);
 392                }
 393                return;
 394        }
 395
 396        /*
 397         * Ok, the commit wasn't uninteresting. Try to
 398         * simplify the commit history and find the parent
 399         * that has no differences in the path set if one exists.
 400         */
 401        if (revs->prune_fn)
 402                revs->prune_fn(revs, commit);
 403
 404        if (revs->no_walk)
 405                return;
 406
 407        left_flag = (commit->object.flags & SYMMETRIC_LEFT);
 408        parent = commit->parents;
 409        while (parent) {
 410                struct commit *p = parent->item;
 411
 412                parent = parent->next;
 413
 414                parse_commit(p);
 415                p->object.flags |= left_flag;
 416                if (p->object.flags & SEEN)
 417                        continue;
 418                p->object.flags |= SEEN;
 419                insert_by_date(p, list);
 420        }
 421}
 422
 423static void limit_list(struct rev_info *revs)
 424{
 425        struct commit_list *list = revs->commits;
 426        struct commit_list *newlist = NULL;
 427        struct commit_list **p = &newlist;
 428
 429        while (list) {
 430                struct commit_list *entry = list;
 431                struct commit *commit = list->item;
 432                struct object *obj = &commit->object;
 433
 434                list = list->next;
 435                free(entry);
 436
 437                if (revs->max_age != -1 && (commit->date < revs->max_age))
 438                        obj->flags |= UNINTERESTING;
 439                add_parents_to_list(revs, commit, &list);
 440                if (obj->flags & UNINTERESTING) {
 441                        mark_parents_uninteresting(commit);
 442                        if (everybody_uninteresting(list))
 443                                break;
 444                        continue;
 445                }
 446                if (revs->min_age != -1 && (commit->date > revs->min_age))
 447                        continue;
 448                p = &commit_list_insert(commit, p)->next;
 449        }
 450        revs->commits = newlist;
 451}
 452
 453struct all_refs_cb {
 454        int all_flags;
 455        int warned_bad_reflog;
 456        struct rev_info *all_revs;
 457        const char *name_for_errormsg;
 458};
 459
 460static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 461{
 462        struct all_refs_cb *cb = cb_data;
 463        struct object *object = get_reference(cb->all_revs, path, sha1,
 464                                              cb->all_flags);
 465        add_pending_object(cb->all_revs, object, path);
 466        return 0;
 467}
 468
 469static void handle_all(struct rev_info *revs, unsigned flags)
 470{
 471        struct all_refs_cb cb;
 472        cb.all_revs = revs;
 473        cb.all_flags = flags;
 474        for_each_ref(handle_one_ref, &cb);
 475}
 476
 477static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
 478{
 479        struct all_refs_cb *cb = cb_data;
 480        if (!is_null_sha1(sha1)) {
 481                struct object *o = parse_object(sha1);
 482                if (o) {
 483                        o->flags |= cb->all_flags;
 484                        add_pending_object(cb->all_revs, o, "");
 485                }
 486                else if (!cb->warned_bad_reflog) {
 487                        warn("reflog of '%s' references pruned commits",
 488                                cb->name_for_errormsg);
 489                        cb->warned_bad_reflog = 1;
 490                }
 491        }
 492}
 493
 494static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 495                const char *email, unsigned long timestamp, int tz,
 496                const char *message, void *cb_data)
 497{
 498        handle_one_reflog_commit(osha1, cb_data);
 499        handle_one_reflog_commit(nsha1, cb_data);
 500        return 0;
 501}
 502
 503static int handle_one_reflog(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 504{
 505        struct all_refs_cb *cb = cb_data;
 506        cb->warned_bad_reflog = 0;
 507        cb->name_for_errormsg = path;
 508        for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
 509        return 0;
 510}
 511
 512static void handle_reflog(struct rev_info *revs, unsigned flags)
 513{
 514        struct all_refs_cb cb;
 515        cb.all_revs = revs;
 516        cb.all_flags = flags;
 517        for_each_reflog(handle_one_reflog, &cb);
 518}
 519
 520static int add_parents_only(struct rev_info *revs, const char *arg, int flags)
 521{
 522        unsigned char sha1[20];
 523        struct object *it;
 524        struct commit *commit;
 525        struct commit_list *parents;
 526
 527        if (*arg == '^') {
 528                flags ^= UNINTERESTING;
 529                arg++;
 530        }
 531        if (get_sha1(arg, sha1))
 532                return 0;
 533        while (1) {
 534                it = get_reference(revs, arg, sha1, 0);
 535                if (it->type != OBJ_TAG)
 536                        break;
 537                hashcpy(sha1, ((struct tag*)it)->tagged->sha1);
 538        }
 539        if (it->type != OBJ_COMMIT)
 540                return 0;
 541        commit = (struct commit *)it;
 542        for (parents = commit->parents; parents; parents = parents->next) {
 543                it = &parents->item->object;
 544                it->flags |= flags;
 545                add_pending_object(revs, it, arg);
 546        }
 547        return 1;
 548}
 549
 550void init_revisions(struct rev_info *revs, const char *prefix)
 551{
 552        memset(revs, 0, sizeof(*revs));
 553
 554        revs->abbrev = DEFAULT_ABBREV;
 555        revs->ignore_merges = 1;
 556        revs->simplify_history = 1;
 557        revs->pruning.recursive = 1;
 558        revs->pruning.quiet = 1;
 559        revs->pruning.add_remove = file_add_remove;
 560        revs->pruning.change = file_change;
 561        revs->lifo = 1;
 562        revs->dense = 1;
 563        revs->prefix = prefix;
 564        revs->max_age = -1;
 565        revs->min_age = -1;
 566        revs->skip_count = -1;
 567        revs->max_count = -1;
 568
 569        revs->prune_fn = NULL;
 570        revs->prune_data = NULL;
 571
 572        revs->topo_setter = topo_sort_default_setter;
 573        revs->topo_getter = topo_sort_default_getter;
 574
 575        revs->commit_format = CMIT_FMT_DEFAULT;
 576
 577        diff_setup(&revs->diffopt);
 578}
 579
 580static void add_pending_commit_list(struct rev_info *revs,
 581                                    struct commit_list *commit_list,
 582                                    unsigned int flags)
 583{
 584        while (commit_list) {
 585                struct object *object = &commit_list->item->object;
 586                object->flags |= flags;
 587                add_pending_object(revs, object, sha1_to_hex(object->sha1));
 588                commit_list = commit_list->next;
 589        }
 590}
 591
 592static void prepare_show_merge(struct rev_info *revs)
 593{
 594        struct commit_list *bases;
 595        struct commit *head, *other;
 596        unsigned char sha1[20];
 597        const char **prune = NULL;
 598        int i, prune_num = 1; /* counting terminating NULL */
 599
 600        if (get_sha1("HEAD", sha1) || !(head = lookup_commit(sha1)))
 601                die("--merge without HEAD?");
 602        if (get_sha1("MERGE_HEAD", sha1) || !(other = lookup_commit(sha1)))
 603                die("--merge without MERGE_HEAD?");
 604        add_pending_object(revs, &head->object, "HEAD");
 605        add_pending_object(revs, &other->object, "MERGE_HEAD");
 606        bases = get_merge_bases(head, other, 1);
 607        while (bases) {
 608                struct commit *it = bases->item;
 609                struct commit_list *n = bases->next;
 610                free(bases);
 611                bases = n;
 612                it->object.flags |= UNINTERESTING;
 613                add_pending_object(revs, &it->object, "(merge-base)");
 614        }
 615
 616        if (!active_nr)
 617                read_cache();
 618        for (i = 0; i < active_nr; i++) {
 619                struct cache_entry *ce = active_cache[i];
 620                if (!ce_stage(ce))
 621                        continue;
 622                if (ce_path_match(ce, revs->prune_data)) {
 623                        prune_num++;
 624                        prune = xrealloc(prune, sizeof(*prune) * prune_num);
 625                        prune[prune_num-2] = ce->name;
 626                        prune[prune_num-1] = NULL;
 627                }
 628                while ((i+1 < active_nr) &&
 629                       ce_same_name(ce, active_cache[i+1]))
 630                        i++;
 631        }
 632        revs->prune_data = prune;
 633}
 634
 635int handle_revision_arg(const char *arg, struct rev_info *revs,
 636                        int flags,
 637                        int cant_be_filename)
 638{
 639        char *dotdot;
 640        struct object *object;
 641        unsigned char sha1[20];
 642        int local_flags;
 643
 644        dotdot = strstr(arg, "..");
 645        if (dotdot) {
 646                unsigned char from_sha1[20];
 647                const char *next = dotdot + 2;
 648                const char *this = arg;
 649                int symmetric = *next == '.';
 650                unsigned int flags_exclude = flags ^ UNINTERESTING;
 651
 652                *dotdot = 0;
 653                next += symmetric;
 654
 655                if (!*next)
 656                        next = "HEAD";
 657                if (dotdot == arg)
 658                        this = "HEAD";
 659                if (!get_sha1(this, from_sha1) &&
 660                    !get_sha1(next, sha1)) {
 661                        struct commit *a, *b;
 662                        struct commit_list *exclude;
 663
 664                        a = lookup_commit_reference(from_sha1);
 665                        b = lookup_commit_reference(sha1);
 666                        if (!a || !b) {
 667                                die(symmetric ?
 668                                    "Invalid symmetric difference expression %s...%s" :
 669                                    "Invalid revision range %s..%s",
 670                                    arg, next);
 671                        }
 672
 673                        if (!cant_be_filename) {
 674                                *dotdot = '.';
 675                                verify_non_filename(revs->prefix, arg);
 676                        }
 677
 678                        if (symmetric) {
 679                                exclude = get_merge_bases(a, b, 1);
 680                                add_pending_commit_list(revs, exclude,
 681                                                        flags_exclude);
 682                                free_commit_list(exclude);
 683                                a->object.flags |= flags | SYMMETRIC_LEFT;
 684                        } else
 685                                a->object.flags |= flags_exclude;
 686                        b->object.flags |= flags;
 687                        add_pending_object(revs, &a->object, this);
 688                        add_pending_object(revs, &b->object, next);
 689                        return 0;
 690                }
 691                *dotdot = '.';
 692        }
 693        dotdot = strstr(arg, "^@");
 694        if (dotdot && !dotdot[2]) {
 695                *dotdot = 0;
 696                if (add_parents_only(revs, arg, flags))
 697                        return 0;
 698                *dotdot = '^';
 699        }
 700        dotdot = strstr(arg, "^!");
 701        if (dotdot && !dotdot[2]) {
 702                *dotdot = 0;
 703                if (!add_parents_only(revs, arg, flags ^ UNINTERESTING))
 704                        *dotdot = '^';
 705        }
 706
 707        local_flags = 0;
 708        if (*arg == '^') {
 709                local_flags = UNINTERESTING;
 710                arg++;
 711        }
 712        if (get_sha1(arg, sha1))
 713                return -1;
 714        if (!cant_be_filename)
 715                verify_non_filename(revs->prefix, arg);
 716        object = get_reference(revs, arg, sha1, flags ^ local_flags);
 717        add_pending_object(revs, object, arg);
 718        return 0;
 719}
 720
 721static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
 722{
 723        if (!revs->grep_filter) {
 724                struct grep_opt *opt = xcalloc(1, sizeof(*opt));
 725                opt->status_only = 1;
 726                opt->pattern_tail = &(opt->pattern_list);
 727                opt->regflags = REG_NEWLINE;
 728                revs->grep_filter = opt;
 729        }
 730        append_grep_pattern(revs->grep_filter, ptn,
 731                            "command line", 0, what);
 732}
 733
 734static void add_header_grep(struct rev_info *revs, const char *field, const char *pattern)
 735{
 736        char *pat;
 737        const char *prefix;
 738        int patlen, fldlen;
 739
 740        fldlen = strlen(field);
 741        patlen = strlen(pattern);
 742        pat = xmalloc(patlen + fldlen + 10);
 743        prefix = ".*";
 744        if (*pattern == '^') {
 745                prefix = "";
 746                pattern++;
 747        }
 748        sprintf(pat, "^%s %s%s", field, prefix, pattern);
 749        add_grep(revs, pat, GREP_PATTERN_HEAD);
 750}
 751
 752static void add_message_grep(struct rev_info *revs, const char *pattern)
 753{
 754        add_grep(revs, pattern, GREP_PATTERN_BODY);
 755}
 756
 757static void add_ignore_packed(struct rev_info *revs, const char *name)
 758{
 759        int num = ++revs->num_ignore_packed;
 760
 761        revs->ignore_packed = xrealloc(revs->ignore_packed,
 762                                       sizeof(const char **) * (num + 1));
 763        revs->ignore_packed[num-1] = name;
 764        revs->ignore_packed[num] = NULL;
 765}
 766
 767/*
 768 * Parse revision information, filling in the "rev_info" structure,
 769 * and removing the used arguments from the argument list.
 770 *
 771 * Returns the number of arguments left that weren't recognized
 772 * (which are also moved to the head of the argument list)
 773 */
 774int setup_revisions(int argc, const char **argv, struct rev_info *revs, const char *def)
 775{
 776        int i, flags, seen_dashdash, show_merge;
 777        const char **unrecognized = argv + 1;
 778        int left = 1;
 779        int all_match = 0;
 780
 781        /* First, search for "--" */
 782        seen_dashdash = 0;
 783        for (i = 1; i < argc; i++) {
 784                const char *arg = argv[i];
 785                if (strcmp(arg, "--"))
 786                        continue;
 787                argv[i] = NULL;
 788                argc = i;
 789                revs->prune_data = get_pathspec(revs->prefix, argv + i + 1);
 790                seen_dashdash = 1;
 791                break;
 792        }
 793
 794        flags = show_merge = 0;
 795        for (i = 1; i < argc; i++) {
 796                const char *arg = argv[i];
 797                if (*arg == '-') {
 798                        int opts;
 799                        if (!prefixcmp(arg, "--max-count=")) {
 800                                revs->max_count = atoi(arg + 12);
 801                                continue;
 802                        }
 803                        if (!prefixcmp(arg, "--skip=")) {
 804                                revs->skip_count = atoi(arg + 7);
 805                                continue;
 806                        }
 807                        /* accept -<digit>, like traditional "head" */
 808                        if ((*arg == '-') && isdigit(arg[1])) {
 809                                revs->max_count = atoi(arg + 1);
 810                                continue;
 811                        }
 812                        if (!strcmp(arg, "-n")) {
 813                                if (argc <= i + 1)
 814                                        die("-n requires an argument");
 815                                revs->max_count = atoi(argv[++i]);
 816                                continue;
 817                        }
 818                        if (!prefixcmp(arg, "-n")) {
 819                                revs->max_count = atoi(arg + 2);
 820                                continue;
 821                        }
 822                        if (!prefixcmp(arg, "--max-age=")) {
 823                                revs->max_age = atoi(arg + 10);
 824                                continue;
 825                        }
 826                        if (!prefixcmp(arg, "--since=")) {
 827                                revs->max_age = approxidate(arg + 8);
 828                                continue;
 829                        }
 830                        if (!prefixcmp(arg, "--after=")) {
 831                                revs->max_age = approxidate(arg + 8);
 832                                continue;
 833                        }
 834                        if (!prefixcmp(arg, "--min-age=")) {
 835                                revs->min_age = atoi(arg + 10);
 836                                continue;
 837                        }
 838                        if (!prefixcmp(arg, "--before=")) {
 839                                revs->min_age = approxidate(arg + 9);
 840                                continue;
 841                        }
 842                        if (!prefixcmp(arg, "--until=")) {
 843                                revs->min_age = approxidate(arg + 8);
 844                                continue;
 845                        }
 846                        if (!strcmp(arg, "--all")) {
 847                                handle_all(revs, flags);
 848                                continue;
 849                        }
 850                        if (!strcmp(arg, "--reflog")) {
 851                                handle_reflog(revs, flags);
 852                                continue;
 853                        }
 854                        if (!strcmp(arg, "-g") ||
 855                                        !strcmp(arg, "--walk-reflogs")) {
 856                                init_reflog_walk(&revs->reflog_info);
 857                                continue;
 858                        }
 859                        if (!strcmp(arg, "--not")) {
 860                                flags ^= UNINTERESTING;
 861                                continue;
 862                        }
 863                        if (!strcmp(arg, "--default")) {
 864                                if (++i >= argc)
 865                                        die("bad --default argument");
 866                                def = argv[i];
 867                                continue;
 868                        }
 869                        if (!strcmp(arg, "--merge")) {
 870                                show_merge = 1;
 871                                continue;
 872                        }
 873                        if (!strcmp(arg, "--topo-order")) {
 874                                revs->topo_order = 1;
 875                                continue;
 876                        }
 877                        if (!strcmp(arg, "--date-order")) {
 878                                revs->lifo = 0;
 879                                revs->topo_order = 1;
 880                                continue;
 881                        }
 882                        if (!strcmp(arg, "--parents")) {
 883                                revs->parents = 1;
 884                                continue;
 885                        }
 886                        if (!strcmp(arg, "--dense")) {
 887                                revs->dense = 1;
 888                                continue;
 889                        }
 890                        if (!strcmp(arg, "--sparse")) {
 891                                revs->dense = 0;
 892                                continue;
 893                        }
 894                        if (!strcmp(arg, "--remove-empty")) {
 895                                revs->remove_empty_trees = 1;
 896                                continue;
 897                        }
 898                        if (!strcmp(arg, "--no-merges")) {
 899                                revs->no_merges = 1;
 900                                continue;
 901                        }
 902                        if (!strcmp(arg, "--boundary")) {
 903                                revs->boundary = 1;
 904                                continue;
 905                        }
 906                        if (!strcmp(arg, "--left-right")) {
 907                                revs->left_right = 1;
 908                                continue;
 909                        }
 910                        if (!strcmp(arg, "--objects")) {
 911                                revs->tag_objects = 1;
 912                                revs->tree_objects = 1;
 913                                revs->blob_objects = 1;
 914                                continue;
 915                        }
 916                        if (!strcmp(arg, "--objects-edge")) {
 917                                revs->tag_objects = 1;
 918                                revs->tree_objects = 1;
 919                                revs->blob_objects = 1;
 920                                revs->edge_hint = 1;
 921                                continue;
 922                        }
 923                        if (!strcmp(arg, "--unpacked")) {
 924                                revs->unpacked = 1;
 925                                free(revs->ignore_packed);
 926                                revs->ignore_packed = NULL;
 927                                revs->num_ignore_packed = 0;
 928                                continue;
 929                        }
 930                        if (!prefixcmp(arg, "--unpacked=")) {
 931                                revs->unpacked = 1;
 932                                add_ignore_packed(revs, arg+11);
 933                                continue;
 934                        }
 935                        if (!strcmp(arg, "-r")) {
 936                                revs->diff = 1;
 937                                revs->diffopt.recursive = 1;
 938                                continue;
 939                        }
 940                        if (!strcmp(arg, "-t")) {
 941                                revs->diff = 1;
 942                                revs->diffopt.recursive = 1;
 943                                revs->diffopt.tree_in_recursive = 1;
 944                                continue;
 945                        }
 946                        if (!strcmp(arg, "-m")) {
 947                                revs->ignore_merges = 0;
 948                                continue;
 949                        }
 950                        if (!strcmp(arg, "-c")) {
 951                                revs->diff = 1;
 952                                revs->dense_combined_merges = 0;
 953                                revs->combine_merges = 1;
 954                                continue;
 955                        }
 956                        if (!strcmp(arg, "--cc")) {
 957                                revs->diff = 1;
 958                                revs->dense_combined_merges = 1;
 959                                revs->combine_merges = 1;
 960                                continue;
 961                        }
 962                        if (!strcmp(arg, "-v")) {
 963                                revs->verbose_header = 1;
 964                                continue;
 965                        }
 966                        if (!prefixcmp(arg, "--pretty")) {
 967                                revs->verbose_header = 1;
 968                                revs->commit_format = get_commit_format(arg+8);
 969                                continue;
 970                        }
 971                        if (!strcmp(arg, "--root")) {
 972                                revs->show_root_diff = 1;
 973                                continue;
 974                        }
 975                        if (!strcmp(arg, "--no-commit-id")) {
 976                                revs->no_commit_id = 1;
 977                                continue;
 978                        }
 979                        if (!strcmp(arg, "--always")) {
 980                                revs->always_show_header = 1;
 981                                continue;
 982                        }
 983                        if (!strcmp(arg, "--no-abbrev")) {
 984                                revs->abbrev = 0;
 985                                continue;
 986                        }
 987                        if (!strcmp(arg, "--abbrev")) {
 988                                revs->abbrev = DEFAULT_ABBREV;
 989                                continue;
 990                        }
 991                        if (!prefixcmp(arg, "--abbrev=")) {
 992                                revs->abbrev = strtoul(arg + 9, NULL, 10);
 993                                if (revs->abbrev < MINIMUM_ABBREV)
 994                                        revs->abbrev = MINIMUM_ABBREV;
 995                                else if (revs->abbrev > 40)
 996                                        revs->abbrev = 40;
 997                                continue;
 998                        }
 999                        if (!strcmp(arg, "--abbrev-commit")) {
1000                                revs->abbrev_commit = 1;
1001                                continue;
1002                        }
1003                        if (!strcmp(arg, "--full-diff")) {
1004                                revs->diff = 1;
1005                                revs->full_diff = 1;
1006                                continue;
1007                        }
1008                        if (!strcmp(arg, "--full-history")) {
1009                                revs->simplify_history = 0;
1010                                continue;
1011                        }
1012                        if (!strcmp(arg, "--relative-date")) {
1013                                revs->relative_date = 1;
1014                                continue;
1015                        }
1016
1017                        /*
1018                         * Grepping the commit log
1019                         */
1020                        if (!prefixcmp(arg, "--author=")) {
1021                                add_header_grep(revs, "author", arg+9);
1022                                continue;
1023                        }
1024                        if (!prefixcmp(arg, "--committer=")) {
1025                                add_header_grep(revs, "committer", arg+12);
1026                                continue;
1027                        }
1028                        if (!prefixcmp(arg, "--grep=")) {
1029                                add_message_grep(revs, arg+7);
1030                                continue;
1031                        }
1032                        if (!strcmp(arg, "--all-match")) {
1033                                all_match = 1;
1034                                continue;
1035                        }
1036                        if (!prefixcmp(arg, "--encoding=")) {
1037                                arg += 11;
1038                                if (strcmp(arg, "none"))
1039                                        git_log_output_encoding = xstrdup(arg);
1040                                else
1041                                        git_log_output_encoding = "";
1042                                continue;
1043                        }
1044                        if (!strcmp(arg, "--reverse")) {
1045                                revs->reverse ^= 1;
1046                                continue;
1047                        }
1048
1049                        opts = diff_opt_parse(&revs->diffopt, argv+i, argc-i);
1050                        if (opts > 0) {
1051                                revs->diff = 1;
1052                                i += opts - 1;
1053                                continue;
1054                        }
1055                        *unrecognized++ = arg;
1056                        left++;
1057                        continue;
1058                }
1059
1060                if (handle_revision_arg(arg, revs, flags, seen_dashdash)) {
1061                        int j;
1062                        if (seen_dashdash || *arg == '^')
1063                                die("bad revision '%s'", arg);
1064
1065                        /* If we didn't have a "--":
1066                         * (1) all filenames must exist;
1067                         * (2) all rev-args must not be interpretable
1068                         *     as a valid filename.
1069                         * but the latter we have checked in the main loop.
1070                         */
1071                        for (j = i; j < argc; j++)
1072                                verify_filename(revs->prefix, argv[j]);
1073
1074                        revs->prune_data = get_pathspec(revs->prefix,
1075                                                        argv + i);
1076                        break;
1077                }
1078        }
1079
1080        if (show_merge)
1081                prepare_show_merge(revs);
1082        if (def && !revs->pending.nr) {
1083                unsigned char sha1[20];
1084                struct object *object;
1085                if (get_sha1(def, sha1))
1086                        die("bad default revision '%s'", def);
1087                object = get_reference(revs, def, sha1, 0);
1088                add_pending_object(revs, object, def);
1089        }
1090
1091        if (revs->topo_order)
1092                revs->limited = 1;
1093
1094        if (revs->prune_data) {
1095                diff_tree_setup_paths(revs->prune_data, &revs->pruning);
1096                revs->prune_fn = try_to_simplify_commit;
1097                if (!revs->full_diff)
1098                        diff_tree_setup_paths(revs->prune_data, &revs->diffopt);
1099        }
1100        if (revs->combine_merges) {
1101                revs->ignore_merges = 0;
1102                if (revs->dense_combined_merges && !revs->diffopt.output_format)
1103                        revs->diffopt.output_format = DIFF_FORMAT_PATCH;
1104        }
1105        revs->diffopt.abbrev = revs->abbrev;
1106        if (diff_setup_done(&revs->diffopt) < 0)
1107                die("diff_setup_done failed");
1108
1109        if (revs->grep_filter) {
1110                revs->grep_filter->all_match = all_match;
1111                compile_grep_patterns(revs->grep_filter);
1112        }
1113
1114        return left;
1115}
1116
1117void prepare_revision_walk(struct rev_info *revs)
1118{
1119        int nr = revs->pending.nr;
1120        struct object_array_entry *e, *list;
1121
1122        e = list = revs->pending.objects;
1123        revs->pending.nr = 0;
1124        revs->pending.alloc = 0;
1125        revs->pending.objects = NULL;
1126        while (--nr >= 0) {
1127                struct commit *commit = handle_commit(revs, e->item, e->name);
1128                if (commit) {
1129                        if (!(commit->object.flags & SEEN)) {
1130                                commit->object.flags |= SEEN;
1131                                insert_by_date(commit, &revs->commits);
1132                        }
1133                }
1134                e++;
1135        }
1136        free(list);
1137
1138        if (revs->no_walk)
1139                return;
1140        if (revs->limited)
1141                limit_list(revs);
1142        if (revs->topo_order)
1143                sort_in_topological_order_fn(&revs->commits, revs->lifo,
1144                                             revs->topo_setter,
1145                                             revs->topo_getter);
1146}
1147
1148static int rewrite_one(struct rev_info *revs, struct commit **pp)
1149{
1150        for (;;) {
1151                struct commit *p = *pp;
1152                if (!revs->limited)
1153                        add_parents_to_list(revs, p, &revs->commits);
1154                if (p->parents && p->parents->next)
1155                        return 0;
1156                if (p->object.flags & (TREECHANGE | UNINTERESTING))
1157                        return 0;
1158                if (!p->parents)
1159                        return -1;
1160                *pp = p->parents->item;
1161        }
1162}
1163
1164static void rewrite_parents(struct rev_info *revs, struct commit *commit)
1165{
1166        struct commit_list **pp = &commit->parents;
1167        while (*pp) {
1168                struct commit_list *parent = *pp;
1169                if (rewrite_one(revs, &parent->item) < 0) {
1170                        *pp = parent->next;
1171                        continue;
1172                }
1173                pp = &parent->next;
1174        }
1175}
1176
1177static int commit_match(struct commit *commit, struct rev_info *opt)
1178{
1179        if (!opt->grep_filter)
1180                return 1;
1181        return grep_buffer(opt->grep_filter,
1182                           NULL, /* we say nothing, not even filename */
1183                           commit->buffer, strlen(commit->buffer));
1184}
1185
1186static struct commit *get_revision_1(struct rev_info *revs)
1187{
1188        if (!revs->commits)
1189                return NULL;
1190
1191        do {
1192                struct commit_list *entry = revs->commits;
1193                struct commit *commit = entry->item;
1194
1195                revs->commits = entry->next;
1196                free(entry);
1197
1198                if (revs->reflog_info)
1199                        fake_reflog_parent(revs->reflog_info, commit);
1200
1201                /*
1202                 * If we haven't done the list limiting, we need to look at
1203                 * the parents here. We also need to do the date-based limiting
1204                 * that we'd otherwise have done in limit_list().
1205                 */
1206                if (!revs->limited) {
1207                        if (revs->max_age != -1 &&
1208                            (commit->date < revs->max_age))
1209                                continue;
1210                        add_parents_to_list(revs, commit, &revs->commits);
1211                }
1212                if (commit->object.flags & SHOWN)
1213                        continue;
1214
1215                if (revs->unpacked && has_sha1_pack(commit->object.sha1,
1216                                                    revs->ignore_packed))
1217                    continue;
1218
1219                if (commit->object.flags & UNINTERESTING)
1220                        continue;
1221                if (revs->min_age != -1 && (commit->date > revs->min_age))
1222                        continue;
1223                if (revs->no_merges &&
1224                    commit->parents && commit->parents->next)
1225                        continue;
1226                if (!commit_match(commit, revs))
1227                        continue;
1228                if (revs->prune_fn && revs->dense) {
1229                        /* Commit without changes? */
1230                        if (!(commit->object.flags & TREECHANGE)) {
1231                                /* drop merges unless we want parenthood */
1232                                if (!revs->parents)
1233                                        continue;
1234                                /* non-merge - always ignore it */
1235                                if (!commit->parents || !commit->parents->next)
1236                                        continue;
1237                        }
1238                        if (revs->parents)
1239                                rewrite_parents(revs, commit);
1240                }
1241                return commit;
1242        } while (revs->commits);
1243        return NULL;
1244}
1245
1246static void gc_boundary(struct object_array *array)
1247{
1248        unsigned nr = array->nr;
1249        unsigned alloc = array->alloc;
1250        struct object_array_entry *objects = array->objects;
1251
1252        if (alloc <= nr) {
1253                unsigned i, j;
1254                for (i = j = 0; i < nr; i++) {
1255                        if (objects[i].item->flags & SHOWN)
1256                                continue;
1257                        if (i != j)
1258                                objects[j] = objects[i];
1259                        j++;
1260                }
1261                for (i = j; i < nr; i++)
1262                        objects[i].item = NULL;
1263                array->nr = j;
1264        }
1265}
1266
1267struct commit *get_revision(struct rev_info *revs)
1268{
1269        struct commit *c = NULL;
1270        struct commit_list *l;
1271
1272        if (revs->boundary == 2) {
1273                unsigned i;
1274                struct object_array *array = &revs->boundary_commits;
1275                struct object_array_entry *objects = array->objects;
1276                for (i = 0; i < array->nr; i++) {
1277                        c = (struct commit *)(objects[i].item);
1278                        if (!c)
1279                                continue;
1280                        if (!(c->object.flags & CHILD_SHOWN))
1281                                continue;
1282                        if (!(c->object.flags & SHOWN))
1283                                break;
1284                }
1285                if (array->nr <= i)
1286                        return NULL;
1287
1288                c->object.flags |= SHOWN | BOUNDARY;
1289                return c;
1290        }
1291
1292        if (revs->reverse) {
1293                int limit = -1;
1294
1295                if (0 <= revs->max_count) {
1296                        limit = revs->max_count;
1297                        if (0 < revs->skip_count)
1298                                limit += revs->skip_count;
1299                }
1300                l = NULL;
1301                while ((c = get_revision_1(revs))) {
1302                        commit_list_insert(c, &l);
1303                        if ((0 < limit) && !--limit)
1304                                break;
1305                }
1306                revs->commits = l;
1307                revs->reverse = 0;
1308                revs->max_count = -1;
1309                c = NULL;
1310        }
1311
1312        /*
1313         * Now pick up what they want to give us
1314         */
1315        c = get_revision_1(revs);
1316        if (c) {
1317                while (0 < revs->skip_count) {
1318                        revs->skip_count--;
1319                        c = get_revision_1(revs);
1320                        if (!c)
1321                                break;
1322                }
1323        }
1324
1325        /*
1326         * Check the max_count.
1327         */
1328        switch (revs->max_count) {
1329        case -1:
1330                break;
1331        case 0:
1332                c = NULL;
1333                break;
1334        default:
1335                revs->max_count--;
1336        }
1337
1338        if (c)
1339                c->object.flags |= SHOWN;
1340
1341        if (!revs->boundary) {
1342                return c;
1343        }
1344
1345        if (!c) {
1346                /*
1347                 * get_revision_1() runs out the commits, and
1348                 * we are done computing the boundaries.
1349                 * switch to boundary commits output mode.
1350                 */
1351                revs->boundary = 2;
1352                return get_revision(revs);
1353        }
1354
1355        /*
1356         * boundary commits are the commits that are parents of the
1357         * ones we got from get_revision_1() but they themselves are
1358         * not returned from get_revision_1().  Before returning
1359         * 'c', we need to mark its parents that they could be boundaries.
1360         */
1361
1362        for (l = c->parents; l; l = l->next) {
1363                struct object *p;
1364                p = &(l->item->object);
1365                if (p->flags & (CHILD_SHOWN | SHOWN))
1366                        continue;
1367                p->flags |= CHILD_SHOWN;
1368                gc_boundary(&revs->boundary_commits);
1369                add_object_array(p, NULL, &revs->boundary_commits);
1370        }
1371
1372        return c;
1373}