Blame src/graph.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 "revwalk.h"
Packit Service 20376f
#include "merge.h"
Packit Service 20376f
#include "git2/graph.h"
Packit Service 20376f
Packit Service 20376f
static int interesting(git_pqueue *list, git_commit_list *roots)
Packit Service 20376f
{
Packit Service 20376f
	unsigned int i;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < git_pqueue_size(list); i++) {
Packit Service 20376f
		git_commit_list_node *commit = git_pqueue_get(list, i);
Packit Service 20376f
		if ((commit->flags & STALE) == 0)
Packit Service 20376f
			return 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	while(roots) {
Packit Service 20376f
		if ((roots->item->flags & STALE) == 0)
Packit Service 20376f
			return 1;
Packit Service 20376f
		roots = roots->next;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int mark_parents(git_revwalk *walk, git_commit_list_node *one,
Packit Service 20376f
	git_commit_list_node *two)
Packit Service 20376f
{
Packit Service 20376f
	unsigned int i;
Packit Service 20376f
	git_commit_list *roots = NULL;
Packit Service 20376f
	git_pqueue list;
Packit Service 20376f
Packit Service 20376f
	/* if the commit is repeated, we have a our merge base already */
Packit Service 20376f
	if (one == two) {
Packit Service 20376f
		one->flags |= PARENT1 | PARENT2 | RESULT;
Packit Service 20376f
		return 0;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (git_pqueue_init(&list, 0, 2, git_commit_list_time_cmp) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if (git_commit_list_parse(walk, one) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	one->flags |= PARENT1;
Packit Service 20376f
	if (git_pqueue_insert(&list, one) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	if (git_commit_list_parse(walk, two) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	two->flags |= PARENT2;
Packit Service 20376f
	if (git_pqueue_insert(&list, two) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	/* as long as there are non-STALE commits */
Packit Service 20376f
	while (interesting(&list, roots)) {
Packit Service 20376f
		git_commit_list_node *commit = git_pqueue_pop(&list);
Packit Service 20376f
		unsigned int flags;
Packit Service 20376f
Packit Service 20376f
		if (commit == NULL)
Packit Service 20376f
			break;
Packit Service 20376f
Packit Service 20376f
		flags = commit->flags & (PARENT1 | PARENT2 | STALE);
Packit Service 20376f
		if (flags == (PARENT1 | PARENT2)) {
Packit Service 20376f
			if (!(commit->flags & RESULT))
Packit Service 20376f
				commit->flags |= RESULT;
Packit Service 20376f
			/* we mark the parents of a merge stale */
Packit Service 20376f
			flags |= STALE;
Packit Service 20376f
		}
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
			if ((p->flags & flags) == flags)
Packit Service 20376f
				continue;
Packit Service 20376f
Packit Service 20376f
			if (git_commit_list_parse(walk, p) < 0)
Packit Service 20376f
				goto on_error;
Packit Service 20376f
Packit Service 20376f
			p->flags |= flags;
Packit Service 20376f
			if (git_pqueue_insert(&list, p) < 0)
Packit Service 20376f
				goto on_error;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		/* Keep track of root commits, to make sure the path gets marked */
Packit Service 20376f
		if (commit->out_degree == 0) {
Packit Service 20376f
			if (git_commit_list_insert(commit, &roots) == NULL)
Packit Service 20376f
				goto on_error;
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_commit_list_free(&roots);
Packit Service 20376f
	git_pqueue_free(&list);
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
on_error:
Packit Service 20376f
	git_commit_list_free(&roots);
Packit Service 20376f
	git_pqueue_free(&list);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
static int ahead_behind(git_commit_list_node *one, git_commit_list_node *two,
Packit Service 20376f
	size_t *ahead, size_t *behind)
Packit Service 20376f
{
Packit Service 20376f
	git_commit_list_node *commit;
Packit Service 20376f
	git_pqueue pq;
Packit Service 20376f
	int error = 0, i;
Packit Service 20376f
	*ahead = 0;
Packit Service 20376f
	*behind = 0;
Packit Service 20376f
Packit Service 20376f
	if (git_pqueue_init(&pq, 0, 2, git_commit_list_time_cmp) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	if ((error = git_pqueue_insert(&pq, one)) < 0 ||
Packit Service 20376f
		(error = git_pqueue_insert(&pq, two)) < 0)
Packit Service 20376f
		goto done;
Packit Service 20376f
Packit Service 20376f
	while ((commit = git_pqueue_pop(&pq)) != NULL) {
Packit Service 20376f
		if (commit->flags & RESULT ||
Packit Service 20376f
			(commit->flags & (PARENT1 | PARENT2)) == (PARENT1 | PARENT2))
Packit Service 20376f
			continue;
Packit Service 20376f
		else if (commit->flags & PARENT1)
Packit Service 20376f
			(*ahead)++;
Packit Service 20376f
		else if (commit->flags & PARENT2)
Packit Service 20376f
			(*behind)++;
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
			if ((error = git_pqueue_insert(&pq, p)) < 0)
Packit Service 20376f
				goto done;
Packit Service 20376f
		}
Packit Service 20376f
		commit->flags |= RESULT;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
done:
Packit Service 20376f
	git_pqueue_free(&pq;;
Packit Service 20376f
	return error;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_graph_ahead_behind(size_t *ahead, size_t *behind, git_repository *repo,
Packit Service 20376f
	const git_oid *local, const git_oid *upstream)
Packit Service 20376f
{
Packit Service 20376f
	git_revwalk *walk;
Packit Service 20376f
	git_commit_list_node *commit_u, *commit_l;
Packit Service 20376f
Packit Service 20376f
	if (git_revwalk_new(&walk, repo) < 0)
Packit Service 20376f
		return -1;
Packit Service 20376f
Packit Service 20376f
	commit_u = git_revwalk__commit_lookup(walk, upstream);
Packit Service 20376f
	if (commit_u == NULL)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	commit_l = git_revwalk__commit_lookup(walk, local);
Packit Service 20376f
	if (commit_l == NULL)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	if (mark_parents(walk, commit_l, commit_u) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
	if (ahead_behind(commit_l, commit_u, ahead, behind) < 0)
Packit Service 20376f
		goto on_error;
Packit Service 20376f
Packit Service 20376f
	git_revwalk_free(walk);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
Packit Service 20376f
on_error:
Packit Service 20376f
	git_revwalk_free(walk);
Packit Service 20376f
	return -1;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_graph_descendant_of(git_repository *repo, const git_oid *commit, const git_oid *ancestor)
Packit Service 20376f
{
Packit Service 20376f
	git_oid merge_base;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	if (git_oid_equal(commit, ancestor))
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	error = git_merge_base(&merge_base, repo, commit, ancestor);
Packit Service 20376f
	/* No merge-base found, it's not a descendant */
Packit Service 20376f
	if (error == GIT_ENOTFOUND)
Packit Service 20376f
		return 0;
Packit Service 20376f
Packit Service 20376f
	if (error < 0)
Packit Service 20376f
		return error;
Packit Service 20376f
Packit Service 20376f
	return git_oid_equal(&merge_base, ancestor);
Packit Service 20376f
}