summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorScott J. Goldman <scottjg@github.com>2012-11-27 16:36:50 -0800
committerScott J. Goldman <scottjg@github.com>2012-11-28 18:54:56 -0800
commitbff53e5405e686f78e1ae81a4521566e3c67b5df (patch)
treeabe7ecd8e54cfb8005291952452c9edb2cb1b4e3 /src
parent693021262ba0eeac2923bbce1b2262717019c807 (diff)
downloadlibgit2-bff53e5405e686f78e1ae81a4521566e3c67b5df.tar.gz
Add initial implementation of ahead-behind count
Diffstat (limited to 'src')
-rw-r--r--src/merge.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/merge.c b/src/merge.c
index c795b808b..e0fc0abf1 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -242,3 +242,84 @@ int git_merge__bases_many(git_commit_list **out, git_revwalk *walk, git_commit_l
return 0;
}
+static int count_ahead_behind(git_commit_list_node *one, git_commit_list_node *two,
+ int *ahead, int *behind)
+{
+ git_commit_list_node *commit;
+ git_pqueue pq;
+ int i;
+ *ahead = 0;
+ *behind = 0;
+
+ if (git_pqueue_init(&pq, 2, git_commit_list_time_cmp) < 0)
+ return -1;
+ if (git_pqueue_insert(&pq, one) < 0)
+ return -1;
+ if (git_pqueue_insert(&pq, two) < 0)
+ return -1;
+
+ while((commit = git_pqueue_pop(&pq)) != NULL) {
+ if (commit->flags & RESULT ||
+ (commit->flags & (PARENT1 | PARENT2)) == (PARENT1 | PARENT2))
+ continue;
+ else if (commit->flags & PARENT1)
+ (*behind)++;
+ else if (commit->flags & PARENT2)
+ (*ahead)++;
+
+ for (i = 0; i < commit->out_degree; i++) {
+ git_commit_list_node *p = commit->parents[i];
+ if (git_pqueue_insert(&pq, p) < 0)
+ return -1;
+ }
+ commit->flags |= RESULT;
+ }
+
+ return 0;
+}
+
+int git_count_ahead_behind(int *ahead, int *behind, git_repository *repo, git_oid *one,
+ 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;
+
+ commit2 = commit_lookup(walk, two);
+ 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 = commit_lookup(walk, one);
+ if (commit1 == NULL)
+ goto on_error;
+
+ if (git_merge__bases_many(&result, walk, commit1, &list) < 0)
+ goto on_error;
+ if (count_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;
+
+on_error:
+ git_revwalk_free(walk);
+ return -1;
+}