]> git.proxmox.com Git - libgit2.git/blob - tests/revwalk/mergebase.c
bee0b926e0851bbb1d4f90061a654f4da258e231
[libgit2.git] / tests / revwalk / mergebase.c
1 #include "clar_libgit2.h"
2 #include "vector.h"
3 #include <stdarg.h>
4
5 static git_repository *_repo;
6 static git_repository *_repo2;
7
8 void test_revwalk_mergebase__initialize(void)
9 {
10 cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
11 cl_git_pass(git_repository_open(&_repo2, cl_fixture("twowaymerge.git")));
12 }
13
14 void test_revwalk_mergebase__cleanup(void)
15 {
16 git_repository_free(_repo);
17 _repo = NULL;
18
19 git_repository_free(_repo2);
20 _repo2 = NULL;
21 }
22
23 void test_revwalk_mergebase__single1(void)
24 {
25 git_oid result, one, two, expected;
26 size_t ahead, behind;
27
28 cl_git_pass(git_oid_fromstr(&one, "c47800c7266a2be04c571c04d5a6614691ea99bd "));
29 cl_git_pass(git_oid_fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
30 cl_git_pass(git_oid_fromstr(&expected, "5b5b025afb0b4c913b4c338a42934a3863bf3644"));
31
32 cl_git_pass(git_merge_base(&result, _repo, &one, &two));
33 cl_assert_equal_oid(&expected, &result);
34
35 cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two));
36 cl_assert_equal_sz(ahead, 1);
37 cl_assert_equal_sz(behind, 2);
38
39 cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &two, &one));
40 cl_assert_equal_sz(ahead, 2);
41 cl_assert_equal_sz(behind, 1);
42 }
43
44 void test_revwalk_mergebase__single2(void)
45 {
46 git_oid result, one, two, expected;
47 size_t ahead, behind;
48
49 cl_git_pass(git_oid_fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af"));
50 cl_git_pass(git_oid_fromstr(&two, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
51 cl_git_pass(git_oid_fromstr(&expected, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
52
53 cl_git_pass(git_merge_base(&result, _repo, &one, &two));
54 cl_assert_equal_oid(&expected, &result);
55
56 cl_git_pass(git_graph_ahead_behind( &ahead, &behind, _repo, &one, &two));
57 cl_assert_equal_sz(ahead, 1);
58 cl_assert_equal_sz(behind, 4);
59
60 cl_git_pass(git_graph_ahead_behind( &ahead, &behind, _repo, &two, &one));
61 cl_assert_equal_sz(ahead, 4);
62 cl_assert_equal_sz(behind, 1);
63 }
64
65 void test_revwalk_mergebase__merged_branch(void)
66 {
67 git_oid result, one, two, expected;
68 size_t ahead, behind;
69
70 cl_git_pass(git_oid_fromstr(&one, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
71 cl_git_pass(git_oid_fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
72 cl_git_pass(git_oid_fromstr(&expected, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
73
74 cl_git_pass(git_merge_base(&result, _repo, &one, &two));
75 cl_assert_equal_oid(&expected, &result);
76
77 cl_git_pass(git_merge_base(&result, _repo, &two, &one));
78 cl_assert_equal_oid(&expected, &result);
79
80 cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two));
81 cl_assert_equal_sz(ahead, 3);
82 cl_assert_equal_sz(behind, 0);
83
84 cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &two, &one));
85 cl_assert_equal_sz(ahead, 0);
86 cl_assert_equal_sz(behind, 3);
87 }
88
89 void test_revwalk_mergebase__two_way_merge(void)
90 {
91 git_oid one, two;
92 size_t ahead, behind;
93
94 cl_git_pass(git_oid_fromstr(&one, "9b219343610c88a1187c996d0dc58330b55cee28"));
95 cl_git_pass(git_oid_fromstr(&two, "a953a018c5b10b20c86e69fef55ebc8ad4c5a417"));
96 cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo2, &one, &two));
97
98 cl_assert_equal_sz(ahead, 8);
99 cl_assert_equal_sz(behind, 2);
100
101 cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo2, &two, &one));
102
103 cl_assert_equal_sz(ahead, 2);
104 cl_assert_equal_sz(behind, 8);
105 }
106
107 void test_revwalk_mergebase__no_common_ancestor_returns_ENOTFOUND(void)
108 {
109 git_oid result, one, two;
110 size_t ahead, behind;
111 int error;
112
113 cl_git_pass(git_oid_fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af"));
114 cl_git_pass(git_oid_fromstr(&two, "e90810b8df3e80c413d903f631643c716887138d"));
115
116 error = git_merge_base(&result, _repo, &one, &two);
117 cl_git_fail(error);
118
119 cl_assert_equal_i(GIT_ENOTFOUND, error);
120
121 cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two));
122 cl_assert_equal_sz(4, ahead);
123 cl_assert_equal_sz(2, behind);
124 }
125
126 void test_revwalk_mergebase__prefer_youngest_merge_base(void)
127 {
128 git_oid result, one, two, expected;
129
130 cl_git_pass(git_oid_fromstr(&one, "a4a7dce85cf63874e984719f4fdd239f5145052f"));
131 cl_git_pass(git_oid_fromstr(&two, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
132 cl_git_pass(git_oid_fromstr(&expected, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
133
134 cl_git_pass(git_merge_base(&result, _repo, &one, &two));
135 cl_assert_equal_oid(&expected, &result);
136 }
137
138 void test_revwalk_mergebase__multiple_merge_bases(void)
139 {
140 git_oid one, two, expected1, expected2;
141 git_oidarray result = {NULL, 0};
142
143 cl_git_pass(git_oid_fromstr(&one, "a4a7dce85cf63874e984719f4fdd239f5145052f"));
144 cl_git_pass(git_oid_fromstr(&two, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
145 cl_git_pass(git_oid_fromstr(&expected1, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
146 cl_git_pass(git_oid_fromstr(&expected2, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
147
148 cl_git_pass(git_merge_bases(&result, _repo, &one, &two));
149 cl_assert_equal_i(2, result.count);
150 cl_assert_equal_oid(&expected1, &result.ids[0]);
151 cl_assert_equal_oid(&expected2, &result.ids[1]);
152
153 git_oidarray_free(&result);
154 }
155
156 void test_revwalk_mergebase__multiple_merge_bases_many_commits(void)
157 {
158 git_oid expected1, expected2;
159 git_oidarray result = {NULL, 0};
160
161 git_oid *input = git__malloc(sizeof(git_oid) * 2);
162
163 cl_git_pass(git_oid_fromstr(&input[0], "a4a7dce85cf63874e984719f4fdd239f5145052f"));
164 cl_git_pass(git_oid_fromstr(&input[1], "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
165 cl_git_pass(git_oid_fromstr(&expected1, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
166 cl_git_pass(git_oid_fromstr(&expected2, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
167
168 cl_git_pass(git_merge_bases_many(&result, _repo, 2, input));
169 cl_assert_equal_i(2, result.count);
170 cl_assert_equal_oid(&expected1, &result.ids[0]);
171 cl_assert_equal_oid(&expected2, &result.ids[1]);
172
173 git_oidarray_free(&result);
174 git__free(input);
175 }
176
177 void test_revwalk_mergebase__no_off_by_one_missing(void)
178 {
179 git_oid result, one, two;
180
181 cl_git_pass(git_oid_fromstr(&one, "1a443023183e3f2bfbef8ac923cd81c1018a18fd"));
182 cl_git_pass(git_oid_fromstr(&two, "9f13f7d0a9402c681f91dc590cf7b5470e6a77d2"));
183 cl_git_pass(git_merge_base(&result, _repo, &one, &two));
184 }
185
186 static void assert_mergebase_many(const char *expected_sha, int count, ...)
187 {
188 va_list ap;
189 int i;
190 git_oid *oids;
191 git_oid oid, expected;
192 char *partial_oid;
193 git_object *object;
194
195 oids = git__malloc(count * sizeof(git_oid));
196 cl_assert(oids != NULL);
197
198 memset(oids, 0x0, count * sizeof(git_oid));
199
200 va_start(ap, count);
201
202 for (i = 0; i < count; ++i) {
203 partial_oid = va_arg(ap, char *);
204 cl_git_pass(git_oid_fromstrn(&oid, partial_oid, strlen(partial_oid)));
205
206 cl_git_pass(git_object_lookup_prefix(&object, _repo, &oid, strlen(partial_oid), GIT_OBJECT_COMMIT));
207 git_oid_cpy(&oids[i], git_object_id(object));
208 git_object_free(object);
209 }
210
211 va_end(ap);
212
213 if (expected_sha == NULL)
214 cl_assert_equal_i(GIT_ENOTFOUND, git_merge_base_many(&oid, _repo, count, oids));
215 else {
216 cl_git_pass(git_merge_base_many(&oid, _repo, count, oids));
217 cl_git_pass(git_oid_fromstr(&expected, expected_sha));
218
219 cl_assert_equal_oid(&expected, &oid);
220 }
221
222 git__free(oids);
223 }
224
225 void test_revwalk_mergebase__many_no_common_ancestor_returns_ENOTFOUND(void)
226 {
227 assert_mergebase_many(NULL, 3, "41bc8c", "e90810", "a65fed");
228 assert_mergebase_many(NULL, 3, "e90810", "41bc8c", "a65fed");
229 assert_mergebase_many(NULL, 3, "e90810", "a65fed", "41bc8c");
230 assert_mergebase_many(NULL, 3, "a65fed", "e90810", "41bc8c");
231 assert_mergebase_many(NULL, 3, "a65fed", "41bc8c", "e90810");
232
233 assert_mergebase_many(NULL, 3, "e90810", "763d71", "a65fed");
234 }
235
236 void test_revwalk_mergebase__many_merge_branch(void)
237 {
238 assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "763d71", "849607");
239
240 assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "763d71", "e90810", "a65fed");
241 assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "763d71", "a65fed", "e90810");
242
243 assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "763d71", "849607");
244 assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "849607", "763d71");
245 assert_mergebase_many("8496071c1b46c854b31185ea97743be6a8774479", 3, "849607", "a65fed", "763d71");
246
247 assert_mergebase_many("5b5b025afb0b4c913b4c338a42934a3863bf3644", 5, "5b5b02", "763d71", "a4a7dc", "a65fed", "41bc8c");
248 }
249
250 static void assert_mergebase_octopus(const char *expected_sha, int count, ...)
251 {
252 va_list ap;
253 int i;
254 git_oid *oids;
255 git_oid oid, expected;
256 char *partial_oid;
257 git_object *object;
258
259 oids = git__malloc(count * sizeof(git_oid));
260 cl_assert(oids != NULL);
261
262 memset(oids, 0x0, count * sizeof(git_oid));
263
264 va_start(ap, count);
265
266 for (i = 0; i < count; ++i) {
267 partial_oid = va_arg(ap, char *);
268 cl_git_pass(git_oid_fromstrn(&oid, partial_oid, strlen(partial_oid)));
269
270 cl_git_pass(git_object_lookup_prefix(&object, _repo, &oid, strlen(partial_oid), GIT_OBJECT_COMMIT));
271 git_oid_cpy(&oids[i], git_object_id(object));
272 git_object_free(object);
273 }
274
275 va_end(ap);
276
277 if (expected_sha == NULL)
278 cl_assert_equal_i(GIT_ENOTFOUND, git_merge_base_octopus(&oid, _repo, count, oids));
279 else {
280 cl_git_pass(git_merge_base_octopus(&oid, _repo, count, oids));
281 cl_git_pass(git_oid_fromstr(&expected, expected_sha));
282
283 cl_assert_equal_oid(&expected, &oid);
284 }
285
286 git__free(oids);
287 }
288
289 void test_revwalk_mergebase__octopus_no_common_ancestor_returns_ENOTFOUND(void)
290 {
291 assert_mergebase_octopus(NULL, 3, "41bc8c", "e90810", "a65fed");
292 assert_mergebase_octopus(NULL, 3, "e90810", "41bc8c", "a65fed");
293 assert_mergebase_octopus(NULL, 3, "e90810", "a65fed", "41bc8c");
294 assert_mergebase_octopus(NULL, 3, "a65fed", "e90810", "41bc8c");
295 assert_mergebase_octopus(NULL, 3, "a65fed", "41bc8c", "e90810");
296
297 assert_mergebase_octopus(NULL, 3, "e90810", "763d71", "a65fed");
298
299 assert_mergebase_octopus(NULL, 3, "763d71", "e90810", "a65fed");
300 assert_mergebase_octopus(NULL, 3, "763d71", "a65fed", "e90810");
301
302 assert_mergebase_octopus(NULL, 5, "5b5b02", "763d71", "a4a7dc", "a65fed", "41bc8c");
303 }
304
305 void test_revwalk_mergebase__octopus_merge_branch(void)
306 {
307 assert_mergebase_octopus("8496071c1b46c854b31185ea97743be6a8774479", 3, "a65fed", "763d71", "849607");
308
309 assert_mergebase_octopus("8496071c1b46c854b31185ea97743be6a8774479", 3, "a65fed", "763d71", "849607");
310 assert_mergebase_octopus("8496071c1b46c854b31185ea97743be6a8774479", 3, "a65fed", "849607", "763d71");
311 assert_mergebase_octopus("8496071c1b46c854b31185ea97743be6a8774479", 3, "849607", "a65fed", "763d71");
312 }
313
314 /*
315 * testrepo.git $ git log --graph --all
316 * * commit 763d71aadf09a7951596c9746c024e7eece7c7af
317 * | Author: nulltoken <emeric.fermas@gmail.com>
318 * | Date: Sun Oct 9 12:54:47 2011 +0200
319 * |
320 * | Add some files into subdirectories
321 * |
322 * | * commit a65fedf39aefe402d3bb6e24df4d4f5fe4547750
323 * | | Author: Scott Chacon <schacon@gmail.com>
324 * | | Date: Tue Aug 9 19:33:46 2011 -0700
325 * | |
326 * | * commit be3563ae3f795b2b4353bcce3a527ad0a4f7f644
327 * | |\ Merge: 9fd738e c47800c
328 * | |/ Author: Scott Chacon <schacon@gmail.com>
329 * |/| Date: Tue May 25 11:58:27 2010 -0700
330 * | |
331 * | | Merge branch 'br2'
332 * | |
333 * | | * commit e90810b8df3e80c413d903f631643c716887138d
334 * | | | Author: Vicent Marti <tanoku@gmail.com>
335 * | | | Date: Thu Aug 5 18:42:20 2010 +0200
336 * | | |
337 * | | | Test commit 2
338 * | | |
339 * | | * commit 6dcf9bf7541ee10456529833502442f385010c3d
340 * | | Author: Vicent Marti <tanoku@gmail.com>
341 * | | Date: Thu Aug 5 18:41:33 2010 +0200
342 * | |
343 * | | Test commit 1
344 * | |
345 * | | * commit a4a7dce85cf63874e984719f4fdd239f5145052f
346 * | | |\ Merge: c47800c 9fd738e
347 * | |/ / Author: Scott Chacon <schacon@gmail.com>
348 * |/| / Date: Tue May 25 12:00:23 2010 -0700
349 * | |/
350 * | | Merge branch 'master' into br2
351 * | |
352 * | * commit 9fd738e8f7967c078dceed8190330fc8648ee56a
353 * | | Author: Scott Chacon <schacon@gmail.com>
354 * | | Date: Mon May 24 10:19:19 2010 -0700
355 * | |
356 * | | a fourth commit
357 * | |
358 * | * commit 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
359 * | | Author: Scott Chacon <schacon@gmail.com>
360 * | | Date: Mon May 24 10:19:04 2010 -0700
361 * | |
362 * | | a third commit
363 * | |
364 * * | commit c47800c7266a2be04c571c04d5a6614691ea99bd
365 * |/ Author: Scott Chacon <schacon@gmail.com>
366 * | Date: Tue May 25 11:58:14 2010 -0700
367 * |
368 * | branch commit one
369 * |
370 * * commit 5b5b025afb0b4c913b4c338a42934a3863bf3644
371 * | Author: Scott Chacon <schacon@gmail.com>
372 * | Date: Tue May 11 13:38:42 2010 -0700
373 * |
374 * | another commit
375 * |
376 * * commit 8496071c1b46c854b31185ea97743be6a8774479
377 * Author: Scott Chacon <schacon@gmail.com>
378 * Date: Sat May 8 16:13:06 2010 -0700
379 *
380 * testing
381 *
382 * * commit 41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9
383 * | Author: Scott Chacon <schacon@gmail.com>
384 * | Date: Tue May 11 13:40:41 2010 -0700
385 * |
386 * | packed commit two
387 * |
388 * * commit 5001298e0c09ad9c34e4249bc5801c75e9754fa5
389 * Author: Scott Chacon <schacon@gmail.com>
390 * Date: Tue May 11 13:40:23 2010 -0700
391 *
392 * packed commit one
393 */
394
395 /*
396 * twowaymerge.git $ git log --graph --all
397 * * commit 9b219343610c88a1187c996d0dc58330b55cee28
398 * |\ Merge: c37a783 2224e19
399 * | | Author: Scott J. Goldman <scottjg@github.com>
400 * | | Date: Tue Nov 27 20:31:04 2012 -0800
401 * | |
402 * | | Merge branch 'first-branch' into second-branch
403 * | |
404 * | * commit 2224e191514cb4bd8c566d80dac22dfcb1e9bb83
405 * | | Author: Scott J. Goldman <scottjg@github.com>
406 * | | Date: Tue Nov 27 20:28:51 2012 -0800
407 * | |
408 * | | j
409 * | |
410 * | * commit a41a49f8f5cd9b6cb14a076bf8394881ed0b4d19
411 * | | Author: Scott J. Goldman <scottjg@github.com>
412 * | | Date: Tue Nov 27 20:28:39 2012 -0800
413 * | |
414 * | | i
415 * | |
416 * | * commit 82bf9a1a10a4b25c1f14c9607b60970705e92545
417 * | | Author: Scott J. Goldman <scottjg@github.com>
418 * | | Date: Tue Nov 27 20:28:28 2012 -0800
419 * | |
420 * | | h
421 * | |
422 * * | commit c37a783c20d92ac92362a78a32860f7eebf938ef
423 * | | Author: Scott J. Goldman <scottjg@github.com>
424 * | | Date: Tue Nov 27 20:30:57 2012 -0800
425 * | |
426 * | | n
427 * | |
428 * * | commit 8b82fb1794cb1c8c7f172ec730a4c2db0ae3e650
429 * | | Author: Scott J. Goldman <scottjg@github.com>
430 * | | Date: Tue Nov 27 20:30:43 2012 -0800
431 * | |
432 * | | m
433 * | |
434 * * | commit 6ab5d28acbf3c3bdff276f7ccfdf29c1520e542f
435 * | | Author: Scott J. Goldman <scottjg@github.com>
436 * | | Date: Tue Nov 27 20:30:38 2012 -0800
437 * | |
438 * | | l
439 * | |
440 * * | commit 7b8c336c45fc6895c1c60827260fe5d798e5d247
441 * | | Author: Scott J. Goldman <scottjg@github.com>
442 * | | Date: Tue Nov 27 20:30:24 2012 -0800
443 * | |
444 * | | k
445 * | |
446 * | | * commit 1c30b88f5f3ee66d78df6520a7de9e89b890818b
447 * | | | Author: Scott J. Goldman <scottjg@github.com>
448 * | | | Date: Tue Nov 27 20:28:10 2012 -0800
449 * | | |
450 * | | | e
451 * | | |
452 * | | * commit 42b7311aa626e712891940c1ec5d5cba201946a4
453 * | | | Author: Scott J. Goldman <scottjg@github.com>
454 * | | | Date: Tue Nov 27 20:28:06 2012 -0800
455 * | | |
456 * | | | d
457 * | | |
458 * | | * commit a953a018c5b10b20c86e69fef55ebc8ad4c5a417
459 * | | |\ Merge: bd1732c cdf97fd
460 * | | |/ Author: Scott J. Goldman <scottjg@github.com>
461 * | |/| Date: Tue Nov 27 20:26:43 2012 -0800
462 * | | |
463 * | | | Merge branch 'first-branch'
464 * | | |
465 * | * | commit cdf97fd3bb48eb3827638bb33d208f5fd32d0aa6
466 * | | | Author: Scott J. Goldman <scottjg@github.com>
467 * | | | Date: Tue Nov 27 20:24:46 2012 -0800
468 * | | |
469 * | | | g
470 * | | |
471 * | * | commit ef0488f0b722f0be8bcb90a7730ac7efafd1d694
472 * | | | Author: Scott J. Goldman <scottjg@github.com>
473 * | | | Date: Tue Nov 27 20:24:39 2012 -0800
474 * | | |
475 * | | | f
476 * | | |
477 * | | * commit bd1732c43c68d712ad09e1d872b9be6d4b9efdc4
478 * | |/ Author: Scott J. Goldman <scottjg@github.com>
479 * | | Date: Tue Nov 27 17:43:58 2012 -0800
480 * | |
481 * | | c
482 * | |
483 * | * commit 0c8a3f1f3d5f421cf83048c7c73ee3b55a5e0f29
484 * |/ Author: Scott J. Goldman <scottjg@github.com>
485 * | Date: Tue Nov 27 17:43:48 2012 -0800
486 * |
487 * | b
488 * |
489 * * commit 1f4c0311a24b63f6fc209a59a1e404942d4a5006
490 * Author: Scott J. Goldman <scottjg@github.com>
491 * Date: Tue Nov 27 17:43:41 2012 -0800
492 *
493 * a
494 */
495
496 void test_revwalk_mergebase__remove_redundant(void)
497 {
498 git_repository *repo;
499 git_oid one, two, base;
500 git_oidarray result = {NULL, 0};
501
502 cl_git_pass(git_repository_open(&repo, cl_fixture("redundant.git")));
503
504 cl_git_pass(git_oid_fromstr(&one, "d89137c93ba1ee749214ff4ce52ae9137bc833f9"));
505 cl_git_pass(git_oid_fromstr(&two, "91f4b95df4a59504a9813ba66912562931d990e3"));
506 cl_git_pass(git_oid_fromstr(&base, "6cb1f2352d974e1c5a776093017e8772416ac97a"));
507
508 cl_git_pass(git_merge_bases(&result, repo, &one, &two));
509 cl_assert_equal_i(1, result.count);
510 cl_assert_equal_oid(&base, &result.ids[0]);
511
512 git_oidarray_free(&result);
513 git_repository_free(repo);
514 }