Implement git_merge_base() It's implemented in revwalk.c so it has access to the revision walker's commit cache and related functions. The algorithm is the one used by git, modified so it fits better with the library's functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
diff --git a/include/git2/revwalk.h b/include/git2/revwalk.h
index 632c675..db27c62 100644
--- a/include/git2/revwalk.h
+++ b/include/git2/revwalk.h
@@ -232,6 +232,9 @@ GIT_EXTERN(void) git_revwalk_free(git_revwalk *walk);
*/
GIT_EXTERN(git_repository *) git_revwalk_repository(git_revwalk *walk);
+GIT_EXTERN(int) git_merge_base(git_oid *out, git_repository *repo, git_oid *one, git_oid *two);
+
+
/** @} */
GIT_END_DECL
#endif
diff --git a/src/revwalk.c b/src/revwalk.c
index 1a398ce..92885d6 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -15,13 +15,19 @@
#include <regex.h>
+#define PARENT1 (1 << 0)
+#define PARENT2 (1 << 1)
+#define RESULT (1 << 2)
+#define STALE (1 << 3)
+
typedef struct commit_object {
git_oid oid;
uint32_t time;
unsigned int seen:1,
uninteresting:1,
topo_delay:1,
- parsed:1;
+ parsed:1,
+ flags : 4;
unsigned short in_degree;
unsigned short out_degree;
@@ -55,6 +61,14 @@ struct git_revwalk {
unsigned int sorting;
};
+static int commit_time_cmp(void *a, void *b)
+{
+ commit_object *commit_a = (commit_object *)a;
+ commit_object *commit_b = (commit_object *)b;
+
+ return (commit_a->time < commit_b->time);
+}
+
static commit_list *commit_list_insert(commit_object *item, commit_list **list_p)
{
commit_list *new_list = git__malloc(sizeof(commit_list));
@@ -67,6 +81,20 @@ static commit_list *commit_list_insert(commit_object *item, commit_list **list_p
return new_list;
}
+static commit_list *commit_list_insert_by_date(commit_object *item, commit_list **list_p)
+{
+ commit_list **pp = list_p;
+ commit_list *p;
+
+ while ((p = *pp) != NULL) {
+ if (commit_time_cmp(p->item, item) < 0)
+ break;
+
+ pp = &p->next;
+ }
+
+ return commit_list_insert(item, pp);
+}
static void commit_list_free(commit_list **list_p)
{
commit_list *list = *list_p;
@@ -92,14 +120,6 @@ static commit_object *commit_list_pop(commit_list **stack)
return item;
}
-static int commit_time_cmp(void *a, void *b)
-{
- commit_object *commit_a = (commit_object *)a;
- commit_object *commit_b = (commit_object *)b;
-
- return (commit_a->time < commit_b->time);
-}
-
static uint32_t object_table_hash(const void *key, int hash_id)
{
uint32_t r;
@@ -244,6 +264,144 @@ static int commit_parse(git_revwalk *walk, commit_object *commit)
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to parse commit");
}
+static commit_object *interesting(commit_list *list)
+{
+ while (list) {
+ commit_object *commit = list->item;
+ list = list->next;
+ if (commit->flags & STALE)
+ continue;
+
+ return commit;
+ }
+
+ return NULL;
+}
+
+static int merge_bases_many(commit_list **out, git_revwalk *walk, commit_object *one, git_vector *twos)
+{
+ int error;
+ unsigned int i;
+ commit_object *two;
+ commit_list *list = NULL, *result = NULL;
+
+ /* if the commit is repeated, we have a our merge base already */
+ git_vector_foreach(twos, i, two) {
+ if (one == two)
+ return commit_list_insert(one, out) ? GIT_SUCCESS : GIT_ENOMEM;
+ }
+
+ if ((error = commit_parse(walk, one)) < GIT_SUCCESS)
+ return error;
+
+ one->flags |= PARENT1;
+ if (commit_list_insert(one, &list) == NULL)
+ return GIT_ENOMEM;
+
+ git_vector_foreach(twos, i, two) {
+ commit_parse(walk, two);
+ two->flags |= PARENT2;
+ if (commit_list_insert_by_date(two, &list) == NULL)
+ return GIT_ENOMEM;
+ }
+
+ /* as long as there are non-STALE commits */
+ while (interesting(list)) {
+ commit_object *commit = list->item;
+ commit_list *next;
+ int flags;
+
+ next = list->next;
+ git__free(list);
+ list = next;
+
+ flags = commit->flags & (PARENT1 | PARENT2 | STALE);
+ if (flags == (PARENT1 | PARENT2)) {
+ if (!(commit->flags & RESULT)) {
+ commit->flags |= RESULT;
+ if (commit_list_insert_by_date(commit, &result) == NULL)
+ return GIT_ENOMEM;
+ }
+ /* we mark the parents of a merge stale */
+ flags |= STALE;
+ }
+
+ for (i = 0; i < commit->out_degree; i++) {
+ commit_object *p = commit->parents[i];
+ if ((p->flags & flags) == flags)
+ continue;
+
+ if ((error = commit_parse(walk, p)) < GIT_SUCCESS)
+ return error;
+
+ p->flags |= flags;
+ if (commit_list_insert_by_date(p, &list) == NULL)
+ return GIT_ENOMEM;
+ }
+ }
+
+ commit_list_free(&list);
+ /* filter out any stale commits in the results */
+ list = result;
+ result = NULL;
+
+ while (list) {
+ struct commit_list *next = list->next;
+ if (!(list->item->flags & STALE))
+ if (commit_list_insert_by_date(list->item, &result) == NULL)
+ return GIT_ENOMEM;
+
+ free(list);
+ list = next;
+ }
+
+ *out = result;
+ return GIT_SUCCESS;
+}
+
+int git_merge_base(git_oid *out, git_repository *repo, git_oid *one, git_oid *two)
+{
+ git_revwalk *walk;
+ git_vector list;
+ commit_list *result;
+ commit_object *commit;
+ void *contents[1];
+ int error;
+
+ error = git_revwalk_new(&walk, repo);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ commit = commit_lookup(walk, two);
+ if (commit == NULL)
+ goto cleanup;
+
+ /* This is just one value, so we can do it on the stack */
+ memset(&list, 0x0, sizeof(git_vector));
+ contents[0] = commit;
+ list.length = 1;
+ list.contents = contents;
+
+ commit = commit_lookup(walk, one);
+ if (commit == NULL)
+ goto cleanup;
+
+ error = merge_bases_many(&result, walk, commit, &list);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ if (result == NULL)
+ return GIT_ENOTFOUND;
+
+ git_oid_cpy(out, &result->item->oid);
+ commit_list_free(&result);
+
+cleanup:
+ git_revwalk_free(walk);
+
+ return GIT_SUCCESS;
+}
+
static void mark_uninteresting(commit_object *commit)
{
unsigned short i;
diff --git a/tests-clar/revwalk/mergebase.c b/tests-clar/revwalk/mergebase.c
new file mode 100644
index 0000000..91bc6ae
--- /dev/null
+++ b/tests-clar/revwalk/mergebase.c
@@ -0,0 +1,37 @@
+#include "clar_libgit2.h"
+
+static git_repository *_repo;
+
+void test_revwalk_mergebase__initialize(void)
+{
+ cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
+}
+
+void test_revwalk_mergebase__cleanup(void)
+{
+ git_repository_free(_repo);
+}
+
+void test_revwalk_mergebase__single1(void)
+{
+ git_oid result, one, two, expected;
+
+ git_oid_fromstr(&one, "c47800c7266a2be04c571c04d5a6614691ea99bd ");
+ git_oid_fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a");
+ git_oid_fromstr(&expected, "5b5b025afb0b4c913b4c338a42934a3863bf3644");
+
+ cl_git_pass(git_merge_base(&result, _repo, &one, &two));
+ cl_assert(git_oid_cmp(&result, &expected) == 0);
+}
+
+void test_revwalk_mergebase__single2(void)
+{
+ git_oid result, one, two, expected;
+
+ git_oid_fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af");
+ git_oid_fromstr(&two, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
+ git_oid_fromstr(&expected, "c47800c7266a2be04c571c04d5a6614691ea99bd");
+
+ cl_git_pass(git_merge_base(&result, _repo, &one, &two));
+ cl_assert(git_oid_cmp(&result, &expected) == 0);
+}