reuc.c 10.8 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	cl_assert(reuc->mode[0] == 0100644);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
	cl_assert(git_oid_cmp(&reuc->oid[0], &ancestor_oid) == 0);
	cl_assert(git_oid_cmp(&reuc->oid[1], &our_oid) == 0);
	cl_assert(git_oid_cmp(&reuc->oid[2], &their_oid) == 0);
}

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 81 82 83 84 85
	cl_assert(reuc->mode[0] == 0);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
	cl_assert(git_oid_cmp(&reuc->oid[0], &ancestor_oid) == 0);
	cl_assert(git_oid_cmp(&reuc->oid[1], &our_oid) == 0);
	cl_assert(git_oid_cmp(&reuc->oid[2], &their_oid) == 0);
}

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 100 101 102 103 104 105 106 107
	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);
	cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0);
	git_oid_fromstr(&oid, TWO_OUR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0);
	git_oid_fromstr(&oid, TWO_THEIR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0);

	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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	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);
	cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0);
	git_oid_fromstr(&oid, ONE_OUR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0);
	git_oid_fromstr(&oid, ONE_THEIR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0);
}

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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	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);
	cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0);
	git_oid_fromstr(&oid, TWO_OUR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0);
	git_oid_fromstr(&oid, TWO_THEIR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0);
}

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 166 167 168 169 170 171 172 173
	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);
	cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0);
	git_oid_fromstr(&oid, ONE_OUR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0);
	git_oid_fromstr(&oid, ONE_THEIR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0);

	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 179 180 181 182 183 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
	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);
	cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0);
	git_oid_fromstr(&oid, TWO_OUR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0);
	git_oid_fromstr(&oid, TWO_THEIR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0);
}

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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
	git_oid_fromstr(&oid, TWO_OUR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0);
	git_oid_fromstr(&oid, TWO_THEIR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0);
	git_oid_fromstr(&oid, TWO_ANCESTOR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0);
}

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 245 246 247 248 249 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 283 284
	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);
	cl_assert(git_oid_cmp(&reuc->oid[0], &oid) == 0);
	git_oid_fromstr(&oid, TWO_OUR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[1], &oid) == 0);
	git_oid_fromstr(&oid, TWO_THEIR_OID);
	cl_assert(git_oid_cmp(&reuc->oid[2], &oid) == 0);
}

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_git_pass(git_index_read(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));
285
	cl_assert_equal_s("one.txt", reuc->path);
nulltoken committed
286

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

Edward Thomson committed
291 292 293 294 295 296 297 298
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
299

Edward Thomson committed
300
	retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876");
nulltoken committed
301

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

Edward Thomson committed
306 307 308 309 310 311
	git_object_free(target);
}

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

Edward Thomson committed
313 314
	retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876");

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

Edward Thomson committed
319 320 321 322 323 324
	git_object_free(target);
}

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

Edward Thomson committed
326
	retrieve_target_from_oid(&target, repo, "3a34580a35add43a4cf361e8e9a30060a905c876");
nulltoken committed
327

Edward Thomson committed
328 329 330 331 332
	git_reset(repo, target, GIT_RESET_HARD);

	test_index_reuc__add();
	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
	cl_assert(reuc_entry_exists() == true);
nulltoken committed
333

Edward Thomson committed
334 335 336 337 338 339 340 341
	git_object_free(target);
}

void test_index_reuc__cleaned_on_checkout_tree(void)
{
	git_oid oid;
	git_object *obj;
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
nulltoken committed
342

Edward Thomson committed
343 344 345 346 347 348 349
	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
350

Edward Thomson committed
351 352 353 354 355 356
	git_object_free(obj);
}

void test_index_reuc__cleaned_on_checkout_head(void)
{
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
nulltoken committed
357

Edward Thomson committed
358
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
nulltoken committed
359

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

void test_index_reuc__retained_on_checkout_index(void)
{
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
nulltoken committed
368

Edward Thomson committed
369
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;
nulltoken committed
370

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