trivial.c 9.4 KB
Newer Older
Edward Thomson committed
1 2 3 4 5 6 7
#include "clar_libgit2.h"
#include "git2/repository.h"
#include "git2/merge.h"
#include "merge.h"
#include "../merge_helpers.h"
#include "refs.h"
#include "fileops.h"
8
#include "git2/sys/index.h"
Edward Thomson committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

static git_repository *repo;

#define TEST_REPO_PATH "merge-resolve"
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"


// Fixture setup and teardown
void test_merge_trees_trivial__initialize(void)
{
	repo = cl_git_sandbox_init(TEST_REPO_PATH);
}

void test_merge_trees_trivial__cleanup(void)
{
	cl_git_sandbox_cleanup();
}


28
static int merge_trivial(git_index **index, const char *ours, const char *theirs)
Edward Thomson committed
29 30 31 32 33
{
	git_commit *our_commit, *their_commit, *ancestor_commit;
	git_tree *our_tree, *their_tree, *ancestor_tree;
	git_oid our_oid, their_oid, ancestor_oid;
	git_buf branch_buf = GIT_BUF_INIT;
34
	git_merge_options opts = GIT_MERGE_OPTIONS_INIT;
Edward Thomson committed
35 36 37 38 39 40 41 42 43 44 45 46

	git_buf_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, ours);
	cl_git_pass(git_reference_name_to_id(&our_oid, repo, branch_buf.ptr));
	cl_git_pass(git_commit_lookup(&our_commit, repo, &our_oid));

	git_buf_clear(&branch_buf);
	git_buf_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, theirs);
	cl_git_pass(git_reference_name_to_id(&their_oid, repo, branch_buf.ptr));
	cl_git_pass(git_commit_lookup(&their_commit, repo, &their_oid));

	cl_git_pass(git_merge_base(&ancestor_oid, repo, git_commit_id(our_commit), git_commit_id(their_commit)));
	cl_git_pass(git_commit_lookup(&ancestor_commit, repo, &ancestor_oid));
nulltoken committed
47

Edward Thomson committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	cl_git_pass(git_commit_tree(&ancestor_tree, ancestor_commit));
	cl_git_pass(git_commit_tree(&our_tree, our_commit));
	cl_git_pass(git_commit_tree(&their_tree, their_commit));

	cl_git_pass(git_merge_trees(index, repo, ancestor_tree, our_tree, their_tree, &opts));

	git_buf_free(&branch_buf);
	git_tree_free(our_tree);
	git_tree_free(their_tree);
	git_tree_free(ancestor_tree);
	git_commit_free(our_commit);
	git_commit_free(their_commit);
	git_commit_free(ancestor_commit);

	return 0;
}

static int merge_trivial_conflict_entrycount(git_index *index)
{
	const git_index_entry *entry;
Russell Belfer committed
68
	int count = 0;
Edward Thomson committed
69 70 71 72 73
	size_t i;

	for (i = 0; i < git_index_entrycount(index); i++) {
		cl_assert(entry = git_index_get_byindex(index, i));

74
		if (git_index_entry_is_conflict(entry))
Edward Thomson committed
75 76 77 78 79 80 81 82 83 84 85 86
			count++;
	}

	return count;
}

/* 2ALT: ancest:(empty)+, head:*empty*, remote:remote = result:remote */
void test_merge_trees_trivial__2alt(void)
{
	git_index *result;
	const git_index_entry *entry;

87
	cl_git_pass(merge_trivial(&result, "trivial-2alt", "trivial-2alt-branch"));
Edward Thomson committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101

	cl_assert(entry = git_index_get_bypath(result, "new-in-branch.txt", 0));
	cl_assert(git_index_reuc_entrycount(result) == 0);
	cl_assert(merge_trivial_conflict_entrycount(result) == 0);

	git_index_free(result);
}

/* 3ALT: ancest:(empty)+, head:head, remote:*empty* = result:head */
void test_merge_trees_trivial__3alt(void)
{
	git_index *result;
	const git_index_entry *entry;

102
	cl_git_pass(merge_trivial(&result, "trivial-3alt", "trivial-3alt-branch"));
Edward Thomson committed
103 104 105 106

	cl_assert(entry = git_index_get_bypath(result, "new-in-3alt.txt", 0));
	cl_assert(git_index_reuc_entrycount(result) == 0);
	cl_assert(merge_trivial_conflict_entrycount(result) == 0);
nulltoken committed
107

Edward Thomson committed
108 109 110 111 112 113 114 115 116
	git_index_free(result);
}

/* 4: ancest:(empty)^, head:head, remote:remote = result:no merge */
void test_merge_trees_trivial__4(void)
{
	git_index *result;
	const git_index_entry *entry;

117
	cl_git_pass(merge_trivial(&result, "trivial-4", "trivial-4-branch"));
Edward Thomson committed
118 119 120 121 122 123 124

	cl_assert((entry = git_index_get_bypath(result, "new-and-different.txt", 0)) == NULL);
	cl_assert(git_index_reuc_entrycount(result) == 0);

	cl_assert(merge_trivial_conflict_entrycount(result) == 2);
	cl_assert(entry = git_index_get_bypath(result, "new-and-different.txt", 2));
	cl_assert(entry = git_index_get_bypath(result, "new-and-different.txt", 3));
nulltoken committed
125

Edward Thomson committed
126 127 128 129 130 131 132 133 134
	git_index_free(result);
}

/* 5ALT: ancest:*, head:head, remote:head = result:head */
void test_merge_trees_trivial__5alt_1(void)
{
	git_index *result;
	const git_index_entry *entry;

135
	cl_git_pass(merge_trivial(&result, "trivial-5alt-1", "trivial-5alt-1-branch"));
Edward Thomson committed
136 137 138 139

	cl_assert(entry = git_index_get_bypath(result, "new-and-same.txt", 0));
	cl_assert(git_index_reuc_entrycount(result) == 0);
	cl_assert(merge_trivial_conflict_entrycount(result) == 0);
nulltoken committed
140

Edward Thomson committed
141 142 143 144 145 146 147 148 149
	git_index_free(result);
}

/* 5ALT: ancest:*, head:head, remote:head = result:head */
void test_merge_trees_trivial__5alt_2(void)
{
	git_index *result;
	const git_index_entry *entry;

150
	cl_git_pass(merge_trivial(&result, "trivial-5alt-2", "trivial-5alt-2-branch"));
Edward Thomson committed
151 152 153 154

	cl_assert(entry = git_index_get_bypath(result, "modified-to-same.txt", 0));
	cl_assert(git_index_reuc_entrycount(result) == 0);
	cl_assert(merge_trivial_conflict_entrycount(result) == 0);
nulltoken committed
155

Edward Thomson committed
156 157 158 159 160 161 162 163 164 165
	git_index_free(result);
}

/* 6: ancest:ancest+, head:(empty), remote:(empty) = result:no merge */
void test_merge_trees_trivial__6(void)
{
	git_index *result;
	const git_index_entry *entry;
	const git_index_reuc_entry *reuc;

166
	cl_git_pass(merge_trivial(&result, "trivial-6", "trivial-6-branch"));
Edward Thomson committed
167 168 169 170 171 172

	cl_assert((entry = git_index_get_bypath(result, "removed-in-both.txt", 0)) == NULL);
	cl_assert(git_index_reuc_entrycount(result) == 1);
	cl_assert(reuc = git_index_reuc_get_bypath(result, "removed-in-both.txt"));

	cl_assert(merge_trivial_conflict_entrycount(result) == 0);
nulltoken committed
173

Edward Thomson committed
174 175 176 177 178 179 180 181 182 183
	git_index_free(result);
}

/* 8: ancest:ancest^, head:(empty), remote:ancest = result:no merge */
void test_merge_trees_trivial__8(void)
{
	git_index *result;
	const git_index_entry *entry;
	const git_index_reuc_entry *reuc;

184
	cl_git_pass(merge_trivial(&result, "trivial-8", "trivial-8-branch"));
Edward Thomson committed
185 186 187 188 189 190 191

	cl_assert((entry = git_index_get_bypath(result, "removed-in-8.txt", 0)) == NULL);

	cl_assert(git_index_reuc_entrycount(result) == 1);
	cl_assert(reuc = git_index_reuc_get_bypath(result, "removed-in-8.txt"));

	cl_assert(merge_trivial_conflict_entrycount(result) == 0);
nulltoken committed
192

Edward Thomson committed
193 194 195 196 197 198 199 200 201
	git_index_free(result);
}

/* 7: ancest:ancest+, head:(empty), remote:remote = result:no merge */
void test_merge_trees_trivial__7(void)
{
	git_index *result;
	const git_index_entry *entry;

202
	cl_git_pass(merge_trivial(&result, "trivial-7", "trivial-7-branch"));
Edward Thomson committed
203 204 205 206 207 208 209

	cl_assert((entry = git_index_get_bypath(result, "removed-in-7.txt", 0)) == NULL);
	cl_assert(git_index_reuc_entrycount(result) == 0);

	cl_assert(merge_trivial_conflict_entrycount(result) == 2);
	cl_assert(entry = git_index_get_bypath(result, "removed-in-7.txt", 1));
	cl_assert(entry = git_index_get_bypath(result, "removed-in-7.txt", 3));
nulltoken committed
210

Edward Thomson committed
211 212 213 214 215 216 217 218 219 220
	git_index_free(result);
}

/* 10: ancest:ancest^, head:ancest, remote:(empty) = result:no merge */
void test_merge_trees_trivial__10(void)
{
	git_index *result;
	const git_index_entry *entry;
	const git_index_reuc_entry *reuc;

221
	cl_git_pass(merge_trivial(&result, "trivial-10", "trivial-10-branch"));
Edward Thomson committed
222 223 224 225 226 227 228

	cl_assert((entry = git_index_get_bypath(result, "removed-in-10-branch.txt", 0)) == NULL);

	cl_assert(git_index_reuc_entrycount(result) == 1);
	cl_assert(reuc = git_index_reuc_get_bypath(result, "removed-in-10-branch.txt"));

	cl_assert(merge_trivial_conflict_entrycount(result) == 0);
nulltoken committed
229

Edward Thomson committed
230 231 232 233 234 235 236 237 238
	git_index_free(result);
}

/* 9: ancest:ancest+, head:head, remote:(empty) = result:no merge */
void test_merge_trees_trivial__9(void)
{
	git_index *result;
	const git_index_entry *entry;

239
	cl_git_pass(merge_trivial(&result, "trivial-9", "trivial-9-branch"));
Edward Thomson committed
240 241 242 243 244 245 246

	cl_assert((entry = git_index_get_bypath(result, "removed-in-9-branch.txt", 0)) == NULL);
	cl_assert(git_index_reuc_entrycount(result) == 0);

	cl_assert(merge_trivial_conflict_entrycount(result) == 2);
	cl_assert(entry = git_index_get_bypath(result, "removed-in-9-branch.txt", 1));
	cl_assert(entry = git_index_get_bypath(result, "removed-in-9-branch.txt", 2));
nulltoken committed
247

Edward Thomson committed
248 249 250 251 252 253 254 255 256 257
	git_index_free(result);
}

/* 13: ancest:ancest+, head:head, remote:ancest = result:head */
void test_merge_trees_trivial__13(void)
{
	git_index *result;
	const git_index_entry *entry;
	git_oid expected_oid;

258
	cl_git_pass(merge_trivial(&result, "trivial-13", "trivial-13-branch"));
Edward Thomson committed
259 260 261

	cl_assert(entry = git_index_get_bypath(result, "modified-in-13.txt", 0));
	cl_git_pass(git_oid_fromstr(&expected_oid, "1cff9ec6a47a537380dedfdd17c9e76d74259a2b"));
262
	cl_assert_equal_oid(&expected_oid, &entry->id);
Edward Thomson committed
263 264 265

	cl_assert(git_index_reuc_entrycount(result) == 0);
	cl_assert(merge_trivial_conflict_entrycount(result) == 0);
nulltoken committed
266

Edward Thomson committed
267 268 269 270 271 272 273 274 275 276
	git_index_free(result);
}

/* 14: ancest:ancest+, head:ancest, remote:remote = result:remote */
void test_merge_trees_trivial__14(void)
{
	git_index *result;
	const git_index_entry *entry;
	git_oid expected_oid;

277
	cl_git_pass(merge_trivial(&result, "trivial-14", "trivial-14-branch"));
Edward Thomson committed
278 279 280

	cl_assert(entry = git_index_get_bypath(result, "modified-in-14-branch.txt", 0));
	cl_git_pass(git_oid_fromstr(&expected_oid, "26153a3ff3649b6c2bb652d3f06878c6e0a172f9"));
281
	cl_assert(git_oid_cmp(&entry->id, &expected_oid) == 0);
Edward Thomson committed
282 283 284

	cl_assert(git_index_reuc_entrycount(result) == 0);
	cl_assert(merge_trivial_conflict_entrycount(result) == 0);
nulltoken committed
285

Edward Thomson committed
286 287 288 289 290 291 292 293 294
	git_index_free(result);
}

/* 11: ancest:ancest+, head:head, remote:remote = result:no merge */
void test_merge_trees_trivial__11(void)
{
	git_index *result;
	const git_index_entry *entry;

295
	cl_git_pass(merge_trivial(&result, "trivial-11", "trivial-11-branch"));
Edward Thomson committed
296 297 298 299 300 301 302 303

	cl_assert((entry = git_index_get_bypath(result, "modified-in-both.txt", 0)) == NULL);
	cl_assert(git_index_reuc_entrycount(result) == 0);

	cl_assert(merge_trivial_conflict_entrycount(result) == 3);
	cl_assert(entry = git_index_get_bypath(result, "modified-in-both.txt", 1));
	cl_assert(entry = git_index_get_bypath(result, "modified-in-both.txt", 2));
	cl_assert(entry = git_index_get_bypath(result, "modified-in-both.txt", 3));
nulltoken committed
304

Edward Thomson committed
305 306
	git_index_free(result);
}