#include "packfile.h"
#include "worktree.h"
#include "argv-array.h"
+#include "commit-reach.h"
+#include "commit-graph.h"
+#include "prio-queue.h"
volatile show_early_output_fn_t show_early_output;
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;
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 */
free_tree_buffer(tree);
}
-void mark_tree_uninteresting(struct tree *tree)
+void mark_tree_uninteresting(struct repository *r, struct tree *tree)
{
struct object *obj;
if (obj->flags & UNINTERESTING)
return;
obj->flags |= UNINTERESTING;
- mark_tree_contents_uninteresting(tree);
+ mark_tree_contents_uninteresting(r, tree);
}
struct commit_stack {
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);
}
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");
{
struct object *object;
- object = parse_object(the_repository, oid);
+ object = parse_object(revs->repo, oid);
if (!object) {
if (revs->ignore_missing)
return object;
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, &tag->tagged->oid);
if (!object) {
if (revs->ignore_missing_links || (flags & UNINTERESTING))
return NULL;
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);
*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 commit_list **cache_ptr)
{
struct commit_list *parent = commit->parents;
unsigned left_flag;
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_cached(p, list, cached_base, cache_ptr);
}
return 0;
}
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_cached(p, list, cached_base, cache_ptr);
}
if (revs->first_parent_only)
break;
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 */
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);
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)
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)
{
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 ??? */
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;
}
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);
}
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)
}
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);
}
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;
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);
}
}
{
struct worktree **worktrees, **p;
- read_cache();
- do_add_index_objects_to_pending(revs, &the_index);
+ read_index(revs->repo->index);
+ do_add_index_objects_to_pending(revs, revs->repo->index, flags);
if (revs->single_worktree)
return;
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);
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->simplify_history = 1;
revs->commit_format = CMIT_FMT_DEFAULT;
revs->expand_tabs_in_log_default = 8;
- init_grep_defaults();
- grep_init(&revs->grep_filter, prefix);
+ init_grep_defaults(revs->repo);
+ 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);
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?");
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)
+ read_index(istate);
+ 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);
*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);
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);
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!
*/
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;
+ int i, flags, left, seen_dashdash, got_rev_arg = 0, revarg_opt;
struct argv_array prune_data = ARGV_ARRAY_INIT;
const char *submodule = NULL;
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 == '-') {
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;
if (revs->diffopt.objfind)
revs->simplify_history = 0;
- if (revs->topo_order)
+ if (revs->topo_order && !generation_numbers_enabled(the_repository))
revs->limited = 1;
if (revs->prune_data.nr) {
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 (parse_commit_gently(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)) &&
+ c->generation >= 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 (parse_commit_gently(c, 1) < 0)
+ return;
+
+ explore_to_depth(revs, c->generation);
+
+ 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)) &&
+ c->generation >= gen_cutoff)
+ indegree_walk_step(revs);
+}
+
+static void init_topo_walk(struct rev_info *revs)
+{
+ struct topo_walk_info *info;
+ struct commit_list *list;
+ 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;
+
+ if (parse_commit_gently(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);
+
+ if (c->generation < info->min_generation)
+ info->min_generation = c->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;
+
+ if (parse_commit_gently(parent, 1) < 0)
+ continue;
+
+ if (parent->generation < info->min_generation) {
+ info->min_generation = parent->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;
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);
}
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->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)
line_log_filter(revs);
if (revs->simplify_merges)
for (;;) {
struct commit *p = *pp;
if (!revs->limited)
- if (add_parents_to_list(revs, p, &revs->commits, &cache) < 0)
+ if (process_parents(revs, p, &revs->commits, &cache) < 0)
return rewrite_one_error;
if (p->object.flags & UNINTERESTING)
return rewrite_one_ok;
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;
}
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)
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));