Blame src/revwalk.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
#include "commit.h"
Packit Service 20376f
#include "odb.h"
Packit Service 20376f
#include "pool.h"
Packit Service 20376f
Packit Service 20376f
#include "revwalk.h"
Packit Service 20376f
#include "git2/revparse.h"
Packit Service 20376f
#include "merge.h"
Packit Service 20376f
#include "vector.h"
Packit Service 20376f
Packit Service 20376f
git_commit_list_node *git_revwalk__commit_lookup(
Packit Service 20376f
	git_revwalk *walk, const git_oid *oid)
Packit Service 20376f
{
Packit Service 20376f
	git_commit_list_node *commit;
Packit Service 20376f
	khiter_t pos;
Packit Service 20376f
	int ret;
Packit Service 20376f
Packit Service 20376f
	/* lookup and reserve space if not already present */
Packit Service 20376f
	pos = git_oidmap_lookup_index(walk->commits, oid);
Packit Service 20376f
	if (git_oidmap_valid_index(walk->commits, pos))
Packit Service 20376f
		return git_oidmap_value_at(walk->commits, pos);
Packit Service 20376f
Packit Service 20376f
	commit = git_commit_list_alloc_node(walk);
Packit Service 20376f
	if (commit == NULL)
Packit Service 20376f
		return NULL;
Packit Service 20376f
Packit Service 20376f
	git_oid_cpy(&commit->oid, oid);
Packit Service 20376f
Packit Service 20376f
	pos = git_oidmap_put(walk->commits, &commit->oid, &ret;;
Packit Service 20376f
	assert(ret != 0);
Packit Service 20376f
	git_oidmap_set_value_at(walk->commits, pos, commit);
Packit Service 20376f
Packit Service 20376f
	return commit;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting, int from_glob)
Packit Service 20376f
{
Packit Service 20376f
	git_oid commit_id;
Packit Service 20376f
	int error;
Packit Service 20376f
	git_object *obj, *oobj;
Packit Service 20376f
	git_commit_list_node *commit;
Packit Service 20376f
	git_commit_list *list;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_object_lookup(&oobj, walk->repo, oid, GIT_OBJ_ANY)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	error = git_object_peel(&obj, oobj, GIT_OBJ_COMMIT);
Packit Service 20376f
	git_object_free(oobj);
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_ENOTFOUND || error == GIT_EINVALIDSPEC || error == GIT_EPEEL) {
Packit Service 20376f
		/* If this comes from e.g. push_glob("tags"), ignore this */
Packit Service 20376f
		if (from_glob)
Packit Service 20376f
			return 0;
Packit Service 20376f
Packit Service 20376f
		giterr_set(GITERR_INVALID, "object is not a committish");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	git_oid_cpy(&commit_id, git_object_id(obj));
Packit Service 20376f
	git_object_free(obj);
Packit Service 20376f
Packit Service 20376f
	commit = git_revwalk__commit_lookup(walk, &commit_id);
Packit Service 20376f
	if (commit == NULL)
Packit Service 20376f
		return -1; /* error already reported by failed lookup */
Packit Service 20376f
Packit Service 20376f
	/* A previous hide already told us we don't want this commit  */
Packit Service 20376f
	if (commit->uninteresting)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (uninteresting)
Packit Service 20376f
		walk->did_hide = 1;
Packit Service 20376f
	else
Packit Service 20376f
		walk->did_push = 1;
Packit Service 20376f
Packit Service 20376f
	commit->uninteresting = uninteresting;
Packit Service 20376f
	list = walk->user_input;
Packit Service 20376f
	if (git_commit_list_insert(commit, &list) == NULL) {
Packit Service 20376f
		giterr_set_oom();
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	walk->user_input = list;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_revwalk_push(git_revwalk *walk, const git_oid *oid)
Packit Service 20376f
{
Packit Service 20376f
	assert(walk && oid);
Packit Service 20376f
	return push_commit(walk, oid, 0, false);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
int git_revwalk_hide(git_revwalk *walk, const git_oid *oid)
Packit Service 20376f
{
Packit Service 20376f
	assert(walk && oid);
Packit Service 20376f
	return push_commit(walk, oid, 1, false);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int push_ref(git_revwalk *walk, const char *refname, int hide, int from_glob)
Packit Service 20376f
{
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
Packit Service 20376f
	if (git_reference_name_to_id(&oid, walk->repo, refname) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	return push_commit(walk, &oid, hide, from_glob);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int push_glob(git_revwalk *walk, const char *glob, int hide)
Packit Service 20376f
{
Packit Service 20376f
	int error = 0;
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
	git_reference *ref;
Packit Service 20376f
	git_reference_iterator *iter;
Packit Service 20376f
	size_t wildcard;
Packit Service 20376f
Packit Service 20376f
	assert(walk && glob);
Packit Service 20376f
Packit Service 20376f
	/* refs/ is implied if not given in the glob */
Packit Service 20376f
	if (git__prefixcmp(glob, GIT_REFS_DIR) != 0)
Packit Service 20376f
		git_buf_joinpath(&buf, GIT_REFS_DIR, glob);
Packit Service 20376f
	else
Packit Service 20376f
		git_buf_puts(&buf, glob);
Packit Service 20376f
	GITERR_CHECK_ALLOC_BUF(&buf;;
Packit Service 20376f
Packit Service 20376f
	/* If no '?', '*' or '[' exist, we append '/ *' to the glob */
Packit Service 20376f
	wildcard = strcspn(glob, "?*[");
Packit Service 20376f
	if (!glob[wildcard])
Packit Service 20376f
		git_buf_put(&buf, "/*", 2);
Packit Service 20376f
Packit Service 20376f
	if ((error = git_reference_iterator_glob_new(&iter, walk->repo, buf.ptr)) < 0)
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	while ((error = git_reference_next(&ref, iter)) == 0) {
Packit Service 20376f
		error = push_ref(walk, git_reference_name(ref), hide, true);
Packit Service 20376f
		git_reference_free(ref);
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
	git_reference_iterator_free(iter);
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_ITEROVER)
Packit Service 20376f
		error = 0;
Packit Service 20376f
out:
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_revwalk_push_glob(git_revwalk *walk, const char *glob)
Packit Service 20376f
{
Packit Service 20376f
	assert(walk && glob);
Packit Service 20376f
	return push_glob(walk, glob, 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_revwalk_hide_glob(git_revwalk *walk, const char *glob)
Packit Service 20376f
{
Packit Service 20376f
	assert(walk && glob);
Packit Service 20376f
	return push_glob(walk, glob, 1);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_revwalk_push_head(git_revwalk *walk)
Packit Service 20376f
{
Packit Service 20376f
	assert(walk);
Packit Service 20376f
	return push_ref(walk, GIT_HEAD_FILE, 0, false);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_revwalk_hide_head(git_revwalk *walk)
Packit Service 20376f
{
Packit Service 20376f
	assert(walk);
Packit Service 20376f
	return push_ref(walk, GIT_HEAD_FILE, 1, false);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_revwalk_push_ref(git_revwalk *walk, const char *refname)
Packit Service 20376f
{
Packit Service 20376f
	assert(walk && refname);
Packit Service 20376f
	return push_ref(walk, refname, 0, false);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_revwalk_push_range(git_revwalk *walk, const char *range)
Packit Service 20376f
{
Packit Service 20376f
	git_revspec revspec;
Packit Service 20376f
	int error = 0;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_revparse(&revspec, walk->repo, range)))
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (revspec.flags & GIT_REVPARSE_MERGE_BASE) {
Packit Service 20376f
		/* TODO: support "<commit>...<commit>" */
Packit Service 20376f
		giterr_set(GITERR_INVALID, "symmetric differences not implemented in revwalk");
Packit Service 20376f
		return GIT_EINVALIDSPEC;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = push_commit(walk, git_object_id(revspec.from), 1, false)))
Packit Service 20376f
		goto out;
Packit Service 20376f
Packit Service 20376f
	error = push_commit(walk, git_object_id(revspec.to), 0, false);
Packit Service 20376f
Packit Service 20376f
out:
Packit Service 20376f
	git_object_free(revspec.from);
Packit Service 20376f
	git_object_free(revspec.to);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_revwalk_hide_ref(git_revwalk *walk, const char *refname)
Packit Service 20376f
{
Packit Service 20376f
	assert(walk && refname);
Packit Service 20376f
	return push_ref(walk, refname, 1, false);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int revwalk_enqueue_timesort(git_revwalk *walk, git_commit_list_node *commit)
Packit Service 20376f
{
Packit Service 20376f
	return git_pqueue_insert(&walk->iterator_time, commit);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int revwalk_enqueue_unsorted(git_revwalk *walk, git_commit_list_node *commit)
Packit Service 20376f
{
Packit Service 20376f
	return git_commit_list_insert(commit, &walk->iterator_rand) ? 0 : -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int revwalk_next_timesort(git_commit_list_node **object_out, git_revwalk *walk)
Packit Service 20376f
{
Packit Service 20376f
	git_commit_list_node *next;
Packit Service 20376f
Packit Service 20376f
	while ((next = git_pqueue_pop(&walk->iterator_time)) != NULL) {
Packit Service 20376f
		/* Some commits might become uninteresting after being added to the list */
Packit Service 20376f
		if (!next->uninteresting) {
Packit Service 20376f
			*object_out = next;
Packit Service 20376f
			return 0;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	giterr_clear();
Packit Service 20376f
	return GIT_ITEROVER;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int revwalk_next_unsorted(git_commit_list_node **object_out, git_revwalk *walk)
Packit Service 20376f
{
Packit Service 20376f
	git_commit_list_node *next;
Packit Service 20376f
Packit Service 20376f
	while ((next = git_commit_list_pop(&walk->iterator_rand)) != NULL) {
Packit Service 20376f
		/* Some commits might become uninteresting after being added to the list */
Packit Service 20376f
		if (!next->uninteresting) {
Packit Service 20376f
			*object_out = next;
Packit Service 20376f
			return 0;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	giterr_clear();
Packit Service 20376f
	return GIT_ITEROVER;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int revwalk_next_toposort(git_commit_list_node **object_out, git_revwalk *walk)
Packit Service 20376f
{
Packit Service 20376f
	git_commit_list_node *next;
Packit Service 20376f
Packit Service 20376f
	while ((next = git_commit_list_pop(&walk->iterator_topo)) != NULL) {
Packit Service 20376f
		/* Some commits might become uninteresting after being added to the list */
Packit Service 20376f
		if (!next->uninteresting) {
Packit Service 20376f
			*object_out = next;
Packit Service 20376f
			return 0;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	giterr_clear();
Packit Service 20376f
	return GIT_ITEROVER;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int revwalk_next_reverse(git_commit_list_node **object_out, git_revwalk *walk)
Packit Service 20376f
{
Packit Service 20376f
	*object_out = git_commit_list_pop(&walk->iterator_reverse);
Packit Service 20376f
	return *object_out ? 0 : GIT_ITEROVER;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void mark_parents_uninteresting(git_commit_list_node *commit)
Packit Service 20376f
{
Packit Service 20376f
	unsigned short i;
Packit Service 20376f
	git_commit_list *parents = NULL;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < commit->out_degree; i++)
Packit Service 20376f
		git_commit_list_insert(commit->parents[i], &parents);
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
	while (parents) {
Packit Service 20376f
		commit = git_commit_list_pop(&parents);
Packit Service 20376f
Packit Service 20376f
		while (commit) {
Packit Service 20376f
			if (commit->uninteresting)
Packit Service 20376f
				break;
Packit Service 20376f
Packit Service 20376f
			commit->uninteresting = 1;
Packit Service 20376f
			/*
Packit Service 20376f
			 * If we've reached this commit some other way
Packit Service 20376f
			 * already, we need to mark its parents uninteresting
Packit Service 20376f
			 * as well.
Packit Service 20376f
			 */
Packit Service 20376f
			if (!commit->parents)
Packit Service 20376f
				break;
Packit Service 20376f
Packit Service 20376f
			for (i = 0; i < commit->out_degree; i++)
Packit Service 20376f
				git_commit_list_insert(commit->parents[i], &parents);
Packit Service 20376f
			commit = commit->parents[0];
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int add_parents_to_list(git_revwalk *walk, git_commit_list_node *commit, git_commit_list **list)
Packit Service 20376f
{
Packit Service 20376f
	unsigned short i;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if (commit->added)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	commit->added = 1;
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * Go full on in the uninteresting case as we want to include
Packit Service 20376f
	 * as many of these as we can.
Packit Service 20376f
	 *
Packit Service 20376f
	 * Usually we haven't parsed the parent of a parent, but if we
Packit Service 20376f
	 * have it we reached it via other means so we want to mark
Packit Service 20376f
	 * its parents recursively too.
Packit Service 20376f
	 */
Packit Service 20376f
	if (commit->uninteresting) {
Packit Service 20376f
		for (i = 0; i < commit->out_degree; i++) {
Packit Service 20376f
			git_commit_list_node *p = commit->parents[i];
Packit Service 20376f
			p->uninteresting = 1;
Packit Service 20376f
Packit Service 20376f
			/* git does it gently here, but we don't like missing objects */
Packit Service 20376f
			if ((error = git_commit_list_parse(walk, p)) < 0)
Packit Service 20376f
				return error;
Packit Service 20376f
Packit Service 20376f
			if (p->parents)
Packit Service 20376f
				mark_parents_uninteresting(p);
Packit Service 20376f
Packit Service 20376f
			p->seen = 1;
Packit Service 20376f
			git_commit_list_insert_by_date(p, list);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * Now on to what we do if the commit is indeed
Packit Service 20376f
	 * interesting. Here we do want things like first-parent take
Packit Service 20376f
	 * effect as this is what we'll be showing.
Packit Service 20376f
	 */
Packit Service 20376f
	for (i = 0; i < commit->out_degree; i++) {
Packit Service 20376f
		git_commit_list_node *p = commit->parents[i];
Packit Service 20376f
Packit Service 20376f
		if ((error = git_commit_list_parse(walk, p)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		if (walk->hide_cb && walk->hide_cb(&p->oid, walk->hide_cb_payload))
Packit Service 20376f
			continue;
Packit Service 20376f
Packit Service 20376f
		if (!p->seen) {
Packit Service 20376f
			p->seen = 1;
Packit Service 20376f
			git_commit_list_insert_by_date(p, list);
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (walk->first_parent)
Packit Service 20376f
			break;
Packit Service 20376f
	}
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int everybody_uninteresting(git_commit_list *orig)
Packit Service 20376f
{
Packit Service 20376f
	git_commit_list *list = orig;
Packit Service 20376f
Packit Service 20376f
	while (list) {
Packit Service 20376f
		git_commit_list_node *commit = list->item;
Packit Service 20376f
		list = list->next;
Packit Service 20376f
		if (!commit->uninteresting)
Packit Service 20376f
			return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* How many unintersting commits we want to look at after we run out of interesting ones */
Packit Service 20376f
#define SLOP 5
Packit Service 20376f
Packit Service 20376f
static int still_interesting(git_commit_list *list, int64_t time, int slop)
Packit Service 20376f
{
Packit Service 20376f
	/* The empty list is pretty boring */
Packit Service 20376f
	if (!list)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * If the destination list has commits with an earlier date
Packit Service 20376f
	 * than our source we want to continue looking.
Packit Service 20376f
	 */
Packit Service 20376f
	if (time <= list->item->time)
Packit Service 20376f
		return SLOP;
Packit Service 20376f
Packit Service 20376f
	/* If we find interesting commits, we reset the slop count */
Packit Service 20376f
	if (!everybody_uninteresting(list))
Packit Service 20376f
		return SLOP;
Packit Service 20376f
Packit Service 20376f
	/* Everything's uninteresting, reduce the count */
Packit Service 20376f
	return slop - 1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int limit_list(git_commit_list **out, git_revwalk *walk, git_commit_list *commits)
Packit Service 20376f
{
Packit Service 20376f
	int error, slop = SLOP;
Packit Service 20376f
	int64_t time = ~0ll;
Packit Service 20376f
	git_commit_list *list = commits;
Packit Service 20376f
	git_commit_list *newlist = NULL;
Packit Service 20376f
	git_commit_list **p = &newlist;
Packit Service 20376f
Packit Service 20376f
	while (list) {
Packit Service 20376f
		git_commit_list_node *commit = git_commit_list_pop(&list);
Packit Service 20376f
Packit Service 20376f
		if ((error = add_parents_to_list(walk, commit, &list)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		if (commit->uninteresting) {
Packit Service 20376f
			mark_parents_uninteresting(commit);
Packit Service 20376f
Packit Service 20376f
			slop = still_interesting(list, time, slop);
Packit Service 20376f
			if (slop)
Packit Service 20376f
				continue;
Packit Service 20376f
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		if (!commit->uninteresting && walk->hide_cb && walk->hide_cb(&commit->oid, walk->hide_cb_payload))
Packit Service 20376f
				continue;
Packit Service 20376f
Packit Service 20376f
		time = commit->time;
Packit Service 20376f
		p = &git_commit_list_insert(commit, p)->next;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_commit_list_free(&list);
Packit Service 20376f
	*out = newlist;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int sort_in_topological_order(git_commit_list **out, git_revwalk *walk, git_commit_list *list)
Packit Service 20376f
{
Packit Service 20376f
	git_commit_list *ll = NULL, *newlist, **pptr;
Packit Service 20376f
	git_commit_list_node *next;
Packit Service 20376f
	git_pqueue queue;
Packit Service 20376f
	git_vector_cmp queue_cmp = NULL;
Packit Service 20376f
	unsigned short i;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if (walk->sorting & GIT_SORT_TIME)
Packit Service 20376f
		queue_cmp = git_commit_list_time_cmp;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_pqueue_init(&queue, 0, 8, queue_cmp)))
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * Start by resetting the in-degree to 1 for the commits in
Packit Service 20376f
	 * our list. We want to go through this list again, so we
Packit Service 20376f
	 * store it in the commit list as we extract it from the lower
Packit Service 20376f
	 * machinery.
Packit Service 20376f
	 */
Packit Service 20376f
	for (ll = list; ll; ll = ll->next) {
Packit Service 20376f
		ll->item->in_degree = 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * Count up how many children each commit has. We limit
Packit Service 20376f
	 * ourselves to those commits in the original list (in-degree
Packit Service 20376f
	 * of 1) avoiding setting it for any parent that was hidden.
Packit Service 20376f
	 */
Packit Service 20376f
	for(ll = list; ll; ll = ll->next) {
Packit Service 20376f
		for (i = 0; i < ll->item->out_degree; ++i) {
Packit Service 20376f
			git_commit_list_node *parent = ll->item->parents[i];
Packit Service 20376f
			if (parent->in_degree)
Packit Service 20376f
				parent->in_degree++;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * Now we find the tips i.e. those not reachable from any other node
Packit Service 20376f
	 * i.e. those which still have an in-degree of 1.
Packit Service 20376f
	 */
Packit Service 20376f
	for(ll = list; ll; ll = ll->next) {
Packit Service 20376f
		if (ll->item->in_degree == 1) {
Packit Service 20376f
			if ((error = git_pqueue_insert(&queue, ll->item)))
Packit Service 20376f
				goto cleanup;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * We need to output the tips in the order that they came out of the
Packit Service 20376f
	 * traversal, so if we're not doing time-sorting, we need to reverse the
Packit Service 20376f
	 * pqueue in order to get them to come out as we inserted them.
Packit Service 20376f
	 */
Packit Service 20376f
	if ((walk->sorting & GIT_SORT_TIME) == 0)
Packit Service 20376f
		git_pqueue_reverse(&queue);
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
	pptr = &newlist;
Packit Service 20376f
	newlist = NULL;
Packit Service 20376f
	while ((next = git_pqueue_pop(&queue)) != NULL) {
Packit Service 20376f
		for (i = 0; i < next->out_degree; ++i) {
Packit Service 20376f
			git_commit_list_node *parent = next->parents[i];
Packit Service 20376f
			if (parent->in_degree == 0)
Packit Service 20376f
				continue;
Packit Service 20376f
Packit Service 20376f
			if (--parent->in_degree == 1) {
Packit Service 20376f
				if ((error = git_pqueue_insert(&queue, parent)))
Packit Service 20376f
					goto cleanup;
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* All the children of 'item' have been emitted (since we got to it via the priority queue) */
Packit Service 20376f
		next->in_degree = 0;
Packit Service 20376f
Packit Service 20376f
		pptr = &git_commit_list_insert(next, pptr)->next;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*out = newlist;
Packit Service 20376f
	error = 0;
Packit Service 20376f
Packit Service 20376f
cleanup:
Packit Service 20376f
	git_pqueue_free(&queue);
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int prepare_walk(git_revwalk *walk)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_commit_list *list, *commits = NULL;
Packit Service 20376f
	git_commit_list_node *next;
Packit Service 20376f
Packit Service 20376f
	/* If there were no pushes, we know that the walk is already over */
Packit Service 20376f
	if (!walk->did_push) {
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
		return GIT_ITEROVER;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	for (list = walk->user_input; list; list = list->next) {
Packit Service 20376f
		git_commit_list_node *commit = list->item;
Packit Service 20376f
		if ((error = git_commit_list_parse(walk, commit)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		if (commit->uninteresting)
Packit Service 20376f
			mark_parents_uninteresting(commit);
Packit Service 20376f
Packit Service 20376f
		if (!commit->seen) {
Packit Service 20376f
			commit->seen = 1;
Packit Service 20376f
			git_commit_list_insert(commit, &commits);
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if ((error = limit_list(&commits, walk, commits)) < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	if (walk->sorting & GIT_SORT_TOPOLOGICAL) {
Packit Service 20376f
		error = sort_in_topological_order(&walk->iterator_topo, walk, commits);
Packit Service 20376f
		git_commit_list_free(&commits);
Packit Service 20376f
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		walk->get_next = &revwalk_next_toposort;
Packit Service 20376f
	} else if (walk->sorting & GIT_SORT_TIME) {
Packit Service 20376f
		for (list = commits; list && !error; list = list->next)
Packit Service 20376f
			error = walk->enqueue(walk, list->item);
Packit Service 20376f
Packit Service 20376f
		git_commit_list_free(&commits);
Packit Service 20376f
Packit Service 20376f
		if (error < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
	} else {
Packit Service 20376f
		walk->iterator_rand = commits;
Packit Service 20376f
		walk->get_next = revwalk_next_unsorted;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (walk->sorting & GIT_SORT_REVERSE) {
Packit Service 20376f
Packit Service 20376f
		while ((error = walk->get_next(&next, walk)) == 0)
Packit Service 20376f
			if (git_commit_list_insert(next, &walk->iterator_reverse) == NULL)
Packit Service 20376f
				return -1;
Packit Service 20376f
Packit Service 20376f
		if (error != GIT_ITEROVER)
Packit Service 20376f
			return error;
Packit Service 20376f
Packit Service 20376f
		walk->get_next = &revwalk_next_reverse;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	walk->walking = 1;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
Packit Service 20376f
{
Packit Service 20376f
	git_revwalk *walk = git__calloc(1, sizeof(git_revwalk));
Packit Service 20376f
	GITERR_CHECK_ALLOC(walk);
Packit Service 20376f
Packit Service 20376f
	walk->commits = git_oidmap_alloc();
Packit Service 20376f
	GITERR_CHECK_ALLOC(walk->commits);
Packit Service 20376f
Packit Service 20376f
	if (git_pqueue_init(&walk->iterator_time, 0, 8, git_commit_list_time_cmp) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	git_pool_init(&walk->commit_pool, COMMIT_ALLOC);
Packit Service 20376f
	walk->get_next = &revwalk_next_unsorted;
Packit Service 20376f
	walk->enqueue = &revwalk_enqueue_unsorted;
Packit Service 20376f
Packit Service 20376f
	walk->repo = repo;
Packit Service 20376f
Packit Service 20376f
	if (git_repository_odb(&walk->odb, repo) < 0) {
Packit Service 20376f
		git_revwalk_free(walk);
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	*revwalk_out = walk;
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_revwalk_free(git_revwalk *walk)
Packit Service 20376f
{
Packit Service 20376f
	if (walk == NULL)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	git_revwalk_reset(walk);
Packit Service 20376f
	git_odb_free(walk->odb);
Packit Service 20376f
Packit Service 20376f
	git_oidmap_free(walk->commits);
Packit Service 20376f
	git_pool_clear(&walk->commit_pool);
Packit Service 20376f
	git_pqueue_free(&walk->iterator_time);
Packit Service 20376f
	git__free(walk);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
git_repository *git_revwalk_repository(git_revwalk *walk)
Packit Service 20376f
{
Packit Service 20376f
	assert(walk);
Packit Service 20376f
	return walk->repo;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_revwalk_sorting(git_revwalk *walk, unsigned int sort_mode)
Packit Service 20376f
{
Packit Service 20376f
	assert(walk);
Packit Service 20376f
Packit Service 20376f
	if (walk->walking)
Packit Service 20376f
		git_revwalk_reset(walk);
Packit Service 20376f
Packit Service 20376f
	walk->sorting = sort_mode;
Packit Service 20376f
Packit Service 20376f
	if (walk->sorting & GIT_SORT_TIME) {
Packit Service 20376f
		walk->get_next = &revwalk_next_timesort;
Packit Service 20376f
		walk->enqueue = &revwalk_enqueue_timesort;
Packit Service 20376f
	} else {
Packit Service 20376f
		walk->get_next = &revwalk_next_unsorted;
Packit Service 20376f
		walk->enqueue = &revwalk_enqueue_unsorted;
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_revwalk_simplify_first_parent(git_revwalk *walk)
Packit Service 20376f
{
Packit Service 20376f
	walk->first_parent = 1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_revwalk_next(git_oid *oid, git_revwalk *walk)
Packit Service 20376f
{
Packit Service 20376f
	int error;
Packit Service 20376f
	git_commit_list_node *next;
Packit Service 20376f
Packit Service 20376f
	assert(walk && oid);
Packit Service 20376f
Packit Service 20376f
	if (!walk->walking) {
Packit Service 20376f
		if ((error = prepare_walk(walk)) < 0)
Packit Service 20376f
			return error;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	error = walk->get_next(&next, walk);
Packit Service 20376f
Packit Service 20376f
	if (error == GIT_ITEROVER) {
Packit Service 20376f
		git_revwalk_reset(walk);
Packit Service 20376f
		giterr_clear();
Packit Service 20376f
		return GIT_ITEROVER;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!error)
Packit Service 20376f
		git_oid_cpy(oid, &next->oid);
Packit Service 20376f
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_revwalk_reset(git_revwalk *walk)
Packit Service 20376f
{
Packit Service 20376f
	git_commit_list_node *commit;
Packit Service 20376f
Packit Service 20376f
	assert(walk);
Packit Service 20376f
Packit Service 20376f
	git_oidmap_foreach_value(walk->commits, commit, {
Packit Service 20376f
		commit->seen = 0;
Packit Service 20376f
		commit->in_degree = 0;
Packit Service 20376f
		commit->topo_delay = 0;
Packit Service 20376f
		commit->uninteresting = 0;
Packit Service 20376f
		commit->added = 0;
Packit Service 20376f
		commit->flags = 0;
Packit Service 20376f
		});
Packit Service 20376f
Packit Service 20376f
	git_pqueue_clear(&walk->iterator_time);
Packit Service 20376f
	git_commit_list_free(&walk->iterator_topo);
Packit Service 20376f
	git_commit_list_free(&walk->iterator_rand);
Packit Service 20376f
	git_commit_list_free(&walk->iterator_reverse);
Packit Service 20376f
	git_commit_list_free(&walk->user_input);
Packit Service 20376f
	walk->first_parent = 0;
Packit Service 20376f
	walk->walking = 0;
Packit Service 20376f
	walk->did_push = walk->did_hide = 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_revwalk_add_hide_cb(
Packit Service 20376f
	git_revwalk *walk,
Packit Service 20376f
	git_revwalk_hide_cb hide_cb,
Packit Service 20376f
	void *payload)
Packit Service 20376f
{
Packit Service 20376f
	assert(walk);
Packit Service 20376f
Packit Service 20376f
	if (walk->walking)
Packit Service 20376f
		git_revwalk_reset(walk);
Packit Service 20376f
Packit Service 20376f
	if (walk->hide_cb) {
Packit Service 20376f
		/* There is already a callback added */
Packit Service 20376f
		giterr_set(GITERR_INVALID, "there is already a callback added to hide commits in revwalk");
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	walk->hide_cb = hide_cb;
Packit Service 20376f
	walk->hide_cb_payload = payload;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f