diff options
author | Scott J. Goldman <scottjg@github.com> | 2012-11-29 06:27:43 +0400 |
---|---|---|
committer | Scott J. Goldman <scottjg@github.com> | 2012-11-29 06:54:57 +0400 |
commit | 0984c8768d36c73adeabe0229960e651531edf17 (patch) | |
tree | d8002b68a51c99f4c57b6bd26c1d4a0eee13bdbb /src/graph.c | |
parent | c6d03c958fc5604c8a00c9a512ec342caa3e43ef (diff) |
Rename git_count_ahead_behind -> git_graph_ahead_behind
Moved it into graph.{c,h} which i created for the new "graph"
functions namespace. Also adjusted the function prototype
to use `size_t` and `const git_oid *`.
Diffstat (limited to 'src/graph.c')
-rw-r--r-- | src/graph.c | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/graph.c b/src/graph.c new file mode 100644 index 000000000..db4d73025 --- /dev/null +++ b/src/graph.c @@ -0,0 +1,100 @@ + +/* + * Copyright (C) 2009-2012 the libgit2 contributors + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "repository.h" +#include "revwalk.h" +#include "buffer.h" +#include "merge.h" +#include "refs.h" +#include "git2/repository.h" +#include "git2/merge.h" +#include "git2/reset.h" +#include "commit_list.h" +#include "git2/graph.h" + +static int ahead_behind(git_commit_list_node *one, git_commit_list_node *two, + size_t *ahead, size_t *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_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; + + 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 (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; +} |