#include "clar_libgit2.h"
#include "vector.h"
#include <stdarg.h>
static git_repository *_repo;
static git_repository *_repo2;
void test_revwalk_mergebase__initialize(void)
{
cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
cl_git_pass(git_repository_open(&_repo2, cl_fixture("twowaymerge.git")));
}
void test_revwalk_mergebase__cleanup(void)
{
git_repository_free(_repo);
_repo = NULL;
git_repository_free(_repo2);
_repo2 = NULL;
}
void test_revwalk_mergebase__single1(void)
{
git_oid result, one, two, expected;
size_t ahead, behind;
cl_git_pass(git_oid_fromstr(&one, "c47800c7266a2be04c571c04d5a6614691ea99bd "));
cl_git_pass(git_oid_fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
cl_git_pass(git_oid_fromstr(&expected, "5b5b025afb0b4c913b4c338a42934a3863bf3644"));
cl_git_pass(git_merge_base(&result, _repo, &one, &two));
cl_assert_equal_oid(&expected, &result);
cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two));
cl_assert_equal_sz(ahead, 1);
cl_assert_equal_sz(behind, 2);
cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &two, &one));
cl_assert_equal_sz(ahead, 2);
cl_assert_equal_sz(behind, 1);
}
void test_revwalk_mergebase__single2(void)
{
git_oid result, one, two, expected;
size_t ahead, behind;
cl_git_pass(git_oid_fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af"));
cl_git_pass(git_oid_fromstr(&two, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
cl_git_pass(git_oid_fromstr(&expected, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
cl_git_pass(git_merge_base(&result, _repo, &one, &two));
cl_assert_equal_oid(&expected, &result);
cl_git_pass(git_graph_ahead_behind( &ahead, &behind, _repo, &one, &two));
cl_assert_equal_sz(ahead, 1);
cl_assert_equal_sz(behind, 4);
cl_git_pass(git_graph_ahead_behind( &ahead, &behind, _repo, &two, &one));
cl_assert_equal_sz(ahead, 4);
cl_assert_equal_sz(behind, 1);
}
void test_revwalk_mergebase__merged_branch(void)
{
git_oid result, one, two, expected;
size_t ahead, behind;
cl_git_pass(git_oid_fromstr(&one, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750"));
cl_git_pass(git_oid_fromstr(&two, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
cl_git_pass(git_oid_fromstr(&expected, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
cl_git_pass(git_merge_base(&result, _repo, &one, &two));
cl_assert_equal_oid(&expected, &result);
cl_git_pass(git_merge_base(&result, _repo, &two, &one));
cl_assert_equal_oid(&expected, &result);
cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two));
cl_assert_equal_sz(ahead, 3);
cl_assert_equal_sz(behind, 0);
cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &two, &one));
cl_assert_equal_sz(ahead, 0);
cl_assert_equal_sz(behind, 3);
}
void test_revwalk_mergebase__two_way_merge(void)
{
git_oid one, two;
size_t ahead, behind;
cl_git_pass(git_oid_fromstr(&one, "9b219343610c88a1187c996d0dc58330b55cee28"));
cl_git_pass(git_oid_fromstr(&two, "a953a018c5b10b20c86e69fef55ebc8ad4c5a417"));
cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo2, &one, &two));
cl_assert_equal_sz(ahead, 8);
cl_assert_equal_sz(behind, 2);
cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo2, &two, &one));
cl_assert_equal_sz(ahead, 2);
cl_assert_equal_sz(behind, 8);
}
void test_revwalk_mergebase__no_common_ancestor_returns_ENOTFOUND(void)
{
git_oid result, one, two;
size_t ahead, behind;
int error;
cl_git_pass(git_oid_fromstr(&one, "763d71aadf09a7951596c9746c024e7eece7c7af"));
cl_git_pass(git_oid_fromstr(&two, "e90810b8df3e80c413d903f631643c716887138d"));
error = git_merge_base(&result, _repo, &one, &two);
cl_git_fail(error);
cl_assert_equal_i(GIT_ENOTFOUND, error);
cl_git_pass(git_graph_ahead_behind(&ahead, &behind, _repo, &one, &two));
cl_assert_equal_sz(4, ahead);
cl_assert_equal_sz(2, behind);
}
void test_revwalk_mergebase__prefer_youngest_merge_base(void)
{
git_oid result, one, two, expected;
cl_git_pass(git_oid_fromstr(&one, "a4a7dce85cf63874e984719f4fdd239f5145052f"));
cl_git_pass(git_oid_fromstr(&two, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
cl_git_pass(git_oid_fromstr(&expected, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
cl_git_pass(git_merge_base(&result, _repo, &one, &two));
cl_assert_equal_oid(&expected, &result);
}
void test_revwalk_mergebase__multiple_merge_bases(void)
{
git_oid one, two, expected1, expected2;
git_oidarray result = {NULL, 0};
cl_git_pass(git_oid_fromstr(&one, "a4a7dce85cf63874e984719f4fdd239f5145052f"));
cl_git_pass(git_oid_fromstr(&two, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
cl_git_pass(git_oid_fromstr(&expected1, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
cl_git_pass(git_oid_fromstr(&expected2, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
cl_git_pass(git_merge_bases(&result, _repo, &one, &two));
cl_assert_equal_i(2, result.count);
cl_assert_equal_oid(&expected1, &result.ids[0]);
cl_assert_equal_oid(&expected2, &result.ids[1]);
git_oidarray_free(&result);
}
void test_revwalk_mergebase__multiple_merge_bases_many_commits(void)
{
git_oid expected1, expected2;
git_oidarray result = {NULL, 0};
git_oid *input = git__malloc(sizeof(git_oid) * 2);
cl_git_pass(git_oid_fromstr(&input[0], "a4a7dce85cf63874e984719f4fdd239f5145052f"));
cl_git_pass(git_oid_fromstr(&input[1], "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
cl_git_pass(git_oid_fromstr(&expected1, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
cl_git_pass(git_oid_fromstr(&expected2, "9fd738e8f7967c078dceed8190330fc8648ee56a"));
cl_git_pass(git_merge_bases_many(&result, _repo, 2, input));
cl_assert_equal_i(2, result.count);
cl_assert_equal_oid(&expected1, &result.ids[0]);
cl_assert_equal_oid(&expected2, &result.ids[1]);
git_oidarray_free(&result);
git__free(input);
}
void test_revwalk_mergebase__no_off_by_one_missing(void)
{
git_oid result, one, two;
cl_git_pass(git_oid_fromstr(&one, "1a443023183e3f2bfbef8ac923cd81c1018a18fd"));
cl_git_pass(git_oid_fromstr(&two, "9f13f7d0a9402c681f91dc590cf7b5470e6a77d2"));
cl_git_pass(git_merge_base(&result, _repo, &one, &two));
}
static void assert_mergebase_many(const char *expected_sha, int count, ...)
{
va_list ap;
int i;
git_oid *oids;
git_oid oid, expected;
char *partial_oid;
git_object *object;
oids = git__malloc(count * sizeof(git_oid));
cl_assert(oids != NULL);
memset(oids, 0x0, count * sizeof(git_oid));
va_start(ap, count);
for (i = 0; i < count; ++i) {
partial_oid = va_arg(ap, char *);
cl_git_pass(git_oid_fromstrn(&oid, partial_oid, strlen(partial_oid)));
cl_git_pass(git_object_lookup_prefix(&object, _repo, &oid, strlen(partial_oid), GIT_OBJ_COMMIT));
git_oid_cpy(&oids[i], git_object_id(object));
git_object_free(object);
}
va_end(ap);
if (expected_sha == NULL)
cl_assert_equal_i(GIT_ENOTFOUND, git_merge_base_many(&oid, _repo, count, oids));
else {
cl_git_pass(git_merge_base_many(&oid, _repo, count, oids));
cl_git_pass(git_oid_fromstr(&expected, expected_sha));
cl_assert_equal_oid(&expected, &oid);
}
git__free(oids);
}
void test_revwalk_mergebase__many_no_common_ancestor_returns_ENOTFOUND(void)
{
assert_mergebase_many(NULL, 3, "41bc8c", "e90810", "a65fed");
assert_mergebase_many(NULL, 3, "e90810", "41bc8c", "a65fed");
assert_mergebase_many(NULL, 3, "e90810", "a65fed", "41bc8c");
assert_mergebase_many(NULL, 3, "a65fed", "e90810", "41bc8c");
assert_mergebase_many(NULL, 3, "a65fed", "41bc8c", "e90810");
assert_mergebase_many(NULL, 3, "e90810", "763d71", "a65fed");
}
void test_revwalk_mergebase__many_merge_branch(void)
{
assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "763d71", "849607");
assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "763d71", "e90810", "a65fed");
assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "763d71", "a65fed", "e90810");
assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "763d71", "849607");
assert_mergebase_many("c47800c7266a2be04c571c04d5a6614691ea99bd", 3, "a65fed", "849607", "763d71");
assert_mergebase_many("8496071c1b46c854b31185ea97743be6a8774479", 3, "849607", "a65fed", "763d71");
assert_mergebase_many("5b5b025afb0b4c913b4c338a42934a3863bf3644", 5, "5b5b02", "763d71", "a4a7dc", "a65fed", "41bc8c");
}
static void assert_mergebase_octopus(const char *expected_sha, int count, ...)
{
va_list ap;
int i;
git_oid *oids;
git_oid oid, expected;
char *partial_oid;
git_object *object;
oids = git__malloc(count * sizeof(git_oid));
cl_assert(oids != NULL);
memset(oids, 0x0, count * sizeof(git_oid));
va_start(ap, count);
for (i = 0; i < count; ++i) {
partial_oid = va_arg(ap, char *);
cl_git_pass(git_oid_fromstrn(&oid, partial_oid, strlen(partial_oid)));
cl_git_pass(git_object_lookup_prefix(&object, _repo, &oid, strlen(partial_oid), GIT_OBJ_COMMIT));
git_oid_cpy(&oids[i], git_object_id(object));
git_object_free(object);
}
va_end(ap);
if (expected_sha == NULL)
cl_assert_equal_i(GIT_ENOTFOUND, git_merge_base_octopus(&oid, _repo, count, oids));
else {
cl_git_pass(git_merge_base_octopus(&oid, _repo, count, oids));
cl_git_pass(git_oid_fromstr(&expected, expected_sha));
cl_assert_equal_oid(&expected, &oid);
}
git__free(oids);
}
void test_revwalk_mergebase__octopus_no_common_ancestor_returns_ENOTFOUND(void)
{
assert_mergebase_octopus(NULL, 3, "41bc8c", "e90810", "a65fed");
assert_mergebase_octopus(NULL, 3, "e90810", "41bc8c", "a65fed");
assert_mergebase_octopus(NULL, 3, "e90810", "a65fed", "41bc8c");
assert_mergebase_octopus(NULL, 3, "a65fed", "e90810", "41bc8c");
assert_mergebase_octopus(NULL, 3, "a65fed", "41bc8c", "e90810");
assert_mergebase_octopus(NULL, 3, "e90810", "763d71", "a65fed");
assert_mergebase_octopus(NULL, 3, "763d71", "e90810", "a65fed");
assert_mergebase_octopus(NULL, 3, "763d71", "a65fed", "e90810");
assert_mergebase_octopus(NULL, 5, "5b5b02", "763d71", "a4a7dc", "a65fed", "41bc8c");
}
void test_revwalk_mergebase__octopus_merge_branch(void)
{
assert_mergebase_octopus("8496071c1b46c854b31185ea97743be6a8774479", 3, "a65fed", "763d71", "849607");
assert_mergebase_octopus("8496071c1b46c854b31185ea97743be6a8774479", 3, "a65fed", "763d71", "849607");
assert_mergebase_octopus("8496071c1b46c854b31185ea97743be6a8774479", 3, "a65fed", "849607", "763d71");
assert_mergebase_octopus("8496071c1b46c854b31185ea97743be6a8774479", 3, "849607", "a65fed", "763d71");
}