reuc.c 11.1 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

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"

21
/* Fixture setup and teardown */
Edward Thomson committed
22 23 24 25 26 27 28 29 30
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
void test_index_reuc__add(void)
{
	git_oid ancestor_oid, our_oid, their_oid;
	const git_index_reuc_entry *reuc;

41 42 43
	git_oid__fromstr(&ancestor_oid, ONE_ANCESTOR_OID, GIT_OID_SHA1);
	git_oid__fromstr(&our_oid, ONE_OUR_OID, GIT_OID_SHA1);
	git_oid__fromstr(&their_oid, ONE_THEIR_OID, GIT_OID_SHA1);
44 45 46 47 48 49 50 51

	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

	cl_git_pass(git_index_write(repo_index));
61 62 63 64 65 66 67 68
}

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));
69 70
	git_oid__fromstr(&our_oid, ONE_OUR_OID, GIT_OID_SHA1);
	git_oid__fromstr(&their_oid, ONE_THEIR_OID, GIT_OID_SHA1);
71 72 73 74 75 76 77 78

	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"));

79
	cl_assert_equal_s("newfile.txt", reuc->path);
80 81 82
	cl_assert(reuc->mode[0] == 0);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
83 84 85
	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);
86 87

	cl_git_pass(git_index_write(repo_index));
88 89
}

Edward Thomson committed
90 91 92 93 94 95 96 97 98
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"));

99
	cl_assert_equal_s("two.txt", reuc->path);
Edward Thomson committed
100 101 102
	cl_assert(reuc->mode[0] == 0100644);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
103
	git_oid__fromstr(&oid, TWO_ANCESTOR_OID, GIT_OID_SHA1);
104
	cl_assert_equal_oid(&reuc->oid[0], &oid);
105
	git_oid__fromstr(&oid, TWO_OUR_OID, GIT_OID_SHA1);
106
	cl_assert_equal_oid(&reuc->oid[1], &oid);
107
	git_oid__fromstr(&oid, TWO_THEIR_OID, GIT_OID_SHA1);
108
	cl_assert_equal_oid(&reuc->oid[2], &oid);
Edward Thomson committed
109 110 111

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

112
	cl_assert_equal_s("one.txt", reuc->path);
Edward Thomson committed
113 114 115
	cl_assert(reuc->mode[0] == 0100644);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
116
	git_oid__fromstr(&oid, ONE_ANCESTOR_OID, GIT_OID_SHA1);
117
	cl_assert_equal_oid(&reuc->oid[0], &oid);
118
	git_oid__fromstr(&oid, ONE_OUR_OID, GIT_OID_SHA1);
119
	cl_assert_equal_oid(&reuc->oid[1], &oid);
120
	git_oid__fromstr(&oid, ONE_THEIR_OID, GIT_OID_SHA1);
121
	cl_assert_equal_oid(&reuc->oid[2], &oid);
Edward Thomson committed
122 123 124 125 126 127 128 129 130 131
}

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);

132
	index_caps &= ~GIT_INDEX_CAPABILITY_IGNORE_CASE;
Edward Thomson committed
133 134 135 136
	cl_git_pass(git_index_set_caps(repo_index, index_caps));

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

137
	index_caps |= GIT_INDEX_CAPABILITY_IGNORE_CASE;
Edward Thomson committed
138 139 140 141 142 143
	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"));

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

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));

165
	cl_assert_equal_s("one.txt", reuc->path);
Edward Thomson committed
166 167 168
	cl_assert(reuc->mode[0] == 0100644);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
169
	git_oid__fromstr(&oid, ONE_ANCESTOR_OID, GIT_OID_SHA1);
170
	cl_assert_equal_oid(&reuc->oid[0], &oid);
171
	git_oid__fromstr(&oid, ONE_OUR_OID, GIT_OID_SHA1);
172
	cl_assert_equal_oid(&reuc->oid[1], &oid);
173
	git_oid__fromstr(&oid, ONE_THEIR_OID, GIT_OID_SHA1);
174
	cl_assert_equal_oid(&reuc->oid[2], &oid);
Edward Thomson committed
175 176 177

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

178
	cl_assert_equal_s("two.txt", reuc->path);
Edward Thomson committed
179 180 181
	cl_assert(reuc->mode[0] == 0100644);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
182
	git_oid__fromstr(&oid, TWO_ANCESTOR_OID, GIT_OID_SHA1);
183
	cl_assert_equal_oid(&reuc->oid[0], &oid);
184
	git_oid__fromstr(&oid, TWO_OUR_OID, GIT_OID_SHA1);
185
	cl_assert_equal_oid(&reuc->oid[1], &oid);
186
	git_oid__fromstr(&oid, TWO_THEIR_OID, GIT_OID_SHA1);
187
	cl_assert_equal_oid(&reuc->oid[2], &oid);
Edward Thomson committed
188 189 190 191 192 193 194 195 196 197 198 199
}

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);

200
	index_caps |= GIT_INDEX_CAPABILITY_IGNORE_CASE;
Edward Thomson committed
201 202
	cl_git_pass(git_index_set_caps(repo_index, index_caps));

203 204 205
	git_oid__fromstr(&ancestor_oid, TWO_ANCESTOR_OID, GIT_OID_SHA1);
	git_oid__fromstr(&our_oid, TWO_OUR_OID, GIT_OID_SHA1);
	git_oid__fromstr(&their_oid, TWO_THEIR_OID, GIT_OID_SHA1);
Edward Thomson committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

	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));

221
	cl_assert_equal_s("TWO.txt", reuc->path);
222
	git_oid__fromstr(&oid, TWO_OUR_OID, GIT_OID_SHA1);
223
	cl_assert_equal_oid(&reuc->oid[0], &oid);
224
	git_oid__fromstr(&oid, TWO_THEIR_OID, GIT_OID_SHA1);
225
	cl_assert_equal_oid(&reuc->oid[1], &oid);
226
	git_oid__fromstr(&oid, TWO_ANCESTOR_OID, GIT_OID_SHA1);
227
	cl_assert_equal_oid(&reuc->oid[2], &oid);
Edward Thomson committed
228 229 230 231 232 233 234 235 236 237 238
}

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
239

Edward Thomson committed
240 241 242 243
	cl_assert_equal_i(1, git_index_reuc_entrycount(repo_index));

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

244
	cl_assert_equal_s("two.txt", reuc->path);
Edward Thomson committed
245 246 247
	cl_assert(reuc->mode[0] == 0100644);
	cl_assert(reuc->mode[1] == 0100644);
	cl_assert(reuc->mode[2] == 0100644);
248
	git_oid__fromstr(&oid, TWO_ANCESTOR_OID, GIT_OID_SHA1);
249
	cl_assert_equal_oid(&reuc->oid[0], &oid);
250
	git_oid__fromstr(&oid, TWO_OUR_OID, GIT_OID_SHA1);
251
	cl_assert_equal_oid(&reuc->oid[1], &oid);
252
	git_oid__fromstr(&oid, TWO_THEIR_OID, GIT_OID_SHA1);
253
	cl_assert_equal_oid(&reuc->oid[2], &oid);
Edward Thomson committed
254 255 256 257 258 259 260 261 262 263
}

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 */
264 265 266
	git_oid__fromstr(&ancestor_oid, TWO_ANCESTOR_OID, GIT_OID_SHA1);
	git_oid__fromstr(&our_oid, TWO_OUR_OID, GIT_OID_SHA1);
	git_oid__fromstr(&their_oid, TWO_THEIR_OID, GIT_OID_SHA1);
Edward Thomson committed
267 268 269 270 271 272

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

273 274 275
	git_oid__fromstr(&ancestor_oid, ONE_ANCESTOR_OID, GIT_OID_SHA1);
	git_oid__fromstr(&our_oid, ONE_OUR_OID, GIT_OID_SHA1);
	git_oid__fromstr(&their_oid, ONE_THEIR_OID, GIT_OID_SHA1);
Edward Thomson committed
276 277 278 279 280 281 282 283 284 285 286

	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));
287
	cl_assert_equal_s("one.txt", reuc->path);
nulltoken committed
288

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

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

302
	cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
nulltoken committed
303

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

Edward Thomson committed
308 309 310 311 312 313
	git_object_free(target);
}

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

315
	cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
Edward Thomson committed
316

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

Edward Thomson committed
321 322 323 324 325 326
	git_object_free(target);
}

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

328
	cl_git_pass(git_revparse_single(&target, repo, "3a34580"));
nulltoken committed
329

330
	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
Edward Thomson committed
331 332

	test_index_reuc__add();
333
	cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
Edward Thomson committed
334
	cl_assert(reuc_entry_exists() == true);
nulltoken committed
335

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

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

345
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
Edward Thomson committed
346 347

	test_index_reuc__add();
348
	cl_git_pass(git_reference_name_to_id(&oid, repo, "refs/heads/master"));
349
	cl_git_pass(git_object_lookup(&obj, repo, &oid, GIT_OBJECT_ANY));
350
	cl_git_pass(git_checkout_tree(repo, obj, &opts));
Edward Thomson committed
351
	cl_assert(reuc_entry_exists() == false);
nulltoken committed
352

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

void test_index_reuc__cleaned_on_checkout_head(void)
{
358
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
nulltoken committed
359

360
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
nulltoken committed
361

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

void test_index_reuc__retained_on_checkout_index(void)
{
369
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
nulltoken committed
370

371
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
nulltoken committed
372

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