conflicts.c 9.42 KB
Newer Older
Edward Thomson committed
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
#include "clar_libgit2.h"
#include "index.h"
#include "git2/repository.h"

static git_repository *repo;
static git_index *repo_index;

#define TEST_REPO_PATH "mergedrepo"
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"

#define CONFLICTS_ONE_ANCESTOR_OID "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81"
#define CONFLICTS_ONE_OUR_OID "6aea5f295304c36144ad6e9247a291b7f8112399"
#define CONFLICTS_ONE_THEIR_OID "516bd85f78061e09ccc714561d7b504672cb52da"

#define CONFLICTS_TWO_ANCESTOR_OID "84af62840be1b1c47b778a8a249f3ff45155038c"
#define CONFLICTS_TWO_OUR_OID "8b3f43d2402825c200f835ca1762413e386fd0b2"
#define CONFLICTS_TWO_THEIR_OID "220bd62631c8cf7a83ef39c6b94595f00517211e"

#define TEST_ANCESTOR_OID "f00ff00ff00ff00ff00ff00ff00ff00ff00ff00f"
#define TEST_OUR_OID "b44bb44bb44bb44bb44bb44bb44bb44bb44bb44b"
#define TEST_THEIR_OID "0123456789abcdef0123456789abcdef01234567"

// Fixture setup and teardown
void test_index_conflicts__initialize(void)
{
	repo = cl_git_sandbox_init("mergedrepo");
	git_repository_index(&repo_index, repo);
}

void test_index_conflicts__cleanup(void)
{
	git_index_free(repo_index);
33 34
	repo_index = NULL;

Edward Thomson committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
	cl_git_sandbox_cleanup();
}

void test_index_conflicts__add(void)
{
	git_index_entry ancestor_entry, our_entry, their_entry;

	cl_assert(git_index_entrycount(repo_index) == 8);

	memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
	memset(&our_entry, 0x0, sizeof(git_index_entry));
	memset(&their_entry, 0x0, sizeof(git_index_entry));

	ancestor_entry.path = "test-one.txt";
	ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT);
50
	git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID);
Edward Thomson committed
51 52 53

	our_entry.path = "test-one.txt";
	ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT);
54
	git_oid_fromstr(&our_entry.id, TEST_OUR_OID);
Edward Thomson committed
55 56 57

	their_entry.path = "test-one.txt";
	ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT);
58
	git_oid_fromstr(&their_entry.id, TEST_THEIR_OID);
Edward Thomson committed
59 60 61 62 63 64 65 66 67

	cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry));

	cl_assert(git_index_entrycount(repo_index) == 11);
}

void test_index_conflicts__add_fixes_incorrect_stage(void)
{
	git_index_entry ancestor_entry, our_entry, their_entry;
68
	const git_index_entry *conflict_entry[3];
Edward Thomson committed
69 70 71 72 73 74 75 76 77

	cl_assert(git_index_entrycount(repo_index) == 8);

	memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
	memset(&our_entry, 0x0, sizeof(git_index_entry));
	memset(&their_entry, 0x0, sizeof(git_index_entry));

	ancestor_entry.path = "test-one.txt";
	ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT);
78
	git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID);
Edward Thomson committed
79 80 81

	our_entry.path = "test-one.txt";
	ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT);
82
	git_oid_fromstr(&our_entry.id, TEST_OUR_OID);
Edward Thomson committed
83 84 85

	their_entry.path = "test-one.txt";
	ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT);
86
	git_oid_fromstr(&their_entry.id, TEST_THEIR_OID);
Edward Thomson committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100

	cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, &our_entry, &their_entry));

	cl_assert(git_index_entrycount(repo_index) == 11);

	cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], repo_index, "test-one.txt"));

	cl_assert(git_index_entry_stage(conflict_entry[0]) == 1);
	cl_assert(git_index_entry_stage(conflict_entry[1]) == 2);
	cl_assert(git_index_entry_stage(conflict_entry[2]) == 3);
}

void test_index_conflicts__get(void)
{
101
	const git_index_entry *conflict_entry[3];
Edward Thomson committed
102 103 104 105 106
	git_oid oid;

	cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1],
		&conflict_entry[2], repo_index, "conflicts-one.txt"));

107
	cl_assert_equal_s("conflicts-one.txt", conflict_entry[0]->path);
Edward Thomson committed
108 109

	git_oid_fromstr(&oid, CONFLICTS_ONE_ANCESTOR_OID);
110
	cl_assert(git_oid_cmp(&conflict_entry[0]->id, &oid) == 0);
Edward Thomson committed
111 112

	git_oid_fromstr(&oid, CONFLICTS_ONE_OUR_OID);
113
	cl_assert(git_oid_cmp(&conflict_entry[1]->id, &oid) == 0);
Edward Thomson committed
114 115

	git_oid_fromstr(&oid, CONFLICTS_ONE_THEIR_OID);
116
	cl_assert(git_oid_cmp(&conflict_entry[2]->id, &oid) == 0);
Edward Thomson committed
117 118 119 120

	cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1],
		&conflict_entry[2], repo_index, "conflicts-two.txt"));

121
	cl_assert_equal_s("conflicts-two.txt", conflict_entry[0]->path);
Edward Thomson committed
122 123

	git_oid_fromstr(&oid, CONFLICTS_TWO_ANCESTOR_OID);
124
	cl_assert(git_oid_cmp(&conflict_entry[0]->id, &oid) == 0);
Edward Thomson committed
125 126

	git_oid_fromstr(&oid, CONFLICTS_TWO_OUR_OID);
127
	cl_assert(git_oid_cmp(&conflict_entry[1]->id, &oid) == 0);
Edward Thomson committed
128 129

	git_oid_fromstr(&oid, CONFLICTS_TWO_THEIR_OID);
130
	cl_assert(git_oid_cmp(&conflict_entry[2]->id, &oid) == 0);
Edward Thomson committed
131 132
}

133 134 135 136 137 138 139 140 141 142 143
void test_index_conflicts__iterate(void)
{
	git_index_conflict_iterator *iterator;
	const git_index_entry *conflict_entry[3];
	git_oid oid;

	cl_git_pass(git_index_conflict_iterator_new(&iterator, repo_index));

	cl_git_pass(git_index_conflict_next(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], iterator));

	git_oid_fromstr(&oid, CONFLICTS_ONE_ANCESTOR_OID);
144
	cl_assert(git_oid_cmp(&conflict_entry[0]->id, &oid) == 0);
145 146 147
	cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-one.txt") == 0);

	git_oid_fromstr(&oid, CONFLICTS_ONE_OUR_OID);
148
	cl_assert(git_oid_cmp(&conflict_entry[1]->id, &oid) == 0);
149 150 151
	cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-one.txt") == 0);

	git_oid_fromstr(&oid, CONFLICTS_ONE_THEIR_OID);
152
	cl_assert(git_oid_cmp(&conflict_entry[2]->id, &oid) == 0);
153 154 155 156 157
	cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-one.txt") == 0);

	cl_git_pass(git_index_conflict_next(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], iterator));

	git_oid_fromstr(&oid, CONFLICTS_TWO_ANCESTOR_OID);
158
	cl_assert(git_oid_cmp(&conflict_entry[0]->id, &oid) == 0);
159 160 161
	cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-two.txt") == 0);

	git_oid_fromstr(&oid, CONFLICTS_TWO_OUR_OID);
162
	cl_assert(git_oid_cmp(&conflict_entry[1]->id, &oid) == 0);
163 164 165
	cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-two.txt") == 0);

	git_oid_fromstr(&oid, CONFLICTS_TWO_THEIR_OID);
166
	cl_assert(git_oid_cmp(&conflict_entry[2]->id, &oid) == 0);
167 168 169 170 171 172 173 174 175 176 177
	cl_assert(git__strcmp(conflict_entry[0]->path, "conflicts-two.txt") == 0);

	cl_assert(git_index_conflict_next(&conflict_entry[0], &conflict_entry[1], &conflict_entry[2], iterator) == GIT_ITEROVER);

	cl_assert(conflict_entry[0] == NULL);
	cl_assert(conflict_entry[2] == NULL);
	cl_assert(conflict_entry[2] == NULL);

	git_index_conflict_iterator_free(iterator);
}

Edward Thomson committed
178 179
void test_index_conflicts__remove(void)
{
Ben Straub committed
180
	const git_index_entry *entry;
Edward Thomson committed
181
	size_t i;
182

Edward Thomson committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	cl_assert(git_index_entrycount(repo_index) == 8);

	cl_git_pass(git_index_conflict_remove(repo_index, "conflicts-one.txt"));
	cl_assert(git_index_entrycount(repo_index) == 5);

	for (i = 0; i < git_index_entrycount(repo_index); i++) {
		cl_assert(entry = git_index_get_byindex(repo_index, i));
		cl_assert(strcmp(entry->path, "conflicts-one.txt") != 0);
	}

	cl_git_pass(git_index_conflict_remove(repo_index, "conflicts-two.txt"));
	cl_assert(git_index_entrycount(repo_index) == 2);

	for (i = 0; i < git_index_entrycount(repo_index); i++) {
		cl_assert(entry = git_index_get_byindex(repo_index, i));
		cl_assert(strcmp(entry->path, "conflicts-two.txt") != 0);
	}
}

202
void test_index_conflicts__moved_to_reuc_on_add(void)
Edward Thomson committed
203
{
Ben Straub committed
204
	const git_index_entry *entry;
Edward Thomson committed
205 206 207 208 209 210
	size_t i;

	cl_assert(git_index_entrycount(repo_index) == 8);

	cl_git_mkfile("./mergedrepo/conflicts-one.txt", "new-file\n");

211
	cl_git_pass(git_index_add_bypath(repo_index, "conflicts-one.txt"));
Edward Thomson committed
212 213 214 215 216 217 218 219 220 221 222

	cl_assert(git_index_entrycount(repo_index) == 6);

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

		if (strcmp(entry->path, "conflicts-one.txt") == 0)
			cl_assert(git_index_entry_stage(entry) == 0);
	}
}

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
void test_index_conflicts__moved_to_reuc_on_remove(void)
{
	const git_index_entry *entry;
	size_t i;

	cl_assert(git_index_entrycount(repo_index) == 8);

	cl_git_pass(p_unlink("./mergedrepo/conflicts-one.txt"));

	cl_git_pass(git_index_remove_bypath(repo_index, "conflicts-one.txt"));

	cl_assert(git_index_entrycount(repo_index) == 5);

	for (i = 0; i < git_index_entrycount(repo_index); i++) {
		cl_assert(entry = git_index_get_byindex(repo_index, i));
		cl_assert(strcmp(entry->path, "conflicts-one.txt") != 0);
	}
}

Edward Thomson committed
242 243 244
void test_index_conflicts__remove_all_conflicts(void)
{
	size_t i;
Ben Straub committed
245
	const git_index_entry *entry;
Edward Thomson committed
246 247 248

	cl_assert(git_index_entrycount(repo_index) == 8);

249 250
	cl_assert_equal_i(true, git_index_has_conflicts(repo_index));

Edward Thomson committed
251 252
	git_index_conflict_cleanup(repo_index);

253 254
	cl_assert_equal_i(false, git_index_has_conflicts(repo_index));

Edward Thomson committed
255 256 257 258 259 260 261 262 263 264 265
	cl_assert(git_index_entrycount(repo_index) == 2);

	for (i = 0; i < git_index_entrycount(repo_index); i++) {
		cl_assert(entry = git_index_get_byindex(repo_index, i));
		cl_assert(git_index_entry_stage(entry) == 0);
	}
}

void test_index_conflicts__partial(void)
{
	git_index_entry ancestor_entry, our_entry, their_entry;
266
	const git_index_entry *conflict_entry[3];
Edward Thomson committed
267 268 269 270 271 272 273 274 275

	cl_assert(git_index_entrycount(repo_index) == 8);

	memset(&ancestor_entry, 0x0, sizeof(git_index_entry));
	memset(&our_entry, 0x0, sizeof(git_index_entry));
	memset(&their_entry, 0x0, sizeof(git_index_entry));

	ancestor_entry.path = "test-one.txt";
	ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT);
276
	git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID);
Edward Thomson committed
277 278 279 280 281 282 283

	cl_git_pass(git_index_conflict_add(repo_index, &ancestor_entry, NULL, NULL));
	cl_assert(git_index_entrycount(repo_index) == 9);

	cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1],
		&conflict_entry[2], repo_index, "test-one.txt"));

284
	cl_assert(git_oid_cmp(&ancestor_entry.id, &conflict_entry[0]->id) == 0);
Edward Thomson committed
285 286 287
	cl_assert(conflict_entry[1] == NULL);
	cl_assert(conflict_entry[2] == NULL);
}