]>
git.proxmox.com Git - libgit2.git/blob - tests/revwalk/mergebase.c
bee0b926e0851bbb1d4f90061a654f4da258e231
1 #include "clar_libgit2.h"
5 static git_repository
*_repo
;
6 static git_repository
*_repo2
;
8 void test_revwalk_mergebase__initialize(void)
10 cl_git_pass(git_repository_open(&_repo
, cl_fixture("testrepo.git")));
11 cl_git_pass(git_repository_open(&_repo2
, cl_fixture("twowaymerge.git")));
14 void test_revwalk_mergebase__cleanup(void)
16 git_repository_free(_repo
);
19 git_repository_free(_repo2
);
23 void test_revwalk_mergebase__single1(void)
25 git_oid result
, one
, two
, expected
;
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"));
32 cl_git_pass(git_merge_base(&result
, _repo
, &one
, &two
));
33 cl_assert_equal_oid(&expected
, &result
);
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);
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);
44 void test_revwalk_mergebase__single2(void)
46 git_oid result
, one
, two
, expected
;
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"));
53 cl_git_pass(git_merge_base(&result
, _repo
, &one
, &two
));
54 cl_assert_equal_oid(&expected
, &result
);
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);
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);
65 void test_revwalk_mergebase__merged_branch(void)
67 git_oid result
, one
, two
, expected
;
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"));
74 cl_git_pass(git_merge_base(&result
, _repo
, &one
, &two
));
75 cl_assert_equal_oid(&expected
, &result
);
77 cl_git_pass(git_merge_base(&result
, _repo
, &two
, &one
));
78 cl_assert_equal_oid(&expected
, &result
);
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);
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);
89 void test_revwalk_mergebase__two_way_merge(void)
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
));
98 cl_assert_equal_sz(ahead
, 8);
99 cl_assert_equal_sz(behind
, 2);
101 cl_git_pass(git_graph_ahead_behind(&ahead
, &behind
, _repo2
, &two
, &one
));
103 cl_assert_equal_sz(ahead
, 2);
104 cl_assert_equal_sz(behind
, 8);
107 void test_revwalk_mergebase__no_common_ancestor_returns_ENOTFOUND(void)
109 git_oid result
, one
, two
;
110 size_t ahead
, behind
;
113 cl_git_pass(git_oid_fromstr(&one
, "763d71aadf09a7951596c9746c024e7eece7c7af"));
114 cl_git_pass(git_oid_fromstr(&two
, "e90810b8df3e80c413d903f631643c716887138d"));
116 error
= git_merge_base(&result
, _repo
, &one
, &two
);
119 cl_assert_equal_i(GIT_ENOTFOUND
, error
);
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
);
126 void test_revwalk_mergebase__prefer_youngest_merge_base(void)
128 git_oid result
, one
, two
, expected
;
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"));
134 cl_git_pass(git_merge_base(&result
, _repo
, &one
, &two
));
135 cl_assert_equal_oid(&expected
, &result
);
138 void test_revwalk_mergebase__multiple_merge_bases(void)
140 git_oid one
, two
, expected1
, expected2
;
141 git_oidarray result
= {NULL
, 0};
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"));
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]);
153 git_oidarray_free(&result
);
156 void test_revwalk_mergebase__multiple_merge_bases_many_commits(void)
158 git_oid expected1
, expected2
;
159 git_oidarray result
= {NULL
, 0};
161 git_oid
*input
= git__malloc(sizeof(git_oid
) * 2);
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"));
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]);
173 git_oidarray_free(&result
);
177 void test_revwalk_mergebase__no_off_by_one_missing(void)
179 git_oid result
, one
, two
;
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
));
186 static void assert_mergebase_many(const char *expected_sha
, int count
, ...)
191 git_oid oid
, expected
;
195 oids
= git__malloc(count
* sizeof(git_oid
));
196 cl_assert(oids
!= NULL
);
198 memset(oids
, 0x0, count
* sizeof(git_oid
));
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
)));
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
);
213 if (expected_sha
== NULL
)
214 cl_assert_equal_i(GIT_ENOTFOUND
, git_merge_base_many(&oid
, _repo
, count
, oids
));
216 cl_git_pass(git_merge_base_many(&oid
, _repo
, count
, oids
));
217 cl_git_pass(git_oid_fromstr(&expected
, expected_sha
));
219 cl_assert_equal_oid(&expected
, &oid
);
225 void test_revwalk_mergebase__many_no_common_ancestor_returns_ENOTFOUND(void)
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");
233 assert_mergebase_many(NULL
, 3, "e90810", "763d71", "a65fed");
236 void test_revwalk_mergebase__many_merge_branch(void)
238 assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "763d71", "849607");
240 assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "763d71", "e90810", "a65fed");
241 assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "763d71", "a65fed", "e90810");
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");
247 assert_mergebase_many("5b5b025afb0b4c913b4c338a42934a3863bf3644", 5, "5b5b02", "763d71", "a4a7dc", "a65fed", "41bc8c");
250 static void assert_mergebase_octopus(const char *expected_sha
, int count
, ...)
255 git_oid oid
, expected
;
259 oids
= git__malloc(count
* sizeof(git_oid
));
260 cl_assert(oids
!= NULL
);
262 memset(oids
, 0x0, count
* sizeof(git_oid
));
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
)));
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
);
277 if (expected_sha
== NULL
)
278 cl_assert_equal_i(GIT_ENOTFOUND
, git_merge_base_octopus(&oid
, _repo
, count
, oids
));
280 cl_git_pass(git_merge_base_octopus(&oid
, _repo
, count
, oids
));
281 cl_git_pass(git_oid_fromstr(&expected
, expected_sha
));
283 cl_assert_equal_oid(&expected
, &oid
);
289 void test_revwalk_mergebase__octopus_no_common_ancestor_returns_ENOTFOUND(void)
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");
297 assert_mergebase_octopus(NULL
, 3, "e90810", "763d71", "a65fed");
299 assert_mergebase_octopus(NULL
, 3, "763d71", "e90810", "a65fed");
300 assert_mergebase_octopus(NULL
, 3, "763d71", "a65fed", "e90810");
302 assert_mergebase_octopus(NULL
, 5, "5b5b02", "763d71", "a4a7dc", "a65fed", "41bc8c");
305 void test_revwalk_mergebase__octopus_merge_branch(void)
307 assert_mergebase_octopus("8496071c1b46c854b31185ea97743be6a8774479", 3, "a65fed", "763d71", "849607");
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");
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
320 * | Add some files into subdirectories
322 * | * commit a65fedf39aefe402d3bb6e24df4d4f5fe4547750
323 * | | Author: Scott Chacon <schacon@gmail.com>
324 * | | Date: Tue Aug 9 19:33:46 2011 -0700
326 * | * commit be3563ae3f795b2b4353bcce3a527ad0a4f7f644
327 * | |\ Merge: 9fd738e c47800c
328 * | |/ Author: Scott Chacon <schacon@gmail.com>
329 * |/| Date: Tue May 25 11:58:27 2010 -0700
331 * | | Merge branch 'br2'
333 * | | * commit e90810b8df3e80c413d903f631643c716887138d
334 * | | | Author: Vicent Marti <tanoku@gmail.com>
335 * | | | Date: Thu Aug 5 18:42:20 2010 +0200
337 * | | | Test commit 2
339 * | | * commit 6dcf9bf7541ee10456529833502442f385010c3d
340 * | | Author: Vicent Marti <tanoku@gmail.com>
341 * | | Date: Thu Aug 5 18:41:33 2010 +0200
345 * | | * commit a4a7dce85cf63874e984719f4fdd239f5145052f
346 * | | |\ Merge: c47800c 9fd738e
347 * | |/ / Author: Scott Chacon <schacon@gmail.com>
348 * |/| / Date: Tue May 25 12:00:23 2010 -0700
350 * | | Merge branch 'master' into br2
352 * | * commit 9fd738e8f7967c078dceed8190330fc8648ee56a
353 * | | Author: Scott Chacon <schacon@gmail.com>
354 * | | Date: Mon May 24 10:19:19 2010 -0700
356 * | | a fourth commit
358 * | * commit 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
359 * | | Author: Scott Chacon <schacon@gmail.com>
360 * | | Date: Mon May 24 10:19:04 2010 -0700
364 * * | commit c47800c7266a2be04c571c04d5a6614691ea99bd
365 * |/ Author: Scott Chacon <schacon@gmail.com>
366 * | Date: Tue May 25 11:58:14 2010 -0700
368 * | branch commit one
370 * * commit 5b5b025afb0b4c913b4c338a42934a3863bf3644
371 * | Author: Scott Chacon <schacon@gmail.com>
372 * | Date: Tue May 11 13:38:42 2010 -0700
376 * * commit 8496071c1b46c854b31185ea97743be6a8774479
377 * Author: Scott Chacon <schacon@gmail.com>
378 * Date: Sat May 8 16:13:06 2010 -0700
382 * * commit 41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9
383 * | Author: Scott Chacon <schacon@gmail.com>
384 * | Date: Tue May 11 13:40:41 2010 -0700
386 * | packed commit two
388 * * commit 5001298e0c09ad9c34e4249bc5801c75e9754fa5
389 * Author: Scott Chacon <schacon@gmail.com>
390 * Date: Tue May 11 13:40:23 2010 -0700
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
402 * | | Merge branch 'first-branch' into second-branch
404 * | * commit 2224e191514cb4bd8c566d80dac22dfcb1e9bb83
405 * | | Author: Scott J. Goldman <scottjg@github.com>
406 * | | Date: Tue Nov 27 20:28:51 2012 -0800
410 * | * commit a41a49f8f5cd9b6cb14a076bf8394881ed0b4d19
411 * | | Author: Scott J. Goldman <scottjg@github.com>
412 * | | Date: Tue Nov 27 20:28:39 2012 -0800
416 * | * commit 82bf9a1a10a4b25c1f14c9607b60970705e92545
417 * | | Author: Scott J. Goldman <scottjg@github.com>
418 * | | Date: Tue Nov 27 20:28:28 2012 -0800
422 * * | commit c37a783c20d92ac92362a78a32860f7eebf938ef
423 * | | Author: Scott J. Goldman <scottjg@github.com>
424 * | | Date: Tue Nov 27 20:30:57 2012 -0800
428 * * | commit 8b82fb1794cb1c8c7f172ec730a4c2db0ae3e650
429 * | | Author: Scott J. Goldman <scottjg@github.com>
430 * | | Date: Tue Nov 27 20:30:43 2012 -0800
434 * * | commit 6ab5d28acbf3c3bdff276f7ccfdf29c1520e542f
435 * | | Author: Scott J. Goldman <scottjg@github.com>
436 * | | Date: Tue Nov 27 20:30:38 2012 -0800
440 * * | commit 7b8c336c45fc6895c1c60827260fe5d798e5d247
441 * | | Author: Scott J. Goldman <scottjg@github.com>
442 * | | Date: Tue Nov 27 20:30:24 2012 -0800
446 * | | * commit 1c30b88f5f3ee66d78df6520a7de9e89b890818b
447 * | | | Author: Scott J. Goldman <scottjg@github.com>
448 * | | | Date: Tue Nov 27 20:28:10 2012 -0800
452 * | | * commit 42b7311aa626e712891940c1ec5d5cba201946a4
453 * | | | Author: Scott J. Goldman <scottjg@github.com>
454 * | | | Date: Tue Nov 27 20:28:06 2012 -0800
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
463 * | | | Merge branch 'first-branch'
465 * | * | commit cdf97fd3bb48eb3827638bb33d208f5fd32d0aa6
466 * | | | Author: Scott J. Goldman <scottjg@github.com>
467 * | | | Date: Tue Nov 27 20:24:46 2012 -0800
471 * | * | commit ef0488f0b722f0be8bcb90a7730ac7efafd1d694
472 * | | | Author: Scott J. Goldman <scottjg@github.com>
473 * | | | Date: Tue Nov 27 20:24:39 2012 -0800
477 * | | * commit bd1732c43c68d712ad09e1d872b9be6d4b9efdc4
478 * | |/ Author: Scott J. Goldman <scottjg@github.com>
479 * | | Date: Tue Nov 27 17:43:58 2012 -0800
483 * | * commit 0c8a3f1f3d5f421cf83048c7c73ee3b55a5e0f29
484 * |/ Author: Scott J. Goldman <scottjg@github.com>
485 * | Date: Tue Nov 27 17:43:48 2012 -0800
489 * * commit 1f4c0311a24b63f6fc209a59a1e404942d4a5006
490 * Author: Scott J. Goldman <scottjg@github.com>
491 * Date: Tue Nov 27 17:43:41 2012 -0800
496 void test_revwalk_mergebase__remove_redundant(void)
498 git_repository
*repo
;
499 git_oid one
, two
, base
;
500 git_oidarray result
= {NULL
, 0};
502 cl_git_pass(git_repository_open(&repo
, cl_fixture("redundant.git")));
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"));
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]);
512 git_oidarray_free(&result
);
513 git_repository_free(repo
);