packaging: Add contrib installation
[platform/upstream/git.git] / revision.c
index de4dce6..7ec9b63 100644 (file)
 #include "bisect.h"
 #include "packfile.h"
 #include "worktree.h"
-#include "argv-array.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;
 
@@ -32,6 +39,8 @@ 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;
@@ -51,7 +60,8 @@ 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;
@@ -63,10 +73,10 @@ static void mark_tree_contents_uninteresting(struct tree *tree)
        while (tree_entry(&desc, &entry)) {
                switch (object_type(entry.mode)) {
                case OBJ_TREE:
-                       mark_tree_uninteresting(lookup_tree(the_repository, entry.oid));
+                       mark_tree_uninteresting(r, lookup_tree(r, &entry.oid));
                        break;
                case OBJ_BLOB:
-                       mark_blob_uninteresting(lookup_blob(the_repository, entry.oid));
+                       mark_blob_uninteresting(lookup_blob(r, &entry.oid));
                        break;
                default:
                        /* Subproject commit - not in this repository */
@@ -81,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;
 
@@ -92,7 +102,146 @@ 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);
+}
+
+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)
+{
+       const struct path_and_oids_entry *e1, *e2;
+
+       e1 = container_of(eptr, const struct path_and_oids_entry, ent);
+       e2 = container_of(entry_or_key, const struct path_and_oids_entry, ent);
+
+       return strcmp(e1->path, e2->path);
+}
+
+static void paths_and_oids_clear(struct hashmap *map)
+{
+       struct hashmap_iter iter;
+       struct path_and_oids_entry *entry;
+
+       hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
+               oidset_clear(&entry->trees);
+               free(entry->path);
+       }
+
+       hashmap_clear_and_free(map, struct path_and_oids_entry, ent);
+}
+
+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);
+}
+
+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 {
@@ -159,13 +308,14 @@ 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, 0);
+               int len = interpret_branch_name(name, 0, &buf, &options);
 
                if (0 < len && name[len] && buf.len)
                        strbuf_addstr(&buf, name + len);
@@ -175,7 +325,6 @@ static void add_pending_object_with_path(struct rev_info *revs,
                strbuf_release(&buf);
                return; /* do not add the commit itself */
        }
-       obj->flags |= USER_GIVEN;
        add_object_array_with_path(obj, name, &revs->pending, mode, path);
 }
 
@@ -198,7 +347,7 @@ void add_head_to_pending(struct rev_info *revs)
        struct object *obj;
        if (get_oid("HEAD", &oid))
                return;
-       obj = parse_object(the_repository, &oid);
+       obj = parse_object(revs->repo, &oid);
        if (!obj)
                return;
        add_pending_object(revs, obj, "HEAD");
@@ -210,7 +359,20 @@ static struct object *get_reference(struct rev_info *revs, const char *name,
 {
        struct object *object;
 
-       object = parse_object(the_repository, oid);
+       /*
+        * 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;
@@ -245,9 +407,7 @@ 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(the_repository, &tag->tagged->oid);
+               object = parse_object(revs->repo, get_tagged_oid(tag));
                if (!object) {
                        if (revs->ignore_missing_links || (flags & UNINTERESTING))
                                return NULL;
@@ -273,11 +433,13 @@ 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);
@@ -297,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);
@@ -460,11 +622,156 @@ static void file_change(struct diff_options *options,
        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 = get_commit_tree(parent);
        struct tree *t2 = get_commit_tree(commit);
+       int bloom_ret = 1;
 
        if (!t1)
                return REV_TREE_NEW;
@@ -489,17 +796,26 @@ 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;
        revs->pruning.flags.has_changes = 0;
-       if (diff_tree_oid(&t1->object.oid, &t2->object.oid, "",
-                          &revs->pruning) < 0)
-               return REV_TREE_DIFFERENT;
+       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 = get_commit_tree(commit);
 
        if (!t1)
@@ -507,9 +823,9 @@ static int rev_same_tree_as_empty(struct rev_info *revs, struct commit *commit)
 
        tree_difference = REV_TREE_SAME;
        revs->pruning.flags.has_changes = 0;
-       retval = diff_tree_oid(NULL, &t1->object.oid, "", &revs->pruning);
+       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 {
@@ -687,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
@@ -706,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:
@@ -720,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));
@@ -733,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));
@@ -752,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;
@@ -799,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;
        }
@@ -827,7 +1147,7 @@ static int add_parents_to_list(struct rev_info *revs, struct commit *commit,
                struct commit *p = parent->item;
                int gently = revs->ignore_missing_links ||
                             revs->exclude_promisor_objects;
-               if (parse_commit_gently(p, gently) < 0) {
+               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)
@@ -845,7 +1165,10 @@ static int add_parents_to_list(struct rev_info *revs, struct commit *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;
@@ -877,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 */
@@ -918,12 +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;
 
                commit->object.flags |= cherry_flag;
-               id->commit->object.flags |= cherry_flag;
+               do {
+                       id->commit->object.flags |= cherry_flag;
+               } while ((id = patch_id_iter_next(id, &ids)));
        }
 
        free_patch_ids(&ids);
@@ -1089,7 +1414,7 @@ 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);
@@ -1098,7 +1423,8 @@ static int limit_list(struct rev_info *revs)
                                continue;
                        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;
@@ -1176,7 +1502,7 @@ struct all_refs_cb {
        int warned_bad_reflog;
        struct rev_info *all_revs;
        const char *name_for_errormsg;
-       struct ref_store *refs;
+       struct worktree *wt;
 };
 
 int ref_excluded(struct string_list *ref_excludes, const char *path)
@@ -1213,7 +1539,7 @@ 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->refs = NULL;
+       cb->wt = NULL;
 }
 
 void clear_ref_exclusion(struct string_list **ref_excludes_p)
@@ -1253,7 +1579,7 @@ static void handle_one_reflog_commit(struct object_id *oid, void *cb_data)
 {
        struct all_refs_cb *cb = cb_data;
        if (!is_null_oid(oid)) {
-               struct object *o = parse_object(the_repository, oid);
+               struct object *o = parse_object(cb->all_revs->repo, oid);
                if (o) {
                        o->flags |= cb->all_flags;
                        /* ??? CMDLINEFLAGS ??? */
@@ -1276,14 +1602,20 @@ static int handle_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
        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;
-       refs_for_each_reflog_ent(cb->refs, path,
+       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;
 }
 
@@ -1291,15 +1623,15 @@ static void add_other_reflogs_to_pending(struct all_refs_cb *cb)
 {
        struct worktree **worktrees, **p;
 
-       worktrees = get_worktrees(0);
+       worktrees = get_worktrees();
        for (p = worktrees; *p; p++) {
                struct worktree *wt = *p;
 
                if (wt->is_current)
                        continue;
 
-               cb->refs = get_worktree_ref_store(wt);
-               refs_for_each_reflog(cb->refs,
+               cb->wt = wt;
+               refs_for_each_reflog(get_worktree_ref_store(wt),
                                     handle_one_reflog,
                                     cb);
        }
@@ -1312,7 +1644,7 @@ void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
 
        cb.all_revs = revs;
        cb.all_flags = flags;
-       cb.refs = get_main_ref_store(the_repository);
+       cb.wt = NULL;
        for_each_reflog(handle_one_reflog, &cb);
 
        if (!revs->single_worktree)
@@ -1320,13 +1652,14 @@ void add_reflogs_to_pending(struct rev_info *revs, unsigned flags)
 }
 
 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(the_repository, &it->oid);
+               struct tree *tree = lookup_tree(revs->repo, &it->oid);
+               tree->object.flags |= flags;
                add_pending_object_with_path(revs, &tree->object, "",
                                             040000, path->buf);
        }
@@ -1334,14 +1667,15 @@ 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);
        }
 
 }
 
 static void do_add_index_objects_to_pending(struct rev_info *revs,
-                                           struct index_state *istate)
+                                           struct index_state *istate,
+                                           unsigned int flags)
 {
        int i;
 
@@ -1352,16 +1686,17 @@ static void do_add_index_objects_to_pending(struct rev_info *revs,
                if (S_ISGITLINK(ce->ce_mode))
                        continue;
 
-               blob = lookup_blob(the_repository, &ce->oid);
+               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 (istate->cache_tree) {
                struct strbuf path = STRBUF_INIT;
-               add_cache_tree(istate->cache_tree, revs, &path);
+               add_cache_tree(istate->cache_tree, revs, &path, flags);
                strbuf_release(&path);
        }
 }
@@ -1370,13 +1705,13 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
 {
        struct worktree **worktrees, **p;
 
-       read_cache();
-       do_add_index_objects_to_pending(revs, &the_index);
+       repo_read_index(revs->repo);
+       do_add_index_objects_to_pending(revs, revs->repo->index, flags);
 
        if (revs->single_worktree)
                return;
 
-       worktrees = get_worktrees(0);
+       worktrees = get_worktrees();
        for (p = worktrees; *p; p++) {
                struct worktree *wt = *p;
                struct index_state istate = { NULL };
@@ -1387,12 +1722,38 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags)
                if (read_index_from(&istate,
                                    worktree_git_path(wt, "index"),
                                    get_worktree_git_dir(wt)) > 0)
-                       do_add_index_objects_to_pending(revs, &istate);
+                       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)
+{
+       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)
 {
@@ -1439,13 +1800,17 @@ 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;
+       revs->pruning.repo = r;
        revs->pruning.flags.recursive = 1;
        revs->pruning.flags.quick = 1;
        revs->pruning.add_remove = file_add_remove;
@@ -1464,22 +1829,21 @@ void init_revisions(struct rev_info *revs, const char *prefix)
        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;
 
-       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;
@@ -1496,6 +1860,7 @@ static void prepare_show_merge(struct rev_info *revs)
        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_oid("HEAD", &oid))
                die("--merge without HEAD?");
@@ -1511,20 +1876,20 @@ 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(&the_index, 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++;
        }
        clear_pathspec(&revs->prune_data);
@@ -1571,8 +1936,8 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
        if (!*b_name)
                b_name = "HEAD";
 
-       if (get_oid_with_context(a_name, oc_flags, &a_oid, a_oc) ||
-           get_oid_with_context(b_name, oc_flags, &b_oid, b_oc))
+       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) {
@@ -1581,8 +1946,8 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
                *dotdot = '\0';
        }
 
-       a_obj = parse_object(the_repository, &a_oid);
-       b_obj = parse_object(the_repository, &b_oid);
+       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);
 
@@ -1595,8 +1960,8 @@ static int handle_dotdot_1(const char *arg, char *dotdot,
                struct commit *a, *b;
                struct commit_list *exclude;
 
-               a = lookup_commit_reference(the_repository, &a_obj->oid);
-               b = lookup_commit_reference(the_repository, &b_obj->oid);
+               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);
 
@@ -1644,7 +2009,7 @@ static int handle_dotdot(const char *arg,
        return ret;
 }
 
-int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
+static int handle_revision_arg_1(const char *arg_, struct rev_info *revs, int flags, unsigned revarg_opt)
 {
        struct object_context oc;
        char *mark;
@@ -1706,26 +2071,36 @@ int handle_revision_arg(const char *arg_, struct rev_info *revs, int flags, unsi
        if (revarg_opt & REVARG_COMMITTISH)
                get_sha1_flags |= GET_OID_COMMITTISH;
 
-       if (get_oid_with_context(arg, get_sha1_flags, &oid, &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, &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_path(revs, object, arg, oc.mode, oc.path);
        free(oc.path);
        return 0;
 }
 
-static void read_pathspec_from_stdin(struct rev_info *revs, struct strbuf *sb,
-                                    struct argv_array *prune)
+int handle_revision_arg(const char *arg, struct rev_info *revs, int flags, unsigned revarg_opt)
+{
+       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 strbuf *sb,
+                                    struct strvec *prune)
 {
        while (strbuf_getline(sb, stdin) != EOF)
-               argv_array_push(prune, sb->buf);
+               strvec_push(prune, sb->buf);
 }
 
 static void read_revisions_from_stdin(struct rev_info *revs,
-                                     struct argv_array *prune)
+                                     struct strvec *prune)
 {
        struct strbuf sb;
        int seen_dashdash = 0;
@@ -1751,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;
@@ -1773,7 +2148,8 @@ 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;
@@ -1787,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="))
@@ -1867,7 +2244,6 @@ 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(NULL, DECORATE_SHORT_REFS);
        } else if (!strcmp(arg, "--date-order")) {
                revs->sort_order = REV_SORT_BY_COMMIT_DATE;
                revs->topo_order = 1;
@@ -1959,7 +2335,7 @@ 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;
                revs->diffopt.flags.recursive = 1;
@@ -1968,11 +2344,29 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
                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;
@@ -2002,9 +2396,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
                        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, "--no-show-signature")) {
@@ -2019,25 +2412,14 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
                revs->track_first_time = 1;
        } else if (skip_prefix(arg, "--show-notes=", &optarg) ||
                   skip_prefix(arg, "--notes=", &optarg)) {
-               struct strbuf buf = STRBUF_INIT;
-               revs->show_notes = 1;
-               revs->show_notes_given = 1;
                if (starts_with(arg, "--show-notes=") &&
                    revs->notes_opt.use_default_notes < 0)
                        revs->notes_opt.use_default_notes = 1;
-               strbuf_addstr(&buf, optarg);
-               expand_notes_ref(&buf);
-               string_list_append(&revs->notes_opt.extra_notes_refs,
-                                  strbuf_detach(&buf, NULL));
+               enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
+               revs->show_notes_given = 1;
        } 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;
@@ -2052,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")) {
@@ -2076,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")) {
@@ -2133,7 +2521,8 @@ 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 (!strcmp(arg, "--exclude-promisor-objects")) {
+       } 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;
@@ -2154,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);
@@ -2185,8 +2574,8 @@ static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void
 }
 
 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;
@@ -2204,7 +2593,7 @@ static int handle_revision_pseudo_opt(const char *submodule,
                        BUG("--single-worktree cannot be used together with submodule");
                refs = get_submodule_ref_store(submodule);
        } else
-               refs = get_main_ref_store(the_repository);
+               refs = get_main_ref_store(revs->repo);
 
        /*
         * NOTE!
@@ -2269,6 +2658,8 @@ static int handle_revision_pseudo_opt(const char *submodule,
                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")) {
@@ -2318,9 +2709,10 @@ 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 argv_array prune_data = ARGV_ARRAY_INIT;
+       int i, flags, left, seen_dashdash, revarg_opt;
+       struct strvec prune_data = STRVEC_INIT;
        const char *submodule = NULL;
+       int seen_end_of_options = 0;
 
        if (opt)
                submodule = opt->submodule;
@@ -2337,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])
-                               argv_array_pushv(&prune_data, argv + i + 1);
+                               strvec_pushv(&prune_data, argv + i + 1);
                        seen_dashdash = 1;
                        break;
                }
@@ -2348,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;
@@ -2367,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;
@@ -2398,14 +2795,12 @@ 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);
 
-                       argv_array_pushv(&prune_data, argv + i);
+                       strvec_pushv(&prune_data, argv + i);
                        break;
                }
-               else
-                       got_rev_arg = 1;
        }
 
-       if (prune_data.argc) {
+       if (prune_data.nr) {
                /*
                 * If we need to introduce the magic "a lone ':' means no
                 * pathspec whatsoever", here is the place to do so.
@@ -2421,9 +2816,9 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
                 * }
                 */
                parse_pathspec(&revs->prune_data, 0, 0,
-                              revs->prefix, prune_data.argv);
+                              revs->prefix, prune_data.v);
        }
-       argv_array_clear(&prune_data);
+       strvec_clear(&prune_data);
 
        if (revs->def == NULL)
                revs->def = opt ? opt->def : NULL;
@@ -2431,11 +2826,11 @@ 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 && !revs->rev_input_given && !got_rev_arg) {
+       if (revs->def && !revs->pending.nr && !revs->rev_input_given) {
                struct object_id oid;
                struct object *object;
                struct object_context oc;
-               if (get_oid_with_context(revs->def, 0, &oid, &oc))
+               if (get_oid_with_context(revs->repo, revs->def, 0, &oid, &oc))
                        diagnose_missing_default(revs->def);
                object = get_reference(revs, revs->def, &oid, 0);
                add_pending_object_with_mode(revs, object, revs->def, oc.mode);
@@ -2454,7 +2849,13 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
        if (revs->diffopt.objfind)
                revs->simplify_history = 0;
 
-       if (revs->topo_order)
+       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) {
@@ -2466,19 +2867,21 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
                        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)
@@ -2501,8 +2904,9 @@ 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;
@@ -2563,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;
@@ -2599,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;
@@ -2791,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)
@@ -2812,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);
@@ -2879,19 +3284,254 @@ 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 *unused)
+                             void *cb)
 {
-       struct object *o = parse_object(the_repository, oid);
+       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)
 {
        int i;
@@ -2920,36 +3560,48 @@ int prepare_revision_walk(struct rev_info *revs)
                revs->treesame.name = "treesame";
 
        if (revs->exclude_promisor_objects) {
-               for_each_packed_object(mark_uninteresting, NULL,
+               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;
@@ -2963,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)
 {
@@ -3118,6 +3795,22 @@ enum commit_action get_commit_action(struct rev_info *revs, struct commit *commi
                return commit_ignore;
        if (commit->object.flags & UNINTERESTING)
                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;
@@ -3137,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.
@@ -3238,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;
        }
@@ -3257,6 +3954,8 @@ static struct commit *get_revision_1(struct rev_info *revs)
 
                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);
 
@@ -3278,7 +3977,9 @@ static struct commit *get_revision_1(struct rev_info *revs)
 
                        if (revs->reflog_info)
                                try_to_simplify_commit(revs, commit);
-                       else if (add_parents_to_list(revs, commit, &revs->commits, NULL) < 0) {
+                       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));
@@ -3479,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 "-";
@@ -3501,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);