reuc.c 10.5 KB
Newer Older
Edward Thomson committed
1 2
#include "clar_libgit2.h"
#include "index.h"
3
#include "git2/sys/index.h"
Edward Thomson committed
4
#include "git2/repository.h"
Edward Thomson committed
5
#include "../reset/reset_helpers.h"
Edward Thomson committed
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

static git_repository *repo;
static git_index *repo_index;

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

#define ONE_ANCESTOR_OID "478871385b9cd03908c5383acfd568bef023c6b3"
#define ONE_OUR_OID "4458b8bc9e72b6c8755ae456f60e9844d0538d8c"
#define ONE_THEIR_OID "8b72416545c7e761b64cecad4f1686eae4078aa8"

#define TWO_ANCESTOR_OID "9d81f82fccc7dcd7de7a1ffead1815294c2e092c"
#define TWO_OUR_OID "8f3c06cff9a83757cec40c80bc9bf31a2582bde9"
#define TWO_THEIR_OID "887b153b165d32409c70163e0f734c090f12f673"

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

void test_index_reuc__cleanup(void)
{
	git_index_free(repo_index);
31 32
	repo_index = NULL;

Edward Thomson committed
33 34 35
	cl_git_sandbox_cleanup();
}

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
void test_index_reuc__add(void)
{
	git_oid ancestor_oid, our_oid, their_oid;
	const git_index_reuc_entry *reuc;

	git_oid_fromstr(&ancestor_oid, ONE_ANCESTOR_OID);
	git_oid_fromstr(&our_oid, ONE_OUR_OID);
	git_oid_fromstr(&their_oid, ONE_THEIR_OID);

	cl_git_pass(git_index_reuc_add(repo_index, "newfile.txt",
		0100644, &ancestor_oid,
		0100644, &our_oid,
		0100644, &their_oid));

	cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "newfile.txt"));

52
	cl_assert_equal_s("newfile.txt", reuc->path);
53 54 55
	cl_assert(reuc->mode[0] == 0100644);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
56 57 58
	cl_assert_equal_oid(&reuc->oid[0], &ancestor_oid);
	cl_assert_equal_oid(&reuc->oid[1], &our_oid);
	cl_assert_equal_oid(&reuc->oid[2], &their_oid);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
}

void test_index_reuc__add_no_ancestor(void)
{
	git_oid ancestor_oid, our_oid, their_oid;
	const git_index_reuc_entry *reuc;

	memset(&ancestor_oid, 0x0, sizeof(git_oid));
	git_oid_fromstr(&our_oid, ONE_OUR_OID);
	git_oid_fromstr(&their_oid, ONE_THEIR_OID);

	cl_git_pass(git_index_reuc_add(repo_index, "newfile.txt",
		0, NULL,
		0100644, &our_oid,
		0100644, &their_oid));

	cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "newfile.txt"));

77
	cl_assert_equal_s("newfile.txt", reuc->path);
78 79 80
	cl_assert(reuc->mode[0] == 0);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
81 82 83
	cl_assert_equal_oid(&reuc->oid[0], &ancestor_oid);
	cl_assert_equal_oid(&reuc->oid[1], &our_oid);
	cl_assert_equal_oid(&reuc->oid[2], &their_oid);
84 85
}

Edward Thomson committed
86 87 88 89 90 91 92 93 94
void test_index_reuc__read_bypath(void)
{
	const git_index_reuc_entry *reuc;
	git_oid oid;

	cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));

	cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "two.txt"));

95
	cl_assert_equal_s("two.txt", reuc->path);
Edward Thomson committed
96 97 98 99
	cl_assert(reuc->mode[0] == 0100644);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
	git_oid_fromstr(&oid, TWO_ANCESTOR_OID);
100
	cl_assert_equal_oid(&reuc->oid[0], &oid);
Edward Thomson committed
101
	git_oid_fromstr(&oid, TWO_OUR_OID);
102
	cl_assert_equal_oid(&reuc->oid[1], &oid);
Edward Thomson committed
103
	git_oid_fromstr(&oid, TWO_THEIR_OID);
104
	cl_assert_equal_oid(&reuc->oid[2], &oid);
Edward Thomson committed
105 106 107

	cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "one.txt"));

108
	cl_assert_equal_s("one.txt", reuc->path);
Edward Thomson committed
109 110 111 112
	cl_assert(reuc->mode[0] == 0100644);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
	git_oid_fromstr(&oid, ONE_ANCESTOR_OID);
113
	cl_assert_equal_oid(&reuc->oid[0], &oid);
Edward Thomson committed
114
	git_oid_fromstr(&oid, ONE_OUR_OID);
115
	cl_assert_equal_oid(&reuc->oid[1], &oid);
Edward Thomson committed
116
	git_oid_fromstr(&oid, ONE_THEIR_OID);
117
	cl_assert_equal_oid(&reuc->oid[2], &oid);
Edward Thomson committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
}

void test_index_reuc__ignore_case(void)
{
	const git_index_reuc_entry *reuc;
	git_oid oid;
	int index_caps;

	index_caps = git_index_caps(repo_index);

	index_caps &= ~GIT_INDEXCAP_IGNORE_CASE;
	cl_git_pass(git_index_set_caps(repo_index, index_caps));

	cl_assert(!git_index_reuc_get_bypath(repo_index, "TWO.txt"));

	index_caps |= GIT_INDEXCAP_IGNORE_CASE;
	cl_git_pass(git_index_set_caps(repo_index, index_caps));

	cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));

	cl_assert(reuc = git_index_reuc_get_bypath(repo_index, "TWO.txt"));

140
	cl_assert_equal_s("two.txt", reuc->path);
Edward Thomson committed
141 142 143 144
	cl_assert(reuc->mode[0] == 0100644);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
	git_oid_fromstr(&oid, TWO_ANCESTOR_OID);
145
	cl_assert_equal_oid(&reuc->oid[0], &oid);
Edward Thomson committed
146
	git_oid_fromstr(&oid, TWO_OUR_OID);
147
	cl_assert_equal_oid(&reuc->oid[1], &oid);
Edward Thomson committed
148
	git_oid_fromstr(&oid, TWO_THEIR_OID);
149
	cl_assert_equal_oid(&reuc->oid[2], &oid);
Edward Thomson committed
150 151 152 153 154 155 156 157 158 159 160
}

void test_index_reuc__read_byindex(void)
{
	const git_index_reuc_entry *reuc;
	git_oid oid;

	cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));

	cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0));

161
	cl_assert_equal_s("one.txt", reuc->path);
Edward Thomson committed
162 163 164 165
	cl_assert(reuc->mode[0] == 0100644);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
	git_oid_fromstr(&oid, ONE_ANCESTOR_OID);
166
	cl_assert_equal_oid(&reuc->oid[0], &oid);
Edward Thomson committed
167
	git_oid_fromstr(&oid, ONE_OUR_OID);
168
	cl_assert_equal_oid(&reuc->oid[1], &oid);
Edward Thomson committed
169
	git_oid_fromstr(&oid, ONE_THEIR_OID);
170
	cl_assert_equal_oid(&reuc->oid[2], &oid);
Edward Thomson committed
171 172 173

	cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 1));

174
	cl_assert_equal_s("two.txt", reuc->path);
Edward Thomson committed
175 176 177 178
	cl_assert(reuc->mode[0] == 0100644);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
	git_oid_fromstr(&oid, TWO_ANCESTOR_OID);
179
	cl_assert_equal_oid(&reuc->oid[0], &oid);
Edward Thomson committed
180
	git_oid_fromstr(&oid, TWO_OUR_OID);
181
	cl_assert_equal_oid(&reuc->oid[1], &oid);
Edward Thomson committed
182
	git_oid_fromstr(&oid, TWO_THEIR_OID);
183
	cl_assert_equal_oid(&reuc->oid[2], &oid);
Edward Thomson committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

void test_index_reuc__updates_existing(void)
{
	const git_index_reuc_entry *reuc;
	git_oid ancestor_oid, our_oid, their_oid, oid;
	int index_caps;

	git_index_clear(repo_index);

	index_caps = git_index_caps(repo_index);

	index_caps |= GIT_INDEXCAP_IGNORE_CASE;
	cl_git_pass(git_index_set_caps(repo_index, index_caps));

	git_oid_fromstr(&ancestor_oid, TWO_ANCESTOR_OID);
	git_oid_fromstr(&our_oid, TWO_OUR_OID);
	git_oid_fromstr(&their_oid, TWO_THEIR_OID);

	cl_git_pass(git_index_reuc_add(repo_index, "two.txt",
		0100644, &ancestor_oid,
		0100644, &our_oid,
		0100644, &their_oid));

	cl_git_pass(git_index_reuc_add(repo_index, "TWO.txt",
		0100644, &our_oid,
		0100644, &their_oid,
		0100644, &ancestor_oid));

	cl_assert_equal_i(1, git_index_reuc_entrycount(repo_index));

	cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0));

217
	cl_assert_equal_s("TWO.txt", reuc->path);
Edward Thomson committed
218
	git_oid_fromstr(&oid, TWO_OUR_OID);
219
	cl_assert_equal_oid(&reuc->oid[0], &oid);
Edward Thomson committed
220
	git_oid_fromstr(&oid, TWO_THEIR_OID);
221
	cl_assert_equal_oid(&reuc->oid[1], &oid);
Edward Thomson committed
222
	git_oid_fromstr(&oid, TWO_ANCESTOR_OID);
223
	cl_assert_equal_oid(&reuc->oid[2], &oid);
Edward Thomson committed
224 225 226 227 228 229 230 231 232 233 234
}

void test_index_reuc__remove(void)
{
	git_oid oid;
	const git_index_reuc_entry *reuc;

	cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));

	cl_git_pass(git_index_reuc_remove(repo_index, 0));
	cl_git_fail(git_index_reuc_remove(repo_index, 1));
nulltoken committed
235

Edward Thomson committed
236 237 238 239
	cl_assert_equal_i(1, git_index_reuc_entrycount(repo_index));

	cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0));

240
	cl_assert_equal_s("two.txt", reuc->path);
Edward Thomson committed
241 242 243 244
	cl_assert(reuc->mode[0] == 0100644);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
	git_oid_fromstr(&oid, TWO_ANCESTOR_OID);
245
	cl_assert_equal_oid(&reuc->oid[0], &oid);
Edward Thomson committed
246
	git_oid_fromstr(&oid, TWO_OUR_OID);
247
	cl_assert_equal_oid(&reuc->oid[1], &oid);
Edward Thomson committed
248
	git_oid_fromstr(&oid, TWO_THEIR_OID);
249
	cl_assert_equal_oid(&reuc->oid[2], &oid);
Edward Thomson committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
}

void test_index_reuc__write(void)
{
	git_oid ancestor_oid, our_oid, their_oid;
	const git_index_reuc_entry *reuc;

	git_index_clear(repo_index);

	/* Write out of order to ensure sorting is correct */
	git_oid_fromstr(&ancestor_oid, TWO_ANCESTOR_OID);
	git_oid_fromstr(&our_oid, TWO_OUR_OID);
	git_oid_fromstr(&their_oid, TWO_THEIR_OID);

	cl_git_pass(git_index_reuc_add(repo_index, "two.txt",
		0100644, &ancestor_oid,
		0100644, &our_oid,
		0100644, &their_oid));

	git_oid_fromstr(&ancestor_oid, ONE_ANCESTOR_OID);
	git_oid_fromstr(&our_oid, ONE_OUR_OID);
	git_oid_fromstr(&their_oid, ONE_THEIR_OID);

	cl_git_pass(git_index_reuc_add(repo_index, "one.txt",
		0100644, &ancestor_oid,
		0100644, &our_oid,
		0100644, &their_oid));

	cl_git_pass(git_index_write(repo_index));
	cl_assert_equal_i(2, git_index_reuc_entrycount(repo_index));

	/* ensure sort order was round-tripped correct */
	cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 0));
283
	cl_assert_equal_s("one.txt", reuc->path);
nulltoken committed
284

Edward Thomson committed
285
	cl_assert(reuc = git_index_reuc_get_byindex(repo_index, 1));
286
	cl_assert_equal_s("two.txt", reuc->path);
Edward Thomson committed
287 288
}

Edward Thomson committed
289 290 291 292 293 294 295 296
static int reuc_entry_exists(void)
{
	return (git_index_reuc_get_bypath(repo_index, "newfile.txt") != NULL);
}

void test_index_reuc__cleaned_on_reset_hard(void)
{
	git_object *target;
nulltoken committed
297

298
	cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
nulltoken committed
299

Edward Thomson committed
300
	test_index_reuc__add();
301
	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
Edward Thomson committed
302
	cl_assert(reuc_entry_exists() == false);
nulltoken committed
303

Edward Thomson committed
304 305 306 307 308 309
	git_object_free(target);
}

void test_index_reuc__cleaned_on_reset_mixed(void)
{
	git_object *target;
nulltoken committed
310

311
	cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
Edward Thomson committed
312

nulltoken committed
313
	test_index_reuc__add();
314
	cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL));
Edward Thomson committed
315
	cl_assert(reuc_entry_exists() == false);
nulltoken committed
316

Edward Thomson committed
317 318 319 320 321 322
	git_object_free(target);
}

void test_index_reuc__retained_on_reset_soft(void)
{
	git_object *target;
nulltoken committed
323

324
	cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
nulltoken committed
325

326
	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
Edward Thomson committed
327 328

	test_index_reuc__add();
329
	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
Edward Thomson committed
330
	cl_assert(reuc_entry_exists() == true);
nulltoken committed
331

Edward Thomson committed
332 333 334 335 336 337 338
	git_object_free(target);
}

void test_index_reuc__cleaned_on_checkout_tree(void)
{
	git_oid oid;
	git_object *obj;
339
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
nulltoken committed
340

Edward Thomson committed
341 342 343 344 345 346 347
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;

	test_index_reuc__add();
	git_reference_name_to_id(&oid, repo, "refs/heads/master");
	git_object_lookup(&obj, repo, &oid, GIT_OBJ_ANY);
	git_checkout_tree(repo, obj, &opts);
	cl_assert(reuc_entry_exists() == false);
nulltoken committed
348

Edward Thomson committed
349 350 351 352 353
	git_object_free(obj);
}

void test_index_reuc__cleaned_on_checkout_head(void)
{
354
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
nulltoken committed
355

Edward Thomson committed
356
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
nulltoken committed
357

Edward Thomson committed
358 359 360 361 362 363 364
	test_index_reuc__add();
	git_checkout_head(repo, &opts);
	cl_assert(reuc_entry_exists() == false);
}

void test_index_reuc__retained_on_checkout_index(void)
{
365
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
nulltoken committed
366

Edward Thomson committed
367
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
nulltoken committed
368

Edward Thomson committed
369 370 371 372
	test_index_reuc__add();
	git_checkout_index(repo, repo_index, &opts);
	cl_assert(reuc_entry_exists() == true);
}