Commit d74b1bc5292a438d68d802082825cbf3a6661074

Vicent Martí 2013-01-05T15:44:19

Merge pull request #1131 from libgit2/correct-ahead-behind Fix an issue with ahead-behind for lopsided traversal

diff --git a/src/graph.c b/src/graph.c
index fd789d6..e61fc84 100644
--- a/src/graph.c
+++ b/src/graph.c
@@ -10,6 +10,101 @@
 #include "merge.h"
 #include "git2/graph.h"
 
+static int interesting(git_pqueue *list, git_commit_list *roots)
+{
+	unsigned int i;
+	/* element 0 isn't used - we need to start at 1 */
+	for (i = 1; i < list->size; i++) {
+		git_commit_list_node *commit = list->d[i];
+		if ((commit->flags & STALE) == 0)
+			return 1;
+	}
+
+	while(roots) {
+		if ((roots->item->flags & STALE) == 0)
+			return 1;
+		roots = roots->next;
+	}
+
+	return 0;
+}
+
+static int mark_parents(git_revwalk *walk, git_commit_list_node *one,
+	git_commit_list_node *two)
+{
+	unsigned int i;
+	git_commit_list *roots = NULL;
+	git_pqueue list;
+
+	/* if the commit is repeated, we have a our merge base already */
+	if (one == two) {
+		one->flags |= PARENT1 | PARENT2 | RESULT;
+		return 0;
+	}
+
+	if (git_pqueue_init(&list, 2, git_commit_list_time_cmp) < 0)
+		return -1;
+
+	if (git_commit_list_parse(walk, one) < 0)
+		goto on_error;
+	one->flags |= PARENT1;
+	if (git_pqueue_insert(&list, one) < 0)
+		goto on_error;
+
+	if (git_commit_list_parse(walk, two) < 0)
+		goto on_error;
+	two->flags |= PARENT2;
+	if (git_pqueue_insert(&list, two) < 0)
+		goto on_error;
+
+	/* as long as there are non-STALE commits */
+	while (interesting(&list, roots)) {
+		git_commit_list_node *commit;
+		int flags;
+
+		commit = git_pqueue_pop(&list);
+		if (commit == NULL)
+			break;
+
+		flags = commit->flags & (PARENT1 | PARENT2 | STALE);
+		if (flags == (PARENT1 | PARENT2)) {
+			if (!(commit->flags & RESULT))
+				commit->flags |= RESULT;
+			/* we mark the parents of a merge stale */
+			flags |= STALE;
+		}
+
+		for (i = 0; i < commit->out_degree; i++) {
+			git_commit_list_node *p = commit->parents[i];
+			if ((p->flags & flags) == flags)
+				continue;
+
+			if (git_commit_list_parse(walk, p) < 0)
+				goto on_error;
+
+			p->flags |= flags;
+			if (git_pqueue_insert(&list, p) < 0)
+				goto on_error;
+		}
+
+		/* Keep track of root commits, to make sure the path gets marked */
+		if (commit->out_degree == 0) {
+			if (git_commit_list_insert(commit, &roots) == NULL)
+				goto on_error;
+		}
+	}
+
+	git_commit_list_free(&roots);
+	git_pqueue_free(&list);
+	return 0;
+
+on_error:
+	git_commit_list_free(&roots);
+	git_pqueue_free(&list);
+	return -1;
+}
+
+
 static int ahead_behind(git_commit_list_node *one, git_commit_list_node *two,
 	size_t *ahead, size_t *behind)
 {
@@ -55,10 +150,7 @@ int git_graph_ahead_behind(size_t *ahead, size_t *behind, git_repository *repo,
 	const git_oid *one, const git_oid *two)
 {
 	git_revwalk *walk;
-	git_vector list;
-	struct git_commit_list *result = NULL;
 	git_commit_list_node *commit1, *commit2;
-	void *contents[1];
 
 	if (git_revwalk_new(&walk, repo) < 0)
 		return -1;
@@ -67,27 +159,15 @@ int git_graph_ahead_behind(size_t *ahead, size_t *behind, git_repository *repo,
 	if (commit2 == NULL)
 		goto on_error;
 
-	/* This is just one value, so we can do it on the stack */
-	memset(&list, 0x0, sizeof(git_vector));
-	contents[0] = commit2;
-	list.length = 1;
-	list.contents = contents;
-
 	commit1 = git_revwalk__commit_lookup(walk, one);
 	if (commit1 == NULL)
 		goto on_error;
 
-	if (git_merge__bases_many(&result, walk, commit1, &list) < 0)
+	if (mark_parents(walk, commit1, commit2) < 0)
 		goto on_error;
 	if (ahead_behind(commit1, commit2, ahead, behind) < 0)
 		goto on_error;
 
-	if (!result) {
-		git_revwalk_free(walk);
-		return GIT_ENOTFOUND;
-	}
-
-	git_commit_list_free(&result);
 	git_revwalk_free(walk);
 
 	return 0;
diff --git a/tests-clar/revwalk/mergebase.c b/tests-clar/revwalk/mergebase.c
index 4faf615..293a585 100644
--- a/tests-clar/revwalk/mergebase.c
+++ b/tests-clar/revwalk/mergebase.c
@@ -118,10 +118,9 @@ void test_revwalk_mergebase__no_common_ancestor_returns_ENOTFOUND(void)
 
 	cl_assert_equal_i(GIT_ENOTFOUND, error);
 
-	cl_git_fail(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two));
-	cl_git_fail(error);
-
-	cl_assert_equal_i(GIT_ENOTFOUND, error);
+	cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two));
+	cl_assert_equal_i(2, ahead);
+	cl_assert_equal_i(4, behind);
 }
 
 void test_revwalk_mergebase__no_off_by_one_missing(void)