packaging: Add contrib installation
[platform/upstream/git.git] / revision.c
index 8b2dfe3..7ec9b63 100644 (file)
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "object-store.h"
 #include "tag.h"
 #include "blob.h"
 #include "tree.h"
@@ -6,6 +7,7 @@
 #include "diff.h"
 #include "refs.h"
 #include "revision.h"
+#include "repository.h"
 #include "graph.h"
 #include "grep.h"
 #include "reflog-walk.h"
 #include "dir.h"
 #include "cache-tree.h"
 #include "bisect.h"
+#include "packfile.h"
+#include "worktree.h"
+#include "strvec.h"
+#include "commit-reach.h"
+#include "commit-graph.h"
+#include "prio-queue.h"
+#include "hashmap.h"
+#include "utf8.h"
+#include "bloom.h"
+#include "json-writer.h"
 
 volatile show_early_output_fn_t show_early_output;
 
 static const char *term_bad;
 static const char *term_good;
 
+implement_shared_commit_slab(revision_sources, char *);
+
+static inline int want_ancestry(const struct rev_info *revs);
+
 void show_object_with_name(FILE *out, struct object *obj, const char *name)
 {
        const char *p;
@@ -44,25 +60,23 @@ static void mark_blob_uninteresting(struct blob *blob)
        blob->object.flags |= UNINTERESTING;
 }
 
-static void mark_tree_contents_uninteresting(struct tree *tree)
+static void mark_tree_contents_uninteresting(struct repository *r,
+                                            struct tree *tree)
 {
        struct tree_desc desc;
        struct name_entry entry;
-       struct object *obj = &tree->object;
 
-       if (!has_object_file(&obj->oid))
+       if (parse_tree_gently(tree, 1) < 0)
                return;
-       if (parse_tree(tree) < 0)
-               die("bad tree %s", oid_to_hex(&obj->oid));
 
        init_tree_desc(&desc, tree->buffer, tree->size);
        while (tree_entry(&desc, &entry)) {
                switch (object_type(entry.mode)) {
                case OBJ_TREE:
-                       mark_tree_uninteresting(lookup_tree(entry.sha1));
+                       mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
                        break;
                case OBJ_BLOB:
-                       mark_blob_uninteresting(lookup_blob(entry.sha1));
+                       mark_blob_uninteresting(lookup_blob(r, &entry.oid));
                        break;
                default:
                        /* Subproject commit - not in this repository */
@@ -77,7 +91,7 @@ static void mark_tree_contents_uninteresting(struct tree *tree)
        free_tree_buffer(tree);
 }
 
-void mark_tree_uninteresting(struct tree *tree)
+void mark_tree_uninteresting(struct repository *r, struct tree *tree)
 {
        struct object *obj;
 
@@ -88,52 +102,205 @@ void mark_tree_uninteresting(struct tree *tree)
        if (obj->flags & UNINTERESTING)
                return;
        obj->flags |= UNINTERESTING;
-       mark_tree_contents_uninteresting(tree);
+       mark_tree_contents_uninteresting(r, tree);
 }
 
-void mark_parents_uninteresting(struct commit *commit)
+struct path_and_oids_entry {
+       struct hashmap_entry ent;
+       char *path;
+       struct oidset trees;
+};
+
+static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
+                            const struct hashmap_entry *eptr,
+                            const struct hashmap_entry *entry_or_key,
+                            const void *keydata)
 {
-       struct commit_list *parents = NULL, *l;
+       const struct path_and_oids_entry *e1, *e2;
 
-       for (l = commit->parents; l; l = l->next)
-               commit_list_insert(l->item, &parents);
+       e1 = container_of(eptr, const struct path_and_oids_entry, ent);
+       e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
 
-       while (parents) {
-               struct commit *commit = pop_commit(&parents);
+       return strcmp(e1->path, e2->path);
+}
 
-               while (commit) {
-                       /*
-                        * A missing commit is ok iff its parent is marked
-                        * uninteresting.
-                        *
-                        * We just mark such a thing parsed, so that when
-                        * it is popped next time around, we won't be trying
-                        * to parse it and get an error.
-                        */
-                       if (!has_object_file(&commit->object.oid))
-                               commit->object.parsed = 1;
+static void paths_and_oids_clear(struct hashmap *map)
+{
+       struct hashmap_iter iter;
+       struct path_and_oids_entry *entry;
 
-                       if (commit->object.flags & UNINTERESTING)
-                               break;
+       hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
+               oidset_clear(&entry->trees);
+               free(entry->path);
+       }
 
-                       commit->object.flags |= UNINTERESTING;
+       hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
+}
 
-                       /*
-                        * Normally we haven't parsed the parent
-                        * yet, so we won't have a parent of a parent
-                        * here. However, it may turn out that we've
-                        * reached this commit some other way (where it
-                        * wasn't uninteresting), in which case we need
-                        * to mark its parents recursively too..
-                        */
-                       if (!commit->parents)
-                               break;
+static void paths_and_oids_insert(struct hashmap *map,
+                                 const char *path,
+                                 const struct object_id *oid)
+{
+       int hash = strhash(path);
+       struct path_and_oids_entry key;
+       struct path_and_oids_entry *entry;
+
+       hashmap_entry_init(&key.ent, hash);
+
+       /* use a shallow copy for the lookup */
+       key.path = (char *)path;
+       oidset_init(&key.trees, 0);
+
+       entry = hashmap_get_entry(map, &key, ent, NULL);
+       if (!entry) {
+               entry = xcalloc(1, sizeof(struct path_and_oids_entry));
+               hashmap_entry_init(&entry->ent, hash);
+               entry->path = xstrdup(key.path);
+               oidset_init(&entry->trees, 16);
+               hashmap_put(map, &entry->ent);
+       }
+
+       oidset_insert(&entry->trees, oid);
+}
 
-                       for (l = commit->parents->next; l; l = l->next)
-                               commit_list_insert(l->item, &parents);
-                       commit = commit->parents->item;
+static void add_children_by_path(struct repository *r,
+                                struct tree *tree,
+                                struct hashmap *map)
+{
+       struct tree_desc desc;
+       struct name_entry entry;
+
+       if (!tree)
+               return;
+
+       if (parse_tree_gently(tree, 1) < 0)
+               return;
+
+       init_tree_desc(&desc, tree->buffer, tree->size);
+       while (tree_entry(&desc, &entry)) {
+               switch (object_type(entry.mode)) {
+               case OBJ_TREE:
+                       paths_and_oids_insert(map, entry.path, &entry.oid);
+
+                       if (tree->object.flags & UNINTERESTING) {
+                               struct tree *child = lookup_tree(r, &entry.oid);
+                               if (child)
+                                       child->object.flags |= UNINTERESTING;
+                       }
+                       break;
+               case OBJ_BLOB:
+                       if (tree->object.flags & UNINTERESTING) {
+                               struct blob *child = lookup_blob(r, &entry.oid);
+                               if (child)
+                                       child->object.flags |= UNINTERESTING;
+                       }
+                       break;
+               default:
+                       /* Subproject commit - not in this repository */
+                       break;
                }
        }
+
+       free_tree_buffer(tree);
+}
+
+void mark_trees_uninteresting_sparse(struct repository *r,
+                                    struct oidset *trees)
+{
+       unsigned has_interesting = 0, has_uninteresting = 0;
+       struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
+       struct hashmap_iter map_iter;
+       struct path_and_oids_entry *entry;
+       struct object_id *oid;
+       struct oidset_iter iter;
+
+       oidset_iter_init(trees, &iter);
+       while ((!has_interesting || !has_uninteresting) &&
+              (oid = oidset_iter_next(&iter))) {
+               struct tree *tree = lookup_tree(r, oid);
+
+               if (!tree)
+                       continue;
+
+               if (tree->object.flags & UNINTERESTING)
+                       has_uninteresting = 1;
+               else
+                       has_interesting = 1;
+       }
+
+       /* Do not walk unless we have both types of trees. */
+       if (!has_uninteresting || !has_interesting)
+               return;
+
+       oidset_iter_init(trees, &iter);
+       while ((oid = oidset_iter_next(&iter))) {
+               struct tree *tree = lookup_tree(r, oid);
+               add_children_by_path(r, tree, &map);
+       }
+
+       hashmap_for_each_entry(&map, &map_iter, entry, ent /* member name */)
+               mark_trees_uninteresting_sparse(r, &entry->trees);
+
+       paths_and_oids_clear(&map);
+}
+
+struct commit_stack {
+       struct commit **items;
+       size_t nr, alloc;
+};
+#define COMMIT_STACK_INIT { NULL, 0, 0 }
+
+static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
+{
+       ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
+       stack->items[stack->nr++] = commit;
+}
+
+static struct commit *commit_stack_pop(struct commit_stack *stack)
+{
+       return stack->nr ? stack->items[--stack->nr] : NULL;
+}
+
+static void commit_stack_clear(struct commit_stack *stack)
+{
+       FREE_AND_NULL(stack->items);
+       stack->nr = stack->alloc = 0;
+}
+
+static void mark_one_parent_uninteresting(struct commit *commit,
+                                         struct commit_stack *pending)
+{
+       struct commit_list *l;
+
+       if (commit->object.flags & UNINTERESTING)
+               return;
+       commit->object.flags |= UNINTERESTING;
+
+       /*
+        * Normally we haven't parsed the parent
+        * yet, so we won't have a parent of a parent
+        * here. However, it may turn out that we've
+        * reached this commit some other way (where it
+        * wasn't uninteresting), in which case we need
+        * to mark its parents recursively too..
+        */
+       for (l = commit->parents; l; l = l->next)
+               commit_stack_push(pending, l->item);
+}
+
+void mark_parents_uninteresting(struct commit *commit)
+{
+       struct commit_stack pending = COMMIT_STACK_INIT;
+       struct commit_list *l;
+
+       for (l = commit->parents; l; l = l->next)
+               mark_one_parent_uninteresting(l->item, &pending);
+
+       while (pending.nr > 0)
+               mark_one_parent_uninteresting(commit_stack_pop(&pending),
+                                             &pending);
+
+       commit_stack_clear(&pending);
 }
 
 static void add_pending_object_with_path(struct rev_info *revs,
@@ -141,23 +308,22 @@ static void add_pending_object_with_path(struct rev_info *revs,
                                         const char *name, unsigned mode,
                                         const char *path)
 {
+       struct interpret_branch_name_options options = { 0 };
        if (!obj)
                return;
        if (revs->no_walk && (obj->flags & UNINTERESTING))
                revs->no_walk = 0;
        if (revs->reflog_info && obj->type == OBJ_COMMIT) {
                struct strbuf buf = STRBUF_INIT;
-               int len = interpret_branch_name(name, 0, &buf);
-               int st;
+               int len = interpret_branch_name(name, 0, &buf, &options);
 
                if (0 < len && name[len] && buf.len)
                        strbuf_addstr(&buf, name + len);
-               st = add_reflog_for_walk(revs->reflog_info,
-                                        (struct commit *)obj,
-                                        buf.buf[0] ? buf.buf: name);
+               add_reflog_for_walk(revs->reflog_info,
+                                   (struct commit *)obj,
+                                   buf.buf[0] ? buf.buf: name);
                strbuf_release(&buf);
-               if (st)
-                       return;
+               return; /* do not add the commit itself */
        }
        add_object_array_with_path(obj, name, &revs->pending, mode, path);
 }
@@ -177,36 +343,51 @@ void add_pending_object(struct rev_info *revs,
 
 void add_head_to_pending(struct rev_info *revs)
 {
-       unsigned char sha1[20];
+       struct object_id oid;
        struct object *obj;
-       if (get_sha1("HEAD", sha1))
+       if (get_oid("HEAD", &oid))
                return;
-       obj = parse_object(sha1);
+       obj = parse_object(revs->repo, &oid);
        if (!obj)
                return;
        add_pending_object(revs, obj, "HEAD");
 }
 
 static struct object *get_reference(struct rev_info *revs, const char *name,
-                                   const unsigned char *sha1,
+                                   const struct object_id *oid,
                                    unsigned int flags)
 {
        struct object *object;
 
-       object = parse_object(sha1);
+       /*
+        * If the repository has commit graphs, repo_parse_commit() avoids
+        * reading the object buffer, so use it whenever possible.
+        */
+       if (oid_object_info(revs->repo, oid, NULL) == OBJ_COMMIT) {
+               struct commit *c = lookup_commit(revs->repo, oid);
+               if (!repo_parse_commit(revs->repo, c))
+                       object = (struct object *) c;
+               else
+                       object = NULL;
+       } else {
+               object = parse_object(revs->repo, oid);
+       }
+
        if (!object) {
                if (revs->ignore_missing)
                        return object;
+               if (revs->exclude_promisor_objects && is_promisor_object(oid))
+                       return NULL;
                die("bad object %s", name);
        }
        object->flags |= flags;
        return object;
 }
 
-void add_pending_sha1(struct rev_info *revs, const char *name,
-                     const unsigned char *sha1, unsigned int flags)
+void add_pending_oid(struct rev_info *revs, const char *name,
+                     const struct object_id *oid, unsigned int flags)
 {
-       struct object *object = get_reference(revs, name, sha1, flags);
+       struct object *object = get_reference(revs, name, oid, flags);
        add_pending_object(revs, object, name);
 }
 
@@ -226,11 +407,12 @@ static struct commit *handle_commit(struct rev_info *revs,
                struct tag *tag = (struct tag *) object;
                if (revs->tag_objects && !(flags & UNINTERESTING))
                        add_pending_object(revs, object, tag->tag);
-               if (!tag->tagged)
-                       die("bad tag");
-               object = parse_object(tag->tagged->oid.hash);
+               object = parse_object(revs->repo, get_tagged_oid(tag));
                if (!object) {
-                       if (flags & UNINTERESTING)
+                       if (revs->ignore_missing_links || (flags & UNINTERESTING))
+                               return NULL;
+                       if (revs->exclude_promisor_objects &&
+                           is_promisor_object(&tag->tagged->oid))
                                return NULL;
                        die("bad object %s", oid_to_hex(&tag->tagged->oid));
                }
@@ -250,14 +432,21 @@ static struct commit *handle_commit(struct rev_info *revs,
         */
        if (object->type == OBJ_COMMIT) {
                struct commit *commit = (struct commit *)object;
-               if (parse_commit(commit) < 0)
+
+               if (repo_parse_commit(revs->repo, commit) < 0)
                        die("unable to parse commit %s", name);
                if (flags & UNINTERESTING) {
                        mark_parents_uninteresting(commit);
-                       revs->limited = 1;
+
+                       if (!revs->topo_order || !generation_numbers_enabled(the_repository))
+                               revs->limited = 1;
+               }
+               if (revs->sources) {
+                       char **slot = revision_sources_at(revs->sources, commit);
+
+                       if (!*slot)
+                               *slot = xstrdup(name);
                }
-               if (revs->show_source && !commit->util)
-                       commit->util = xstrdup(name);
                return commit;
        }
 
@@ -270,7 +459,7 @@ static struct commit *handle_commit(struct rev_info *revs,
                if (!revs->tree_objects)
                        return NULL;
                if (flags & UNINTERESTING) {
-                       mark_tree_contents_uninteresting(tree);
+                       mark_tree_contents_uninteresting(revs->repo, tree);
                        return NULL;
                }
                add_pending_object_with_path(revs, object, name, mode, path);
@@ -394,41 +583,195 @@ static struct commit *one_relevant_parent(const struct rev_info *revs,
  * if the whole diff is removal of old data, and otherwise
  * REV_TREE_DIFFERENT (of course if the trees are the same we
  * want REV_TREE_SAME).
- * That means that once we get to REV_TREE_DIFFERENT, we do not
- * have to look any further.
+ *
+ * The only time we care about the distinction is when
+ * remove_empty_trees is in effect, in which case we care only about
+ * whether the whole change is REV_TREE_NEW, or if there's another type
+ * of change. Which means we can stop the diff early in either of these
+ * cases:
+ *
+ *   1. We're not using remove_empty_trees at all.
+ *
+ *   2. We saw anything except REV_TREE_NEW.
  */
 static int tree_difference = REV_TREE_SAME;
 
 static void file_add_remove(struct diff_options *options,
                    int addremove, unsigned mode,
-                   const unsigned char *sha1,
-                   int sha1_valid,
+                   const struct object_id *oid,
+                   int oid_valid,
                    const char *fullpath, unsigned dirty_submodule)
 {
        int diff = addremove == '+' ? REV_TREE_NEW : REV_TREE_OLD;
+       struct rev_info *revs = options->change_fn_data;
 
        tree_difference |= diff;
-       if (tree_difference == REV_TREE_DIFFERENT)
-               DIFF_OPT_SET(options, HAS_CHANGES);
+       if (!revs->remove_empty_trees || tree_difference != REV_TREE_NEW)
+               options->flags.has_changes = 1;
 }
 
 static void file_change(struct diff_options *options,
                 unsigned old_mode, unsigned new_mode,
-                const unsigned char *old_sha1,
-                const unsigned char *new_sha1,
-                int old_sha1_valid, int new_sha1_valid,
+                const struct object_id *old_oid,
+                const struct object_id *new_oid,
+                int old_oid_valid, int new_oid_valid,
                 const char *fullpath,
                 unsigned old_dirty_submodule, unsigned new_dirty_submodule)
 {
        tree_difference = REV_TREE_DIFFERENT;
-       DIFF_OPT_SET(options, HAS_CHANGES);
+       options->flags.has_changes = 1;
+}
+
+static int bloom_filter_atexit_registered;
+static unsigned int count_bloom_filter_maybe;
+static unsigned int count_bloom_filter_definitely_not;
+static unsigned int count_bloom_filter_false_positive;
+static unsigned int count_bloom_filter_not_present;
+
+static void trace2_bloom_filter_statistics_atexit(void)
+{
+       struct json_writer jw = JSON_WRITER_INIT;
+
+       jw_object_begin(&jw, 0);
+       jw_object_intmax(&jw, "filter_not_present", count_bloom_filter_not_present);
+       jw_object_intmax(&jw, "maybe", count_bloom_filter_maybe);
+       jw_object_intmax(&jw, "definitely_not", count_bloom_filter_definitely_not);
+       jw_object_intmax(&jw, "false_positive", count_bloom_filter_false_positive);
+       jw_end(&jw);
+
+       trace2_data_json("bloom", the_repository, "statistics", &jw);
+
+       jw_release(&jw);
+}
+
+static int forbid_bloom_filters(struct pathspec *spec)
+{
+       if (spec->has_wildcard)
+               return 1;
+       if (spec->nr > 1)
+               return 1;
+       if (spec->magic & ~PATHSPEC_LITERAL)
+               return 1;
+       if (spec->nr && (spec->items[0].magic & ~PATHSPEC_LITERAL))
+               return 1;
+
+       return 0;
+}
+
+static void prepare_to_use_bloom_filter(struct rev_info *revs)
+{
+       struct pathspec_item *pi;
+       char *path_alloc = NULL;
+       const char *path, *p;
+       size_t len;
+       int path_component_nr = 1;
+
+       if (!revs->commits)
+               return;
+
+       if (forbid_bloom_filters(&revs->prune_data))
+               return;
+
+       repo_parse_commit(revs->repo, revs->commits->item);
+
+       revs->bloom_filter_settings = get_bloom_filter_settings(revs->repo);
+       if (!revs->bloom_filter_settings)
+               return;
+
+       if (!revs->pruning.pathspec.nr)
+               return;
+
+       pi = &revs->pruning.pathspec.items[0];
+
+       /* remove single trailing slash from path, if needed */
+       if (pi->len > 0 && pi->match[pi->len - 1] == '/') {
+               path_alloc = xmemdupz(pi->match, pi->len - 1);
+               path = path_alloc;
+       } else
+               path = pi->match;
+
+       len = strlen(path);
+       if (!len) {
+               revs->bloom_filter_settings = NULL;
+               free(path_alloc);
+               return;
+       }
+
+       p = path;
+       while (*p) {
+               /*
+                * At this point, the path is normalized to use Unix-style
+                * path separators. This is required due to how the
+                * changed-path Bloom filters store the paths.
+                */
+               if (*p == '/')
+                       path_component_nr++;
+               p++;
+       }
+
+       revs->bloom_keys_nr = path_component_nr;
+       ALLOC_ARRAY(revs->bloom_keys, revs->bloom_keys_nr);
+
+       fill_bloom_key(path, len, &revs->bloom_keys[0],
+                      revs->bloom_filter_settings);
+       path_component_nr = 1;
+
+       p = path + len - 1;
+       while (p > path) {
+               if (*p == '/')
+                       fill_bloom_key(path, p - path,
+                                      &revs->bloom_keys[path_component_nr++],
+                                      revs->bloom_filter_settings);
+               p--;
+       }
+
+       if (trace2_is_enabled() && !bloom_filter_atexit_registered) {
+               atexit(trace2_bloom_filter_statistics_atexit);
+               bloom_filter_atexit_registered = 1;
+       }
+
+       free(path_alloc);
+}
+
+static int check_maybe_different_in_bloom_filter(struct rev_info *revs,
+                                                struct commit *commit)
+{
+       struct bloom_filter *filter;
+       int result = 1, j;
+
+       if (!revs->repo->objects->commit_graph)
+               return -1;
+
+       if (commit_graph_generation(commit) == GENERATION_NUMBER_INFINITY)
+               return -1;
+
+       filter = get_bloom_filter(revs->repo, commit);
+
+       if (!filter) {
+               count_bloom_filter_not_present++;
+               return -1;
+       }
+
+       for (j = 0; result && j < revs->bloom_keys_nr; j++) {
+               result = bloom_filter_contains(filter,
+                                              &revs->bloom_keys[j],
+                                              revs->bloom_filter_settings);
+       }
+
+       if (result)
+               count_bloom_filter_maybe++;
+       else
+               count_bloom_filter_definitely_not++;
+
+       return result;
 }
 
 static int rev_compare_tree(struct rev_info *revs,
-                           struct commit *parent, struct commit *commit)
+                           struct commit *parent, struct commit *commit, int nth_parent)
 {
-       struct tree *t1 = parent->tree;
-       struct tree *t2 = commit->tree;
+       struct tree *t1 = get_commit_tree(parent);
+       struct tree *t2 = get_commit_tree(commit);
+       int bloom_ret = 1;
 
        if (!t1)
                return REV_TREE_NEW;
@@ -453,27 +796,36 @@ static int rev_compare_tree(struct rev_info *revs,
                        return REV_TREE_SAME;
        }
 
+       if (revs->bloom_keys_nr && !nth_parent) {
+               bloom_ret = check_maybe_different_in_bloom_filter(revs, commit);
+
+               if (bloom_ret == 0)
+                       return REV_TREE_SAME;
+       }
+
        tree_difference = REV_TREE_SAME;
-       DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
-       if (diff_tree_sha1(t1->object.oid.hash, t2->object.oid.hash, "",
-                          &revs->pruning) < 0)
-               return REV_TREE_DIFFERENT;
+       revs->pruning.flags.has_changes = 0;
+       diff_tree_oid(&t1->object.oid, &t2->object.oid, "", &revs->pruning);
+
+       if (!nth_parent)
+               if (bloom_ret == 1 && tree_difference == REV_TREE_SAME)
+                       count_bloom_filter_false_positive++;
+
        return tree_difference;
 }
 
 static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
 {
-       int retval;
-       struct tree *t1 = commit->tree;
+       struct tree *t1 = get_commit_tree(commit);
 
        if (!t1)
                return 0;
 
        tree_difference = REV_TREE_SAME;
-       DIFF_OPT_CLR(&revs->pruning, HAS_CHANGES);
-       retval = diff_tree_sha1(NULL, t1->object.oid.hash, "", &revs->pruning);
+       revs->pruning.flags.has_changes = 0;
+       diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
 
-       return retval >= 0 && (tree_difference == REV_TREE_SAME);
+       return tree_difference == REV_TREE_SAME;
 }
 
 struct treesame_state {
@@ -602,7 +954,7 @@ static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
        if (!revs->prune)
                return;
 
-       if (!commit->tree)
+       if (!get_commit_tree(commit))
                return;
 
        if (!commit->parents) {
@@ -651,11 +1003,11 @@ static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
                                        ts->treesame[0] = 1;
                        }
                }
-               if (parse_commit(p) < 0)
+               if (repo_parse_commit(revs->repo, p) < 0)
                        die("cannot simplify commit %s (because of %s)",
                            oid_to_hex(&commit->object.oid),
                            oid_to_hex(&p->object.oid));
-               switch (rev_compare_tree(revs, p, commit)) {
+               switch (rev_compare_tree(revs, p, commit, nth_parent)) {
                case REV_TREE_SAME:
                        if (!revs->simplify_history || !relevant_commit(p)) {
                                /* Even if a merge with an uninteresting
@@ -670,7 +1022,19 @@ static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
                        }
                        parent->next = NULL;
                        commit->parents = parent;
-                       commit->object.flags |= TREESAME;
+
+                       /*
+                        * A merge commit is a "diversion" if it is not
+                        * TREESAME to its first parent but is TREESAME
+                        * to a later parent. In the simplified history,
+                        * we "divert" the history walk to the later
+                        * parent. These commits are shown when "show_pulls"
+                        * is enabled, so do not mark the object as
+                        * TREESAME here.
+                        */
+                       if (!revs->show_pulls || !nth_parent)
+                               commit->object.flags |= TREESAME;
+
                        return;
 
                case REV_TREE_NEW:
@@ -684,7 +1048,7 @@ static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
                                 * IOW, we pretend this parent is a
                                 * "root" commit.
                                 */
-                               if (parse_commit(p) < 0)
+                               if (repo_parse_commit(revs->repo, p) < 0)
                                        die("cannot simplify commit %s (invalid %s)",
                                            oid_to_hex(&commit->object.oid),
                                            oid_to_hex(&p->object.oid));
@@ -697,6 +1061,10 @@ static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
                                relevant_change = 1;
                        else
                                irrelevant_change = 1;
+
+                       if (!nth_parent)
+                               commit->object.flags |= PULL_MERGE;
+
                        continue;
                }
                die("bad tree compare for commit %s", oid_to_hex(&commit->object.oid));
@@ -716,26 +1084,11 @@ static void try_to_simplify_commit(struct rev_info *revs, struct commit *commit)
                commit->object.flags |= TREESAME;
 }
 
-static void commit_list_insert_by_date_cached(struct commit *p, struct commit_list **head,
-                   struct commit_list *cached_base, struct commit_list **cache)
-{
-       struct commit_list *new_entry;
-
-       if (cached_base && p->date < cached_base->item->date)
-               new_entry = commit_list_insert_by_date(p, &cached_base->next);
-       else
-               new_entry = commit_list_insert_by_date(p, head);
-
-       if (cache && (!*cache || p->date < (*cache)->item->date))
-               *cache = new_entry;
-}
-
-static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
-                   struct commit_list **list, struct commit_list **cache_ptr)
+static int process_parents(struct rev_info *revs, struct commit *commit,
+                          struct commit_list **list, struct prio_queue *queue)
 {
        struct commit_list *parent = commit->parents;
        unsigned left_flag;
-       struct commit_list *cached_base = cache_ptr ? *cache_ptr : NULL;
 
        if (commit->object.flags & ADDED)
                return 0;
@@ -763,14 +1116,17 @@ static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
                        parent = parent->next;
                        if (p)
                                p->object.flags |= UNINTERESTING;
-                       if (parse_commit_gently(p, 1) < 0)
+                       if (repo_parse_commit_gently(revs->repo, p, 1) < 0)
                                continue;
                        if (p->parents)
                                mark_parents_uninteresting(p);
                        if (p->object.flags & SEEN)
                                continue;
                        p->object.flags |= SEEN;
-                       commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
+                       if (list)
+                               commit_list_insert_by_date(p, list);
+                       if (queue)
+                               prio_queue_put(queue, p);
                }
                return 0;
        }
@@ -789,15 +1145,30 @@ static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
 
        for (parent = commit->parents; parent; parent = parent->next) {
                struct commit *p = parent->item;
-
-               if (parse_commit_gently(p, revs->ignore_missing_links) < 0)
+               int gently = revs->ignore_missing_links ||
+                            revs->exclude_promisor_objects;
+               if (repo_parse_commit_gently(revs->repo, p, gently) < 0) {
+                       if (revs->exclude_promisor_objects &&
+                           is_promisor_object(&p->object.oid)) {
+                               if (revs->first_parent_only)
+                                       break;
+                               continue;
+                       }
                        return -1;
-               if (revs->show_source && !p->util)
-                       p->util = commit->util;
+               }
+               if (revs->sources) {
+                       char **slot = revision_sources_at(revs->sources, p);
+
+                       if (!*slot)
+                               *slot = *revision_sources_at(revs->sources, commit);
+               }
                p->object.flags |= left_flag;
                if (!(p->object.flags & SEEN)) {
                        p->object.flags |= SEEN;
-                       commit_list_insert_by_date_cached(p, list, cached_base, cache_ptr);
+                       if (list)
+                               commit_list_insert_by_date(p, list);
+                       if (queue)
+                               prio_queue_put(queue, p);
                }
                if (revs->first_parent_only)
                        break;
@@ -829,7 +1200,7 @@ static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
                return;
 
        left_first = left_count < right_count;
-       init_patch_ids(&ids);
+       init_patch_ids(revs->repo, &ids);
        ids.diffopts.pathspec = revs->diffopt.pathspec;
 
        /* Compute patch-ids for one side */
@@ -846,7 +1217,7 @@ static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
                 */
                if (left_first != !!(flags & SYMMETRIC_LEFT))
                        continue;
-               commit->util = add_commit_patch_id(commit, &ids);
+               add_commit_patch_id(commit, &ids);
        }
 
        /* either cherry_mark or cherry_pick are true */
@@ -870,24 +1241,14 @@ static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
                /*
                 * Have we seen the same patch id?
                 */
-               id = has_commit_patch_id(commit, &ids);
+               id = patch_id_iter_first(commit, &ids);
                if (!id)
                        continue;
-               id->seen = 1;
-               commit->object.flags |= cherry_flag;
-       }
-
-       /* Now check the original side for seen ones */
-       for (p = list; p; p = p->next) {
-               struct commit *commit = p->item;
-               struct patch_id *ent;
 
-               ent = commit->util;
-               if (!ent)
-                       continue;
-               if (ent->seen)
-                       commit->object.flags |= cherry_flag;
-               commit->util = NULL;
+               commit->object.flags |= cherry_flag;
+               do {
+                       id->commit->object.flags |= cherry_flag;
+               } while ((id = patch_id_iter_next(id, &ids)));
        }
 
        free_patch_ids(&ids);
@@ -896,7 +1257,7 @@ static void cherry_pick_list(struct commit_list *list, struct rev_info *revs)
 /* How many extra uninteresting commits we want to see.. */
 #define SLOP 5
 
-static int still_interesting(struct commit_list *src, unsigned long date, int slop,
+static int still_interesting(struct commit_list *src, timestamp_t date, int slop,
                             struct commit **interesting_cache)
 {
        /*
@@ -1030,7 +1391,7 @@ static void limit_left_right(struct commit_list *list, struct rev_info *revs)
 static int limit_list(struct rev_info *revs)
 {
        int slop = SLOP;
-       unsigned long date = ~0ul;
+       timestamp_t date = TIME_MAX;
        struct commit_list *list = revs->commits;
        struct commit_list *newlist = NULL;
        struct commit_list **p = &newlist;
@@ -1053,21 +1414,17 @@ static int limit_list(struct rev_info *revs)
 
                if (revs->max_age != -1 && (commit->date < revs->max_age))
                        obj->flags |= UNINTERESTING;
-               if (add_parents_to_list(revs, commit, &list, NULL) < 0)
+               if (process_parents(revs, commit, &list, NULL) < 0)
                        return -1;
                if (obj->flags & UNINTERESTING) {
                        mark_parents_uninteresting(commit);
-                       if (revs->show_all)
-                               p = &commit_list_insert(commit, p)->next;
                        slop = still_interesting(list, date, slop, &interesting_cache);
                        if (slop)
                                continue;
-                       /* If showing all, add the whole pending list to the end */
-                       if (revs->show_all)
-                               *p = list;
                        break;
                }
-               if (revs->min_age != -1 && (commit->date > revs->min_age))
+               if (revs->min_age != -1 && (commit->date > revs->min_age) &&
+                   !revs->line_level_traverse)
                        continue;
                date = commit->date;
                p = &commit_list_insert(commit, p)->next;
@@ -1117,7 +1474,7 @@ static void add_rev_cmdline(struct rev_info *revs,
                            unsigned flags)
 {
        struct rev_cmdline_info *info = &revs->cmdline;
-       int nr = info->nr;
+       unsigned int nr = info->nr;
 
        ALLOC_GROW(info->rev, nr + 1, info->alloc);
        info->rev[nr].item = item;
@@ -1145,6 +1502,7 @@ struct all_refs_cb {
        int warned_bad_reflog;
        struct rev_info *all_revs;
        const char *name_for_errormsg;
+       struct worktree *wt;
 };
 
 int ref_excluded(struct string_list *ref_excludes, const char *path)
@@ -1154,7 +1512,7 @@ int ref_excluded(struct string_list *ref_excludes, const char *path)
        if (!ref_excludes)
                return 0;
        for_each_string_list_item(item, ref_excludes) {
-               if (!wildmatch(item->string, path, 0, NULL))
+               if (!wildmatch(item->string, path, 0))
                        return 1;
        }
        return 0;
@@ -1169,9 +1527,9 @@ static int handle_one_ref(const char *path, const struct object_id *oid,
        if (ref_excluded(cb->all_revs->ref_excludes, path))
            return 0;
 
-       object = get_reference(cb->all_revs, path, oid->hash, cb->all_flags);
+       object = get_reference(cb->all_revs, path, oid, cb->all_flags);
        add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
-       add_pending_sha1(cb->all_revs, path, oid->hash, cb->all_flags);
+       add_pending_oid(cb->all_revs, path, oid, cb->all_flags);
        return 0;
 }
 
@@ -1180,6 +1538,8 @@ static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
 {
        cb->all_revs = revs;
        cb->all_flags = flags;
+       revs->rev_input_given = 1;
+       cb->wt = NULL;
 }
 
 void clear_ref_exclusion(struct string_list **ref_excludes_p)
@@ -1200,19 +1560,26 @@ void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
        string_list_append(*ref_excludes_p, exclude);
 }
 
-static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
-               int (*for_each)(const char *, each_ref_fn, void *))
+static void handle_refs(struct ref_store *refs,
+                       struct rev_info *revs, unsigned flags,
+                       int (*for_each)(struct ref_store *, each_ref_fn, void *))
 {
        struct all_refs_cb cb;
+
+       if (!refs) {
+               /* this could happen with uninitialized submodules */
+               return;
+       }
+
        init_all_refs_cb(&cb, revs, flags);
-       for_each(submodule, handle_one_ref, &cb);
+       for_each(refs, handle_one_ref, &cb);
 }
 
-static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
+static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
 {
        struct all_refs_cb *cb = cb_data;
-       if (!is_null_sha1(sha1)) {
-               struct object *o = parse_object(sha1);
+       if (!is_null_oid(oid)) {
+               struct object *o = parse_object(cb->all_revs->repo, oid);
                if (o) {
                        o->flags |= cb->all_flags;
                        /* ??? CMDLINEFLAGS ??? */
@@ -1226,42 +1593,73 @@ static void handle_one_reflog_commit(unsigned char *sha1, void *cb_data)
        }
 }
 
-static int handle_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
-               const char *email, unsigned long timestamp, int tz,
+static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
+               const char *email, timestamp_t timestamp, int tz,
                const char *message, void *cb_data)
 {
-       handle_one_reflog_commit(osha1, cb_data);
-       handle_one_reflog_commit(nsha1, cb_data);
+       handle_one_reflog_commit(ooid, cb_data);
+       handle_one_reflog_commit(noid, cb_data);
        return 0;
 }
 
-static int handle_one_reflog(const char *path, const struct object_id *oid,
+static int handle_one_reflog(const char *refname_in_wt,
+                            const struct object_id *oid,
                             int flag, void *cb_data)
 {
        struct all_refs_cb *cb = cb_data;
+       struct strbuf refname = STRBUF_INIT;
+
        cb->warned_bad_reflog = 0;
-       cb->name_for_errormsg = path;
-       for_each_reflog_ent(path, handle_one_reflog_ent, cb_data);
+       strbuf_worktree_ref(cb->wt, &refname, refname_in_wt);
+       cb->name_for_errormsg = refname.buf;
+       refs_for_each_reflog_ent(get_main_ref_store(the_repository),
+                                refname.buf,
+                                handle_one_reflog_ent, cb_data);
+       strbuf_release(&refname);
        return 0;
 }
 
+static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
+{
+       struct worktree **worktrees, **p;
+
+       worktrees = get_worktrees();
+       for (p = worktrees; *p; p++) {
+               struct worktree *wt = *p;
+
+               if (wt->is_current)
+                       continue;
+
+               cb->wt = wt;
+               refs_for_each_reflog(get_worktree_ref_store(wt),
+                                    handle_one_reflog,
+                                    cb);
+       }
+       free_worktrees(worktrees);
+}
+
 void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
 {
        struct all_refs_cb cb;
 
        cb.all_revs = revs;
        cb.all_flags = flags;
+       cb.wt = NULL;
        for_each_reflog(handle_one_reflog, &cb);
+
+       if (!revs->single_worktree)
+               add_other_reflogs_to_pending(&cb);
 }
 
 static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
-                          struct strbuf *path)
+                          struct strbuf *path, unsigned int flags)
 {
        size_t baselen = path->len;
        int i;
 
        if (it->entry_count >= 0) {
-               struct tree *tree = lookup_tree(it->sha1);
+               struct tree *tree = lookup_tree(revs->repo, &it->oid);
+               tree->object.flags |= flags;
                add_pending_object_with_path(revs, &tree->object, "",
                                             040000, path->buf);
        }
@@ -1269,66 +1667,131 @@ static void add_cache_tree(struct cache_tree *it, struct rev_info *revs,
        for (i = 0; i < it->subtree_nr; i++) {
                struct cache_tree_sub *sub = it->down[i];
                strbuf_addf(path, "%s%s", baselen ? "/" : "", sub->name);
-               add_cache_tree(sub->cache_tree, revs, path);
+               add_cache_tree(sub->cache_tree, revs, path, flags);
                strbuf_setlen(path, baselen);
        }
 
 }
 
-void add_index_objects_to_pending(struct rev_info *revs, unsigned flags)
+static void do_add_index_objects_to_pending(struct rev_info *revs,
+                                           struct index_state *istate,
+                                           unsigned int flags)
 {
        int i;
 
-       read_cache();
-       for (i = 0; i < active_nr; i++) {
-               struct cache_entry *ce = active_cache[i];
+       for (i = 0; i < istate->cache_nr; i++) {
+               struct cache_entry *ce = istate->cache[i];
                struct blob *blob;
 
                if (S_ISGITLINK(ce->ce_mode))
                        continue;
 
-               blob = lookup_blob(ce->sha1);
+               blob = lookup_blob(revs->repo, &ce->oid);
                if (!blob)
                        die("unable to add index blob to traversal");
+               blob->object.flags |= flags;
                add_pending_object_with_path(revs, &blob->object, "",
                                             ce->ce_mode, ce->name);
        }
 
-       if (active_cache_tree) {
+       if (istate->cache_tree) {
                struct strbuf path = STRBUF_INIT;
-               add_cache_tree(active_cache_tree, revs, &path);
+               add_cache_tree(istate->cache_tree, revs, &path, flags);
                strbuf_release(&path);
        }
 }
 
-static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
+void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
+{
+       struct worktree **worktrees, **p;
+
+       repo_read_index(revs->repo);
+       do_add_index_objects_to_pending(revs, revs->repo->index, flags);
+
+       if (revs->single_worktree)
+               return;
+
+       worktrees = get_worktrees();
+       for (p = worktrees; *p; p++) {
+               struct worktree *wt = *p;
+               struct index_state istate = { NULL };
+
+               if (wt->is_current)
+                       continue; /* current index already taken care of */
+
+               if (read_index_from(&istate,
+                                   worktree_git_path(wt, "index"),
+                                   get_worktree_git_dir(wt)) > 0)
+                       do_add_index_objects_to_pending(revs, &istate, flags);
+               discard_index(&istate);
+       }
+       free_worktrees(worktrees);
+}
+
+struct add_alternate_refs_data {
+       struct rev_info *revs;
+       unsigned int flags;
+};
+
+static void add_one_alternate_ref(const struct object_id *oid,
+                                 void *vdata)
+{
+       const char *name = ".alternate";
+       struct add_alternate_refs_data *data = vdata;
+       struct object *obj;
+
+       obj = get_reference(data->revs, name, oid, data->flags);
+       add_rev_cmdline(data->revs, obj, name, REV_CMD_REV, data->flags);
+       add_pending_object(data->revs, obj, name);
+}
+
+static void add_alternate_refs_to_pending(struct rev_info *revs,
+                                         unsigned int flags)
 {
-       unsigned char sha1[20];
+       struct add_alternate_refs_data data;
+       data.revs = revs;
+       data.flags = flags;
+       for_each_alternate_ref(add_one_alternate_ref, &data);
+}
+
+static int add_parents_only(struct rev_info *revs, const char *arg_, int flags,
+                           int exclude_parent)
+{
+       struct object_id oid;
        struct object *it;
        struct commit *commit;
        struct commit_list *parents;
+       int parent_number;
        const char *arg = arg_;
 
        if (*arg == '^') {
                flags ^= UNINTERESTING | BOTTOM;
                arg++;
        }
-       if (get_sha1_committish(arg, sha1))
+       if (get_oid_committish(arg, &oid))
                return 0;
        while (1) {
-               it = get_reference(revs, arg, sha1, 0);
+               it = get_reference(revs, arg, &oid, 0);
                if (!it && revs->ignore_missing)
                        return 0;
                if (it->type != OBJ_TAG)
                        break;
                if (!((struct tag*)it)->tagged)
                        return 0;
-               hashcpy(sha1, ((struct tag*)it)->tagged->oid.hash);
+               oidcpy(&oid, &((struct tag*)it)->tagged->oid);
        }
        if (it->type != OBJ_COMMIT)
                return 0;
        commit = (struct commit *)it;
-       for (parents = commit->parents; parents; parents = parents->next) {
+       if (exclude_parent &&
+           exclude_parent > commit_list_count(commit->parents))
+               return 0;
+       for (parents = commit->parents, parent_number = 1;
+            parents;
+            parents = parents->next, parent_number++) {
+               if (exclude_parent && parent_number != exclude_parent)
+                       continue;
+
                it = &parents->item->object;
                it->flags |= flags;
                add_rev_cmdline(revs, it, arg_, REV_CMD_PARENTS_ONLY, flags);
@@ -1337,17 +1800,22 @@ static int add_parents_only(struct rev_info *revs, const char *arg_, int flags)
        return 1;
 }
 
-void init_revisions(struct rev_info *revs, const char *prefix)
+void repo_init_revisions(struct repository *r,
+                        struct rev_info *revs,
+                        const char *prefix)
 {
        memset(revs, 0, sizeof(*revs));
 
+       revs->repo = r;
        revs->abbrev = DEFAULT_ABBREV;
-       revs->ignore_merges = 1;
+       revs->ignore_merges = -1;
        revs->simplify_history = 1;
-       DIFF_OPT_SET(&revs->pruning, RECURSIVE);
-       DIFF_OPT_SET(&revs->pruning, QUICK);
+       revs->pruning.repo = r;
+       revs->pruning.flags.recursive = 1;
+       revs->pruning.flags.quick = 1;
        revs->pruning.add_remove = file_add_remove;
        revs->pruning.change = file_change;
+       revs->pruning.change_fn_data = revs;
        revs->sort_order = REV_SORT_IN_GRAPH_ORDER;
        revs->dense = 1;
        revs->prefix = prefix;
@@ -1356,26 +1824,26 @@ void init_revisions(struct rev_info *revs, const char *prefix)
        revs->skip_count = -1;
        revs->max_count = -1;
        revs->max_parents = -1;
+       revs->expand_tabs_in_log = -1;
 
        revs->commit_format = CMIT_FMT_DEFAULT;
+       revs->expand_tabs_in_log_default = 8;
 
-       init_grep_defaults();
-       grep_init(&revs->grep_filter, prefix);
+       grep_init(&revs->grep_filter, revs->repo, prefix);
        revs->grep_filter.status_only = 1;
-       revs->grep_filter.regflags = REG_NEWLINE;
 
-       diff_setup(&revs->diffopt);
+       repo_diff_setup(revs->repo, &revs->diffopt);
        if (prefix && !revs->diffopt.prefix) {
                revs->diffopt.prefix = prefix;
                revs->diffopt.prefix_length = strlen(prefix);
        }
 
-       revs->notes_opt.use_default_notes = -1;
+       init_display_notes(&revs->notes_opt);
 }
 
 static void add_pending_commit_list(struct rev_info *revs,
-                                    struct commit_list *commit_list,
-                                    unsigned int flags)
+                                   struct commit_list *commit_list,
+                                   unsigned int flags)
 {
        while (commit_list) {
                struct object *object = &commit_list->item->object;
@@ -1389,16 +1857,17 @@ static void prepare_show_merge(struct rev_info *revs)
 {
        struct commit_list *bases;
        struct commit *head, *other;
-       unsigned char sha1[20];
+       struct object_id oid;
        const char **prune = NULL;
        int i, prune_num = 1; /* counting terminating NULL */
+       struct index_state *istate = revs->repo->index;
 
-       if (get_sha1("HEAD", sha1))
+       if (get_oid("HEAD", &oid))
                die("--merge without HEAD?");
-       head = lookup_commit_or_die(sha1, "HEAD");
-       if (get_sha1("MERGE_HEAD", sha1))
+       head = lookup_commit_or_die(&oid, "HEAD");
+       if (get_oid("MERGE_HEAD", &oid))
                die("--merge without MERGE_HEAD?");
-       other = lookup_commit_or_die(sha1, "MERGE_HEAD");
+       other = lookup_commit_or_die(&oid, "MERGE_HEAD");
        add_pending_object(revs, &head->object, "HEAD");
        add_pending_object(revs, &other->object, "MERGE_HEAD");
        bases = get_merge_bases(head, other);
@@ -1407,140 +1876,190 @@ static void prepare_show_merge(struct rev_info *revs)
        free_commit_list(bases);
        head->object.flags |= SYMMETRIC_LEFT;
 
-       if (!active_nr)
-               read_cache();
-       for (i = 0; i < active_nr; i++) {
-               const struct cache_entry *ce = active_cache[i];
+       if (!istate->cache_nr)
+               repo_read_index(revs->repo);
+       for (i = 0; i < istate->cache_nr; i++) {
+               const struct cache_entry *ce = istate->cache[i];
                if (!ce_stage(ce))
                        continue;
-               if (ce_path_match(ce, &revs->prune_data, NULL)) {
+               if (ce_path_match(istate, ce, &revs->prune_data, NULL)) {
                        prune_num++;
                        REALLOC_ARRAY(prune, prune_num);
                        prune[prune_num-2] = ce->name;
                        prune[prune_num-1] = NULL;
                }
-               while ((i+1 < active_nr) &&
-                      ce_same_name(ce, active_cache[i+1]))
+               while ((i+1 < istate->cache_nr) &&
+                      ce_same_name(ce, istate->cache[i+1]))
                        i++;
        }
-       free_pathspec(&revs->prune_data);
+       clear_pathspec(&revs->prune_data);
        parse_pathspec(&revs->prune_data, PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
                       PATHSPEC_PREFER_FULL | PATHSPEC_LITERAL_PATH, "", prune);
        revs->limited = 1;
 }
 
-int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
+static int dotdot_missing(const char *arg, char *dotdot,
+                         struct rev_info *revs, int symmetric)
+{
+       if (revs->ignore_missing)
+               return 0;
+       /* de-munge so we report the full argument */
+       *dotdot = '.';
+       die(symmetric
+           ? "Invalid symmetric difference expression %s"
+           : "Invalid revision range %s", arg);
+}
+
+static int handle_dotdot_1(const char *arg, char *dotdot,
+                          struct rev_info *revs, int flags,
+                          int cant_be_filename,
+                          struct object_context *a_oc,
+                          struct object_context *b_oc)
+{
+       const char *a_name, *b_name;
+       struct object_id a_oid, b_oid;
+       struct object *a_obj, *b_obj;
+       unsigned int a_flags, b_flags;
+       int symmetric = 0;
+       unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
+       unsigned int oc_flags = GET_OID_COMMITTISH | GET_OID_RECORD_PATH;
+
+       a_name = arg;
+       if (!*a_name)
+               a_name = "HEAD";
+
+       b_name = dotdot + 2;
+       if (*b_name == '.') {
+               symmetric = 1;
+               b_name++;
+       }
+       if (!*b_name)
+               b_name = "HEAD";
+
+       if (get_oid_with_context(revs->repo, a_name, oc_flags, &a_oid, a_oc) ||
+           get_oid_with_context(revs->repo, b_name, oc_flags, &b_oid, b_oc))
+               return -1;
+
+       if (!cant_be_filename) {
+               *dotdot = '.';
+               verify_non_filename(revs->prefix, arg);
+               *dotdot = '\0';
+       }
+
+       a_obj = parse_object(revs->repo, &a_oid);
+       b_obj = parse_object(revs->repo, &b_oid);
+       if (!a_obj || !b_obj)
+               return dotdot_missing(arg, dotdot, revs, symmetric);
+
+       if (!symmetric) {
+               /* just A..B */
+               b_flags = flags;
+               a_flags = flags_exclude;
+       } else {
+               /* A...B -- find merge bases between the two */
+               struct commit *a, *b;
+               struct commit_list *exclude;
+
+               a = lookup_commit_reference(revs->repo, &a_obj->oid);
+               b = lookup_commit_reference(revs->repo, &b_obj->oid);
+               if (!a || !b)
+                       return dotdot_missing(arg, dotdot, revs, symmetric);
+
+               exclude = get_merge_bases(a, b);
+               add_rev_cmdline_list(revs, exclude, REV_CMD_MERGE_BASE,
+                                    flags_exclude);
+               add_pending_commit_list(revs, exclude, flags_exclude);
+               free_commit_list(exclude);
+
+               b_flags = flags;
+               a_flags = flags | SYMMETRIC_LEFT;
+       }
+
+       a_obj->flags |= a_flags;
+       b_obj->flags |= b_flags;
+       add_rev_cmdline(revs, a_obj, a_name, REV_CMD_LEFT, a_flags);
+       add_rev_cmdline(revs, b_obj, b_name, REV_CMD_RIGHT, b_flags);
+       add_pending_object_with_path(revs, a_obj, a_name, a_oc->mode, a_oc->path);
+       add_pending_object_with_path(revs, b_obj, b_name, b_oc->mode, b_oc->path);
+       return 0;
+}
+
+static int handle_dotdot(const char *arg,
+                        struct rev_info *revs, int flags,
+                        int cant_be_filename)
+{
+       struct object_context a_oc, b_oc;
+       char *dotdot = strstr(arg, "..");
+       int ret;
+
+       if (!dotdot)
+               return -1;
+
+       memset(&a_oc, 0, sizeof(a_oc));
+       memset(&b_oc, 0, sizeof(b_oc));
+
+       *dotdot = '\0';
+       ret = handle_dotdot_1(arg, dotdot, revs, flags, cant_be_filename,
+                             &a_oc, &b_oc);
+       *dotdot = '.';
+
+       free(a_oc.path);
+       free(b_oc.path);
+
+       return ret;
+}
+
+static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
 {
        struct object_context oc;
-       char *dotdot;
+       char *mark;
        struct object *object;
-       unsigned char sha1[20];
+       struct object_id oid;
        int local_flags;
        const char *arg = arg_;
        int cant_be_filename = revarg_opt & REVARG_CANNOT_BE_FILENAME;
-       unsigned get_sha1_flags = 0;
+       unsigned get_sha1_flags = GET_OID_RECORD_PATH;
 
        flags = flags & UNINTERESTING ? flags | BOTTOM : flags & ~BOTTOM;
 
-       dotdot = strstr(arg, "..");
-       if (dotdot) {
-               unsigned char from_sha1[20];
-               const char *next = dotdot + 2;
-               const char *this = arg;
-               int symmetric = *next == '.';
-               unsigned int flags_exclude = flags ^ (UNINTERESTING | BOTTOM);
-               static const char head_by_default[] = "HEAD";
-               unsigned int a_flags;
-
-               *dotdot = 0;
-               next += symmetric;
-
-               if (!*next)
-                       next = head_by_default;
-               if (dotdot == arg)
-                       this = head_by_default;
-               if (this == head_by_default && next == head_by_default &&
-                   !symmetric) {
-                       /*
-                        * Just ".."?  That is not a range but the
-                        * pathspec for the parent directory.
-                        */
-                       if (!cant_be_filename) {
-                               *dotdot = '.';
-                               return -1;
-                       }
-               }
-               if (!get_sha1_committish(this, from_sha1) &&
-                   !get_sha1_committish(next, sha1)) {
-                       struct object *a_obj, *b_obj;
-
-                       if (!cant_be_filename) {
-                               *dotdot = '.';
-                               verify_non_filename(revs->prefix, arg);
-                       }
-
-                       a_obj = parse_object(from_sha1);
-                       b_obj = parse_object(sha1);
-                       if (!a_obj || !b_obj) {
-                       missing:
-                               if (revs->ignore_missing)
-                                       return 0;
-                               die(symmetric
-                                   ? "Invalid symmetric difference expression %s"
-                                   : "Invalid revision range %s", arg);
-                       }
+       if (!cant_be_filename && !strcmp(arg, "..")) {
+               /*
+                * Just ".."?  That is not a range but the
+                * pathspec for the parent directory.
+                */
+               return -1;
+       }
 
-                       if (!symmetric) {
-                               /* just A..B */
-                               a_flags = flags_exclude;
-                       } else {
-                               /* A...B -- find merge bases between the two */
-                               struct commit *a, *b;
-                               struct commit_list *exclude;
-
-                               a = (a_obj->type == OBJ_COMMIT
-                                    ? (struct commit *)a_obj
-                                    : lookup_commit_reference(a_obj->oid.hash));
-                               b = (b_obj->type == OBJ_COMMIT
-                                    ? (struct commit *)b_obj
-                                    : lookup_commit_reference(b_obj->oid.hash));
-                               if (!a || !b)
-                                       goto missing;
-                               exclude = get_merge_bases(a, b);
-                               add_rev_cmdline_list(revs, exclude,
-                                                    REV_CMD_MERGE_BASE,
-                                                    flags_exclude);
-                               add_pending_commit_list(revs, exclude,
-                                                       flags_exclude);
-                               free_commit_list(exclude);
-
-                               a_flags = flags | SYMMETRIC_LEFT;
-                       }
+       if (!handle_dotdot(arg, revs, flags, revarg_opt))
+               return 0;
 
-                       a_obj->flags |= a_flags;
-                       b_obj->flags |= flags;
-                       add_rev_cmdline(revs, a_obj, this,
-                                       REV_CMD_LEFT, a_flags);
-                       add_rev_cmdline(revs, b_obj, next,
-                                       REV_CMD_RIGHT, flags);
-                       add_pending_object(revs, a_obj, this);
-                       add_pending_object(revs, b_obj, next);
+       mark = strstr(arg, "^@");
+       if (mark && !mark[2]) {
+               *mark = 0;
+               if (add_parents_only(revs, arg, flags, 0))
                        return 0;
-               }
-               *dotdot = '.';
+               *mark = '^';
        }
-       dotdot = strstr(arg, "^@");
-       if (dotdot && !dotdot[2]) {
-               *dotdot = 0;
-               if (add_parents_only(revs, arg, flags))
-                       return 0;
-               *dotdot = '^';
+       mark = strstr(arg, "^!");
+       if (mark && !mark[2]) {
+               *mark = 0;
+               if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), 0))
+                       *mark = '^';
        }
-       dotdot = strstr(arg, "^!");
-       if (dotdot && !dotdot[2]) {
-               *dotdot = 0;
-               if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM)))
-                       *dotdot = '^';
+       mark = strstr(arg, "^-");
+       if (mark) {
+               int exclude_parent = 1;
+
+               if (mark[2]) {
+                       char *end;
+                       exclude_parent = strtoul(mark + 2, &end, 10);
+                       if (*end != '\0' || !exclude_parent)
+                               return -1;
+               }
+
+               *mark = 0;
+               if (!add_parents_only(revs, arg, flags ^ (UNINTERESTING | BOTTOM), exclude_parent))
+                       *mark = '^';
        }
 
        local_flags = 0;
@@ -1550,43 +2069,38 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
        }
 
        if (revarg_opt & REVARG_COMMITTISH)
-               get_sha1_flags = GET_SHA1_COMMITTISH;
+               get_sha1_flags |= GET_OID_COMMITTISH;
 
-       if (get_sha1_with_context(arg, get_sha1_flags, sha1, &oc))
+       if (get_oid_with_context(revs->repo, arg, get_sha1_flags, &oid, &oc))
                return revs->ignore_missing ? 0 : -1;
        if (!cant_be_filename)
                verify_non_filename(revs->prefix, arg);
-       object = get_reference(revs, arg, sha1, flags ^ local_flags);
+       object = get_reference(revs, arg, &oid, flags ^ local_flags);
+       if (!object)
+               return revs->ignore_missing ? 0 : -1;
        add_rev_cmdline(revs, object, arg_, REV_CMD_REV, flags ^ local_flags);
-       add_pending_object_with_mode(revs, object, arg, oc.mode);
+       add_pending_object_with_path(revs, object, arg, oc.mode, oc.path);
+       free(oc.path);
        return 0;
 }
 
-struct cmdline_pathspec {
-       int alloc;
-       int nr;
-       const char **path;
-};
-
-static void append_prune_data(struct cmdline_pathspec *prune, const char **av)
+int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
 {
-       while (*av) {
-               ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
-               prune->path[prune->nr++] = *(av++);
-       }
+       int ret = handle_revision_arg_1(arg, revs, flags, revarg_opt);
+       if (!ret)
+               revs->rev_input_given = 1;
+       return ret;
 }
 
-static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
-                                    struct cmdline_pathspec *prune)
+static void read_pathspec_from_stdin(struct strbuf *sb,
+                                    struct strvec *prune)
 {
-       while (strbuf_getline(sb, stdin) != EOF) {
-               ALLOC_GROW(prune->path, prune->nr + 1, prune->alloc);
-               prune->path[prune->nr++] = xstrdup(sb->buf);
-       }
+       while (strbuf_getline(sb, stdin) != EOF)
+               strvec_push(prune, sb->buf);
 }
 
 static void read_revisions_from_stdin(struct rev_info *revs,
-                                     struct cmdline_pathspec *prune)
+                                     struct strvec *prune)
 {
        struct strbuf sb;
        int seen_dashdash = 0;
@@ -1612,7 +2126,7 @@ static void read_revisions_from_stdin(struct rev_info *revs,
                        die("bad revision '%s'", sb.buf);
        }
        if (seen_dashdash)
-               read_pathspec_from_stdin(revs, &sb, prune);
+               read_pathspec_from_stdin(&sb, prune);
 
        strbuf_release(&sb);
        warn_on_object_refname_ambiguity = save_warning;
@@ -1634,11 +2148,13 @@ static void add_message_grep(struct rev_info *revs, const char *pattern)
 }
 
 static int handle_revision_opt(struct rev_info *revs, int argc, const char **argv,
-                              int *unkc, const char **unkv)
+                              int *unkc, const char **unkv,
+                              const struct setup_revision_opt* opt)
 {
        const char *arg = argv[0];
        const char *optarg;
        int argcount;
+       const unsigned hexsz = the_hash_algo->hexsz;
 
        /* pseudo revision arguments */
        if (!strcmp(arg, "--all") || !strcmp(arg, "--branches") ||
@@ -1647,6 +2163,7 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
            !strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
            !strcmp(arg, "--bisect") || starts_with(arg, "--glob=") ||
            !strcmp(arg, "--indexed-objects") ||
+           !strcmp(arg, "--alternate-refs") ||
            starts_with(arg, "--exclude=") ||
            starts_with(arg, "--branches=") || starts_with(arg, "--tags=") ||
            starts_with(arg, "--remotes=") || starts_with(arg, "--no-walk="))
@@ -1674,8 +2191,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
                revs->max_count = atoi(argv[1]);
                revs->no_walk = 0;
                return 2;
-       } else if (starts_with(arg, "-n")) {
-               revs->max_count = atoi(arg + 2);
+       } else if (skip_prefix(arg, "-n", &optarg)) {
+               revs->max_count = atoi(optarg);
                revs->no_walk = 0;
        } else if ((argcount = parse_long_opt("max-age", argv, &optarg))) {
                revs->max_age = atoi(optarg);
@@ -1727,23 +2244,19 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
                revs->simplify_by_decoration = 1;
                revs->limited = 1;
                revs->prune = 1;
-               load_ref_decorations(DECORATE_SHORT_REFS);
        } else if (!strcmp(arg, "--date-order")) {
                revs->sort_order = REV_SORT_BY_COMMIT_DATE;
                revs->topo_order = 1;
        } else if (!strcmp(arg, "--author-date-order")) {
                revs->sort_order = REV_SORT_BY_AUTHOR_DATE;
                revs->topo_order = 1;
-       } else if (starts_with(arg, "--early-output")) {
-               int count = 100;
-               switch (arg[14]) {
-               case '=':
-                       count = atoi(arg+15);
-                       /* Fallthrough */
-               case 0:
-                       revs->topo_order = 1;
-                      revs->early_output = count;
-               }
+       } else if (!strcmp(arg, "--early-output")) {
+               revs->early_output = 100;
+               revs->topo_order = 1;
+       } else if (skip_prefix(arg, "--early-output=", &optarg)) {
+               if (strtoul_ui(optarg, 10, &revs->early_output) < 0)
+                       die("'%s': not a non-negative integer", optarg);
+               revs->topo_order = 1;
        } else if (!strcmp(arg, "--parents")) {
                revs->rewrite_parents = 1;
                revs->print_parents = 1;
@@ -1751,21 +2264,21 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
                revs->dense = 1;
        } else if (!strcmp(arg, "--sparse")) {
                revs->dense = 0;
-       } else if (!strcmp(arg, "--show-all")) {
-               revs->show_all = 1;
+       } else if (!strcmp(arg, "--in-commit-order")) {
+               revs->tree_blobs_in_commit_order = 1;
        } else if (!strcmp(arg, "--remove-empty")) {
                revs->remove_empty_trees = 1;
        } else if (!strcmp(arg, "--merges")) {
                revs->min_parents = 2;
        } else if (!strcmp(arg, "--no-merges")) {
                revs->max_parents = 1;
-       } else if (starts_with(arg, "--min-parents=")) {
-               revs->min_parents = atoi(arg+14);
-       } else if (starts_with(arg, "--no-min-parents")) {
+       } else if (skip_prefix(arg, "--min-parents=", &optarg)) {
+               revs->min_parents = atoi(optarg);
+       } else if (!strcmp(arg, "--no-min-parents")) {
                revs->min_parents = 0;
-       } else if (starts_with(arg, "--max-parents=")) {
-               revs->max_parents = atoi(arg+14);
-       } else if (starts_with(arg, "--no-max-parents")) {
+       } else if (skip_prefix(arg, "--max-parents=", &optarg)) {
+               revs->max_parents = atoi(optarg);
+       } else if (!strcmp(arg, "--no-max-parents")) {
                revs->max_parents = -1;
        } else if (!strcmp(arg, "--boundary")) {
                revs->boundary = 1;
@@ -1822,20 +2335,38 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
        } else if (!strcmp(arg, "--unpacked")) {
                revs->unpacked = 1;
        } else if (starts_with(arg, "--unpacked=")) {
-               die("--unpacked=<packfile> no longer supported.");
+               die(_("--unpacked=<packfile> no longer supported"));
        } else if (!strcmp(arg, "-r")) {
                revs->diff = 1;
-               DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
+               revs->diffopt.flags.recursive = 1;
        } else if (!strcmp(arg, "-t")) {
                revs->diff = 1;
-               DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
-               DIFF_OPT_SET(&revs->diffopt, TREE_IN_RECURSIVE);
+               revs->diffopt.flags.recursive = 1;
+               revs->diffopt.flags.tree_in_recursive = 1;
        } else if (!strcmp(arg, "-m")) {
+               /*
+                * To "diff-index", "-m" means "match missing", and to the "log"
+                * family of commands, it means "show full diff for merges". Set
+                * both fields appropriately.
+                */
                revs->ignore_merges = 0;
+               revs->match_missing = 1;
+       } else if ((argcount = parse_long_opt("diff-merges", argv, &optarg))) {
+               if (!strcmp(optarg, "off")) {
+                       revs->ignore_merges = 1;
+               } else {
+                       die(_("unknown value for --diff-merges: %s"), optarg);
+               }
+               return argcount;
+       } else if (!strcmp(arg, "--no-diff-merges")) {
+               revs->ignore_merges = 1;
        } else if (!strcmp(arg, "-c")) {
                revs->diff = 1;
                revs->dense_combined_merges = 0;
                revs->combine_merges = 1;
+       } else if (!strcmp(arg, "--combined-all-paths")) {
+               revs->diff = 1;
+               revs->combined_all_paths = 1;
        } else if (!strcmp(arg, "--cc")) {
                revs->diff = 1;
                revs->dense_combined_merges = 1;
@@ -1846,52 +2377,49 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
                revs->verbose_header = 1;
                revs->pretty_given = 1;
                get_commit_format(NULL, revs);
-       } else if (starts_with(arg, "--pretty=") || starts_with(arg, "--format=")) {
+       } else if (skip_prefix(arg, "--pretty=", &optarg) ||
+                  skip_prefix(arg, "--format=", &optarg)) {
                /*
                 * Detached form ("--pretty X" as opposed to "--pretty=X")
                 * not allowed, since the argument is optional.
                 */
                revs->verbose_header = 1;
                revs->pretty_given = 1;
-               get_commit_format(arg+9, revs);
+               get_commit_format(optarg, revs);
+       } else if (!strcmp(arg, "--expand-tabs")) {
+               revs->expand_tabs_in_log = 8;
+       } else if (!strcmp(arg, "--no-expand-tabs")) {
+               revs->expand_tabs_in_log = 0;
+       } else if (skip_prefix(arg, "--expand-tabs=", &arg)) {
+               int val;
+               if (strtol_i(arg, 10, &val) < 0 || val < 0)
+                       die("'%s': not a non-negative integer", arg);
+               revs->expand_tabs_in_log = val;
        } else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
-               revs->show_notes = 1;
+               enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
                revs->show_notes_given = 1;
-               revs->notes_opt.use_default_notes = 1;
        } else if (!strcmp(arg, "--show-signature")) {
                revs->show_signature = 1;
-       } else if (!strcmp(arg, "--show-linear-break") ||
-                  starts_with(arg, "--show-linear-break=")) {
-               if (starts_with(arg, "--show-linear-break="))
-                       revs->break_bar = xstrdup(arg + 20);
-               else
-                       revs->break_bar = "                    ..........";
+       } else if (!strcmp(arg, "--no-show-signature")) {
+               revs->show_signature = 0;
+       } else if (!strcmp(arg, "--show-linear-break")) {
+               revs->break_bar = "                    ..........";
                revs->track_linear = 1;
                revs->track_first_time = 1;
-       } else if (starts_with(arg, "--show-notes=") ||
-                  starts_with(arg, "--notes=")) {
-               struct strbuf buf = STRBUF_INIT;
-               revs->show_notes = 1;
+       } else if (skip_prefix(arg, "--show-linear-break=", &optarg)) {
+               revs->break_bar = xstrdup(optarg);
+               revs->track_linear = 1;
+               revs->track_first_time = 1;
+       } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
+                  skip_prefix(arg, "--notes=", &optarg)) {
+               if (starts_with(arg, "--show-notes=") &&
+                   revs->notes_opt.use_default_notes < 0)
+                       revs->notes_opt.use_default_notes = 1;
+               enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
                revs->show_notes_given = 1;
-               if (starts_with(arg, "--show-notes")) {
-                       if (revs->notes_opt.use_default_notes < 0)
-                               revs->notes_opt.use_default_notes = 1;
-                       strbuf_addstr(&buf, arg+13);
-               }
-               else
-                       strbuf_addstr(&buf, arg+8);
-               expand_notes_ref(&buf);
-               string_list_append(&revs->notes_opt.extra_notes_refs,
-                                  strbuf_detach(&buf, NULL));
        } else if (!strcmp(arg, "--no-notes")) {
-               revs->show_notes = 0;
+               disable_display_notes(&revs->notes_opt, &revs->show_notes);
                revs->show_notes_given = 1;
-               revs->notes_opt.use_default_notes = -1;
-               /* we have been strdup'ing ourselves, so trick
-                * string_list into free()ing strings */
-               revs->notes_opt.extra_notes_refs.strdup_strings = 1;
-               string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
-               revs->notes_opt.extra_notes_refs.strdup_strings = 0;
        } else if (!strcmp(arg, "--standard-notes")) {
                revs->show_notes_given = 1;
                revs->notes_opt.use_default_notes = 1;
@@ -1906,6 +2434,10 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
                revs->topo_order = 1;
                revs->rewrite_parents = 1;
                revs->graph = graph_init(revs);
+       } else if (!strcmp(arg, "--encode-email-headers")) {
+               revs->encode_email_headers = 1;
+       } else if (!strcmp(arg, "--no-encode-email-headers")) {
+               revs->encode_email_headers = 0;
        } else if (!strcmp(arg, "--root")) {
                revs->show_root_diff = 1;
        } else if (!strcmp(arg, "--no-commit-id")) {
@@ -1916,12 +2448,12 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
                revs->abbrev = 0;
        } else if (!strcmp(arg, "--abbrev")) {
                revs->abbrev = DEFAULT_ABBREV;
-       } else if (starts_with(arg, "--abbrev=")) {
-               revs->abbrev = strtoul(arg + 9, NULL, 10);
+       } else if (skip_prefix(arg, "--abbrev=", &optarg)) {
+               revs->abbrev = strtoul(optarg, NULL, 10);
                if (revs->abbrev < MINIMUM_ABBREV)
                        revs->abbrev = MINIMUM_ABBREV;
-               else if (revs->abbrev > 40)
-                       revs->abbrev = 40;
+               else if (revs->abbrev > hexsz)
+                       revs->abbrev = hexsz;
        } else if (!strcmp(arg, "--abbrev-commit")) {
                revs->abbrev_commit = 1;
                revs->abbrev_commit_given = 1;
@@ -1930,6 +2462,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
        } else if (!strcmp(arg, "--full-diff")) {
                revs->diff = 1;
                revs->full_diff = 1;
+       } else if (!strcmp(arg, "--show-pulls")) {
+               revs->show_pulls = 1;
        } else if (!strcmp(arg, "--full-history")) {
                revs->simplify_history = 0;
        } else if (!strcmp(arg, "--relative-date")) {
@@ -1960,16 +2494,16 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
        } else if (!strcmp(arg, "--grep-debug")) {
                revs->grep_filter.debug = 1;
        } else if (!strcmp(arg, "--basic-regexp")) {
-               grep_set_pattern_type_option(GREP_PATTERN_TYPE_BRE, &revs->grep_filter);
+               revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_BRE;
        } else if (!strcmp(arg, "--extended-regexp") || !strcmp(arg, "-E")) {
-               grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, &revs->grep_filter);
+               revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_ERE;
        } else if (!strcmp(arg, "--regexp-ignore-case") || !strcmp(arg, "-i")) {
-               revs->grep_filter.regflags |= REG_ICASE;
-               DIFF_OPT_SET(&revs->diffopt, PICKAXE_IGNORE_CASE);
+               revs->grep_filter.ignore_case = 1;
+               revs->diffopt.pickaxe_opts |= DIFF_PICKAXE_IGNORE_CASE;
        } else if (!strcmp(arg, "--fixed-strings") || !strcmp(arg, "-F")) {
-               grep_set_pattern_type_option(GREP_PATTERN_TYPE_FIXED, &revs->grep_filter);
-       } else if (!strcmp(arg, "--perl-regexp")) {
-               grep_set_pattern_type_option(GREP_PATTERN_TYPE_PCRE, &revs->grep_filter);
+               revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_FIXED;
+       } else if (!strcmp(arg, "--perl-regexp") || !strcmp(arg, "-P")) {
+               revs->grep_filter.pattern_type_option = GREP_PATTERN_TYPE_PCRE;
        } else if (!strcmp(arg, "--all-match")) {
                revs->grep_filter.all_match = 1;
        } else if (!strcmp(arg, "--invert-grep")) {
@@ -1987,6 +2521,11 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
                revs->limited = 1;
        } else if (!strcmp(arg, "--ignore-missing")) {
                revs->ignore_missing = 1;
+       } else if (opt && opt->allow_exclude_promisor_objects &&
+                  !strcmp(arg, "--exclude-promisor-objects")) {
+               if (fetch_if_missing)
+                       BUG("exclude_promisor_objects can only be used when fetch_if_missing is 0");
+               revs->exclude_promisor_objects = 1;
        } else {
                int opts = diff_opt_parse(&revs->diffopt, argv, argc, revs->prefix);
                if (!opts)
@@ -2004,7 +2543,7 @@ void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
                        const char * const usagestr[])
 {
        int n = handle_revision_opt(revs, ctx->argc, ctx->argv,
-                                   &ctx->cpidx, ctx->out);
+                                   &ctx->cpidx, ctx->out, NULL);
        if (n <= 0) {
                error("unknown option `%s'", ctx->argv[0]);
                usage_with_options(usagestr, options);
@@ -2013,33 +2552,49 @@ void parse_revision_opt(struct rev_info *revs, struct parse_opt_ctx_t *ctx,
        ctx->argc -= n;
 }
 
-static int for_each_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data, const char *term) {
+static int for_each_bisect_ref(struct ref_store *refs, each_ref_fn fn,
+                              void *cb_data, const char *term)
+{
        struct strbuf bisect_refs = STRBUF_INIT;
        int status;
        strbuf_addf(&bisect_refs, "refs/bisect/%s", term);
-       status = for_each_ref_in_submodule(submodule, bisect_refs.buf, fn, cb_data);
+       status = refs_for_each_fullref_in(refs, bisect_refs.buf, fn, cb_data, 0);
        strbuf_release(&bisect_refs);
        return status;
 }
 
-static int for_each_bad_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
+static int for_each_bad_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
 {
-       return for_each_bisect_ref(submodule, fn, cb_data, term_bad);
+       return for_each_bisect_ref(refs, fn, cb_data, term_bad);
 }
 
-static int for_each_good_bisect_ref(const char *submodule, each_ref_fn fn, void *cb_data)
+static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
 {
-       return for_each_bisect_ref(submodule, fn, cb_data, term_good);
+       return for_each_bisect_ref(refs, fn, cb_data, term_good);
 }
 
 static int handle_revision_pseudo_opt(const char *submodule,
-                               struct rev_info *revs,
-                               int argc, const char **argv, int *flags)
+                                     struct rev_info *revs,
+                                     const char **argv, int *flags)
 {
        const char *arg = argv[0];
        const char *optarg;
+       struct ref_store *refs;
        int argcount;
 
+       if (submodule) {
+               /*
+                * We need some something like get_submodule_worktrees()
+                * before we can go through all worktrees of a submodule,
+                * .e.g with adding all HEADs from --all, which is not
+                * supported right now, so stick to single worktree.
+                */
+               if (!revs->single_worktree)
+                       BUG("--single-worktree cannot be used together with submodule");
+               refs = get_submodule_ref_store(submodule);
+       } else
+               refs = get_main_ref_store(revs->repo);
+
        /*
         * NOTE!
         *
@@ -2051,22 +2606,29 @@ static int handle_revision_pseudo_opt(const char *submodule,
         * register it in the list at the top of handle_revision_opt.
         */
        if (!strcmp(arg, "--all")) {
-               handle_refs(submodule, revs, *flags, for_each_ref_submodule);
-               handle_refs(submodule, revs, *flags, head_ref_submodule);
+               handle_refs(refs, revs, *flags, refs_for_each_ref);
+               handle_refs(refs, revs, *flags, refs_head_ref);
+               if (!revs->single_worktree) {
+                       struct all_refs_cb cb;
+
+                       init_all_refs_cb(&cb, revs, *flags);
+                       other_head_refs(handle_one_ref, &cb);
+               }
                clear_ref_exclusion(&revs->ref_excludes);
        } else if (!strcmp(arg, "--branches")) {
-               handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
+               handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
                clear_ref_exclusion(&revs->ref_excludes);
        } else if (!strcmp(arg, "--bisect")) {
                read_bisect_terms(&term_bad, &term_good);
-               handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
-               handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
+               handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
+               handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
+                           for_each_good_bisect_ref);
                revs->bisect = 1;
        } else if (!strcmp(arg, "--tags")) {
-               handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
+               handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
                clear_ref_exclusion(&revs->ref_excludes);
        } else if (!strcmp(arg, "--remotes")) {
-               handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
+               handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
                clear_ref_exclusion(&revs->ref_excludes);
        } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
                struct all_refs_cb cb;
@@ -2077,42 +2639,46 @@ static int handle_revision_pseudo_opt(const char *submodule,
        } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
                add_ref_exclusion(&revs->ref_excludes, optarg);
                return argcount;
-       } else if (starts_with(arg, "--branches=")) {
+       } else if (skip_prefix(arg, "--branches=", &optarg)) {
                struct all_refs_cb cb;
                init_all_refs_cb(&cb, revs, *flags);
-               for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
+               for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
                clear_ref_exclusion(&revs->ref_excludes);
-       } else if (starts_with(arg, "--tags=")) {
+       } else if (skip_prefix(arg, "--tags=", &optarg)) {
                struct all_refs_cb cb;
                init_all_refs_cb(&cb, revs, *flags);
-               for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
+               for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
                clear_ref_exclusion(&revs->ref_excludes);
-       } else if (starts_with(arg, "--remotes=")) {
+       } else if (skip_prefix(arg, "--remotes=", &optarg)) {
                struct all_refs_cb cb;
                init_all_refs_cb(&cb, revs, *flags);
-               for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
+               for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
                clear_ref_exclusion(&revs->ref_excludes);
        } else if (!strcmp(arg, "--reflog")) {
                add_reflogs_to_pending(revs, *flags);
        } else if (!strcmp(arg, "--indexed-objects")) {
                add_index_objects_to_pending(revs, *flags);
+       } else if (!strcmp(arg, "--alternate-refs")) {
+               add_alternate_refs_to_pending(revs, *flags);
        } else if (!strcmp(arg, "--not")) {
                *flags ^= UNINTERESTING | BOTTOM;
        } else if (!strcmp(arg, "--no-walk")) {
                revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
-       } else if (starts_with(arg, "--no-walk=")) {
+       } else if (skip_prefix(arg, "--no-walk=", &optarg)) {
                /*
                 * Detached form ("--no-walk X" as opposed to "--no-walk=X")
                 * not allowed, since the argument is optional.
                 */
-               if (!strcmp(arg + 10, "sorted"))
+               if (!strcmp(optarg, "sorted"))
                        revs->no_walk = REVISION_WALK_NO_WALK_SORTED;
-               else if (!strcmp(arg + 10, "unsorted"))
+               else if (!strcmp(optarg, "unsorted"))
                        revs->no_walk = REVISION_WALK_NO_WALK_UNSORTED;
                else
                        return error("invalid argument to --no-walk");
        } else if (!strcmp(arg, "--do-walk")) {
                revs->no_walk = 0;
+       } else if (!strcmp(arg, "--single-worktree")) {
+               revs->single_worktree = 1;
        } else {
                return 0;
        }
@@ -2122,11 +2688,10 @@ static int handle_revision_pseudo_opt(const char *submodule,
 
 static void NORETURN diagnose_missing_default(const char *def)
 {
-       unsigned char sha1[20];
        int flags;
        const char *refname;
 
-       refname = resolve_ref_unsafe(def, 0, sha1, &flags);
+       refname = resolve_ref_unsafe(def, 0, NULL, &flags);
        if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
                die(_("your current branch appears to be broken"));
 
@@ -2144,11 +2709,11 @@ static void NORETURN diagnose_missing_default(const char *def)
  */
 int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct setup_revision_opt *opt)
 {
-       int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0, revarg_opt;
-       struct cmdline_pathspec prune_data;
+       int i, flags, left, seen_dashdash, revarg_opt;
+       struct strvec prune_data = STRVEC_INIT;
        const char *submodule = NULL;
+       int seen_end_of_options = 0;
 
-       memset(&prune_data, 0, sizeof(prune_data));
        if (opt)
                submodule = opt->submodule;
 
@@ -2164,7 +2729,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
                        argv[i] = NULL;
                        argc = i;
                        if (argv[i + 1])
-                               append_prune_data(&prune_data, argv + i + 1);
+                               strvec_pushv(&prune_data, argv + i + 1);
                        seen_dashdash = 1;
                        break;
                }
@@ -2175,14 +2740,13 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
        revarg_opt = opt ? opt->revarg_opt : 0;
        if (seen_dashdash)
                revarg_opt |= REVARG_CANNOT_BE_FILENAME;
-       read_from_stdin = 0;
        for (left = i = 1; i < argc; i++) {
                const char *arg = argv[i];
-               if (*arg == '-') {
+               if (!seen_end_of_options && *arg == '-') {
                        int opts;
 
                        opts = handle_revision_pseudo_opt(submodule,
-                                               revs, argc - i, argv + i,
+                                               revs, argv + i,
                                                &flags);
                        if (opts > 0) {
                                i += opts - 1;
@@ -2194,13 +2758,19 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
                                        argv[left++] = arg;
                                        continue;
                                }
-                               if (read_from_stdin++)
+                               if (revs->read_from_stdin++)
                                        die("--stdin given twice?");
                                read_revisions_from_stdin(revs, &prune_data);
                                continue;
                        }
 
-                       opts = handle_revision_opt(revs, argc - i, argv + i, &left, argv);
+                       if (!strcmp(arg, "--end-of-options")) {
+                               seen_end_of_options = 1;
+                               continue;
+                       }
+
+                       opts = handle_revision_opt(revs, argc - i, argv + i,
+                                                  &left, argv, opt);
                        if (opts > 0) {
                                i += opts - 1;
                                continue;
@@ -2225,11 +2795,9 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
                        for (j = i; j < argc; j++)
                                verify_filename(revs->prefix, argv[j], j == i);
 
-                       append_prune_data(&prune_data, argv + i);
+                       strvec_pushv(&prune_data, argv + i);
                        break;
                }
-               else
-                       got_rev_arg = 1;
        }
 
        if (prune_data.nr) {
@@ -2247,11 +2815,10 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
                 *      call init_pathspec() to set revs->prune_data here.
                 * }
                 */
-               ALLOC_GROW(prune_data.path, prune_data.nr + 1, prune_data.alloc);
-               prune_data.path[prune_data.nr++] = NULL;
                parse_pathspec(&revs->prune_data, 0, 0,
-                              revs->prefix, prune_data.path);
+                              revs->prefix, prune_data.v);
        }
+       strvec_clear(&prune_data);
 
        if (revs->def == NULL)
                revs->def = opt ? opt->def : NULL;
@@ -2259,13 +2826,13 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
                opt->tweak(revs, opt);
        if (revs->show_merge)
                prepare_show_merge(revs);
-       if (revs->def && !revs->pending.nr && !got_rev_arg) {
-               unsigned char sha1[20];
+       if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
+               struct object_id oid;
                struct object *object;
                struct object_context oc;
-               if (get_sha1_with_context(revs->def, 0, sha1, &oc))
+               if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
                        diagnose_missing_default(revs->def);
-               object = get_reference(revs, revs->def, sha1, 0);
+               object = get_reference(revs, revs->def, &oid, 0);
                add_pending_object_with_mode(revs, object, revs->def, oc.mode);
        }
 
@@ -2274,40 +2841,53 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
                revs->diff = 1;
 
        /* Pickaxe, diff-filter and rename following need diffs */
-       if (revs->diffopt.pickaxe ||
+       if ((revs->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
            revs->diffopt.filter ||
-           DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
+           revs->diffopt.flags.follow_renames)
                revs->diff = 1;
 
-       if (revs->topo_order)
+       if (revs->diffopt.objfind)
+               revs->simplify_history = 0;
+
+       if (revs->line_level_traverse) {
+               if (want_ancestry(revs))
+                       revs->limited = 1;
+               revs->topo_order = 1;
+       }
+
+       if (revs->topo_order && !generation_numbers_enabled(the_repository))
                revs->limited = 1;
 
        if (revs->prune_data.nr) {
                copy_pathspec(&revs->pruning.pathspec, &revs->prune_data);
                /* Can't prune commits with rename following: the paths change.. */
-               if (!DIFF_OPT_TST(&revs->diffopt, FOLLOW_RENAMES))
+               if (!revs->diffopt.flags.follow_renames)
                        revs->prune = 1;
                if (!revs->full_diff)
                        copy_pathspec(&revs->diffopt.pathspec,
                                      &revs->prune_data);
        }
-       if (revs->combine_merges)
+       if (revs->combine_merges && revs->ignore_merges < 0)
                revs->ignore_merges = 0;
-       revs->diffopt.abbrev = revs->abbrev;
+       if (revs->ignore_merges < 0)
+               revs->ignore_merges = 1;
+       if (revs->combined_all_paths && !revs->combine_merges)
+               die("--combined-all-paths makes no sense without -c or --cc");
 
-       if (revs->line_level_traverse) {
-               revs->limited = 1;
-               revs->topo_order = 1;
-       }
+       revs->diffopt.abbrev = revs->abbrev;
 
        diff_setup_done(&revs->diffopt);
 
        grep_commit_pattern_type(GREP_PATTERN_TYPE_UNSPECIFIED,
                                 &revs->grep_filter);
+       if (!is_encoding_utf8(get_log_output_encoding()))
+               revs->grep_filter.ignore_locale = 1;
        compile_grep_patterns(&revs->grep_filter);
 
        if (revs->reverse && revs->reflog_info)
                die("cannot combine --reverse with --walk-reflogs");
+       if (revs->reflog_info && revs->limited)
+               die("cannot combine --walk-reflogs with history-limiting options");
        if (revs->rewrite_parents && revs->children.name)
                die("cannot combine --parents and --children");
 
@@ -2324,8 +2904,12 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
        if (!revs->reflog_info && revs->grep_filter.use_reflog_filter)
                die("cannot use --grep-reflog without --walk-reflogs");
 
-       if (revs->first_parent_only && revs->bisect)
-               die(_("--first-parent is incompatible with --bisect"));
+       if (revs->line_level_traverse &&
+           (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
+               die(_("-L does not yet support diff formats besides -p and -s"));
+
+       if (revs->expand_tabs_in_log < 0)
+               revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
 
        return left;
 }
@@ -2383,7 +2967,7 @@ static struct merge_simplify_state *locate_simplify_state(struct rev_info *revs,
        return st;
 }
 
-static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
+static int mark_redundant_parents(struct commit *commit)
 {
        struct commit_list *h = reduce_heads(commit->parents);
        int i = 0, marked = 0;
@@ -2419,7 +3003,7 @@ static int mark_redundant_parents(struct rev_info *revs, struct commit *commit)
        return marked;
 }
 
-static int mark_treesame_root_parents(struct rev_info *revs, struct commit *commit)
+static int mark_treesame_root_parents(struct commit *commit)
 {
        struct commit_list *p;
        int marked = 0;
@@ -2611,8 +3195,8 @@ static struct commit_list **simplify_one(struct rev_info *revs, struct commit *c
         * Detect and simplify both cases.
         */
        if (1 < cnt) {
-               int marked = mark_redundant_parents(revs, commit);
-               marked += mark_treesame_root_parents(revs, commit);
+               int marked = mark_redundant_parents(commit);
+               marked += mark_treesame_root_parents(commit);
                if (marked)
                        marked -= leave_one_treesame_to_parent(revs, commit);
                if (marked)
@@ -2632,7 +3216,8 @@ static struct commit_list **simplify_one(struct rev_info *revs, struct commit *c
        if (!cnt ||
            (commit->object.flags & UNINTERESTING) ||
            !(commit->object.flags & TREESAME) ||
-           (parent = one_relevant_parent(revs, commit->parents)) == NULL)
+           (parent = one_relevant_parent(revs, commit->parents)) == NULL ||
+           (revs->show_pulls && (commit->object.flags & PULL_MERGE)))
                st->simplified = commit;
        else {
                pst = locate_simplify_state(revs, parent);
@@ -2699,7 +3284,252 @@ static void set_children(struct rev_info *revs)
 
 void reset_revision_walk(void)
 {
-       clear_object_flags(SEEN | ADDED | SHOWN);
+       clear_object_flags(SEEN | ADDED | SHOWN | TOPO_WALK_EXPLORED | TOPO_WALK_INDEGREE);
+}
+
+static int mark_uninteresting(const struct object_id *oid,
+                             struct packed_git *pack,
+                             uint32_t pos,
+                             void *cb)
+{
+       struct rev_info *revs = cb;
+       struct object *o = parse_object(revs->repo, oid);
+       o->flags |= UNINTERESTING | SEEN;
+       return 0;
+}
+
+define_commit_slab(indegree_slab, int);
+define_commit_slab(author_date_slab, timestamp_t);
+
+struct topo_walk_info {
+       uint32_t min_generation;
+       struct prio_queue explore_queue;
+       struct prio_queue indegree_queue;
+       struct prio_queue topo_queue;
+       struct indegree_slab indegree;
+       struct author_date_slab author_date;
+};
+
+static inline void test_flag_and_insert(struct prio_queue *q, struct commit *c, int flag)
+{
+       if (c->object.flags & flag)
+               return;
+
+       c->object.flags |= flag;
+       prio_queue_put(q, c);
+}
+
+static void explore_walk_step(struct rev_info *revs)
+{
+       struct topo_walk_info *info = revs->topo_walk_info;
+       struct commit_list *p;
+       struct commit *c = prio_queue_get(&info->explore_queue);
+
+       if (!c)
+               return;
+
+       if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
+               return;
+
+       if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
+               record_author_date(&info->author_date, c);
+
+       if (revs->max_age != -1 && (c->date < revs->max_age))
+               c->object.flags |= UNINTERESTING;
+
+       if (process_parents(revs, c, NULL, NULL) < 0)
+               return;
+
+       if (c->object.flags & UNINTERESTING)
+               mark_parents_uninteresting(c);
+
+       for (p = c->parents; p; p = p->next)
+               test_flag_and_insert(&info->explore_queue, p->item, TOPO_WALK_EXPLORED);
+}
+
+static void explore_to_depth(struct rev_info *revs,
+                            uint32_t gen_cutoff)
+{
+       struct topo_walk_info *info = revs->topo_walk_info;
+       struct commit *c;
+       while ((c = prio_queue_peek(&info->explore_queue)) &&
+              commit_graph_generation(c) >= gen_cutoff)
+               explore_walk_step(revs);
+}
+
+static void indegree_walk_step(struct rev_info *revs)
+{
+       struct commit_list *p;
+       struct topo_walk_info *info = revs->topo_walk_info;
+       struct commit *c = prio_queue_get(&info->indegree_queue);
+
+       if (!c)
+               return;
+
+       if (repo_parse_commit_gently(revs->repo, c, 1) < 0)
+               return;
+
+       explore_to_depth(revs, commit_graph_generation(c));
+
+       for (p = c->parents; p; p = p->next) {
+               struct commit *parent = p->item;
+               int *pi = indegree_slab_at(&info->indegree, parent);
+
+               if (*pi)
+                       (*pi)++;
+               else
+                       *pi = 2;
+
+               test_flag_and_insert(&info->indegree_queue, parent, TOPO_WALK_INDEGREE);
+
+               if (revs->first_parent_only)
+                       return;
+       }
+}
+
+static void compute_indegrees_to_depth(struct rev_info *revs,
+                                      uint32_t gen_cutoff)
+{
+       struct topo_walk_info *info = revs->topo_walk_info;
+       struct commit *c;
+       while ((c = prio_queue_peek(&info->indegree_queue)) &&
+              commit_graph_generation(c) >= gen_cutoff)
+               indegree_walk_step(revs);
+}
+
+static void reset_topo_walk(struct rev_info *revs)
+{
+       struct topo_walk_info *info = revs->topo_walk_info;
+
+       clear_prio_queue(&info->explore_queue);
+       clear_prio_queue(&info->indegree_queue);
+       clear_prio_queue(&info->topo_queue);
+       clear_indegree_slab(&info->indegree);
+       clear_author_date_slab(&info->author_date);
+
+       FREE_AND_NULL(revs->topo_walk_info);
+}
+
+static void init_topo_walk(struct rev_info *revs)
+{
+       struct topo_walk_info *info;
+       struct commit_list *list;
+       if (revs->topo_walk_info)
+               reset_topo_walk(revs);
+
+       revs->topo_walk_info = xmalloc(sizeof(struct topo_walk_info));
+       info = revs->topo_walk_info;
+       memset(info, 0, sizeof(struct topo_walk_info));
+
+       init_indegree_slab(&info->indegree);
+       memset(&info->explore_queue, 0, sizeof(info->explore_queue));
+       memset(&info->indegree_queue, 0, sizeof(info->indegree_queue));
+       memset(&info->topo_queue, 0, sizeof(info->topo_queue));
+
+       switch (revs->sort_order) {
+       default: /* REV_SORT_IN_GRAPH_ORDER */
+               info->topo_queue.compare = NULL;
+               break;
+       case REV_SORT_BY_COMMIT_DATE:
+               info->topo_queue.compare = compare_commits_by_commit_date;
+               break;
+       case REV_SORT_BY_AUTHOR_DATE:
+               init_author_date_slab(&info->author_date);
+               info->topo_queue.compare = compare_commits_by_author_date;
+               info->topo_queue.cb_data = &info->author_date;
+               break;
+       }
+
+       info->explore_queue.compare = compare_commits_by_gen_then_commit_date;
+       info->indegree_queue.compare = compare_commits_by_gen_then_commit_date;
+
+       info->min_generation = GENERATION_NUMBER_INFINITY;
+       for (list = revs->commits; list; list = list->next) {
+               struct commit *c = list->item;
+               uint32_t generation;
+
+               if (repo_parse_commit_gently(revs->repo, c, 1))
+                       continue;
+
+               test_flag_and_insert(&info->explore_queue, c, TOPO_WALK_EXPLORED);
+               test_flag_and_insert(&info->indegree_queue, c, TOPO_WALK_INDEGREE);
+
+               generation = commit_graph_generation(c);
+               if (generation < info->min_generation)
+                       info->min_generation = generation;
+
+               *(indegree_slab_at(&info->indegree, c)) = 1;
+
+               if (revs->sort_order == REV_SORT_BY_AUTHOR_DATE)
+                       record_author_date(&info->author_date, c);
+       }
+       compute_indegrees_to_depth(revs, info->min_generation);
+
+       for (list = revs->commits; list; list = list->next) {
+               struct commit *c = list->item;
+
+               if (*(indegree_slab_at(&info->indegree, c)) == 1)
+                       prio_queue_put(&info->topo_queue, c);
+       }
+
+       /*
+        * This is unfortunate; the initial tips need to be shown
+        * in the order given from the revision traversal machinery.
+        */
+       if (revs->sort_order == REV_SORT_IN_GRAPH_ORDER)
+               prio_queue_reverse(&info->topo_queue);
+}
+
+static struct commit *next_topo_commit(struct rev_info *revs)
+{
+       struct commit *c;
+       struct topo_walk_info *info = revs->topo_walk_info;
+
+       /* pop next off of topo_queue */
+       c = prio_queue_get(&info->topo_queue);
+
+       if (c)
+               *(indegree_slab_at(&info->indegree, c)) = 0;
+
+       return c;
+}
+
+static void expand_topo_walk(struct rev_info *revs, struct commit *commit)
+{
+       struct commit_list *p;
+       struct topo_walk_info *info = revs->topo_walk_info;
+       if (process_parents(revs, commit, NULL, NULL) < 0) {
+               if (!revs->ignore_missing_links)
+                       die("Failed to traverse parents of commit %s",
+                           oid_to_hex(&commit->object.oid));
+       }
+
+       for (p = commit->parents; p; p = p->next) {
+               struct commit *parent = p->item;
+               int *pi;
+               uint32_t generation;
+
+               if (parent->object.flags & UNINTERESTING)
+                       continue;
+
+               if (repo_parse_commit_gently(revs->repo, parent, 1) < 0)
+                       continue;
+
+               generation = commit_graph_generation(parent);
+               if (generation < info->min_generation) {
+                       info->min_generation = generation;
+                       compute_indegrees_to_depth(revs, info->min_generation);
+               }
+
+               pi = indegree_slab_at(&info->indegree, parent);
+
+               (*pi)--;
+               if (*pi == 1)
+                       prio_queue_put(&info->topo_queue, parent);
+
+               if (revs->first_parent_only)
+                       return;
+       }
 }
 
 int prepare_revision_walk(struct rev_info *revs)
@@ -2722,40 +3552,56 @@ int prepare_revision_walk(struct rev_info *revs)
                        }
                }
        }
-       if (!revs->leak_pending)
-               object_array_clear(&old_pending);
+       object_array_clear(&old_pending);
 
        /* Signal whether we need per-parent treesame decoration */
        if (revs->simplify_merges ||
            (revs->limited && limiting_can_increase_treesame(revs)))
                revs->treesame.name = "treesame";
 
+       if (revs->exclude_promisor_objects) {
+               for_each_packed_object(mark_uninteresting, revs,
+                                      FOR_EACH_OBJECT_PROMISOR_ONLY);
+       }
+
+       if (!revs->reflog_info)
+               prepare_to_use_bloom_filter(revs);
        if (revs->no_walk != REVISION_WALK_NO_WALK_UNSORTED)
                commit_list_sort_by_date(&revs->commits);
        if (revs->no_walk)
                return 0;
-       if (revs->limited)
+       if (revs->limited) {
                if (limit_list(revs) < 0)
                        return -1;
-       if (revs->topo_order)
-               sort_in_topological_order(&revs->commits, revs->sort_order);
-       if (revs->line_level_traverse)
+               if (revs->topo_order)
+                       sort_in_topological_order(&revs->commits, revs->sort_order);
+       } else if (revs->topo_order)
+               init_topo_walk(revs);
+       if (revs->line_level_traverse && want_ancestry(revs))
+               /*
+                * At the moment we can only do line-level log with parent
+                * rewriting by performing this expensive pre-filtering step.
+                * If parent rewriting is not requested, then we rather
+                * perform the line-level log filtering during the regular
+                * history traversal.
+                */
                line_log_filter(revs);
        if (revs->simplify_merges)
                simplify_merges(revs);
        if (revs->children.name)
                set_children(revs);
+
        return 0;
 }
 
-static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
+static enum rewrite_result rewrite_one_1(struct rev_info *revs,
+                                        struct commit **pp,
+                                        struct prio_queue *queue)
 {
-       struct commit_list *cache = NULL;
-
        for (;;) {
                struct commit *p = *pp;
                if (!revs->limited)
-                       if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
+                       if (process_parents(revs, p, NULL, queue) < 0)
                                return rewrite_one_error;
                if (p->object.flags & UNINTERESTING)
                        return rewrite_one_ok;
@@ -2769,6 +3615,31 @@ static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp
        }
 }
 
+static void merge_queue_into_list(struct prio_queue *q, struct commit_list **list)
+{
+       while (q->nr) {
+               struct commit *item = prio_queue_peek(q);
+               struct commit_list *p = *list;
+
+               if (p && p->item->date >= item->date)
+                       list = &p->next;
+               else {
+                       p = commit_list_insert(item, list);
+                       list = &p->next; /* skip newly added item */
+                       prio_queue_get(q); /* pop item */
+               }
+       }
+}
+
+static enum rewrite_result rewrite_one(struct rev_info *revs, struct commit **pp)
+{
+       struct prio_queue queue = { compare_commits_by_commit_date };
+       enum rewrite_result ret = rewrite_one_1(revs, pp, &queue);
+       merge_queue_into_list(&queue, &revs->commits);
+       clear_prio_queue(&queue);
+       return ret;
+}
+
 int rewrite_parents(struct rev_info *revs, struct commit *commit,
        rewrite_parent_fn_t rewrite_parent)
 {
@@ -2878,7 +3749,7 @@ static int commit_match(struct commit *commit, struct rev_info *opt)
        if (opt->show_notes) {
                if (!buf.len)
                        strbuf_addstr(&buf, message);
-               format_display_notes(commit->object.oid.hash, &buf, encoding, 1);
+               format_display_notes(&commit->object.oid, &buf, encoding, 1);
        }
 
        /*
@@ -2904,18 +3775,45 @@ static inline int want_ancestry(const struct rev_info *revs)
        return (revs->rewrite_parents || revs->children.name);
 }
 
+/*
+ * Return a timestamp to be used for --since/--until comparisons for this
+ * commit, based on the revision options.
+ */
+static timestamp_t comparison_date(const struct rev_info *revs,
+                                  struct commit *commit)
+{
+       return revs->reflog_info ?
+               get_reflog_timestamp(revs->reflog_info) :
+               commit->date;
+}
+
 enum commit_action get_commit_action(struct rev_info *revs, struct commit *commit)
 {
        if (commit->object.flags & SHOWN)
                return commit_ignore;
-       if (revs->unpacked && has_sha1_pack(commit->object.oid.hash))
+       if (revs->unpacked && has_object_pack(&commit->object.oid))
                return commit_ignore;
-       if (revs->show_all)
-               return commit_show;
        if (commit->object.flags & UNINTERESTING)
                return commit_ignore;
-       if (revs->min_age != -1 && (commit->date > revs->min_age))
-               return commit_ignore;
+       if (revs->line_level_traverse && !want_ancestry(revs)) {
+               /*
+                * In case of line-level log with parent rewriting
+                * prepare_revision_walk() already took care of all line-level
+                * log filtering, and there is nothing left to do here.
+                *
+                * If parent rewriting was not requested, then this is the
+                * place to perform the line-level log filtering.  Notably,
+                * this check, though expensive, must come before the other,
+                * cheaper filtering conditions, because the tracked line
+                * ranges must be adjusted even when the commit will end up
+                * being ignored based on other conditions.
+                */
+               if (!line_log_process_ranges_arbitrary_commit(revs, commit))
+                       return commit_ignore;
+       }
+       if (revs->min_age != -1 &&
+           comparison_date(revs, commit) > revs->min_age)
+                       return commit_ignore;
        if (revs->min_parents || (revs->max_parents >= 0)) {
                int n = commit_list_count(commit->parents);
                if ((n < revs->min_parents) ||
@@ -2932,6 +3830,10 @@ enum commit_action get_commit_action(struct rev_info *revs, struct commit *commi
                        /* drop merges unless we want parenthood */
                        if (!want_ancestry(revs))
                                return commit_ignore;
+
+                       if (revs->show_pulls && (commit->object.flags & PULL_MERGE))
+                               return commit_show;
+
                        /*
                         * If we want ancestry, then need to keep any merges
                         * between relevant commits to tie together topology.
@@ -3009,7 +3911,6 @@ enum commit_action simplify_commit(struct rev_info *revs, struct commit *commit)
        enum commit_action action = get_commit_action(revs, commit);
 
        if (action == commit_show &&
-           !revs->show_all &&
            revs->prune && revs->dense && want_ancestry(revs)) {
                /*
                 * --full-diff on simplified parents is no good: it
@@ -3034,7 +3935,7 @@ static void track_linear(struct rev_info *revs, struct commit *commit)
                struct commit_list *p;
                for (p = revs->previous_parents; p; p = p->next)
                        if (p->item == NULL || /* first commit */
-                           !oidcmp(&p->item->object.oid, &commit->object.oid))
+                           oideq(&p->item->object.oid, &commit->object.oid))
                                break;
                revs->linear = p != NULL;
        }
@@ -3048,17 +3949,21 @@ static void track_linear(struct rev_info *revs, struct commit *commit)
 
 static struct commit *get_revision_1(struct rev_info *revs)
 {
-       if (!revs->commits)
-               return NULL;
+       while (1) {
+               struct commit *commit;
 
-       do {
-               struct commit *commit = pop_commit(&revs->commits);
+               if (revs->reflog_info)
+                       commit = next_reflog_entry(revs->reflog_info);
+               else if (revs->topo_walk_info)
+                       commit = next_topo_commit(revs);
+               else
+                       commit = pop_commit(&revs->commits);
 
-               if (revs->reflog_info) {
-                       save_parents(revs, commit);
-                       fake_reflog_parent(revs->reflog_info, commit);
+               if (!commit)
+                       return NULL;
+
+               if (revs->reflog_info)
                        commit->object.flags &= ~(ADDED | SEEN | SHOWN);
-               }
 
                /*
                 * If we haven't done the list limiting, we need to look at
@@ -3067,9 +3972,14 @@ static struct commit *get_revision_1(struct rev_info *revs)
                 */
                if (!revs->limited) {
                        if (revs->max_age != -1 &&
-                           (commit->date < revs->max_age))
+                           comparison_date(revs, commit) < revs->max_age)
                                continue;
-                       if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
+
+                       if (revs->reflog_info)
+                               try_to_simplify_commit(revs, commit);
+                       else if (revs->topo_walk_info)
+                               expand_topo_walk(revs, commit);
+                       else if (process_parents(revs, commit, &revs->commits, NULL) < 0) {
                                if (!revs->ignore_missing_links)
                                        die("Failed to traverse parents of commit %s",
                                                oid_to_hex(&commit->object.oid));
@@ -3087,8 +3997,7 @@ static struct commit *get_revision_1(struct rev_info *revs)
                                track_linear(revs, commit);
                        return commit;
                }
-       } while (revs->commits);
-       return NULL;
+       }
 }
 
 /*
@@ -3271,7 +4180,7 @@ struct commit *get_revision(struct rev_info *revs)
        return c;
 }
 
-char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
+const char *get_revision_mark(const struct rev_info *revs, const struct commit *commit)
 {
        if (commit->object.flags & BOUNDARY)
                return "-";
@@ -3293,7 +4202,7 @@ char *get_revision_mark(const struct rev_info *revs, const struct commit *commit
 
 void put_revision_mark(const struct rev_info *revs, const struct commit *commit)
 {
-       char *mark = get_revision_mark(revs, commit);
+       const char *mark = get_revision_mark(revs, commit);
        if (!strlen(mark))
                return;
        fputs(mark, stdout);