2 #include "object-store.h"
 
  10 #include "repository.h"
 
  13 #include "reflog-walk.h"
 
  14 #include "patch-ids.h"
 
  17 #include "string-list.h"
 
  20 #include "commit-slab.h"
 
  22 #include "cache-tree.h"
 
  26 #include "argv-array.h"
 
  27 #include "commit-reach.h"
 
  28 #include "commit-graph.h"
 
  29 #include "prio-queue.h"
 
  33 volatile show_early_output_fn_t show_early_output;
 
  35 static const char *term_bad;
 
  36 static const char *term_good;
 
  38 implement_shared_commit_slab(revision_sources, char *);
 
  40 void show_object_with_name(FILE *out, struct object *obj, const char *name)
 
  44         fprintf(out, "%s ", oid_to_hex(&obj->oid));
 
  45         for (p = name; *p && *p != '\n'; p++)
 
  50 static void mark_blob_uninteresting(struct blob *blob)
 
  54         if (blob->object.flags & UNINTERESTING)
 
  56         blob->object.flags |= UNINTERESTING;
 
  59 static void mark_tree_contents_uninteresting(struct repository *r,
 
  62         struct tree_desc desc;
 
  63         struct name_entry entry;
 
  65         if (parse_tree_gently(tree, 1) < 0)
 
  68         init_tree_desc(&desc, tree->buffer, tree->size);
 
  69         while (tree_entry(&desc, &entry)) {
 
  70                 switch (object_type(entry.mode)) {
 
  72                         mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
 
  75                         mark_blob_uninteresting(lookup_blob(r, &entry.oid));
 
  78                         /* Subproject commit - not in this repository */
 
  84          * We don't care about the tree any more
 
  85          * after it has been marked uninteresting.
 
  87         free_tree_buffer(tree);
 
  90 void mark_tree_uninteresting(struct repository *r, struct tree *tree)
 
  98         if (obj->flags & UNINTERESTING)
 
 100         obj->flags |= UNINTERESTING;
 
 101         mark_tree_contents_uninteresting(r, tree);
 
 104 struct path_and_oids_entry {
 
 105         struct hashmap_entry ent;
 
 110 static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
 
 111                              const struct hashmap_entry *eptr,
 
 112                              const struct hashmap_entry *entry_or_key,
 
 115         const struct path_and_oids_entry *e1, *e2;
 
 117         e1 = container_of(eptr, const struct path_and_oids_entry, ent);
 
 118         e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
 
 120         return strcmp(e1->path, e2->path);
 
 123 static void paths_and_oids_init(struct hashmap *map)
 
 125         hashmap_init(map, path_and_oids_cmp, NULL, 0);
 
 128 static void paths_and_oids_clear(struct hashmap *map)
 
 130         struct hashmap_iter iter;
 
 131         struct path_and_oids_entry *entry;
 
 133         hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
 
 134                 oidset_clear(&entry->trees);
 
 138         hashmap_free_entries(map, struct path_and_oids_entry, ent);
 
 141 static void paths_and_oids_insert(struct hashmap *map,
 
 143                                   const struct object_id *oid)
 
 145         int hash = strhash(path);
 
 146         struct path_and_oids_entry key;
 
 147         struct path_and_oids_entry *entry;
 
 149         hashmap_entry_init(&key.ent, hash);
 
 151         /* use a shallow copy for the lookup */
 
 152         key.path = (char *)path;
 
 153         oidset_init(&key.trees, 0);
 
 155         entry = hashmap_get_entry(map, &key, ent, NULL);
 
 157                 entry = xcalloc(1, sizeof(struct path_and_oids_entry));
 
 158                 hashmap_entry_init(&entry->ent, hash);
 
 159                 entry->path = xstrdup(key.path);
 
 160                 oidset_init(&entry->trees, 16);
 
 161                 hashmap_put(map, &entry->ent);
 
 164         oidset_insert(&entry->trees, oid);
 
 167 static void add_children_by_path(struct repository *r,
 
 171         struct tree_desc desc;
 
 172         struct name_entry entry;
 
 177         if (parse_tree_gently(tree, 1) < 0)
 
 180         init_tree_desc(&desc, tree->buffer, tree->size);
 
 181         while (tree_entry(&desc, &entry)) {
 
 182                 switch (object_type(entry.mode)) {
 
 184                         paths_and_oids_insert(map, entry.path, &entry.oid);
 
 186                         if (tree->object.flags & UNINTERESTING) {
 
 187                                 struct tree *child = lookup_tree(r, &entry.oid);
 
 189                                         child->object.flags |= UNINTERESTING;
 
 193                         if (tree->object.flags & UNINTERESTING) {
 
 194                                 struct blob *child = lookup_blob(r, &entry.oid);
 
 196                                         child->object.flags |= UNINTERESTING;
 
 200                         /* Subproject commit - not in this repository */
 
 205         free_tree_buffer(tree);
 
 208 void mark_trees_uninteresting_sparse(struct repository *r,
 
 209                                      struct oidset *trees)
 
 211         unsigned has_interesting = 0, has_uninteresting = 0;
 
 213         struct hashmap_iter map_iter;
 
 214         struct path_and_oids_entry *entry;
 
 215         struct object_id *oid;
 
 216         struct oidset_iter iter;
 
 218         oidset_iter_init(trees, &iter);
 
 219         while ((!has_interesting || !has_uninteresting) &&
 
 220                (oid = oidset_iter_next(&iter))) {
 
 221                 struct tree *tree = lookup_tree(r, oid);
 
 226                 if (tree->object.flags & UNINTERESTING)
 
 227                         has_uninteresting = 1;
 
 232         /* Do not walk unless we have both types of trees. */
 
 233         if (!has_uninteresting || !has_interesting)
 
 236         paths_and_oids_init(&map);
 
 238         oidset_iter_init(trees, &iter);
 
 239         while ((oid = oidset_iter_next(&iter))) {
 
 240                 struct tree *tree = lookup_tree(r, oid);
 
 241                 add_children_by_path(r, tree, &map);
 
 244         hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
 
 245                 mark_trees_uninteresting_sparse(r, &entry->trees);
 
 247         paths_and_oids_clear(&map);
 
 250 struct commit_stack {
 
 251         struct commit **items;
 
 254 #define COMMIT_STACK_INIT { NULL, 0, 0 }
 
 256 static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
 
 258         ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
 
 259         stack->items[stack->nr++] = commit;
 
 262 static struct commit *commit_stack_pop(struct commit_stack *stack)
 
 264         return stack->nr ? stack->items[--stack->nr] : NULL;
 
 267 static void commit_stack_clear(struct commit_stack *stack)
 
 269         FREE_AND_NULL(stack->items);
 
 270         stack->nr = stack->alloc = 0;
 
 273 static void mark_one_parent_uninteresting(struct commit *commit,
 
 274                                           struct commit_stack *pending)
 
 276         struct commit_list *l;
 
 278         if (commit->object.flags & UNINTERESTING)
 
 280         commit->object.flags |= UNINTERESTING;
 
 283          * Normally we haven't parsed the parent
 
 284          * yet, so we won't have a parent of a parent
 
 285          * here. However, it may turn out that we've
 
 286          * reached this commit some other way (where it
 
 287          * wasn't uninteresting), in which case we need
 
 288          * to mark its parents recursively too..
 
 290         for (l = commit->parents; l; l = l->next)
 
 291                 commit_stack_push(pending, l->item);
 
 294 void mark_parents_uninteresting(struct commit *commit)
 
 296         struct commit_stack pending = COMMIT_STACK_INIT;
 
 297         struct commit_list *l;
 
 299         for (l = commit->parents; l; l = l->next)
 
 300                 mark_one_parent_uninteresting(l->item, &pending);
 
 302         while (pending.nr > 0)
 
 303                 mark_one_parent_uninteresting(commit_stack_pop(&pending),
 
 306         commit_stack_clear(&pending);
 
 309 static void add_pending_object_with_path(struct rev_info *revs,
 
 311                                          const char *name, unsigned mode,
 
 316         if (revs->no_walk && (obj->flags & UNINTERESTING))
 
 318         if (revs->reflog_info && obj->type == OBJ_COMMIT) {
 
 319                 struct strbuf buf = STRBUF_INIT;
 
 320                 int len = interpret_branch_name(name, 0, &buf, 0);
 
 322                 if (0 < len && name[len] && buf.len)
 
 323                         strbuf_addstr(&buf, name + len);
 
 324                 add_reflog_for_walk(revs->reflog_info,
 
 325                                     (struct commit *)obj,
 
 326                                     buf.buf[0] ? buf.buf: name);
 
 327                 strbuf_release(&buf);
 
 328                 return; /* do not add the commit itself */
 
 330         add_object_array_with_path(obj, name, &revs->pending, mode, path);
 
 333 static void add_pending_object_with_mode(struct rev_info *revs,
 
 335                                          const char *name, unsigned mode)
 
 337         add_pending_object_with_path(revs, obj, name, mode, NULL);
 
 340 void add_pending_object(struct rev_info *revs,
 
 341                         struct object *obj, const char *name)
 
 343         add_pending_object_with_mode(revs, obj, name, S_IFINVALID);
 
 346 void add_head_to_pending(struct rev_info *revs)
 
 348         struct object_id oid;
 
 350         if (get_oid("HEAD", &oid))
 
 352         obj = parse_object(revs->repo, &oid);
 
 355         add_pending_object(revs, obj, "HEAD");
 
 358 static struct object *get_reference(struct rev_info *revs, const char *name,
 
 359                                     const struct object_id *oid,
 
 362         struct object *object;
 
 365          * If the repository has commit graphs, repo_parse_commit() avoids
 
 366          * reading the object buffer, so use it whenever possible.
 
 368         if (oid_object_info(revs->repo, oid, NULL) == OBJ_COMMIT) {
 
 369                 struct commit *c = lookup_commit(revs->repo, oid);
 
 370                 if (!repo_parse_commit(revs->repo, c))
 
 371                         object = (struct object *) c;
 
 375                 object = parse_object(revs->repo, oid);
 
 379                 if (revs->ignore_missing)
 
 381                 if (revs->exclude_promisor_objects && is_promisor_object(oid))
 
 383                 die("bad object %s", name);
 
 385         object->flags |= flags;
 
 389 void add_pending_oid(struct rev_info *revs, const char *name,
 
 390                       const struct object_id *oid, unsigned int flags)
 
 392         struct object *object = get_reference(revs, name, oid, flags);
 
 393         add_pending_object(revs, object, name);
 
 396 static struct commit *handle_commit(struct rev_info *revs,
 
 397                                     struct object_array_entry *entry)
 
 399         struct object *object = entry->item;
 
 400         const char *name = entry->name;
 
 401         const char *path = entry->path;
 
 402         unsigned int mode = entry->mode;
 
 403         unsigned long flags = object->flags;
 
 406          * Tag object? Look what it points to..
 
 408         while (object->type == OBJ_TAG) {
 
 409                 struct tag *tag = (struct tag *) object;
 
 410                 if (revs->tag_objects && !(flags & UNINTERESTING))
 
 411                         add_pending_object(revs, object, tag->tag);
 
 412                 object = parse_object(revs->repo, get_tagged_oid(tag));
 
 414                         if (revs->ignore_missing_links || (flags & UNINTERESTING))
 
 416                         if (revs->exclude_promisor_objects &&
 
 417                             is_promisor_object(&tag->tagged->oid))
 
 419                         die("bad object %s", oid_to_hex(&tag->tagged->oid));
 
 421                 object->flags |= flags;
 
 423                  * We'll handle the tagged object by looping or dropping
 
 424                  * through to the non-tag handlers below. Do not
 
 425                  * propagate path data from the tag's pending entry.
 
 432          * Commit object? Just return it, we'll do all the complex
 
 435         if (object->type == OBJ_COMMIT) {
 
 436                 struct commit *commit = (struct commit *)object;
 
 438                 if (parse_commit(commit) < 0)
 
 439                         die("unable to parse commit %s", name);
 
 440                 if (flags & UNINTERESTING) {
 
 441                         mark_parents_uninteresting(commit);
 
 443                         if (!revs->topo_order || !generation_numbers_enabled(the_repository))
 
 447                         char **slot = revision_sources_at(revs->sources, commit);
 
 450                                 *slot = xstrdup(name);
 
 456          * Tree object? Either mark it uninteresting, or add it
 
 457          * to the list of objects to look at later..
 
 459         if (object->type == OBJ_TREE) {
 
 460                 struct tree *tree = (struct tree *)object;
 
 461                 if (!revs->tree_objects)
 
 463                 if (flags & UNINTERESTING) {
 
 464                         mark_tree_contents_uninteresting(revs->repo, tree);
 
 467                 add_pending_object_with_path(revs, object, name, mode, path);
 
 472          * Blob object? You know the drill by now..
 
 474         if (object->type == OBJ_BLOB) {
 
 475                 if (!revs->blob_objects)
 
 477                 if (flags & UNINTERESTING)
 
 479                 add_pending_object_with_path(revs, object, name, mode, path);
 
 482         die("%s is unknown object", name);
 
 485 static int everybody_uninteresting(struct commit_list *orig,
 
 486                                    struct commit **interesting_cache)
 
 488         struct commit_list *list = orig;
 
 490         if (*interesting_cache) {
 
 491                 struct commit *commit = *interesting_cache;
 
 492                 if (!(commit->object.flags & UNINTERESTING))
 
 497                 struct commit *commit = list->item;
 
 499                 if (commit->object.flags & UNINTERESTING)
 
 502                 *interesting_cache = commit;
 
 509  * A definition of "relevant" commit that we can use to simplify limited graphs
 
 510  * by eliminating side branches.
 
 512  * A "relevant" commit is one that is !UNINTERESTING (ie we are including it
 
 513  * in our list), or that is a specified BOTTOM commit. Then after computing
 
 514  * a limited list, during processing we can generally ignore boundary merges
 
 515  * coming from outside the graph, (ie from irrelevant parents), and treat
 
 516  * those merges as if they were single-parent. TREESAME is defined to consider
 
 517  * only relevant parents, if any. If we are TREESAME to our on-graph parents,
 
 518  * we don't care if we were !TREESAME to non-graph parents.
 
 520  * Treating bottom commits as relevant ensures that a limited graph's
 
 521  * connection to the actual bottom commit is not viewed as a side branch, but
 
 522  * treated as part of the graph. For example:
 
 524  *   ....Z...A---X---o---o---B
 
 528  * When computing "A..B", the A-X connection is at least as important as
 
 529  * Y-X, despite A being flagged UNINTERESTING.
 
 531  * And when computing --ancestry-path "A..B", the A-X connection is more
 
 532  * important than Y-X, despite both A and Y being flagged UNINTERESTING.
 
 534 static inline int relevant_commit(struct commit *commit)
 
 536         return (commit->object.flags & (UNINTERESTING | BOTTOM)) != UNINTERESTING;
 
 540  * Return a single relevant commit from a parent list. If we are a TREESAME
 
 541  * commit, and this selects one of our parents, then we can safely simplify to
 
 544 static struct commit *one_relevant_parent(const struct rev_info *revs,
 
 545                                           struct commit_list *orig)
 
 547         struct commit_list *list = orig;
 
 548         struct commit *relevant = NULL;
 
 554          * For 1-parent commits, or if first-parent-only, then return that
 
 555          * first parent (even if not "relevant" by the above definition).
 
 556          * TREESAME will have been set purely on that parent.
 
 558         if (revs->first_parent_only || !orig->next)
 
 562          * For multi-parent commits, identify a sole relevant parent, if any.
 
 563          * If we have only one relevant parent, then TREESAME will be set purely
 
 564          * with regard to that parent, and we can simplify accordingly.
 
 566          * If we have more than one relevant parent, or no relevant parents
 
 567          * (and multiple irrelevant ones), then we can't select a parent here
 
 571                 struct commit *commit = list->item;
 
 573                 if (relevant_commit(commit)) {
 
 583  * The goal is to get REV_TREE_NEW as the result only if the
 
 584  * diff consists of all '+' (and no other changes), REV_TREE_OLD
 
 585  * if the whole diff is removal of old data, and otherwise
 
 586  * REV_TREE_DIFFERENT (of course if the trees are the same we
 
 587  * want REV_TREE_SAME).
 
 589  * The only time we care about the distinction is when
 
 590  * remove_empty_trees is in effect, in which case we care only about
 
 591  * whether the whole change is REV_TREE_NEW, or if there's another type
 
 592  * of change. Which means we can stop the diff early in either of these
 
 595  *   1. We're not using remove_empty_trees at all.
 
 597  *   2. We saw anything except REV_TREE_NEW.
 
 599 static int tree_difference = REV_TREE_SAME;
 
 601 static void file_add_remove(struct diff_options *options,
 
 602                     int addremove, unsigned mode,
 
 603                     const struct object_id *oid,
 
 605                     const char *fullpath, unsigned dirty_submodule)
 
 607         int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
 
 608         struct rev_info *revs = options->change_fn_data;
 
 610         tree_difference |= diff;
 
 611         if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
 
 612                 options->flags.has_changes = 1;
 
 615 static void file_change(struct diff_options *options,
 
 616                  unsigned old_mode, unsigned new_mode,
 
 617                  const struct object_id *old_oid,
 
 618                  const struct object_id *new_oid,
 
 619                  int old_oid_valid, int new_oid_valid,
 
 620                  const char *fullpath,
 
 621                  unsigned old_dirty_submodule, unsigned new_dirty_submodule)
 
 623         tree_difference = REV_TREE_DIFFERENT;
 
 624         options->flags.has_changes = 1;
 
 627 static int rev_compare_tree(struct rev_info *revs,
 
 628                             struct commit *parent, struct commit *commit)
 
 630         struct tree *t1 = get_commit_tree(parent);
 
 631         struct tree *t2 = get_commit_tree(commit);
 
 638         if (revs->simplify_by_decoration) {
 
 640                  * If we are simplifying by decoration, then the commit
 
 641                  * is worth showing if it has a tag pointing at it.
 
 643                 if (get_name_decoration(&commit->object))
 
 644                         return REV_TREE_DIFFERENT;
 
 646                  * A commit that is not pointed by a tag is uninteresting
 
 647                  * if we are not limited by path.  This means that you will
 
 648                  * see the usual "commits that touch the paths" plus any
 
 649                  * tagged commit by specifying both --simplify-by-decoration
 
 652                 if (!revs->prune_data.nr)
 
 653                         return REV_TREE_SAME;
 
 656         tree_difference = REV_TREE_SAME;
 
 657         revs->pruning.flags.has_changes = 0;
 
 658         if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
 
 660                 return REV_TREE_DIFFERENT;
 
 661         return tree_difference;
 
 664 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
 
 667         struct tree *t1 = get_commit_tree(commit);
 
 672         tree_difference = REV_TREE_SAME;
 
 673         revs->pruning.flags.has_changes = 0;
 
 674         retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
 
 676         return retval >= 0 && (tree_difference == REV_TREE_SAME);
 
 679 struct treesame_state {
 
 680         unsigned int nparents;
 
 681         unsigned char treesame[FLEX_ARRAY];
 
 684 static struct treesame_state *initialise_treesame(struct rev_info *revs, struct commit *commit)
 
 686         unsigned n = commit_list_count(commit->parents);
 
 687         struct treesame_state *st = xcalloc(1, st_add(sizeof(*st), n));
 
 689         add_decoration(&revs->treesame, &commit->object, st);
 
 694  * Must be called immediately after removing the nth_parent from a commit's
 
 695  * parent list, if we are maintaining the per-parent treesame[] decoration.
 
 696  * This does not recalculate the master TREESAME flag - update_treesame()
 
 697  * should be called to update it after a sequence of treesame[] modifications
 
 698  * that may have affected it.
 
 700 static int compact_treesame(struct rev_info *revs, struct commit *commit, unsigned nth_parent)
 
 702         struct treesame_state *st;
 
 705         if (!commit->parents) {
 
 707                  * Have just removed the only parent from a non-merge.
 
 708                  * Different handling, as we lack decoration.
 
 711                         die("compact_treesame %u", nth_parent);
 
 712                 old_same = !!(commit->object.flags & TREESAME);
 
 713                 if (rev_same_tree_as_empty(revs, commit))
 
 714                         commit->object.flags |= TREESAME;
 
 716                         commit->object.flags &= ~TREESAME;
 
 720         st = lookup_decoration(&revs->treesame, &commit->object);
 
 721         if (!st || nth_parent >= st->nparents)
 
 722                 die("compact_treesame %u", nth_parent);
 
 724         old_same = st->treesame[nth_parent];
 
 725         memmove(st->treesame + nth_parent,
 
 726                 st->treesame + nth_parent + 1,
 
 727                 st->nparents - nth_parent - 1);
 
 730          * If we've just become a non-merge commit, update TREESAME
 
 731          * immediately, and remove the no-longer-needed decoration.
 
 732          * If still a merge, defer update until update_treesame().
 
 734         if (--st->nparents == 1) {
 
 735                 if (commit->parents->next)
 
 736                         die("compact_treesame parents mismatch");
 
 737                 if (st->treesame[0] && revs->dense)
 
 738                         commit->object.flags |= TREESAME;
 
 740                         commit->object.flags &= ~TREESAME;
 
 741                 free(add_decoration(&revs->treesame, &commit->object, NULL));
 
 747 static unsigned update_treesame(struct rev_info *revs, struct commit *commit)
 
 749         if (commit->parents && commit->parents->next) {
 
 751                 struct treesame_state *st;
 
 752                 struct commit_list *p;
 
 753                 unsigned relevant_parents;
 
 754                 unsigned relevant_change, irrelevant_change;
 
 756                 st = lookup_decoration(&revs->treesame, &commit->object);
 
 758                         die("update_treesame %s", oid_to_hex(&commit->object.oid));
 
 759                 relevant_parents = 0;
 
 760                 relevant_change = irrelevant_change = 0;
 
 761                 for (p = commit->parents, n = 0; p; n++, p = p->next) {
 
 762                         if (relevant_commit(p->item)) {
 
 763                                 relevant_change |= !st->treesame[n];
 
 766                                 irrelevant_change |= !st->treesame[n];
 
 768                 if (relevant_parents ? relevant_change : irrelevant_change)
 
 769                         commit->object.flags &= ~TREESAME;
 
 771                         commit->object.flags |= TREESAME;
 
 774         return commit->object.flags & TREESAME;
 
 777 static inline int limiting_can_increase_treesame(const struct rev_info *revs)
 
 780          * TREESAME is irrelevant unless prune && dense;
 
 781          * if simplify_history is set, we can't have a mixture of TREESAME and
 
 782          *    !TREESAME INTERESTING parents (and we don't have treesame[]
 
 783          *    decoration anyway);
 
 784          * if first_parent_only is set, then the TREESAME flag is locked
 
 785          *    against the first parent (and again we lack treesame[] decoration).
 
 787         return revs->prune && revs->dense &&
 
 788                !revs->simplify_history &&
 
 789                !revs->first_parent_only;
 
 792 static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
 
 794         struct commit_list **pp, *parent;
 
 795         struct treesame_state *ts = NULL;
 
 796         int relevant_change = 0, irrelevant_change = 0;
 
 797         int relevant_parents, nth_parent;
 
 800          * If we don't do pruning, everything is interesting
 
 805         if (!get_commit_tree(commit))
 
 808         if (!commit->parents) {
 
 809                 if (rev_same_tree_as_empty(revs, commit))
 
 810                         commit->object.flags |= TREESAME;
 
 815          * Normal non-merge commit? If we don't want to make the
 
 816          * history dense, we consider it always to be a change..
 
 818         if (!revs->dense && !commit->parents->next)
 
 821         for (pp = &commit->parents, nth_parent = 0, relevant_parents = 0;
 
 822              (parent = *pp) != NULL;
 
 823              pp = &parent->next, nth_parent++) {
 
 824                 struct commit *p = parent->item;
 
 825                 if (relevant_commit(p))
 
 828                 if (nth_parent == 1) {
 
 830                          * This our second loop iteration - so we now know
 
 831                          * we're dealing with a merge.
 
 833                          * Do not compare with later parents when we care only about
 
 834                          * the first parent chain, in order to avoid derailing the
 
 835                          * traversal to follow a side branch that brought everything
 
 836                          * in the path we are limited to by the pathspec.
 
 838                         if (revs->first_parent_only)
 
 841                          * If this will remain a potentially-simplifiable
 
 842                          * merge, remember per-parent treesame if needed.
 
 843                          * Initialise the array with the comparison from our
 
 846                         if (revs->treesame.name &&
 
 847                             !revs->simplify_history &&
 
 848                             !(commit->object.flags & UNINTERESTING)) {
 
 849                                 ts = initialise_treesame(revs, commit);
 
 850                                 if (!(irrelevant_change || relevant_change))
 
 854                 if (parse_commit(p) < 0)
 
 855                         die("cannot simplify commit %s (because of %s)",
 
 856                             oid_to_hex(&commit->object.oid),
 
 857                             oid_to_hex(&p->object.oid));
 
 858                 switch (rev_compare_tree(revs, p, commit)) {
 
 860                         if (!revs->simplify_history || !relevant_commit(p)) {
 
 861                                 /* Even if a merge with an uninteresting
 
 862                                  * side branch brought the entire change
 
 863                                  * we are interested in, we do not want
 
 864                                  * to lose the other branches of this
 
 865                                  * merge, so we just keep going.
 
 868                                         ts->treesame[nth_parent] = 1;
 
 872                         commit->parents = parent;
 
 873                         commit->object.flags |= TREESAME;
 
 877                         if (revs->remove_empty_trees &&
 
 878                             rev_same_tree_as_empty(revs, p)) {
 
 879                                 /* We are adding all the specified
 
 880                                  * paths from this parent, so the
 
 881                                  * history beyond this parent is not
 
 882                                  * interesting.  Remove its parents
 
 883                                  * (they are grandparents for us).
 
 884                                  * IOW, we pretend this parent is a
 
 887                                 if (parse_commit(p) < 0)
 
 888                                         die("cannot simplify commit %s (invalid %s)",
 
 889                                             oid_to_hex(&commit->object.oid),
 
 890                                             oid_to_hex(&p->object.oid));
 
 895                 case REV_TREE_DIFFERENT:
 
 896                         if (relevant_commit(p))
 
 899                                 irrelevant_change = 1;
 
 902                 die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
 
 906          * TREESAME is straightforward for single-parent commits. For merge
 
 907          * commits, it is most useful to define it so that "irrelevant"
 
 908          * parents cannot make us !TREESAME - if we have any relevant
 
 909          * parents, then we only consider TREESAMEness with respect to them,
 
 910          * allowing irrelevant merges from uninteresting branches to be
 
 911          * simplified away. Only if we have only irrelevant parents do we
 
 912          * base TREESAME on them. Note that this logic is replicated in
 
 913          * update_treesame, which should be kept in sync.
 
 915         if (relevant_parents ? !relevant_change : !irrelevant_change)
 
 916                 commit->object.flags |= TREESAME;
 
 919 static int process_parents(struct rev_info *revs, struct commit *commit,
 
 920                            struct commit_list **list, struct prio_queue *queue)
 
 922         struct commit_list *parent = commit->parents;
 
 925         if (commit->object.flags & ADDED)
 
 927         commit->object.flags |= ADDED;
 
 929         if (revs->include_check &&
 
 930             !revs->include_check(commit, revs->include_check_data))
 
 934          * If the commit is uninteresting, don't try to
 
 935          * prune parents - we want the maximal uninteresting
 
 938          * Normally we haven't parsed the parent
 
 939          * yet, so we won't have a parent of a parent
 
 940          * here. However, it may turn out that we've
 
 941          * reached this commit some other way (where it
 
 942          * wasn't uninteresting), in which case we need
 
 943          * to mark its parents recursively too..
 
 945         if (commit->object.flags & UNINTERESTING) {
 
 947                         struct commit *p = parent->item;
 
 948                         parent = parent->next;
 
 950                                 p->object.flags |= UNINTERESTING;
 
 951                         if (parse_commit_gently(p, 1) < 0)
 
 954                                 mark_parents_uninteresting(p);
 
 955                         if (p->object.flags & SEEN)
 
 957                         p->object.flags |= SEEN;
 
 959                                 commit_list_insert_by_date(p, list);
 
 961                                 prio_queue_put(queue, p);
 
 967          * Ok, the commit wasn't uninteresting. Try to
 
 968          * simplify the commit history and find the parent
 
 969          * that has no differences in the path set if one exists.
 
 971         try_to_simplify_commit(revs, commit);
 
 976         left_flag = (commit->object.flags & SYMMETRIC_LEFT);
 
 978         for (parent = commit->parents; parent; parent = parent->next) {
 
 979                 struct commit *p = parent->item;
 
 980                 int gently = revs->ignore_missing_links ||
 
 981                              revs->exclude_promisor_objects;
 
 982                 if (parse_commit_gently(p, gently) < 0) {
 
 983                         if (revs->exclude_promisor_objects &&
 
 984                             is_promisor_object(&p->object.oid)) {
 
 985                                 if (revs->first_parent_only)
 
 992                         char **slot = revision_sources_at(revs->sources, p);
 
 995                                 *slot = *revision_sources_at(revs->sources, commit);
 
 997                 p->object.flags |= left_flag;
 
 998                 if (!(p->object.flags & SEEN)) {
 
 999                         p->object.flags |= SEEN;
 
1001                                 commit_list_insert_by_date(p, list);
 
1003                                 prio_queue_put(queue, p);
 
1005                 if (revs->first_parent_only)
 
1011 static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
 
1013         struct commit_list *p;
 
1014         int left_count = 0, right_count = 0;
 
1016         struct patch_ids ids;
 
1017         unsigned cherry_flag;
 
1019         /* First count the commits on the left and on the right */
 
1020         for (p = list; p; p = p->next) {
 
1021                 struct commit *commit = p->item;
 
1022                 unsigned flags = commit->object.flags;
 
1023                 if (flags & BOUNDARY)
 
1025                 else if (flags & SYMMETRIC_LEFT)
 
1031         if (!left_count || !right_count)
 
1034         left_first = left_count < right_count;
 
1035         init_patch_ids(revs->repo, &ids);
 
1036         ids.diffopts.pathspec = revs->diffopt.pathspec;
 
1038         /* Compute patch-ids for one side */
 
1039         for (p = list; p; p = p->next) {
 
1040                 struct commit *commit = p->item;
 
1041                 unsigned flags = commit->object.flags;
 
1043                 if (flags & BOUNDARY)
 
1046                  * If we have fewer left, left_first is set and we omit
 
1047                  * commits on the right branch in this loop.  If we have
 
1048                  * fewer right, we skip the left ones.
 
1050                 if (left_first != !!(flags & SYMMETRIC_LEFT))
 
1052                 add_commit_patch_id(commit, &ids);
 
1055         /* either cherry_mark or cherry_pick are true */
 
1056         cherry_flag = revs->cherry_mark ? PATCHSAME : SHOWN;
 
1058         /* Check the other side */
 
1059         for (p = list; p; p = p->next) {
 
1060                 struct commit *commit = p->item;
 
1061                 struct patch_id *id;
 
1062                 unsigned flags = commit->object.flags;
 
1064                 if (flags & BOUNDARY)
 
1067                  * If we have fewer left, left_first is set and we omit
 
1068                  * commits on the left branch in this loop.
 
1070                 if (left_first == !!(flags & SYMMETRIC_LEFT))
 
1074                  * Have we seen the same patch id?
 
1076                 id = has_commit_patch_id(commit, &ids);
 
1080                 commit->object.flags |= cherry_flag;
 
1081                 id->commit->object.flags |= cherry_flag;
 
1084         free_patch_ids(&ids);
 
1087 /* How many extra uninteresting commits we want to see.. */
 
1090 static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
 
1091                              struct commit **interesting_cache)
 
1094          * No source list at all? We're definitely done..
 
1100          * Does the destination list contain entries with a date
 
1101          * before the source list? Definitely _not_ done.
 
1103         if (date <= src->item->date)
 
1107          * Does the source list still have interesting commits in
 
1108          * it? Definitely not done..
 
1110         if (!everybody_uninteresting(src, interesting_cache))
 
1113         /* Ok, we're closing in.. */
 
1118  * "rev-list --ancestry-path A..B" computes commits that are ancestors
 
1119  * of B but not ancestors of A but further limits the result to those
 
1120  * that are descendants of A.  This takes the list of bottom commits and
 
1121  * the result of "A..B" without --ancestry-path, and limits the latter
 
1122  * further to the ones that can reach one of the commits in "bottom".
 
1124 static void limit_to_ancestry(struct commit_list *bottom, struct commit_list *list)
 
1126         struct commit_list *p;
 
1127         struct commit_list *rlist = NULL;
 
1131          * Reverse the list so that it will be likely that we would
 
1132          * process parents before children.
 
1134         for (p = list; p; p = p->next)
 
1135                 commit_list_insert(p->item, &rlist);
 
1137         for (p = bottom; p; p = p->next)
 
1138                 p->item->object.flags |= TMP_MARK;
 
1141          * Mark the ones that can reach bottom commits in "list",
 
1142          * in a bottom-up fashion.
 
1146                 for (p = rlist; p; p = p->next) {
 
1147                         struct commit *c = p->item;
 
1148                         struct commit_list *parents;
 
1149                         if (c->object.flags & (TMP_MARK | UNINTERESTING))
 
1151                         for (parents = c->parents;
 
1153                              parents = parents->next) {
 
1154                                 if (!(parents->item->object.flags & TMP_MARK))
 
1156                                 c->object.flags |= TMP_MARK;
 
1161         } while (made_progress);
 
1164          * NEEDSWORK: decide if we want to remove parents that are
 
1165          * not marked with TMP_MARK from commit->parents for commits
 
1166          * in the resulting list.  We may not want to do that, though.
 
1170          * The ones that are not marked with TMP_MARK are uninteresting
 
1172         for (p = list; p; p = p->next) {
 
1173                 struct commit *c = p->item;
 
1174                 if (c->object.flags & TMP_MARK)
 
1176                 c->object.flags |= UNINTERESTING;
 
1179         /* We are done with the TMP_MARK */
 
1180         for (p = list; p; p = p->next)
 
1181                 p->item->object.flags &= ~TMP_MARK;
 
1182         for (p = bottom; p; p = p->next)
 
1183                 p->item->object.flags &= ~TMP_MARK;
 
1184         free_commit_list(rlist);
 
1188  * Before walking the history, keep the set of "negative" refs the
 
1189  * caller has asked to exclude.
 
1191  * This is used to compute "rev-list --ancestry-path A..B", as we need
 
1192  * to filter the result of "A..B" further to the ones that can actually
 
1195 static struct commit_list *collect_bottom_commits(struct commit_list *list)
 
1197         struct commit_list *elem, *bottom = NULL;
 
1198         for (elem = list; elem; elem = elem->next)
 
1199                 if (elem->item->object.flags & BOTTOM)
 
1200                         commit_list_insert(elem->item, &bottom);
 
1204 /* Assumes either left_only or right_only is set */
 
1205 static void limit_left_right(struct commit_list *list, struct rev_info *revs)
 
1207         struct commit_list *p;
 
1209         for (p = list; p; p = p->next) {
 
1210                 struct commit *commit = p->item;
 
1212                 if (revs->right_only) {
 
1213                         if (commit->object.flags & SYMMETRIC_LEFT)
 
1214                                 commit->object.flags |= SHOWN;
 
1215                 } else  /* revs->left_only is set */
 
1216                         if (!(commit->object.flags & SYMMETRIC_LEFT))
 
1217                                 commit->object.flags |= SHOWN;
 
1221 static int limit_list(struct rev_info *revs)
 
1224         timestamp_t date = TIME_MAX;
 
1225         struct commit_list *list = revs->commits;
 
1226         struct commit_list *newlist = NULL;
 
1227         struct commit_list **p = &newlist;
 
1228         struct commit_list *bottom = NULL;
 
1229         struct commit *interesting_cache = NULL;
 
1231         if (revs->ancestry_path) {
 
1232                 bottom = collect_bottom_commits(list);
 
1234                         die("--ancestry-path given but there are no bottom commits");
 
1238                 struct commit *commit = pop_commit(&list);
 
1239                 struct object *obj = &commit->object;
 
1240                 show_early_output_fn_t show;
 
1242                 if (commit == interesting_cache)
 
1243                         interesting_cache = NULL;
 
1245                 if (revs->max_age != -1 && (commit->date < revs->max_age))
 
1246                         obj->flags |= UNINTERESTING;
 
1247                 if (process_parents(revs, commit, &list, NULL) < 0)
 
1249                 if (obj->flags & UNINTERESTING) {
 
1250                         mark_parents_uninteresting(commit);
 
1251                         slop = still_interesting(list, date, slop, &interesting_cache);
 
1256                 if (revs->min_age != -1 && (commit->date > revs->min_age))
 
1258                 date = commit->date;
 
1259                 p = &commit_list_insert(commit, p)->next;
 
1261                 show = show_early_output;
 
1265                 show(revs, newlist);
 
1266                 show_early_output = NULL;
 
1268         if (revs->cherry_pick || revs->cherry_mark)
 
1269                 cherry_pick_list(newlist, revs);
 
1271         if (revs->left_only || revs->right_only)
 
1272                 limit_left_right(newlist, revs);
 
1275                 limit_to_ancestry(bottom, newlist);
 
1276                 free_commit_list(bottom);
 
1280          * Check if any commits have become TREESAME by some of their parents
 
1281          * becoming UNINTERESTING.
 
1283         if (limiting_can_increase_treesame(revs))
 
1284                 for (list = newlist; list; list = list->next) {
 
1285                         struct commit *c = list->item;
 
1286                         if (c->object.flags & (UNINTERESTING | TREESAME))
 
1288                         update_treesame(revs, c);
 
1291         revs->commits = newlist;
 
1296  * Add an entry to refs->cmdline with the specified information.
 
1299 static void add_rev_cmdline(struct rev_info *revs,
 
1300                             struct object *item,
 
1305         struct rev_cmdline_info *info = &revs->cmdline;
 
1306         unsigned int nr = info->nr;
 
1308         ALLOC_GROW(info->rev, nr + 1, info->alloc);
 
1309         info->rev[nr].item = item;
 
1310         info->rev[nr].name = xstrdup(name);
 
1311         info->rev[nr].whence = whence;
 
1312         info->rev[nr].flags = flags;
 
1316 static void add_rev_cmdline_list(struct rev_info *revs,
 
1317                                  struct commit_list *commit_list,
 
1321         while (commit_list) {
 
1322                 struct object *object = &commit_list->item->object;
 
1323                 add_rev_cmdline(revs, object, oid_to_hex(&object->oid),
 
1325                 commit_list = commit_list->next;
 
1329 struct all_refs_cb {
 
1331         int warned_bad_reflog;
 
1332         struct rev_info *all_revs;
 
1333         const char *name_for_errormsg;
 
1334         struct worktree *wt;
 
1337 int ref_excluded(struct string_list *ref_excludes, const char *path)
 
1339         struct string_list_item *item;
 
1343         for_each_string_list_item(item, ref_excludes) {
 
1344                 if (!wildmatch(item->string, path, 0))
 
1350 static int handle_one_ref(const char *path, const struct object_id *oid,
 
1351                           int flag, void *cb_data)
 
1353         struct all_refs_cb *cb = cb_data;
 
1354         struct object *object;
 
1356         if (ref_excluded(cb->all_revs->ref_excludes, path))
 
1359         object = get_reference(cb->all_revs, path, oid, cb->all_flags);
 
1360         add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
 
1361         add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
 
1365 static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
 
1368         cb->all_revs = revs;
 
1369         cb->all_flags = flags;
 
1370         revs->rev_input_given = 1;
 
1374 void clear_ref_exclusion(struct string_list **ref_excludes_p)
 
1376         if (*ref_excludes_p) {
 
1377                 string_list_clear(*ref_excludes_p, 0);
 
1378                 free(*ref_excludes_p);
 
1380         *ref_excludes_p = NULL;
 
1383 void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
 
1385         if (!*ref_excludes_p) {
 
1386                 *ref_excludes_p = xcalloc(1, sizeof(**ref_excludes_p));
 
1387                 (*ref_excludes_p)->strdup_strings = 1;
 
1389         string_list_append(*ref_excludes_p, exclude);
 
1392 static void handle_refs(struct ref_store *refs,
 
1393                         struct rev_info *revs, unsigned flags,
 
1394                         int (*for_each)(struct ref_store *, each_ref_fn, void *))
 
1396         struct all_refs_cb cb;
 
1399                 /* this could happen with uninitialized submodules */
 
1403         init_all_refs_cb(&cb, revs, flags);
 
1404         for_each(refs, handle_one_ref, &cb);
 
1407 static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
 
1409         struct all_refs_cb *cb = cb_data;
 
1410         if (!is_null_oid(oid)) {
 
1411                 struct object *o = parse_object(cb->all_revs->repo, oid);
 
1413                         o->flags |= cb->all_flags;
 
1414                         /* ??? CMDLINEFLAGS ??? */
 
1415                         add_pending_object(cb->all_revs, o, "");
 
1417                 else if (!cb->warned_bad_reflog) {
 
1418                         warning("reflog of '%s' references pruned commits",
 
1419                                 cb->name_for_errormsg);
 
1420                         cb->warned_bad_reflog = 1;
 
1425 static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
 
1426                 const char *email, timestamp_t timestamp, int tz,
 
1427                 const char *message, void *cb_data)
 
1429         handle_one_reflog_commit(ooid, cb_data);
 
1430         handle_one_reflog_commit(noid, cb_data);
 
1434 static int handle_one_reflog(const char *refname_in_wt,
 
1435                              const struct object_id *oid,
 
1436                              int flag, void *cb_data)
 
1438         struct all_refs_cb *cb = cb_data;
 
1439         struct strbuf refname = STRBUF_INIT;
 
1441         cb->warned_bad_reflog = 0;
 
1442         strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
 
1443         cb->name_for_errormsg = refname.buf;
 
1444         refs_for_each_reflog_ent(get_main_ref_store(the_repository),
 
1446                                  handle_one_reflog_ent, cb_data);
 
1447         strbuf_release(&refname);
 
1451 static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
 
1453         struct worktree **worktrees, **p;
 
1455         worktrees = get_worktrees(0);
 
1456         for (p = worktrees; *p; p++) {
 
1457                 struct worktree *wt = *p;
 
1463                 refs_for_each_reflog(get_worktree_ref_store(wt),
 
1467         free_worktrees(worktrees);
 
1470 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
 
1472         struct all_refs_cb cb;
 
1475         cb.all_flags = flags;
 
1477         for_each_reflog(handle_one_reflog, &cb);
 
1479         if (!revs->single_worktree)
 
1480                 add_other_reflogs_to_pending(&cb);
 
1483 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
 
1484                            struct strbuf *path, unsigned int flags)
 
1486         size_t baselen = path->len;
 
1489         if (it->entry_count >= 0) {
 
1490                 struct tree *tree = lookup_tree(revs->repo, &it->oid);
 
1491                 tree->object.flags |= flags;
 
1492                 add_pending_object_with_path(revs, &tree->object, "",
 
1496         for (i = 0; i < it->subtree_nr; i++) {
 
1497                 struct cache_tree_sub *sub = it->down[i];
 
1498                 strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
 
1499                 add_cache_tree(sub->cache_tree, revs, path, flags);
 
1500                 strbuf_setlen(path, baselen);
 
1505 static void do_add_index_objects_to_pending(struct rev_info *revs,
 
1506                                             struct index_state *istate,
 
1511         for (i = 0; i < istate->cache_nr; i++) {
 
1512                 struct cache_entry *ce = istate->cache[i];
 
1515                 if (S_ISGITLINK(ce->ce_mode))
 
1518                 blob = lookup_blob(revs->repo, &ce->oid);
 
1520                         die("unable to add index blob to traversal");
 
1521                 blob->object.flags |= flags;
 
1522                 add_pending_object_with_path(revs, &blob->object, "",
 
1523                                              ce->ce_mode, ce->name);
 
1526         if (istate->cache_tree) {
 
1527                 struct strbuf path = STRBUF_INIT;
 
1528                 add_cache_tree(istate->cache_tree, revs, &path, flags);
 
1529                 strbuf_release(&path);
 
1533 void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
 
1535         struct worktree **worktrees, **p;
 
1537         repo_read_index(revs->repo);
 
1538         do_add_index_objects_to_pending(revs, revs->repo->index, flags);
 
1540         if (revs->single_worktree)
 
1543         worktrees = get_worktrees(0);
 
1544         for (p = worktrees; *p; p++) {
 
1545                 struct worktree *wt = *p;
 
1546                 struct index_state istate = { NULL };
 
1549                         continue; /* current index already taken care of */
 
1551                 if (read_index_from(&istate,
 
1552                                     worktree_git_path(wt, "index"),
 
1553                                     get_worktree_git_dir(wt)) > 0)
 
1554                         do_add_index_objects_to_pending(revs, &istate, flags);
 
1555                 discard_index(&istate);
 
1557         free_worktrees(worktrees);
 
1560 struct add_alternate_refs_data {
 
1561         struct rev_info *revs;
 
1565 static void add_one_alternate_ref(const struct object_id *oid,
 
1568         const char *name = ".alternate";
 
1569         struct add_alternate_refs_data *data = vdata;
 
1572         obj = get_reference(data->revs, name, oid, data->flags);
 
1573         add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
 
1574         add_pending_object(data->revs, obj, name);
 
1577 static void add_alternate_refs_to_pending(struct rev_info *revs,
 
1580         struct add_alternate_refs_data data;
 
1583         for_each_alternate_ref(add_one_alternate_ref, &data);
 
1586 static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
 
1589         struct object_id oid;
 
1591         struct commit *commit;
 
1592         struct commit_list *parents;
 
1594         const char *arg = arg_;
 
1597                 flags ^= UNINTERESTING | BOTTOM;
 
1600         if (get_oid_committish(arg, &oid))
 
1603                 it = get_reference(revs, arg, &oid, 0);
 
1604                 if (!it && revs->ignore_missing)
 
1606                 if (it->type != OBJ_TAG)
 
1608                 if (!((struct tag*)it)->tagged)
 
1610                 oidcpy(&oid, &((struct tag*)it)->tagged->oid);
 
1612         if (it->type != OBJ_COMMIT)
 
1614         commit = (struct commit *)it;
 
1615         if (exclude_parent &&
 
1616             exclude_parent > commit_list_count(commit->parents))
 
1618         for (parents = commit->parents, parent_number = 1;
 
1620              parents = parents->next, parent_number++) {
 
1621                 if (exclude_parent && parent_number != exclude_parent)
 
1624                 it = &parents->item->object;
 
1626                 add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
 
1627                 add_pending_object(revs, it, arg);
 
1632 void repo_init_revisions(struct repository *r,
 
1633                          struct rev_info *revs,
 
1636         memset(revs, 0, sizeof(*revs));
 
1639         revs->abbrev = DEFAULT_ABBREV;
 
1640         revs->ignore_merges = 1;
 
1641         revs->simplify_history = 1;
 
1642         revs->pruning.repo = r;
 
1643         revs->pruning.flags.recursive = 1;
 
1644         revs->pruning.flags.quick = 1;
 
1645         revs->pruning.add_remove = file_add_remove;
 
1646         revs->pruning.change = file_change;
 
1647         revs->pruning.change_fn_data = revs;
 
1648         revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
 
1650         revs->prefix = prefix;
 
1653         revs->skip_count = -1;
 
1654         revs->max_count = -1;
 
1655         revs->max_parents = -1;
 
1656         revs->expand_tabs_in_log = -1;
 
1658         revs->commit_format = CMIT_FMT_DEFAULT;
 
1659         revs->expand_tabs_in_log_default = 8;
 
1661         init_grep_defaults(revs->repo);
 
1662         grep_init(&revs->grep_filter, revs->repo, prefix);
 
1663         revs->grep_filter.status_only = 1;
 
1665         repo_diff_setup(revs->repo, &revs->diffopt);
 
1666         if (prefix && !revs->diffopt.prefix) {
 
1667                 revs->diffopt.prefix = prefix;
 
1668                 revs->diffopt.prefix_length = strlen(prefix);
 
1671         init_display_notes(&revs->notes_opt);
 
1674 static void add_pending_commit_list(struct rev_info *revs,
 
1675                                     struct commit_list *commit_list,
 
1678         while (commit_list) {
 
1679                 struct object *object = &commit_list->item->object;
 
1680                 object->flags |= flags;
 
1681                 add_pending_object(revs, object, oid_to_hex(&object->oid));
 
1682                 commit_list = commit_list->next;
 
1686 static void prepare_show_merge(struct rev_info *revs)
 
1688         struct commit_list *bases;
 
1689         struct commit *head, *other;
 
1690         struct object_id oid;
 
1691         const char **prune = NULL;
 
1692         int i, prune_num = 1; /* counting terminating NULL */
 
1693         struct index_state *istate = revs->repo->index;
 
1695         if (get_oid("HEAD", &oid))
 
1696                 die("--merge without HEAD?");
 
1697         head = lookup_commit_or_die(&oid, "HEAD");
 
1698         if (get_oid("MERGE_HEAD", &oid))
 
1699                 die("--merge without MERGE_HEAD?");
 
1700         other = lookup_commit_or_die(&oid, "MERGE_HEAD");
 
1701         add_pending_object(revs, &head->object, "HEAD");
 
1702         add_pending_object(revs, &other->object, "MERGE_HEAD");
 
1703         bases = get_merge_bases(head, other);
 
1704         add_rev_cmdline_list(revs, bases, REV_CMD_MERGE_BASE, UNINTERESTING | BOTTOM);
 
1705         add_pending_commit_list(revs, bases, UNINTERESTING | BOTTOM);
 
1706         free_commit_list(bases);
 
1707         head->object.flags |= SYMMETRIC_LEFT;
 
1709         if (!istate->cache_nr)
 
1710                 repo_read_index(revs->repo);
 
1711         for (i = 0; i < istate->cache_nr; i++) {
 
1712                 const struct cache_entry *ce = istate->cache[i];
 
1715                 if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
 
1717                         REALLOC_ARRAY(prune, prune_num);
 
1718                         prune[prune_num-2] = ce->name;
 
1719                         prune[prune_num-1] = NULL;
 
1721                 while ((i+1 < istate->cache_nr) &&
 
1722                        ce_same_name(ce, istate->cache[i+1]))
 
1725         clear_pathspec(&revs->prune_data);
 
1726         parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
 
1727                        PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
 
1731 static int dotdot_missing(const char *arg, char *dotdot,
 
1732                           struct rev_info *revs, int symmetric)
 
1734         if (revs->ignore_missing)
 
1736         /* de-munge so we report the full argument */
 
1739             ? "Invalid symmetric difference expression %s"
 
1740             : "Invalid revision range %s", arg);
 
1743 static int handle_dotdot_1(const char *arg, char *dotdot,
 
1744                            struct rev_info *revs, int flags,
 
1745                            int cant_be_filename,
 
1746                            struct object_context *a_oc,
 
1747                            struct object_context *b_oc)
 
1749         const char *a_name, *b_name;
 
1750         struct object_id a_oid, b_oid;
 
1751         struct object *a_obj, *b_obj;
 
1752         unsigned int a_flags, b_flags;
 
1754         unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
 
1755         unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
 
1761         b_name = dotdot + 2;
 
1762         if (*b_name == '.') {
 
1769         if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
 
1770             get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
 
1773         if (!cant_be_filename) {
 
1775                 verify_non_filename(revs->prefix, arg);
 
1779         a_obj = parse_object(revs->repo, &a_oid);
 
1780         b_obj = parse_object(revs->repo, &b_oid);
 
1781         if (!a_obj || !b_obj)
 
1782                 return dotdot_missing(arg, dotdot, revs, symmetric);
 
1787                 a_flags = flags_exclude;
 
1789                 /* A...B -- find merge bases between the two */
 
1790                 struct commit *a, *b;
 
1791                 struct commit_list *exclude;
 
1793                 a = lookup_commit_reference(revs->repo, &a_obj->oid);
 
1794                 b = lookup_commit_reference(revs->repo, &b_obj->oid);
 
1796                         return dotdot_missing(arg, dotdot, revs, symmetric);
 
1798                 exclude = get_merge_bases(a, b);
 
1799                 add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
 
1801                 add_pending_commit_list(revs, exclude, flags_exclude);
 
1802                 free_commit_list(exclude);
 
1805                 a_flags = flags | SYMMETRIC_LEFT;
 
1808         a_obj->flags |= a_flags;
 
1809         b_obj->flags |= b_flags;
 
1810         add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
 
1811         add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
 
1812         add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
 
1813         add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
 
1817 static int handle_dotdot(const char *arg,
 
1818                          struct rev_info *revs, int flags,
 
1819                          int cant_be_filename)
 
1821         struct object_context a_oc, b_oc;
 
1822         char *dotdot = strstr(arg, "..");
 
1828         memset(&a_oc, 0, sizeof(a_oc));
 
1829         memset(&b_oc, 0, sizeof(b_oc));
 
1832         ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
 
1842 int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
 
1844         struct object_context oc;
 
1846         struct object *object;
 
1847         struct object_id oid;
 
1849         const char *arg = arg_;
 
1850         int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
 
1851         unsigned get_sha1_flags = GET_OID_RECORD_PATH;
 
1853         flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
 
1855         if (!cant_be_filename && !strcmp(arg, "..")) {
 
1857                  * Just ".."?  That is not a range but the
 
1858                  * pathspec for the parent directory.
 
1863         if (!handle_dotdot(arg, revs, flags, revarg_opt))
 
1866         mark = strstr(arg, "^@");
 
1867         if (mark && !mark[2]) {
 
1869                 if (add_parents_only(revs, arg, flags, 0))
 
1873         mark = strstr(arg, "^!");
 
1874         if (mark && !mark[2]) {
 
1876                 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
 
1879         mark = strstr(arg, "^-");
 
1881                 int exclude_parent = 1;
 
1885                         exclude_parent = strtoul(mark + 2, &end, 10);
 
1886                         if (*end != '\0' || !exclude_parent)
 
1891                 if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
 
1897                 local_flags = UNINTERESTING | BOTTOM;
 
1901         if (revarg_opt & REVARG_COMMITTISH)
 
1902                 get_sha1_flags |= GET_OID_COMMITTISH;
 
1904         if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
 
1905                 return revs->ignore_missing ? 0 : -1;
 
1906         if (!cant_be_filename)
 
1907                 verify_non_filename(revs->prefix, arg);
 
1908         object = get_reference(revs, arg, &oid, flags ^ local_flags);
 
1910                 return revs->ignore_missing ? 0 : -1;
 
1911         add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
 
1912         add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
 
1917 static void read_pathspec_from_stdin(struct strbuf *sb,
 
1918                                      struct argv_array *prune)
 
1920         while (strbuf_getline(sb, stdin) != EOF)
 
1921                 argv_array_push(prune, sb->buf);
 
1924 static void read_revisions_from_stdin(struct rev_info *revs,
 
1925                                       struct argv_array *prune)
 
1928         int seen_dashdash = 0;
 
1931         save_warning = warn_on_object_refname_ambiguity;
 
1932         warn_on_object_refname_ambiguity = 0;
 
1934         strbuf_init(&sb, 1000);
 
1935         while (strbuf_getline(&sb, stdin) != EOF) {
 
1939                 if (sb.buf[0] == '-') {
 
1940                         if (len == 2 && sb.buf[1] == '-') {
 
1944                         die("options not supported in --stdin mode");
 
1946                 if (handle_revision_arg(sb.buf, revs, 0,
 
1947                                         REVARG_CANNOT_BE_FILENAME))
 
1948                         die("bad revision '%s'", sb.buf);
 
1951                 read_pathspec_from_stdin(&sb, prune);
 
1953         strbuf_release(&sb);
 
1954         warn_on_object_refname_ambiguity = save_warning;
 
1957 static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
 
1959         append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
 
1962 static void add_header_grep(struct rev_info *revs, enum grep_header_field field, const char *pattern)
 
1964         append_header_grep_pattern(&revs->grep_filter, field, pattern);
 
1967 static void add_message_grep(struct rev_info *revs, const char *pattern)
 
1969         add_grep(revs, pattern, GREP_PATTERN_BODY);
 
1972 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
 
1973                                int *unkc, const char **unkv,
 
1974                                const struct setup_revision_opt* opt)
 
1976         const char *arg = argv[0];
 
1979         const unsigned hexsz = the_hash_algo->hexsz;
 
1981         /* pseudo revision arguments */
 
1982         if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
 
1983             !strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
 
1984             !strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
 
1985             !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
 
1986             !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
 
1987             !strcmp(arg, "--indexed-objects") ||
 
1988             !strcmp(arg, "--alternate-refs") ||
 
1989             starts_with(arg, "--exclude=") ||
 
1990             starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
 
1991             starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
 
1993                 unkv[(*unkc)++] = arg;
 
1997         if ((argcount = parse_long_opt("max-count", argv, &optarg))) {
 
1998                 revs->max_count = atoi(optarg);
 
2001         } else if ((argcount = parse_long_opt("skip", argv, &optarg))) {
 
2002                 revs->skip_count = atoi(optarg);
 
2004         } else if ((*arg == '-') && isdigit(arg[1])) {
 
2005                 /* accept -<digit>, like traditional "head" */
 
2006                 if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
 
2007                     revs->max_count < 0)
 
2008                         die("'%s': not a non-negative integer", arg + 1);
 
2010         } else if (!strcmp(arg, "-n")) {
 
2012                         return error("-n requires an argument");
 
2013                 revs->max_count = atoi(argv[1]);
 
2016         } else if (skip_prefix(arg, "-n", &optarg)) {
 
2017                 revs->max_count = atoi(optarg);
 
2019         } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
 
2020                 revs->max_age = atoi(optarg);
 
2022         } else if ((argcount = parse_long_opt("since", argv, &optarg))) {
 
2023                 revs->max_age = approxidate(optarg);
 
2025         } else if ((argcount = parse_long_opt("after", argv, &optarg))) {
 
2026                 revs->max_age = approxidate(optarg);
 
2028         } else if ((argcount = parse_long_opt("min-age", argv, &optarg))) {
 
2029                 revs->min_age = atoi(optarg);
 
2031         } else if ((argcount = parse_long_opt("before", argv, &optarg))) {
 
2032                 revs->min_age = approxidate(optarg);
 
2034         } else if ((argcount = parse_long_opt("until", argv, &optarg))) {
 
2035                 revs->min_age = approxidate(optarg);
 
2037         } else if (!strcmp(arg, "--first-parent")) {
 
2038                 revs->first_parent_only = 1;
 
2039         } else if (!strcmp(arg, "--ancestry-path")) {
 
2040                 revs->ancestry_path = 1;
 
2041                 revs->simplify_history = 0;
 
2043         } else if (!strcmp(arg, "-g") || !strcmp(arg, "--walk-reflogs")) {
 
2044                 init_reflog_walk(&revs->reflog_info);
 
2045         } else if (!strcmp(arg, "--default")) {
 
2047                         return error("bad --default argument");
 
2048                 revs->def = argv[1];
 
2050         } else if (!strcmp(arg, "--merge")) {
 
2051                 revs->show_merge = 1;
 
2052         } else if (!strcmp(arg, "--topo-order")) {
 
2053                 revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
 
2054                 revs->topo_order = 1;
 
2055         } else if (!strcmp(arg, "--simplify-merges")) {
 
2056                 revs->simplify_merges = 1;
 
2057                 revs->topo_order = 1;
 
2058                 revs->rewrite_parents = 1;
 
2059                 revs->simplify_history = 0;
 
2061         } else if (!strcmp(arg, "--simplify-by-decoration")) {
 
2062                 revs->simplify_merges = 1;
 
2063                 revs->topo_order = 1;
 
2064                 revs->rewrite_parents = 1;
 
2065                 revs->simplify_history = 0;
 
2066                 revs->simplify_by_decoration = 1;
 
2069         } else if (!strcmp(arg, "--date-order")) {
 
2070                 revs->sort_order = REV_SORT_BY_COMMIT_DATE;
 
2071                 revs->topo_order = 1;
 
2072         } else if (!strcmp(arg, "--author-date-order")) {
 
2073                 revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
 
2074                 revs->topo_order = 1;
 
2075         } else if (!strcmp(arg, "--early-output")) {
 
2076                 revs->early_output = 100;
 
2077                 revs->topo_order = 1;
 
2078         } else if (skip_prefix(arg, "--early-output=", &optarg)) {
 
2079                 if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
 
2080                         die("'%s': not a non-negative integer", optarg);
 
2081                 revs->topo_order = 1;
 
2082         } else if (!strcmp(arg, "--parents")) {
 
2083                 revs->rewrite_parents = 1;
 
2084                 revs->print_parents = 1;
 
2085         } else if (!strcmp(arg, "--dense")) {
 
2087         } else if (!strcmp(arg, "--sparse")) {
 
2089         } else if (!strcmp(arg, "--in-commit-order")) {
 
2090                 revs->tree_blobs_in_commit_order = 1;
 
2091         } else if (!strcmp(arg, "--remove-empty")) {
 
2092                 revs->remove_empty_trees = 1;
 
2093         } else if (!strcmp(arg, "--merges")) {
 
2094                 revs->min_parents = 2;
 
2095         } else if (!strcmp(arg, "--no-merges")) {
 
2096                 revs->max_parents = 1;
 
2097         } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
 
2098                 revs->min_parents = atoi(optarg);
 
2099         } else if (!strcmp(arg, "--no-min-parents")) {
 
2100                 revs->min_parents = 0;
 
2101         } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
 
2102                 revs->max_parents = atoi(optarg);
 
2103         } else if (!strcmp(arg, "--no-max-parents")) {
 
2104                 revs->max_parents = -1;
 
2105         } else if (!strcmp(arg, "--boundary")) {
 
2107         } else if (!strcmp(arg, "--left-right")) {
 
2108                 revs->left_right = 1;
 
2109         } else if (!strcmp(arg, "--left-only")) {
 
2110                 if (revs->right_only)
 
2111                         die("--left-only is incompatible with --right-only"
 
2113                 revs->left_only = 1;
 
2114         } else if (!strcmp(arg, "--right-only")) {
 
2115                 if (revs->left_only)
 
2116                         die("--right-only is incompatible with --left-only");
 
2117                 revs->right_only = 1;
 
2118         } else if (!strcmp(arg, "--cherry")) {
 
2119                 if (revs->left_only)
 
2120                         die("--cherry is incompatible with --left-only");
 
2121                 revs->cherry_mark = 1;
 
2122                 revs->right_only = 1;
 
2123                 revs->max_parents = 1;
 
2125         } else if (!strcmp(arg, "--count")) {
 
2127         } else if (!strcmp(arg, "--cherry-mark")) {
 
2128                 if (revs->cherry_pick)
 
2129                         die("--cherry-mark is incompatible with --cherry-pick");
 
2130                 revs->cherry_mark = 1;
 
2131                 revs->limited = 1; /* needs limit_list() */
 
2132         } else if (!strcmp(arg, "--cherry-pick")) {
 
2133                 if (revs->cherry_mark)
 
2134                         die("--cherry-pick is incompatible with --cherry-mark");
 
2135                 revs->cherry_pick = 1;
 
2137         } else if (!strcmp(arg, "--objects")) {
 
2138                 revs->tag_objects = 1;
 
2139                 revs->tree_objects = 1;
 
2140                 revs->blob_objects = 1;
 
2141         } else if (!strcmp(arg, "--objects-edge")) {
 
2142                 revs->tag_objects = 1;
 
2143                 revs->tree_objects = 1;
 
2144                 revs->blob_objects = 1;
 
2145                 revs->edge_hint = 1;
 
2146         } else if (!strcmp(arg, "--objects-edge-aggressive")) {
 
2147                 revs->tag_objects = 1;
 
2148                 revs->tree_objects = 1;
 
2149                 revs->blob_objects = 1;
 
2150                 revs->edge_hint = 1;
 
2151                 revs->edge_hint_aggressive = 1;
 
2152         } else if (!strcmp(arg, "--verify-objects")) {
 
2153                 revs->tag_objects = 1;
 
2154                 revs->tree_objects = 1;
 
2155                 revs->blob_objects = 1;
 
2156                 revs->verify_objects = 1;
 
2157         } else if (!strcmp(arg, "--unpacked")) {
 
2159         } else if (starts_with(arg, "--unpacked=")) {
 
2160                 die("--unpacked=<packfile> no longer supported.");
 
2161         } else if (!strcmp(arg, "-r")) {
 
2163                 revs->diffopt.flags.recursive = 1;
 
2164         } else if (!strcmp(arg, "-t")) {
 
2166                 revs->diffopt.flags.recursive = 1;
 
2167                 revs->diffopt.flags.tree_in_recursive = 1;
 
2168         } else if (!strcmp(arg, "-m")) {
 
2169                 revs->ignore_merges = 0;
 
2170         } else if (!strcmp(arg, "-c")) {
 
2172                 revs->dense_combined_merges = 0;
 
2173                 revs->combine_merges = 1;
 
2174         } else if (!strcmp(arg, "--combined-all-paths")) {
 
2176                 revs->combined_all_paths = 1;
 
2177         } else if (!strcmp(arg, "--cc")) {
 
2179                 revs->dense_combined_merges = 1;
 
2180                 revs->combine_merges = 1;
 
2181         } else if (!strcmp(arg, "-v")) {
 
2182                 revs->verbose_header = 1;
 
2183         } else if (!strcmp(arg, "--pretty")) {
 
2184                 revs->verbose_header = 1;
 
2185                 revs->pretty_given = 1;
 
2186                 get_commit_format(NULL, revs);
 
2187         } else if (skip_prefix(arg, "--pretty=", &optarg) ||
 
2188                    skip_prefix(arg, "--format=", &optarg)) {
 
2190                  * Detached form ("--pretty X" as opposed to "--pretty=X")
 
2191                  * not allowed, since the argument is optional.
 
2193                 revs->verbose_header = 1;
 
2194                 revs->pretty_given = 1;
 
2195                 get_commit_format(optarg, revs);
 
2196         } else if (!strcmp(arg, "--expand-tabs")) {
 
2197                 revs->expand_tabs_in_log = 8;
 
2198         } else if (!strcmp(arg, "--no-expand-tabs")) {
 
2199                 revs->expand_tabs_in_log = 0;
 
2200         } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
 
2202                 if (strtol_i(arg, 10, &val) < 0 || val < 0)
 
2203                         die("'%s': not a non-negative integer", arg);
 
2204                 revs->expand_tabs_in_log = val;
 
2205         } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
 
2206                 enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
 
2207                 revs->show_notes_given = 1;
 
2208         } else if (!strcmp(arg, "--show-signature")) {
 
2209                 revs->show_signature = 1;
 
2210         } else if (!strcmp(arg, "--no-show-signature")) {
 
2211                 revs->show_signature = 0;
 
2212         } else if (!strcmp(arg, "--show-linear-break")) {
 
2213                 revs->break_bar = "                    ..........";
 
2214                 revs->track_linear = 1;
 
2215                 revs->track_first_time = 1;
 
2216         } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
 
2217                 revs->break_bar = xstrdup(optarg);
 
2218                 revs->track_linear = 1;
 
2219                 revs->track_first_time = 1;
 
2220         } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
 
2221                    skip_prefix(arg, "--notes=", &optarg)) {
 
2222                 if (starts_with(arg, "--show-notes=") &&
 
2223                     revs->notes_opt.use_default_notes < 0)
 
2224                         revs->notes_opt.use_default_notes = 1;
 
2225                 enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
 
2226                 revs->show_notes_given = 1;
 
2227         } else if (!strcmp(arg, "--no-notes")) {
 
2228                 disable_display_notes(&revs->notes_opt, &revs->show_notes);
 
2229                 revs->show_notes_given = 1;
 
2230         } else if (!strcmp(arg, "--standard-notes")) {
 
2231                 revs->show_notes_given = 1;
 
2232                 revs->notes_opt.use_default_notes = 1;
 
2233         } else if (!strcmp(arg, "--no-standard-notes")) {
 
2234                 revs->notes_opt.use_default_notes = 0;
 
2235         } else if (!strcmp(arg, "--oneline")) {
 
2236                 revs->verbose_header = 1;
 
2237                 get_commit_format("oneline", revs);
 
2238                 revs->pretty_given = 1;
 
2239                 revs->abbrev_commit = 1;
 
2240         } else if (!strcmp(arg, "--graph")) {
 
2241                 revs->topo_order = 1;
 
2242                 revs->rewrite_parents = 1;
 
2243                 revs->graph = graph_init(revs);
 
2244         } else if (!strcmp(arg, "--root")) {
 
2245                 revs->show_root_diff = 1;
 
2246         } else if (!strcmp(arg, "--no-commit-id")) {
 
2247                 revs->no_commit_id = 1;
 
2248         } else if (!strcmp(arg, "--always")) {
 
2249                 revs->always_show_header = 1;
 
2250         } else if (!strcmp(arg, "--no-abbrev")) {
 
2252         } else if (!strcmp(arg, "--abbrev")) {
 
2253                 revs->abbrev = DEFAULT_ABBREV;
 
2254         } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
 
2255                 revs->abbrev = strtoul(optarg, NULL, 10);
 
2256                 if (revs->abbrev < MINIMUM_ABBREV)
 
2257                         revs->abbrev = MINIMUM_ABBREV;
 
2258                 else if (revs->abbrev > hexsz)
 
2259                         revs->abbrev = hexsz;
 
2260         } else if (!strcmp(arg, "--abbrev-commit")) {
 
2261                 revs->abbrev_commit = 1;
 
2262                 revs->abbrev_commit_given = 1;
 
2263         } else if (!strcmp(arg, "--no-abbrev-commit")) {
 
2264                 revs->abbrev_commit = 0;
 
2265         } else if (!strcmp(arg, "--full-diff")) {
 
2267                 revs->full_diff = 1;
 
2268         } else if (!strcmp(arg, "--full-history")) {
 
2269                 revs->simplify_history = 0;
 
2270         } else if (!strcmp(arg, "--relative-date")) {
 
2271                 revs->date_mode.type = DATE_RELATIVE;
 
2272                 revs->date_mode_explicit = 1;
 
2273         } else if ((argcount = parse_long_opt("date", argv, &optarg))) {
 
2274                 parse_date_format(optarg, &revs->date_mode);
 
2275                 revs->date_mode_explicit = 1;
 
2277         } else if (!strcmp(arg, "--log-size")) {
 
2278                 revs->show_log_size = 1;
 
2281          * Grepping the commit log
 
2283         else if ((argcount = parse_long_opt("author", argv, &optarg))) {
 
2284                 add_header_grep(revs, GREP_HEADER_AUTHOR, optarg);
 
2286         } else if ((argcount = parse_long_opt("committer", argv, &optarg))) {
 
2287                 add_header_grep(revs, GREP_HEADER_COMMITTER, optarg);
 
2289         } else if ((argcount = parse_long_opt("grep-reflog", argv, &optarg))) {
 
2290                 add_header_grep(revs, GREP_HEADER_REFLOG, optarg);
 
2292         } else if ((argcount = parse_long_opt("grep", argv, &optarg))) {
 
2293                 add_message_grep(revs, optarg);
 
2295         } else if (!strcmp(arg, "--grep-debug")) {
 
2296                 revs->grep_filter.debug = 1;
 
2297         } else if (!strcmp(arg, "--basic-regexp")) {
 
2298                 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
 
2299         } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
 
2300                 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
 
2301         } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
 
2302                 revs->grep_filter.ignore_case = 1;
 
2303                 revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
 
2304         } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
 
2305                 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
 
2306         } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
 
2307                 revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
 
2308         } else if (!strcmp(arg, "--all-match")) {
 
2309                 revs->grep_filter.all_match = 1;
 
2310         } else if (!strcmp(arg, "--invert-grep")) {
 
2311                 revs->invert_grep = 1;
 
2312         } else if ((argcount = parse_long_opt("encoding", argv, &optarg))) {
 
2313                 if (strcmp(optarg, "none"))
 
2314                         git_log_output_encoding = xstrdup(optarg);
 
2316                         git_log_output_encoding = "";
 
2318         } else if (!strcmp(arg, "--reverse")) {
 
2320         } else if (!strcmp(arg, "--children")) {
 
2321                 revs->children.name = "children";
 
2323         } else if (!strcmp(arg, "--ignore-missing")) {
 
2324                 revs->ignore_missing = 1;
 
2325         } else if (opt && opt->allow_exclude_promisor_objects &&
 
2326                    !strcmp(arg, "--exclude-promisor-objects")) {
 
2327                 if (fetch_if_missing)
 
2328                         BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
 
2329                 revs->exclude_promisor_objects = 1;
 
2331                 int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
 
2333                         unkv[(*unkc)++] = arg;
 
2336         if (revs->graph && revs->track_linear)
 
2337                 die("--show-linear-break and --graph are incompatible");
 
2342 void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
 
2343                         const struct option *options,
 
2344                         const char * const usagestr[])
 
2346         int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
 
2347                                     &ctx->cpidx, ctx->out, NULL);
 
2349                 error("unknown option `%s'", ctx->argv[0]);
 
2350                 usage_with_options(usagestr, options);
 
2356 static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
 
2357                                void *cb_data, const char *term)
 
2359         struct strbuf bisect_refs = STRBUF_INIT;
 
2361         strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
 
2362         status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
 
2363         strbuf_release(&bisect_refs);
 
2367 static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
 
2369         return for_each_bisect_ref(refs, fn, cb_data, term_bad);
 
2372 static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
 
2374         return for_each_bisect_ref(refs, fn, cb_data, term_good);
 
2377 static int handle_revision_pseudo_opt(const char *submodule,
 
2378                                 struct rev_info *revs,
 
2379                                 int argc, const char **argv, int *flags)
 
2381         const char *arg = argv[0];
 
2383         struct ref_store *refs;
 
2388                  * We need some something like get_submodule_worktrees()
 
2389                  * before we can go through all worktrees of a submodule,
 
2390                  * .e.g with adding all HEADs from --all, which is not
 
2391                  * supported right now, so stick to single worktree.
 
2393                 if (!revs->single_worktree)
 
2394                         BUG("--single-worktree cannot be used together with submodule");
 
2395                 refs = get_submodule_ref_store(submodule);
 
2397                 refs = get_main_ref_store(revs->repo);
 
2402          * Commands like "git shortlog" will not accept the options below
 
2403          * unless parse_revision_opt queues them (as opposed to erroring
 
2406          * When implementing your new pseudo-option, remember to
 
2407          * register it in the list at the top of handle_revision_opt.
 
2409         if (!strcmp(arg, "--all")) {
 
2410                 handle_refs(refs, revs, *flags, refs_for_each_ref);
 
2411                 handle_refs(refs, revs, *flags, refs_head_ref);
 
2412                 if (!revs->single_worktree) {
 
2413                         struct all_refs_cb cb;
 
2415                         init_all_refs_cb(&cb, revs, *flags);
 
2416                         other_head_refs(handle_one_ref, &cb);
 
2418                 clear_ref_exclusion(&revs->ref_excludes);
 
2419         } else if (!strcmp(arg, "--branches")) {
 
2420                 handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
 
2421                 clear_ref_exclusion(&revs->ref_excludes);
 
2422         } else if (!strcmp(arg, "--bisect")) {
 
2423                 read_bisect_terms(&term_bad, &term_good);
 
2424                 handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
 
2425                 handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
 
2426                             for_each_good_bisect_ref);
 
2428         } else if (!strcmp(arg, "--tags")) {
 
2429                 handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
 
2430                 clear_ref_exclusion(&revs->ref_excludes);
 
2431         } else if (!strcmp(arg, "--remotes")) {
 
2432                 handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
 
2433                 clear_ref_exclusion(&revs->ref_excludes);
 
2434         } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
 
2435                 struct all_refs_cb cb;
 
2436                 init_all_refs_cb(&cb, revs, *flags);
 
2437                 for_each_glob_ref(handle_one_ref, optarg, &cb);
 
2438                 clear_ref_exclusion(&revs->ref_excludes);
 
2440         } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
 
2441                 add_ref_exclusion(&revs->ref_excludes, optarg);
 
2443         } else if (skip_prefix(arg, "--branches=", &optarg)) {
 
2444                 struct all_refs_cb cb;
 
2445                 init_all_refs_cb(&cb, revs, *flags);
 
2446                 for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
 
2447                 clear_ref_exclusion(&revs->ref_excludes);
 
2448         } else if (skip_prefix(arg, "--tags=", &optarg)) {
 
2449                 struct all_refs_cb cb;
 
2450                 init_all_refs_cb(&cb, revs, *flags);
 
2451                 for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
 
2452                 clear_ref_exclusion(&revs->ref_excludes);
 
2453         } else if (skip_prefix(arg, "--remotes=", &optarg)) {
 
2454                 struct all_refs_cb cb;
 
2455                 init_all_refs_cb(&cb, revs, *flags);
 
2456                 for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
 
2457                 clear_ref_exclusion(&revs->ref_excludes);
 
2458         } else if (!strcmp(arg, "--reflog")) {
 
2459                 add_reflogs_to_pending(revs, *flags);
 
2460         } else if (!strcmp(arg, "--indexed-objects")) {
 
2461                 add_index_objects_to_pending(revs, *flags);
 
2462         } else if (!strcmp(arg, "--alternate-refs")) {
 
2463                 add_alternate_refs_to_pending(revs, *flags);
 
2464         } else if (!strcmp(arg, "--not")) {
 
2465                 *flags ^= UNINTERESTING | BOTTOM;
 
2466         } else if (!strcmp(arg, "--no-walk")) {
 
2467                 revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
 
2468         } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
 
2470                  * Detached form ("--no-walk X" as opposed to "--no-walk=X")
 
2471                  * not allowed, since the argument is optional.
 
2473                 if (!strcmp(optarg, "sorted"))
 
2474                         revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
 
2475                 else if (!strcmp(optarg, "unsorted"))
 
2476                         revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
 
2478                         return error("invalid argument to --no-walk");
 
2479         } else if (!strcmp(arg, "--do-walk")) {
 
2481         } else if (!strcmp(arg, "--single-worktree")) {
 
2482                 revs->single_worktree = 1;
 
2490 static void NORETURN diagnose_missing_default(const char *def)
 
2493         const char *refname;
 
2495         refname = resolve_ref_unsafe(def, 0, NULL, &flags);
 
2496         if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
 
2497                 die(_("your current branch appears to be broken"));
 
2499         skip_prefix(refname, "refs/heads/", &refname);
 
2500         die(_("your current branch '%s' does not have any commits yet"),
 
2505  * Parse revision information, filling in the "rev_info" structure,
 
2506  * and removing the used arguments from the argument list.
 
2508  * Returns the number of arguments left that weren't recognized
 
2509  * (which are also moved to the head of the argument list)
 
2511 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
 
2513         int i, flags, left, seen_dashdash, got_rev_arg = 0, revarg_opt;
 
2514         struct argv_array prune_data = ARGV_ARRAY_INIT;
 
2515         const char *submodule = NULL;
 
2516         int seen_end_of_options = 0;
 
2519                 submodule = opt->submodule;
 
2521         /* First, search for "--" */
 
2522         if (opt && opt->assume_dashdash) {
 
2526                 for (i = 1; i < argc; i++) {
 
2527                         const char *arg = argv[i];
 
2528                         if (strcmp(arg, "--"))
 
2533                                 argv_array_pushv(&prune_data, argv + i + 1);
 
2539         /* Second, deal with arguments and options */
 
2541         revarg_opt = opt ? opt->revarg_opt : 0;
 
2543                 revarg_opt |= REVARG_CANNOT_BE_FILENAME;
 
2544         for (left = i = 1; i < argc; i++) {
 
2545                 const char *arg = argv[i];
 
2546                 if (!seen_end_of_options && *arg == '-') {
 
2549                         opts = handle_revision_pseudo_opt(submodule,
 
2550                                                 revs, argc - i, argv + i,
 
2557                         if (!strcmp(arg, "--stdin")) {
 
2558                                 if (revs->disable_stdin) {
 
2562                                 if (revs->read_from_stdin++)
 
2563                                         die("--stdin given twice?");
 
2564                                 read_revisions_from_stdin(revs, &prune_data);
 
2568                         if (!strcmp(arg, "--end-of-options")) {
 
2569                                 seen_end_of_options = 1;
 
2573                         opts = handle_revision_opt(revs, argc - i, argv + i,
 
2585                 if (handle_revision_arg(arg, revs, flags, revarg_opt)) {
 
2587                         if (seen_dashdash || *arg == '^')
 
2588                                 die("bad revision '%s'", arg);
 
2590                         /* If we didn't have a "--":
 
2591                          * (1) all filenames must exist;
 
2592                          * (2) all rev-args must not be interpretable
 
2593                          *     as a valid filename.
 
2594                          * but the latter we have checked in the main loop.
 
2596                         for (j = i; j < argc; j++)
 
2597                                 verify_filename(revs->prefix, argv[j], j == i);
 
2599                         argv_array_pushv(&prune_data, argv + i);
 
2606         if (prune_data.argc) {
 
2608                  * If we need to introduce the magic "a lone ':' means no
 
2609                  * pathspec whatsoever", here is the place to do so.
 
2611                  * if (prune_data.nr == 1 && !strcmp(prune_data[0], ":")) {
 
2612                  *      prune_data.nr = 0;
 
2613                  *      prune_data.alloc = 0;
 
2614                  *      free(prune_data.path);
 
2615                  *      prune_data.path = NULL;
 
2617                  *      terminate prune_data.alloc with NULL and
 
2618                  *      call init_pathspec() to set revs->prune_data here.
 
2621                 parse_pathspec(&revs->prune_data, 0, 0,
 
2622                                revs->prefix, prune_data.argv);
 
2624         argv_array_clear(&prune_data);
 
2626         if (revs->def == NULL)
 
2627                 revs->def = opt ? opt->def : NULL;
 
2628         if (opt && opt->tweak)
 
2629                 opt->tweak(revs, opt);
 
2630         if (revs->show_merge)
 
2631                 prepare_show_merge(revs);
 
2632         if (revs->def && !revs->pending.nr && !revs->rev_input_given && !got_rev_arg) {
 
2633                 struct object_id oid;
 
2634                 struct object *object;
 
2635                 struct object_context oc;
 
2636                 if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
 
2637                         diagnose_missing_default(revs->def);
 
2638                 object = get_reference(revs, revs->def, &oid, 0);
 
2639                 add_pending_object_with_mode(revs, object, revs->def, oc.mode);
 
2642         /* Did the user ask for any diff output? Run the diff! */
 
2643         if (revs->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT)
 
2646         /* Pickaxe, diff-filter and rename following need diffs */
 
2647         if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
 
2648             revs->diffopt.filter ||
 
2649             revs->diffopt.flags.follow_renames)
 
2652         if (revs->diffopt.objfind)
 
2653                 revs->simplify_history = 0;
 
2655         if (revs->topo_order && !generation_numbers_enabled(the_repository))
 
2658         if (revs->prune_data.nr) {
 
2659                 copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
 
2660                 /* Can't prune commits with rename following: the paths change.. */
 
2661                 if (!revs->diffopt.flags.follow_renames)
 
2663                 if (!revs->full_diff)
 
2664                         copy_pathspec(&revs->diffopt.pathspec,
 
2667         if (revs->combine_merges)
 
2668                 revs->ignore_merges = 0;
 
2669         if (revs->combined_all_paths && !revs->combine_merges)
 
2670                 die("--combined-all-paths makes no sense without -c or --cc");
 
2672         revs->diffopt.abbrev = revs->abbrev;
 
2674         if (revs->line_level_traverse) {
 
2676                 revs->topo_order = 1;
 
2679         diff_setup_done(&revs->diffopt);
 
2681         grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
 
2682                                  &revs->grep_filter);
 
2683         if (!is_encoding_utf8(get_log_output_encoding()))
 
2684                 revs->grep_filter.ignore_locale = 1;
 
2685         compile_grep_patterns(&revs->grep_filter);
 
2687         if (revs->reverse && revs->reflog_info)
 
2688                 die("cannot combine --reverse with --walk-reflogs");
 
2689         if (revs->reflog_info && revs->limited)
 
2690                 die("cannot combine --walk-reflogs with history-limiting options");
 
2691         if (revs->rewrite_parents && revs->children.name)
 
2692                 die("cannot combine --parents and --children");
 
2695          * Limitations on the graph functionality
 
2697         if (revs->reverse && revs->graph)
 
2698                 die("cannot combine --reverse with --graph");
 
2700         if (revs->reflog_info && revs->graph)
 
2701                 die("cannot combine --walk-reflogs with --graph");
 
2702         if (revs->no_walk && revs->graph)
 
2703                 die("cannot combine --no-walk with --graph");
 
2704         if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
 
2705                 die("cannot use --grep-reflog without --walk-reflogs");
 
2707         if (revs->first_parent_only && revs->bisect)
 
2708                 die(_("--first-parent is incompatible with --bisect"));
 
2710         if (revs->line_level_traverse &&
 
2711             (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
 
2712                 die(_("-L does not yet support diff formats besides -p and -s"));
 
2714         if (revs->expand_tabs_in_log < 0)
 
2715                 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
 
2720 static void add_child(struct rev_info *revs, struct commit *parent, struct commit *child)
 
2722         struct commit_list *l = xcalloc(1, sizeof(*l));
 
2725         l->next = add_decoration(&revs->children, &parent->object, l);
 
2728 static int remove_duplicate_parents(struct rev_info *revs, struct commit *commit)
 
2730         struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
 
2731         struct commit_list **pp, *p;
 
2732         int surviving_parents;
 
2734         /* Examine existing parents while marking ones we have seen... */
 
2735         pp = &commit->parents;
 
2736         surviving_parents = 0;
 
2737         while ((p = *pp) != NULL) {
 
2738                 struct commit *parent = p->item;
 
2739                 if (parent->object.flags & TMP_MARK) {
 
2742                                 compact_treesame(revs, commit, surviving_parents);
 
2745                 parent->object.flags |= TMP_MARK;
 
2746                 surviving_parents++;
 
2749         /* clear the temporary mark */
 
2750         for (p = commit->parents; p; p = p->next) {
 
2751                 p->item->object.flags &= ~TMP_MARK;
 
2753         /* no update_treesame() - removing duplicates can't affect TREESAME */
 
2754         return surviving_parents;
 
2757 struct merge_simplify_state {
 
2758         struct commit *simplified;
 
2761 static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs, struct commit *commit)
 
2763         struct merge_simplify_state *st;
 
2765         st = lookup_decoration(&revs->merge_simplification, &commit->object);
 
2767                 st = xcalloc(1, sizeof(*st));
 
2768                 add_decoration(&revs->merge_simplification, &commit->object, st);
 
2773 static int mark_redundant_parents(struct commit *commit)
 
2775         struct commit_list *h = reduce_heads(commit->parents);
 
2776         int i = 0, marked = 0;
 
2777         struct commit_list *po, *pn;
 
2779         /* Want these for sanity-checking only */
 
2780         int orig_cnt = commit_list_count(commit->parents);
 
2781         int cnt = commit_list_count(h);
 
2784          * Not ready to remove items yet, just mark them for now, based
 
2785          * on the output of reduce_heads(). reduce_heads outputs the reduced
 
2786          * set in its original order, so this isn't too hard.
 
2788         po = commit->parents;
 
2791                 if (pn && po->item == pn->item) {
 
2795                         po->item->object.flags |= TMP_MARK;
 
2801         if (i != cnt || cnt+marked != orig_cnt)
 
2802                 die("mark_redundant_parents %d %d %d %d", orig_cnt, cnt, i, marked);
 
2804         free_commit_list(h);
 
2809 static int mark_treesame_root_parents(struct commit *commit)
 
2811         struct commit_list *p;
 
2814         for (p = commit->parents; p; p = p->next) {
 
2815                 struct commit *parent = p->item;
 
2816                 if (!parent->parents && (parent->object.flags & TREESAME)) {
 
2817                         parent->object.flags |= TMP_MARK;
 
2826  * Awkward naming - this means one parent we are TREESAME to.
 
2827  * cf mark_treesame_root_parents: root parents that are TREESAME (to an
 
2828  * empty tree). Better name suggestions?
 
2830 static int leave_one_treesame_to_parent(struct rev_info *revs, struct commit *commit)
 
2832         struct treesame_state *ts = lookup_decoration(&revs->treesame, &commit->object);
 
2833         struct commit *unmarked = NULL, *marked = NULL;
 
2834         struct commit_list *p;
 
2837         for (p = commit->parents, n = 0; p; p = p->next, n++) {
 
2838                 if (ts->treesame[n]) {
 
2839                         if (p->item->object.flags & TMP_MARK) {
 
2852          * If we are TREESAME to a marked-for-deletion parent, but not to any
 
2853          * unmarked parents, unmark the first TREESAME parent. This is the
 
2854          * parent that the default simplify_history==1 scan would have followed,
 
2855          * and it doesn't make sense to omit that path when asking for a
 
2856          * simplified full history. Retaining it improves the chances of
 
2857          * understanding odd missed merges that took an old version of a file.
 
2861          *   I--------*X       A modified the file, but mainline merge X used
 
2862          *    \       /        "-s ours", so took the version from I. X is
 
2863          *     `-*A--'         TREESAME to I and !TREESAME to A.
 
2865          * Default log from X would produce "I". Without this check,
 
2866          * --full-history --simplify-merges would produce "I-A-X", showing
 
2867          * the merge commit X and that it changed A, but not making clear that
 
2868          * it had just taken the I version. With this check, the topology above
 
2871          * Note that it is possible that the simplification chooses a different
 
2872          * TREESAME parent from the default, in which case this test doesn't
 
2873          * activate, and we _do_ drop the default parent. Example:
 
2875          *   I------X         A modified the file, but it was reverted in B,
 
2876          *    \    /          meaning mainline merge X is TREESAME to both
 
2879          * Default log would produce "I" by following the first parent;
 
2880          * --full-history --simplify-merges will produce "I-A-B". But this is a
 
2881          * reasonable result - it presents a logical full history leading from
 
2882          * I to X, and X is not an important merge.
 
2884         if (!unmarked && marked) {
 
2885                 marked->object.flags &= ~TMP_MARK;
 
2892 static int remove_marked_parents(struct rev_info *revs, struct commit *commit)
 
2894         struct commit_list **pp, *p;
 
2895         int nth_parent, removed = 0;
 
2897         pp = &commit->parents;
 
2899         while ((p = *pp) != NULL) {
 
2900                 struct commit *parent = p->item;
 
2901                 if (parent->object.flags & TMP_MARK) {
 
2902                         parent->object.flags &= ~TMP_MARK;
 
2906                         compact_treesame(revs, commit, nth_parent);
 
2913         /* Removing parents can only increase TREESAMEness */
 
2914         if (removed && !(commit->object.flags & TREESAME))
 
2915                 update_treesame(revs, commit);
 
2920 static struct commit_list **simplify_one(struct rev_info *revs, struct commit *commit, struct commit_list **tail)
 
2922         struct commit_list *p;
 
2923         struct commit *parent;
 
2924         struct merge_simplify_state *st, *pst;
 
2927         st = locate_simplify_state(revs, commit);
 
2930          * Have we handled this one?
 
2936          * An UNINTERESTING commit simplifies to itself, so does a
 
2937          * root commit.  We do not rewrite parents of such commit
 
2940         if ((commit->object.flags & UNINTERESTING) || !commit->parents) {
 
2941                 st->simplified = commit;
 
2946          * Do we know what commit all of our parents that matter
 
2947          * should be rewritten to?  Otherwise we are not ready to
 
2948          * rewrite this one yet.
 
2950         for (cnt = 0, p = commit->parents; p; p = p->next) {
 
2951                 pst = locate_simplify_state(revs, p->item);
 
2952                 if (!pst->simplified) {
 
2953                         tail = &commit_list_insert(p->item, tail)->next;
 
2956                 if (revs->first_parent_only)
 
2960                 tail = &commit_list_insert(commit, tail)->next;
 
2965          * Rewrite our list of parents. Note that this cannot
 
2966          * affect our TREESAME flags in any way - a commit is
 
2967          * always TREESAME to its simplification.
 
2969         for (p = commit->parents; p; p = p->next) {
 
2970                 pst = locate_simplify_state(revs, p->item);
 
2971                 p->item = pst->simplified;
 
2972                 if (revs->first_parent_only)
 
2976         if (revs->first_parent_only)
 
2979                 cnt = remove_duplicate_parents(revs, commit);
 
2982          * It is possible that we are a merge and one side branch
 
2983          * does not have any commit that touches the given paths;
 
2984          * in such a case, the immediate parent from that branch
 
2985          * will be rewritten to be the merge base.
 
2987          *      o----X          X: the commit we are looking at;
 
2988          *     /    /           o: a commit that touches the paths;
 
2991          * Further, a merge of an independent branch that doesn't
 
2992          * touch the path will reduce to a treesame root parent:
 
2994          *  ----o----X          X: the commit we are looking at;
 
2995          *          /           o: a commit that touches the paths;
 
2996          *         r            r: a root commit not touching the paths
 
2998          * Detect and simplify both cases.
 
3001                 int marked = mark_redundant_parents(commit);
 
3002                 marked += mark_treesame_root_parents(commit);
 
3004                         marked -= leave_one_treesame_to_parent(revs, commit);
 
3006                         cnt = remove_marked_parents(revs, commit);
 
3010          * A commit simplifies to itself if it is a root, if it is
 
3011          * UNINTERESTING, if it touches the given paths, or if it is a
 
3012          * merge and its parents don't simplify to one relevant commit
 
3013          * (the first two cases are already handled at the beginning of
 
3016          * Otherwise, it simplifies to what its sole relevant parent
 
3020             (commit->object.flags & UNINTERESTING) ||
 
3021             !(commit->object.flags & TREESAME) ||
 
3022             (parent = one_relevant_parent(revs, commit->parents)) == NULL)
 
3023                 st->simplified = commit;
 
3025                 pst = locate_simplify_state(revs, parent);
 
3026                 st->simplified = pst->simplified;
 
3031 static void simplify_merges(struct rev_info *revs)
 
3033         struct commit_list *list, *next;
 
3034         struct commit_list *yet_to_do, **tail;
 
3035         struct commit *commit;
 
3040         /* feed the list reversed */
 
3042         for (list = revs->commits; list; list = next) {
 
3043                 commit = list->item;
 
3046                  * Do not free(list) here yet; the original list
 
3047                  * is used later in this function.
 
3049                 commit_list_insert(commit, &yet_to_do);
 
3056                         commit = pop_commit(&list);
 
3057                         tail = simplify_one(revs, commit, tail);
 
3061         /* clean up the result, removing the simplified ones */
 
3062         list = revs->commits;
 
3063         revs->commits = NULL;
 
3064         tail = &revs->commits;
 
3066                 struct merge_simplify_state *st;
 
3068                 commit = pop_commit(&list);
 
3069                 st = locate_simplify_state(revs, commit);
 
3070                 if (st->simplified == commit)
 
3071                         tail = &commit_list_insert(commit, tail)->next;
 
3075 static void set_children(struct rev_info *revs)
 
3077         struct commit_list *l;
 
3078         for (l = revs->commits; l; l = l->next) {
 
3079                 struct commit *commit = l->item;
 
3080                 struct commit_list *p;
 
3082                 for (p = commit->parents; p; p = p->next)
 
3083                         add_child(revs, p->item, commit);
 
3087 void reset_revision_walk(void)
 
3089         clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
 
3092 static int mark_uninteresting(const struct object_id *oid,
 
3093                               struct packed_git *pack,
 
3097         struct rev_info *revs = cb;
 
3098         struct object *o = parse_object(revs->repo, oid);
 
3099         o->flags |= UNINTERESTING | SEEN;
 
3103 define_commit_slab(indegree_slab, int);
 
3104 define_commit_slab(author_date_slab, timestamp_t);
 
3106 struct topo_walk_info {
 
3107         uint32_t min_generation;
 
3108         struct prio_queue explore_queue;
 
3109         struct prio_queue indegree_queue;
 
3110         struct prio_queue topo_queue;
 
3111         struct indegree_slab indegree;
 
3112         struct author_date_slab author_date;
 
3115 static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
 
3117         if (c->object.flags & flag)
 
3120         c->object.flags |= flag;
 
3121         prio_queue_put(q, c);
 
3124 static void explore_walk_step(struct rev_info *revs)
 
3126         struct topo_walk_info *info = revs->topo_walk_info;
 
3127         struct commit_list *p;
 
3128         struct commit *c = prio_queue_get(&info->explore_queue);
 
3133         if (parse_commit_gently(c, 1) < 0)
 
3136         if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
 
3137                 record_author_date(&info->author_date, c);
 
3139         if (revs->max_age != -1 && (c->date < revs->max_age))
 
3140                 c->object.flags |= UNINTERESTING;
 
3142         if (process_parents(revs, c, NULL, NULL) < 0)
 
3145         if (c->object.flags & UNINTERESTING)
 
3146                 mark_parents_uninteresting(c);
 
3148         for (p = c->parents; p; p = p->next)
 
3149                 test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
 
3152 static void explore_to_depth(struct rev_info *revs,
 
3153                              uint32_t gen_cutoff)
 
3155         struct topo_walk_info *info = revs->topo_walk_info;
 
3157         while ((c = prio_queue_peek(&info->explore_queue)) &&
 
3158                c->generation >= gen_cutoff)
 
3159                 explore_walk_step(revs);
 
3162 static void indegree_walk_step(struct rev_info *revs)
 
3164         struct commit_list *p;
 
3165         struct topo_walk_info *info = revs->topo_walk_info;
 
3166         struct commit *c = prio_queue_get(&info->indegree_queue);
 
3171         if (parse_commit_gently(c, 1) < 0)
 
3174         explore_to_depth(revs, c->generation);
 
3176         for (p = c->parents; p; p = p->next) {
 
3177                 struct commit *parent = p->item;
 
3178                 int *pi = indegree_slab_at(&info->indegree, parent);
 
3185                 test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
 
3187                 if (revs->first_parent_only)
 
3192 static void compute_indegrees_to_depth(struct rev_info *revs,
 
3193                                        uint32_t gen_cutoff)
 
3195         struct topo_walk_info *info = revs->topo_walk_info;
 
3197         while ((c = prio_queue_peek(&info->indegree_queue)) &&
 
3198                c->generation >= gen_cutoff)
 
3199                 indegree_walk_step(revs);
 
3202 static void reset_topo_walk(struct rev_info *revs)
 
3204         struct topo_walk_info *info = revs->topo_walk_info;
 
3206         clear_prio_queue(&info->explore_queue);
 
3207         clear_prio_queue(&info->indegree_queue);
 
3208         clear_prio_queue(&info->topo_queue);
 
3209         clear_indegree_slab(&info->indegree);
 
3210         clear_author_date_slab(&info->author_date);
 
3212         FREE_AND_NULL(revs->topo_walk_info);
 
3215 static void init_topo_walk(struct rev_info *revs)
 
3217         struct topo_walk_info *info;
 
3218         struct commit_list *list;
 
3219         if (revs->topo_walk_info)
 
3220                 reset_topo_walk(revs);
 
3222         revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
 
3223         info = revs->topo_walk_info;
 
3224         memset(info, 0, sizeof(struct topo_walk_info));
 
3226         init_indegree_slab(&info->indegree);
 
3227         memset(&info->explore_queue, 0, sizeof(info->explore_queue));
 
3228         memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
 
3229         memset(&info->topo_queue, 0, sizeof(info->topo_queue));
 
3231         switch (revs->sort_order) {
 
3232         default: /* REV_SORT_IN_GRAPH_ORDER */
 
3233                 info->topo_queue.compare = NULL;
 
3235         case REV_SORT_BY_COMMIT_DATE:
 
3236                 info->topo_queue.compare = compare_commits_by_commit_date;
 
3238         case REV_SORT_BY_AUTHOR_DATE:
 
3239                 init_author_date_slab(&info->author_date);
 
3240                 info->topo_queue.compare = compare_commits_by_author_date;
 
3241                 info->topo_queue.cb_data = &info->author_date;
 
3245         info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
 
3246         info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
 
3248         info->min_generation = GENERATION_NUMBER_INFINITY;
 
3249         for (list = revs->commits; list; list = list->next) {
 
3250                 struct commit *c = list->item;
 
3252                 if (parse_commit_gently(c, 1))
 
3255                 test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
 
3256                 test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
 
3258                 if (c->generation < info->min_generation)
 
3259                         info->min_generation = c->generation;
 
3261                 *(indegree_slab_at(&info->indegree, c)) = 1;
 
3263                 if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
 
3264                         record_author_date(&info->author_date, c);
 
3266         compute_indegrees_to_depth(revs, info->min_generation);
 
3268         for (list = revs->commits; list; list = list->next) {
 
3269                 struct commit *c = list->item;
 
3271                 if (*(indegree_slab_at(&info->indegree, c)) == 1)
 
3272                         prio_queue_put(&info->topo_queue, c);
 
3276          * This is unfortunate; the initial tips need to be shown
 
3277          * in the order given from the revision traversal machinery.
 
3279         if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
 
3280                 prio_queue_reverse(&info->topo_queue);
 
3283 static struct commit *next_topo_commit(struct rev_info *revs)
 
3286         struct topo_walk_info *info = revs->topo_walk_info;
 
3288         /* pop next off of topo_queue */
 
3289         c = prio_queue_get(&info->topo_queue);
 
3292                 *(indegree_slab_at(&info->indegree, c)) = 0;
 
3297 static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
 
3299         struct commit_list *p;
 
3300         struct topo_walk_info *info = revs->topo_walk_info;
 
3301         if (process_parents(revs, commit, NULL, NULL) < 0) {
 
3302                 if (!revs->ignore_missing_links)
 
3303                         die("Failed to traverse parents of commit %s",
 
3304                             oid_to_hex(&commit->object.oid));
 
3307         for (p = commit->parents; p; p = p->next) {
 
3308                 struct commit *parent = p->item;
 
3311                 if (parent->object.flags & UNINTERESTING)
 
3314                 if (parse_commit_gently(parent, 1) < 0)
 
3317                 if (parent->generation < info->min_generation) {
 
3318                         info->min_generation = parent->generation;
 
3319                         compute_indegrees_to_depth(revs, info->min_generation);
 
3322                 pi = indegree_slab_at(&info->indegree, parent);
 
3326                         prio_queue_put(&info->topo_queue, parent);
 
3328                 if (revs->first_parent_only)
 
3333 int prepare_revision_walk(struct rev_info *revs)
 
3336         struct object_array old_pending;
 
3337         struct commit_list **next = &revs->commits;
 
3339         memcpy(&old_pending, &revs->pending, sizeof(old_pending));
 
3340         revs->pending.nr = 0;
 
3341         revs->pending.alloc = 0;
 
3342         revs->pending.objects = NULL;
 
3343         for (i = 0; i < old_pending.nr; i++) {
 
3344                 struct object_array_entry *e = old_pending.objects + i;
 
3345                 struct commit *commit = handle_commit(revs, e);
 
3347                         if (!(commit->object.flags & SEEN)) {
 
3348                                 commit->object.flags |= SEEN;
 
3349                                 next = commit_list_append(commit, next);
 
3353         object_array_clear(&old_pending);
 
3355         /* Signal whether we need per-parent treesame decoration */
 
3356         if (revs->simplify_merges ||
 
3357             (revs->limited && limiting_can_increase_treesame(revs)))
 
3358                 revs->treesame.name = "treesame";
 
3360         if (revs->exclude_promisor_objects) {
 
3361                 for_each_packed_object(mark_uninteresting, revs,
 
3362                                        FOR_EACH_OBJECT_PROMISOR_ONLY);
 
3365         if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
 
3366                 commit_list_sort_by_date(&revs->commits);
 
3369         if (revs->limited) {
 
3370                 if (limit_list(revs) < 0)
 
3372                 if (revs->topo_order)
 
3373                         sort_in_topological_order(&revs->commits, revs->sort_order);
 
3374         } else if (revs->topo_order)
 
3375                 init_topo_walk(revs);
 
3376         if (revs->line_level_traverse)
 
3377                 line_log_filter(revs);
 
3378         if (revs->simplify_merges)
 
3379                 simplify_merges(revs);
 
3380         if (revs->children.name)
 
3385 static enum rewrite_result rewrite_one_1(struct rev_info *revs,
 
3387                                          struct prio_queue *queue)
 
3390                 struct commit *p = *pp;
 
3392                         if (process_parents(revs, p, NULL, queue) < 0)
 
3393                                 return rewrite_one_error;
 
3394                 if (p->object.flags & UNINTERESTING)
 
3395                         return rewrite_one_ok;
 
3396                 if (!(p->object.flags & TREESAME))
 
3397                         return rewrite_one_ok;
 
3399                         return rewrite_one_noparents;
 
3400                 if ((p = one_relevant_parent(revs, p->parents)) == NULL)
 
3401                         return rewrite_one_ok;
 
3406 static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
 
3409                 struct commit *item = prio_queue_peek(q);
 
3410                 struct commit_list *p = *list;
 
3412                 if (p && p->item->date >= item->date)
 
3415                         p = commit_list_insert(item, list);
 
3416                         list = &p->next; /* skip newly added item */
 
3417                         prio_queue_get(q); /* pop item */
 
3422 static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
 
3424         struct prio_queue queue = { compare_commits_by_commit_date };
 
3425         enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
 
3426         merge_queue_into_list(&queue, &revs->commits);
 
3427         clear_prio_queue(&queue);
 
3431 int rewrite_parents(struct rev_info *revs, struct commit *commit,
 
3432         rewrite_parent_fn_t rewrite_parent)
 
3434         struct commit_list **pp = &commit->parents;
 
3436                 struct commit_list *parent = *pp;
 
3437                 switch (rewrite_parent(revs, &parent->item)) {
 
3438                 case rewrite_one_ok:
 
3440                 case rewrite_one_noparents:
 
3443                 case rewrite_one_error:
 
3448         remove_duplicate_parents(revs, commit);
 
3452 static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
 
3454         char *person, *endp;
 
3455         size_t len, namelen, maillen;
 
3458         struct ident_split ident;
 
3460         person = strstr(buf->buf, what);
 
3464         person += strlen(what);
 
3465         endp = strchr(person, '\n');
 
3469         len = endp - person;
 
3471         if (split_ident_line(&ident, person, len))
 
3474         mail = ident.mail_begin;
 
3475         maillen = ident.mail_end - ident.mail_begin;
 
3476         name = ident.name_begin;
 
3477         namelen = ident.name_end - ident.name_begin;
 
3479         if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
 
3480                 struct strbuf namemail = STRBUF_INIT;
 
3482                 strbuf_addf(&namemail, "%.*s <%.*s>",
 
3483                             (int)namelen, name, (int)maillen, mail);
 
3485                 strbuf_splice(buf, ident.name_begin - buf->buf,
 
3486                               ident.mail_end - ident.name_begin + 1,
 
3487                               namemail.buf, namemail.len);
 
3489                 strbuf_release(&namemail);
 
3497 static int commit_match(struct commit *commit, struct rev_info *opt)
 
3500         const char *encoding;
 
3501         const char *message;
 
3502         struct strbuf buf = STRBUF_INIT;
 
3504         if (!opt->grep_filter.pattern_list && !opt->grep_filter.header_list)
 
3507         /* Prepend "fake" headers as needed */
 
3508         if (opt->grep_filter.use_reflog_filter) {
 
3509                 strbuf_addstr(&buf, "reflog ");
 
3510                 get_reflog_message(&buf, opt->reflog_info);
 
3511                 strbuf_addch(&buf, '\n');
 
3515          * We grep in the user's output encoding, under the assumption that it
 
3516          * is the encoding they are most likely to write their grep pattern
 
3517          * for. In addition, it means we will match the "notes" encoding below,
 
3518          * so we will not end up with a buffer that has two different encodings
 
3521         encoding = get_log_output_encoding();
 
3522         message = logmsg_reencode(commit, NULL, encoding);
 
3524         /* Copy the commit to temporary if we are using "fake" headers */
 
3526                 strbuf_addstr(&buf, message);
 
3528         if (opt->grep_filter.header_list && opt->mailmap) {
 
3530                         strbuf_addstr(&buf, message);
 
3532                 commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
 
3533                 commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
 
3536         /* Append "fake" message parts as needed */
 
3537         if (opt->show_notes) {
 
3539                         strbuf_addstr(&buf, message);
 
3540                 format_display_notes(&commit->object.oid, &buf, encoding, 1);
 
3544          * Find either in the original commit message, or in the temporary.
 
3545          * Note that we cast away the constness of "message" here. It is
 
3546          * const because it may come from the cached commit buffer. That's OK,
 
3547          * because we know that it is modifiable heap memory, and that while
 
3548          * grep_buffer may modify it for speed, it will restore any
 
3549          * changes before returning.
 
3552                 retval = grep_buffer(&opt->grep_filter, buf.buf, buf.len);
 
3554                 retval = grep_buffer(&opt->grep_filter,
 
3555                                      (char *)message, strlen(message));
 
3556         strbuf_release(&buf);
 
3557         unuse_commit_buffer(commit, message);
 
3558         return opt->invert_grep ? !retval : retval;
 
3561 static inline int want_ancestry(const struct rev_info *revs)
 
3563         return (revs->rewrite_parents || revs->children.name);
 
3567  * Return a timestamp to be used for --since/--until comparisons for this
 
3568  * commit, based on the revision options.
 
3570 static timestamp_t comparison_date(const struct rev_info *revs,
 
3571                                    struct commit *commit)
 
3573         return revs->reflog_info ?
 
3574                 get_reflog_timestamp(revs->reflog_info) :
 
3578 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
 
3580         if (commit->object.flags & SHOWN)
 
3581                 return commit_ignore;
 
3582         if (revs->unpacked && has_object_pack(&commit->object.oid))
 
3583                 return commit_ignore;
 
3584         if (commit->object.flags & UNINTERESTING)
 
3585                 return commit_ignore;
 
3586         if (revs->min_age != -1 &&
 
3587             comparison_date(revs, commit) > revs->min_age)
 
3588                         return commit_ignore;
 
3589         if (revs->min_parents || (revs->max_parents >= 0)) {
 
3590                 int n = commit_list_count(commit->parents);
 
3591                 if ((n < revs->min_parents) ||
 
3592                     ((revs->max_parents >= 0) && (n > revs->max_parents)))
 
3593                         return commit_ignore;
 
3595         if (!commit_match(commit, revs))
 
3596                 return commit_ignore;
 
3597         if (revs->prune && revs->dense) {
 
3598                 /* Commit without changes? */
 
3599                 if (commit->object.flags & TREESAME) {
 
3601                         struct commit_list *p;
 
3602                         /* drop merges unless we want parenthood */
 
3603                         if (!want_ancestry(revs))
 
3604                                 return commit_ignore;
 
3606                          * If we want ancestry, then need to keep any merges
 
3607                          * between relevant commits to tie together topology.
 
3608                          * For consistency with TREESAME and simplification
 
3609                          * use "relevant" here rather than just INTERESTING,
 
3610                          * to treat bottom commit(s) as part of the topology.
 
3612                         for (n = 0, p = commit->parents; p; p = p->next)
 
3613                                 if (relevant_commit(p->item))
 
3616                         return commit_ignore;
 
3622 define_commit_slab(saved_parents, struct commit_list *);
 
3624 #define EMPTY_PARENT_LIST ((struct commit_list *)-1)
 
3627  * You may only call save_parents() once per commit (this is checked
 
3628  * for non-root commits).
 
3630 static void save_parents(struct rev_info *revs, struct commit *commit)
 
3632         struct commit_list **pp;
 
3634         if (!revs->saved_parents_slab) {
 
3635                 revs->saved_parents_slab = xmalloc(sizeof(struct saved_parents));
 
3636                 init_saved_parents(revs->saved_parents_slab);
 
3639         pp = saved_parents_at(revs->saved_parents_slab, commit);
 
3642          * When walking with reflogs, we may visit the same commit
 
3643          * several times: once for each appearance in the reflog.
 
3645          * In this case, save_parents() will be called multiple times.
 
3646          * We want to keep only the first set of parents.  We need to
 
3647          * store a sentinel value for an empty (i.e., NULL) parent
 
3648          * list to distinguish it from a not-yet-saved list, however.
 
3652         if (commit->parents)
 
3653                 *pp = copy_commit_list(commit->parents);
 
3655                 *pp = EMPTY_PARENT_LIST;
 
3658 static void free_saved_parents(struct rev_info *revs)
 
3660         if (revs->saved_parents_slab)
 
3661                 clear_saved_parents(revs->saved_parents_slab);
 
3664 struct commit_list *get_saved_parents(struct rev_info *revs, const struct commit *commit)
 
3666         struct commit_list *parents;
 
3668         if (!revs->saved_parents_slab)
 
3669                 return commit->parents;
 
3671         parents = *saved_parents_at(revs->saved_parents_slab, commit);
 
3672         if (parents == EMPTY_PARENT_LIST)
 
3677 enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
 
3679         enum commit_action action = get_commit_action(revs, commit);
 
3681         if (action == commit_show &&
 
3682             revs->prune && revs->dense && want_ancestry(revs)) {
 
3684                  * --full-diff on simplified parents is no good: it
 
3685                  * will show spurious changes from the commits that
 
3686                  * were elided.  So we save the parents on the side
 
3687                  * when --full-diff is in effect.
 
3689                 if (revs->full_diff)
 
3690                         save_parents(revs, commit);
 
3691                 if (rewrite_parents(revs, commit, rewrite_one) < 0)
 
3692                         return commit_error;
 
3697 static void track_linear(struct rev_info *revs, struct commit *commit)
 
3699         if (revs->track_first_time) {
 
3701                 revs->track_first_time = 0;
 
3703                 struct commit_list *p;
 
3704                 for (p = revs->previous_parents; p; p = p->next)
 
3705                         if (p->item == NULL || /* first commit */
 
3706                             oideq(&p->item->object.oid, &commit->object.oid))
 
3708                 revs->linear = p != NULL;
 
3710         if (revs->reverse) {
 
3712                         commit->object.flags |= TRACK_LINEAR;
 
3714         free_commit_list(revs->previous_parents);
 
3715         revs->previous_parents = copy_commit_list(commit->parents);
 
3718 static struct commit *get_revision_1(struct rev_info *revs)
 
3721                 struct commit *commit;
 
3723                 if (revs->reflog_info)
 
3724                         commit = next_reflog_entry(revs->reflog_info);
 
3725                 else if (revs->topo_walk_info)
 
3726                         commit = next_topo_commit(revs);
 
3728                         commit = pop_commit(&revs->commits);
 
3733                 if (revs->reflog_info)
 
3734                         commit->object.flags &= ~(ADDED | SEEN | SHOWN);
 
3737                  * If we haven't done the list limiting, we need to look at
 
3738                  * the parents here. We also need to do the date-based limiting
 
3739                  * that we'd otherwise have done in limit_list().
 
3741                 if (!revs->limited) {
 
3742                         if (revs->max_age != -1 &&
 
3743                             comparison_date(revs, commit) < revs->max_age)
 
3746                         if (revs->reflog_info)
 
3747                                 try_to_simplify_commit(revs, commit);
 
3748                         else if (revs->topo_walk_info)
 
3749                                 expand_topo_walk(revs, commit);
 
3750                         else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
 
3751                                 if (!revs->ignore_missing_links)
 
3752                                         die("Failed to traverse parents of commit %s",
 
3753                                                 oid_to_hex(&commit->object.oid));
 
3757                 switch (simplify_commit(revs, commit)) {
 
3761                         die("Failed to simplify parents of commit %s",
 
3762                             oid_to_hex(&commit->object.oid));
 
3764                         if (revs->track_linear)
 
3765                                 track_linear(revs, commit);
 
3772  * Return true for entries that have not yet been shown.  (This is an
 
3773  * object_array_each_func_t.)
 
3775 static int entry_unshown(struct object_array_entry *entry, void *cb_data_unused)
 
3777         return !(entry->item->flags & SHOWN);
 
3781  * If array is on the verge of a realloc, garbage-collect any entries
 
3782  * that have already been shown to try to free up some space.
 
3784 static void gc_boundary(struct object_array *array)
 
3786         if (array->nr == array->alloc)
 
3787                 object_array_filter(array, entry_unshown, NULL);
 
3790 static void create_boundary_commit_list(struct rev_info *revs)
 
3794         struct object_array *array = &revs->boundary_commits;
 
3795         struct object_array_entry *objects = array->objects;
 
3798          * If revs->commits is non-NULL at this point, an error occurred in
 
3799          * get_revision_1().  Ignore the error and continue printing the
 
3800          * boundary commits anyway.  (This is what the code has always
 
3803         if (revs->commits) {
 
3804                 free_commit_list(revs->commits);
 
3805                 revs->commits = NULL;
 
3809          * Put all of the actual boundary commits from revs->boundary_commits
 
3810          * into revs->commits
 
3812         for (i = 0; i < array->nr; i++) {
 
3813                 c = (struct commit *)(objects[i].item);
 
3816                 if (!(c->object.flags & CHILD_SHOWN))
 
3818                 if (c->object.flags & (SHOWN | BOUNDARY))
 
3820                 c->object.flags |= BOUNDARY;
 
3821                 commit_list_insert(c, &revs->commits);
 
3825          * If revs->topo_order is set, sort the boundary commits
 
3826          * in topological order
 
3828         sort_in_topological_order(&revs->commits, revs->sort_order);
 
3831 static struct commit *get_revision_internal(struct rev_info *revs)
 
3833         struct commit *c = NULL;
 
3834         struct commit_list *l;
 
3836         if (revs->boundary == 2) {
 
3838                  * All of the normal commits have already been returned,
 
3839                  * and we are now returning boundary commits.
 
3840                  * create_boundary_commit_list() has populated
 
3841                  * revs->commits with the remaining commits to return.
 
3843                 c = pop_commit(&revs->commits);
 
3845                         c->object.flags |= SHOWN;
 
3850          * If our max_count counter has reached zero, then we are done. We
 
3851          * don't simply return NULL because we still might need to show
 
3852          * boundary commits. But we want to avoid calling get_revision_1, which
 
3853          * might do a considerable amount of work finding the next commit only
 
3854          * for us to throw it away.
 
3856          * If it is non-zero, then either we don't have a max_count at all
 
3857          * (-1), or it is still counting, in which case we decrement.
 
3859         if (revs->max_count) {
 
3860                 c = get_revision_1(revs);
 
3862                         while (revs->skip_count > 0) {
 
3864                                 c = get_revision_1(revs);
 
3870                 if (revs->max_count > 0)
 
3875                 c->object.flags |= SHOWN;
 
3877         if (!revs->boundary)
 
3882                  * get_revision_1() runs out the commits, and
 
3883                  * we are done computing the boundaries.
 
3884                  * switch to boundary commits output mode.
 
3889                  * Update revs->commits to contain the list of
 
3892                 create_boundary_commit_list(revs);
 
3894                 return get_revision_internal(revs);
 
3898          * boundary commits are the commits that are parents of the
 
3899          * ones we got from get_revision_1() but they themselves are
 
3900          * not returned from get_revision_1().  Before returning
 
3901          * 'c', we need to mark its parents that they could be boundaries.
 
3904         for (l = c->parents; l; l = l->next) {
 
3906                 p = &(l->item->object);
 
3907                 if (p->flags & (CHILD_SHOWN | SHOWN))
 
3909                 p->flags |= CHILD_SHOWN;
 
3910                 gc_boundary(&revs->boundary_commits);
 
3911                 add_object_array(p, NULL, &revs->boundary_commits);
 
3917 struct commit *get_revision(struct rev_info *revs)
 
3920         struct commit_list *reversed;
 
3922         if (revs->reverse) {
 
3924                 while ((c = get_revision_internal(revs)))
 
3925                         commit_list_insert(c, &reversed);
 
3926                 revs->commits = reversed;
 
3928                 revs->reverse_output_stage = 1;
 
3931         if (revs->reverse_output_stage) {
 
3932                 c = pop_commit(&revs->commits);
 
3933                 if (revs->track_linear)
 
3934                         revs->linear = !!(c && c->object.flags & TRACK_LINEAR);
 
3938         c = get_revision_internal(revs);
 
3939         if (c && revs->graph)
 
3940                 graph_update(revs->graph, c);
 
3942                 free_saved_parents(revs);
 
3943                 if (revs->previous_parents) {
 
3944                         free_commit_list(revs->previous_parents);
 
3945                         revs->previous_parents = NULL;
 
3951 const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
 
3953         if (commit->object.flags & BOUNDARY)
 
3955         else if (commit->object.flags & UNINTERESTING)
 
3957         else if (commit->object.flags & PATCHSAME)
 
3959         else if (!revs || revs->left_right) {
 
3960                 if (commit->object.flags & SYMMETRIC_LEFT)
 
3964         } else if (revs->graph)
 
3966         else if (revs->cherry_mark)
 
3971 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
 
3973         const char *mark = get_revision_mark(revs, commit);
 
3976         fputs(mark, stdout);