index.c 26.3 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "checkout_helpers.h"
3 4

#include "git2/checkout.h"
5
#include "futils.h"
6
#include "repository.h"
7
#include "remote.h"
8
#include "repo/repo_helpers.h"
9 10

static git_repository *g_repo;
11
static git_buf g_global_path = GIT_BUF_INIT;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

void test_checkout_index__initialize(void)
{
	git_tree *tree;

	g_repo = cl_git_sandbox_init("testrepo");

	cl_git_pass(git_repository_head_tree(&tree, g_repo));

	reset_index_to_treeish((git_object *)tree);
	git_tree_free(tree);

	cl_git_rewritefile(
		"./testrepo/.gitattributes",
		"* text eol=lf\n");
27 28 29

	git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL,
		&g_global_path);
30 31 32 33
}

void test_checkout_index__cleanup(void)
{
34 35 36 37
	git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL,
		g_global_path.ptr);
	git_buf_dispose(&g_global_path);

38
	cl_git_sandbox_cleanup();
39

40 41 42 43 44
	/* try to remove directories created by tests */
	cl_fixture_cleanup("alternative");
	cl_fixture_cleanup("symlink");
	cl_fixture_cleanup("symlink.git");
	cl_fixture_cleanup("tmp_global_path");
45 46 47 48
}

void test_checkout_index__cannot_checkout_a_bare_repository(void)
{
49
	cl_git_sandbox_cleanup();
50 51
	g_repo = cl_git_sandbox_init("testrepo.git");

52
	cl_git_fail(git_checkout_index(g_repo, NULL, NULL));
53 54
}

55
void test_checkout_index__can_create_missing_files(void)
56
{
57
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
58

59 60 61 62
	cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
	cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
	cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));

63
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
64

65
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
66

67 68 69
	check_file_contents("./testrepo/README", "hey there\n");
	check_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
	check_file_contents("./testrepo/new.txt", "my new file\n");
70 71
}

72 73
void test_checkout_index__can_remove_untracked_files(void)
{
74
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
75

76
	git_futils_mkdir("./testrepo/dir/subdir/subsubdir", 0755, GIT_MKDIR_PATH);
77 78 79 80 81
	cl_git_mkfile("./testrepo/dir/one", "one\n");
	cl_git_mkfile("./testrepo/dir/subdir/two", "two\n");

	cl_assert_equal_i(true, git_path_isdir("./testrepo/dir/subdir/subsubdir"));

82
	opts.checkout_strategy =
83 84 85
		GIT_CHECKOUT_SAFE |
		GIT_CHECKOUT_RECREATE_MISSING |
		GIT_CHECKOUT_REMOVE_UNTRACKED;
86

87
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
88 89 90 91

	cl_assert_equal_i(false, git_path_isdir("./testrepo/dir"));
}

92 93
void test_checkout_index__can_disable_pathspec_match(void)
{
94
	char *files_to_checkout[] = { "test10.txt", "test11.txt"};
95 96 97
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_object *objects;
	git_index *index;
98 99

	/* reset to beginning of history (i.e. just a README file) */
100
	opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
101

102 103 104 105
	cl_git_pass(git_revparse_single(&objects, g_repo, "8496071c1b46c854b31185ea97743be6a8774479"));
	cl_git_pass(git_checkout_tree(g_repo, objects, &opts));
	cl_git_pass(git_repository_set_head_detached(g_repo, git_object_id(objects)));
	git_object_free(objects);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

	cl_git_pass(git_repository_index(&index, g_repo));

	/* We create 4 files and commit them */
	cl_git_mkfile("testrepo/test9.txt", "original\n");
	cl_git_mkfile("testrepo/test10.txt", "original\n");
	cl_git_mkfile("testrepo/test11.txt", "original\n");
	cl_git_mkfile("testrepo/test12.txt", "original\n");

	cl_git_pass(git_index_add_bypath(index, "test9.txt"));
	cl_git_pass(git_index_add_bypath(index, "test10.txt"));
	cl_git_pass(git_index_add_bypath(index, "test11.txt"));
	cl_git_pass(git_index_add_bypath(index, "test12.txt"));
	cl_git_pass(git_index_write(index));

121
	cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "commit our test files");
122 123 124 125 126 127 128 129

	/* We modify the content of all 4 of our files */
	cl_git_rewritefile("testrepo/test9.txt", "modified\n");
	cl_git_rewritefile("testrepo/test10.txt", "modified\n");
	cl_git_rewritefile("testrepo/test11.txt", "modified\n");
	cl_git_rewritefile("testrepo/test12.txt", "modified\n");

	/* We checkout only test10.txt and test11.txt */
130
	opts.checkout_strategy =
131
		GIT_CHECKOUT_FORCE |
132
		GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH;
133 134 135
	opts.paths.strings = files_to_checkout;
	opts.paths.count = ARRAY_SIZE(files_to_checkout);
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
136 137 138 139 140 141 142

	/* The only files that have been reverted to their original content
	   should be test10.txt and test11.txt */
	check_file_contents("testrepo/test9.txt", "modified\n");
	check_file_contents("testrepo/test10.txt", "original\n");
	check_file_contents("testrepo/test11.txt", "original\n");
	check_file_contents("testrepo/test12.txt", "modified\n");
143 144

	git_index_free(index);
145 146
}

147 148
void test_checkout_index__honor_the_specified_pathspecs(void)
{
149
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
150 151
	char *entries[] = { "*.txt" };

152 153
	opts.paths.strings = entries;
	opts.paths.count = 1;
154 155 156 157 158

	cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
	cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
	cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));

159
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
160

161
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
162 163

	cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
164 165
	check_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
	check_file_contents("./testrepo/new.txt", "my new file\n");
166 167 168 169
}

void test_checkout_index__honor_the_gitattributes_directives(void)
{
170
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
171 172 173 174 175
	const char *attributes =
		"branch_file.txt text eol=crlf\n"
		"new.txt text eol=lf\n";

	cl_git_mkfile("./testrepo/.gitattributes", attributes);
176
	cl_repo_set_bool(g_repo, "core.autocrlf", false);
177

178
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
179

180
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
181

182 183 184
	check_file_contents("./testrepo/README", "hey there\n");
	check_file_contents("./testrepo/new.txt", "my new file\n");
	check_file_contents("./testrepo/branch_file.txt", "hi\r\nbye!\r\n");
185 186 187 188 189
}

void test_checkout_index__honor_coreautocrlf_setting_set_to_true(void)
{
#ifdef GIT_WIN32
190
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
191 192 193
	const char *expected_readme_text = "hey there\r\n";

	cl_git_pass(p_unlink("./testrepo/.gitattributes"));
194
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
195

196
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
197

198
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
199

200
	check_file_contents("./testrepo/README", expected_readme_text);
201 202 203
#endif
}

204
static void populate_symlink_workdir(void)
205
{
206
	git_buf path = GIT_BUF_INIT;
207 208 209 210 211 212
	git_repository *repo;
	git_remote *origin;
	git_object *target;

	const char *url = git_repository_path(g_repo);

213 214
	cl_git_pass(git_buf_joinpath(&path, clar_sandbox_path(), "symlink.git"));
	cl_git_pass(git_repository_init(&repo, path.ptr, true));
215 216
	cl_git_pass(git_repository_set_workdir(repo, "symlink", 1));

217 218 219
	/* Delete the `origin` repo (if it exists) so we can recreate it. */
	git_remote_delete(repo, GIT_REMOTE_ORIGIN);

220
	cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url));
221
	cl_git_pass(git_remote_fetch(origin, NULL, NULL, NULL));
222 223 224 225
	git_remote_free(origin);

	cl_git_pass(git_revparse_single(&target, repo, "remotes/origin/master"));
	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
226

227 228
	git_object_free(target);
	git_repository_free(repo);
229
	git_buf_dispose(&path);
230
}
231

232 233 234 235
void test_checkout_index__honor_coresymlinks_default_true(void)
{
	char link_data[GIT_PATH_MAX];
	int link_size = GIT_PATH_MAX;
236

237
	cl_must_pass(p_mkdir("symlink", 0777));
238

239
	if (!git_path_supports_symlinks("symlink/test"))
240
		cl_skip();
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
#ifdef GIT_WIN32
	/*
	 * Windows explicitly requires the global configuration to have
	 * core.symlinks=true in addition to actual filesystem support.
	 */
	create_tmp_global_config("tmp_global_path", "core.symlinks", "true");
#endif

	populate_symlink_workdir();

	link_size = p_readlink("./symlink/link_to_new.txt", link_data, link_size);
	cl_assert(link_size >= 0);

	link_data[link_size] = '\0';
	cl_assert_equal_i(link_size, strlen("new.txt"));
	cl_assert_equal_s(link_data, "new.txt");
	check_file_contents("./symlink/link_to_new.txt", "my new file\n");
}

void test_checkout_index__honor_coresymlinks_default_false(void)
{
	cl_must_pass(p_mkdir("symlink", 0777));

#ifndef GIT_WIN32
	/*
	 * This test is largely for Windows platforms to ensure that
	 * we respect an unset core.symlinks even when the platform
	 * supports symlinks.  Bail entirely on POSIX platforms that
	 * do support symlinks.
	 */
272
	if (git_path_supports_symlinks("symlink/test"))
273 274 275 276 277
		cl_skip();
#endif

	populate_symlink_workdir();
	check_file_contents("./symlink/link_to_new.txt", "new.txt");
278 279
}

280 281 282 283
void test_checkout_index__coresymlinks_set_to_true_fails_when_unsupported(void)
{
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;

284
	if (git_path_supports_symlinks("testrepo/test")) {
285 286 287 288 289 290 291 292 293
		cl_skip();
	}

	cl_repo_set_bool(g_repo, "core.symlinks", true);

	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
	cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
}

294 295
void test_checkout_index__honor_coresymlinks_setting_set_to_true(void)
{
296
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
297 298 299
	char link_data[GIT_PATH_MAX];
	size_t link_size = GIT_PATH_MAX;

300
	if (!git_path_supports_symlinks("testrepo/test")) {
301 302
		cl_skip();
	}
303

304
	cl_repo_set_bool(g_repo, "core.symlinks", true);
305

306
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
307

308
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
309

310 311 312 313 314
	link_size = p_readlink("./testrepo/link_to_new.txt", link_data, link_size);
	link_data[link_size] = '\0';
	cl_assert_equal_i(link_size, strlen("new.txt"));
	cl_assert_equal_s(link_data, "new.txt");
	check_file_contents("./testrepo/link_to_new.txt", "my new file\n");
315 316 317 318
}

void test_checkout_index__honor_coresymlinks_setting_set_to_false(void)
{
319
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
320

321
	cl_repo_set_bool(g_repo, "core.symlinks", false);
322

323
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
324

325
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
326

327
	check_file_contents("./testrepo/link_to_new.txt", "new.txt");
328 329
}

330
void test_checkout_index__donot_overwrite_modified_file_by_default(void)
331
{
332
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
333

334 335
	cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");

336 337 338
	/* set this up to not return an error code on conflicts, but it
	 * still will not have permission to overwrite anything...
	 */
339
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_ALLOW_CONFLICTS;
340

341
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
342

343
	check_file_contents("./testrepo/new.txt", "This isn't what's stored!");
344 345
}

346
void test_checkout_index__can_overwrite_modified_file(void)
347
{
348
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
349

350 351
	cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");

352
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
353

354
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
355

356
	check_file_contents("./testrepo/new.txt", "my new file\n");
357 358 359 360
}

void test_checkout_index__options_disable_filters(void)
{
361
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
362

363 364
	cl_git_mkfile("./testrepo/.gitattributes", "*.txt text eol=crlf\n");

365
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
366
	opts.disable_filters = false;
367

368
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
369

370
	check_file_contents("./testrepo/new.txt", "my new file\r\n");
371 372 373

	p_unlink("./testrepo/new.txt");

374 375
	opts.disable_filters = true;
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
376

377
	check_file_contents("./testrepo/new.txt", "my new file\n");
378 379 380 381
}

void test_checkout_index__options_dir_modes(void)
{
382
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
383 384 385
	struct stat st;
	git_oid oid;
	git_commit *commit;
386
	mode_t um;
387

388 389 390
	if (!cl_is_chmod_supported())
		return;

391
	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
392 393 394 395
	cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));

	reset_index_to_treeish((git_object *)commit);

396
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
397
	opts.dir_mode = 0701;
398

399
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
400

401 402 403
	/* umask will influence actual directory creation mode */
	(void)p_umask(um = p_umask(022));

404
	cl_git_pass(p_stat("./testrepo/a", &st));
405 406
	/* Haiku & Hurd use other mode bits, so we must mask them out */
	cl_assert_equal_i_fmt(st.st_mode & (S_IFMT | 07777), (GIT_FILEMODE_TREE | 0701) & ~um, "%07o");
407 408 409

	/* File-mode test, since we're on the 'dir' branch */
	cl_git_pass(p_stat("./testrepo/a/b.txt", &st));
410
	cl_assert_equal_i_fmt(st.st_mode & (S_IFMT | 07777), GIT_FILEMODE_BLOB_EXECUTABLE & ~um, "%07o");
411 412 413 414 415 416

	git_commit_free(commit);
}

void test_checkout_index__options_override_file_modes(void)
{
417
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
418 419
	struct stat st;

420 421 422
	if (!cl_is_chmod_supported())
		return;

423
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
424
	opts.file_mode = 0700;
425

426
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
427 428

	cl_git_pass(p_stat("./testrepo/new.txt", &st));
Russell Belfer committed
429
	cl_assert_equal_i_fmt(st.st_mode & GIT_MODE_PERMS_MASK, 0700, "%07o");
430 431 432 433
}

void test_checkout_index__options_open_flags(void)
{
434
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
435

436 437
	cl_git_mkfile("./testrepo/new.txt", "hi\n");

438 439
	opts.checkout_strategy =
		GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_REMOVE_EXISTING;
440
	opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND;
441

442
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
443

444
	check_file_contents("./testrepo/new.txt", "hi\nmy new file\n");
445
}
446

447
struct notify_data {
448 449 450 451
	const char *file;
	const char *sha;
};

452 453 454 455 456 457
static int test_checkout_notify_cb(
	git_checkout_notify_t why,
	const char *path,
	const git_diff_file *baseline,
	const git_diff_file *target,
	const git_diff_file *workdir,
458 459
	void *payload)
{
460
	struct notify_data *expectations = (struct notify_data *)payload;
461

462
	GIT_UNUSED(workdir);
463

464 465
	cl_assert_equal_i(GIT_CHECKOUT_NOTIFY_CONFLICT, why);
	cl_assert_equal_s(expectations->file, path);
466 467
	cl_assert_equal_i(0, git_oid_streq(&baseline->id, expectations->sha));
	cl_assert_equal_i(0, git_oid_streq(&target->id, expectations->sha));
468 469 470 471 472 473

	return 0;
}

void test_checkout_index__can_notify_of_skipped_files(void)
{
474
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
475
	struct notify_data data;
476 477 478 479 480 481 482 483 484 485 486 487

	cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");

	/*
	 * $ git ls-tree HEAD
	 * 100644 blob a8233120f6ad708f843d861ce2b7228ec4e3dec6    README
	 * 100644 blob 3697d64be941a53d4ae8f6a271e4e3fa56b022cc    branch_file.txt
	 * 100644 blob a71586c1dfe8a71c6cbf6c129f404c5642ff31bd    new.txt
	 */
	data.file = "new.txt";
	data.sha = "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd";

488 489 490
	opts.checkout_strategy = GIT_CHECKOUT_SAFE |
		GIT_CHECKOUT_RECREATE_MISSING |
		GIT_CHECKOUT_ALLOW_CONFLICTS;
491 492 493
	opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
	opts.notify_cb = test_checkout_notify_cb;
	opts.notify_payload = &data;
494

495
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
496 497
}

498
static int dont_notify_cb(
499 500 501 502 503
	git_checkout_notify_t why,
	const char *path,
	const git_diff_file *baseline,
	const git_diff_file *target,
	const git_diff_file *workdir,
504 505
	void *payload)
{
506 507 508 509 510
	GIT_UNUSED(why);
	GIT_UNUSED(path);
	GIT_UNUSED(baseline);
	GIT_UNUSED(target);
	GIT_UNUSED(workdir);
511 512 513 514 515 516 517 518 519
	GIT_UNUSED(payload);

	cl_assert(false);

	return 0;
}

void test_checkout_index__wont_notify_of_expected_line_ending_changes(void)
{
520
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
521

522
	cl_git_pass(p_unlink("./testrepo/.gitattributes"));
523
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
524

525 526
	cl_git_mkfile("./testrepo/new.txt", "my new file\r\n");

527
	opts.checkout_strategy =
528 529 530
		GIT_CHECKOUT_SAFE |
		GIT_CHECKOUT_RECREATE_MISSING |
		GIT_CHECKOUT_ALLOW_CONFLICTS;
531 532 533
	opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
	opts.notify_cb = dont_notify_cb;
	opts.notify_payload = NULL;
534

535
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
536 537
}

538 539
static void checkout_progress_counter(
	const char *path, size_t cur, size_t tot, void *payload)
540
{
Ben Straub committed
541
	GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
542
	(*(int *)payload)++;
543 544 545 546
}

void test_checkout_index__calls_progress_callback(void)
{
547
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
548 549
	int calls = 0;

550
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
551 552
	opts.progress_cb = checkout_progress_counter;
	opts.progress_payload = &calls;
553

554
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
555
	cl_assert(calls > 0);
556
}
557 558 559

void test_checkout_index__can_overcome_name_clashes(void)
{
560
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
561 562 563 564 565 566 567 568 569
	git_index *index;

	cl_git_pass(git_repository_index(&index, g_repo));
	git_index_clear(index);

	cl_git_mkfile("./testrepo/path0", "content\r\n");
	cl_git_pass(p_mkdir("./testrepo/path1", 0777));
	cl_git_mkfile("./testrepo/path1/file1", "content\r\n");

570 571
	cl_git_pass(git_index_add_bypath(index, "path0"));
	cl_git_pass(git_index_add_bypath(index, "path1/file1"));
572

573 574 575 576 577 578 579 580
	cl_git_pass(p_unlink("./testrepo/path0"));
	cl_git_pass(git_futils_rmdir_r(
		"./testrepo/path1", NULL, GIT_RMDIR_REMOVE_FILES));

	cl_git_mkfile("./testrepo/path1", "content\r\n");
	cl_git_pass(p_mkdir("./testrepo/path0", 0777));
	cl_git_mkfile("./testrepo/path0/file0", "content\r\n");

581 582 583
	cl_assert(git_path_isfile("./testrepo/path1"));
	cl_assert(git_path_isfile("./testrepo/path0/file0"));

584
	opts.checkout_strategy =
585
		GIT_CHECKOUT_SAFE |
586 587
		GIT_CHECKOUT_RECREATE_MISSING |
		GIT_CHECKOUT_ALLOW_CONFLICTS;
588
	cl_git_pass(git_checkout_index(g_repo, index, &opts));
589 590 591 592

	cl_assert(git_path_isfile("./testrepo/path1"));
	cl_assert(git_path_isfile("./testrepo/path0/file0"));

593 594
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
	cl_git_pass(git_checkout_index(g_repo, index, &opts));
595 596 597 598

	cl_assert(git_path_isfile("./testrepo/path0"));
	cl_assert(git_path_isfile("./testrepo/path1/file1"));

599 600
	git_index_free(index);
}
601 602 603

void test_checkout_index__validates_struct_version(void)
{
604
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
605 606
	const git_error *err;

607 608
	opts.version = 1024;
	cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
609

610 611
	err = git_error_last();
	cl_assert_equal_i(err->klass, GIT_ERROR_INVALID);
612

613
	opts.version = 0;
614
	git_error_clear();
615
	cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
616

617 618
	err = git_error_last();
	cl_assert_equal_i(err->klass, GIT_ERROR_INVALID);
619
}
620 621 622

void test_checkout_index__can_update_prefixed_files(void)
{
623
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
624

625 626 627 628
	cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
	cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
	cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));

629 630 631 632 633 634 635
	cl_git_mkfile("./testrepo/READ", "content\n");
	cl_git_mkfile("./testrepo/README.after", "content\n");
	cl_git_pass(p_mkdir("./testrepo/branch_file", 0777));
	cl_git_pass(p_mkdir("./testrepo/branch_file/contained_dir", 0777));
	cl_git_mkfile("./testrepo/branch_file/contained_file", "content\n");
	cl_git_pass(p_mkdir("./testrepo/branch_file.txt.after", 0777));

636
	opts.checkout_strategy =
637 638 639
		GIT_CHECKOUT_SAFE |
		GIT_CHECKOUT_RECREATE_MISSING |
		GIT_CHECKOUT_REMOVE_UNTRACKED;
640 641 642

	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));

643 644 645
	/* remove untracked will remove the .gitattributes file before the blobs
	 * were created, so they will have had crlf filtering applied on Windows
	 */
646 647 648
	check_file_contents_nocr("./testrepo/README", "hey there\n");
	check_file_contents_nocr("./testrepo/branch_file.txt", "hi\nbye!\n");
	check_file_contents_nocr("./testrepo/new.txt", "my new file\n");
649 650 651 652 653 654

	cl_assert(!git_path_exists("testrepo/READ"));
	cl_assert(!git_path_exists("testrepo/README.after"));
	cl_assert(!git_path_exists("testrepo/branch_file"));
	cl_assert(!git_path_exists("testrepo/branch_file.txt.after"));
}
655 656 657

void test_checkout_index__can_checkout_a_newly_initialized_repository(void)
{
658
	cl_git_sandbox_cleanup();
659
	g_repo = cl_git_sandbox_init("empty_standard_repo");
660

661 662 663 664
	cl_git_remove_placeholders(git_repository_path(g_repo), "dummy-marker.txt");

	cl_git_pass(git_checkout_index(g_repo, NULL, NULL));
}
665 666 667

void test_checkout_index__issue_1397(void)
{
668
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
669

670
	cl_git_sandbox_cleanup();
671 672
	g_repo = cl_git_sandbox_init("issue_1397");

Russell Belfer committed
673
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
674 675 676 677 678

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));

679
	check_file_contents("./issue_1397/crlf_file.txt", "first line\r\nsecond line\r\nboth with crlf");
680
}
681 682 683

void test_checkout_index__target_directory(void)
{
684
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
685 686 687
	checkout_counts cts;
	memset(&cts, 0, sizeof(cts));

688 689
	opts.checkout_strategy = GIT_CHECKOUT_SAFE |
		GIT_CHECKOUT_RECREATE_MISSING;
690
	opts.target_directory = "alternative";
691
	cl_assert(!git_path_isdir("alternative"));
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709

	opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
	opts.notify_cb = checkout_count_callback;
	opts.notify_payload = &cts;

	/* create some files that *would* conflict if we were using the wd */
	cl_git_mkfile("testrepo/README", "I'm in the way!\n");
	cl_git_mkfile("testrepo/new.txt", "my new file\n");

	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));

	cl_assert_equal_i(0, cts.n_untracked);
	cl_assert_equal_i(0, cts.n_ignored);
	cl_assert_equal_i(4, cts.n_updates);

	check_file_contents("./alternative/README", "hey there\n");
	check_file_contents("./alternative/branch_file.txt", "hi\nbye!\n");
	check_file_contents("./alternative/new.txt", "my new file\n");
710 711 712 713 714 715 716

	cl_git_pass(git_futils_rmdir_r(
		"alternative", NULL, GIT_RMDIR_REMOVE_FILES));
}

void test_checkout_index__target_directory_from_bare(void)
{
717
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
718 719 720 721 722
	git_index *index;
	git_object *head = NULL;
	checkout_counts cts;
	memset(&cts, 0, sizeof(cts));

723
	cl_git_sandbox_cleanup();
724 725 726 727 728 729 730 731 732
	g_repo = cl_git_sandbox_init("testrepo.git");
	cl_assert(git_repository_is_bare(g_repo));

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_revparse_single(&head, g_repo, "HEAD^{tree}"));
	cl_git_pass(git_index_read_tree(index, (const git_tree *)head));
	cl_git_pass(git_index_write(index));
	git_index_free(index);

733 734
	opts.checkout_strategy = GIT_CHECKOUT_SAFE |
		GIT_CHECKOUT_RECREATE_MISSING;
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

	opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
	opts.notify_cb = checkout_count_callback;
	opts.notify_payload = &cts;

	/* fail to checkout a bare repo */
	cl_git_fail(git_checkout_index(g_repo, NULL, &opts));

	opts.target_directory = "alternative";
	cl_assert(!git_path_isdir("alternative"));

	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));

	cl_assert_equal_i(0, cts.n_untracked);
	cl_assert_equal_i(0, cts.n_ignored);
	cl_assert_equal_i(3, cts.n_updates);

752 753 754 755
	/* files will have been filtered if needed, so strip CR */
	check_file_contents_nocr("./alternative/README", "hey there\n");
	check_file_contents_nocr("./alternative/branch_file.txt", "hi\nbye!\n");
	check_file_contents_nocr("./alternative/new.txt", "my new file\n");
756 757 758

	cl_git_pass(git_futils_rmdir_r(
		"alternative", NULL, GIT_RMDIR_REMOVE_FILES));
nulltoken committed
759 760

	git_object_free(head);
761 762 763 764 765
}

void test_checkout_index__can_get_repo_from_index(void)
{
	git_index *index;
766
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
767 768 769 770 771

	cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
	cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
	cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));

772
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
773 774 775 776 777 778 779 780 781 782

	cl_git_pass(git_repository_index(&index, g_repo));

	cl_git_pass(git_checkout_index(NULL, index, &opts));

	check_file_contents("./testrepo/README", "hey there\n");
	check_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
	check_file_contents("./testrepo/new.txt", "my new file\n");

	git_index_free(index);
783
}
784

785
static void add_conflict(git_index *index, const char *path)
786 787 788 789 790 791
{
	git_index_entry entry;

	memset(&entry, 0, sizeof(git_index_entry));

	entry.mode = 0100644;
792
	entry.path = path;
793 794

	git_oid_fromstr(&entry.id, "d427e0b2e138501a3d15cc376077a3631e15bd46");
795
	GIT_INDEX_ENTRY_STAGE_SET(&entry, 1);
796 797 798
	cl_git_pass(git_index_add(index, &entry));

	git_oid_fromstr(&entry.id, "4e886e602529caa9ab11d71f86634bd1b6e0de10");
799
	GIT_INDEX_ENTRY_STAGE_SET(&entry, 2);
800 801 802
	cl_git_pass(git_index_add(index, &entry));

	git_oid_fromstr(&entry.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863");
803
	GIT_INDEX_ENTRY_STAGE_SET(&entry, 3);
804 805 806 807 808
	cl_git_pass(git_index_add(index, &entry));
}

void test_checkout_index__writes_conflict_file(void)
{
809
	git_index *index;
810 811 812
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_buf conflicting_buf = GIT_BUF_INIT;

813 814 815 816 817
	cl_git_pass(git_repository_index(&index, g_repo));

	add_conflict(index, "conflicting.txt");
	cl_git_pass(git_index_write(index));

818 819 820 821 822 823 824 825 826
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));

	cl_git_pass(git_futils_readbuffer(&conflicting_buf, "testrepo/conflicting.txt"));
	cl_assert(strcmp(conflicting_buf.ptr,
		"<<<<<<< ours\n"
		"this file is changed in master and branch\n"
		"=======\n"
		"this file is changed in branch and master\n"
		">>>>>>> theirs\n") == 0);
827
	git_buf_dispose(&conflicting_buf);
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850

	git_index_free(index);
}

void test_checkout_index__adding_conflict_removes_stage_0(void)
{
	git_index *new_index, *index;
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;

	cl_git_pass(git_index_new(&new_index));

	add_conflict(new_index, "new.txt");
	cl_git_pass(git_checkout_index(g_repo, new_index, &opts));

	cl_git_pass(git_repository_index(&index, g_repo));

	cl_assert(git_index_get_bypath(index, "new.txt", 0) == NULL);
	cl_assert(git_index_get_bypath(index, "new.txt", 1) != NULL);
	cl_assert(git_index_get_bypath(index, "new.txt", 2) != NULL);
	cl_assert(git_index_get_bypath(index, "new.txt", 3) != NULL);

	git_index_free(index);
	git_index_free(new_index);
851 852 853 854 855
}

void test_checkout_index__conflicts_honor_coreautocrlf(void)
{
#ifdef GIT_WIN32
856
	git_index *index;
857 858 859 860 861 862
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_buf conflicting_buf = GIT_BUF_INIT;

	cl_git_pass(p_unlink("./testrepo/.gitattributes"));
	cl_repo_set_bool(g_repo, "core.autocrlf", true);

863 864 865 866 867
	cl_git_pass(git_repository_index(&index, g_repo));

	add_conflict(index, "conflicting.txt");
	cl_git_pass(git_index_write(index));

868 869 870 871 872 873 874 875 876
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));

	cl_git_pass(git_futils_readbuffer(&conflicting_buf, "testrepo/conflicting.txt"));
	cl_assert(strcmp(conflicting_buf.ptr,
		"<<<<<<< ours\r\n"
		"this file is changed in master and branch\r\n"
		"=======\r\n"
		"this file is changed in branch and master\r\n"
		">>>>>>> theirs\r\n") == 0);
877
	git_buf_dispose(&conflicting_buf);
878 879

	git_index_free(index);
880 881
#endif
}