tree.c 51.1 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "checkout_helpers.h"
3 4 5

#include "git2/checkout.h"
#include "repository.h"
6
#include "futils.h"
7 8

static git_repository *g_repo;
9
static git_checkout_options g_opts;
10
static git_object *g_object;
11

12 13 14 15 16 17 18 19 20 21
static void assert_status_entrycount(git_repository *repo, size_t count)
{
	git_status_list *status;

	cl_git_pass(git_status_list_new(&status, repo, NULL));
	cl_assert_equal_i(count, git_status_list_entrycount(status));

	git_status_list_free(status);
}

22 23 24
void test_checkout_tree__initialize(void)
{
	g_repo = cl_git_sandbox_init("testrepo");
25

26
	GIT_INIT_STRUCTURE(&g_opts, GIT_CHECKOUT_OPTIONS_VERSION);
27
	g_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
28 29 30 31
}

void test_checkout_tree__cleanup(void)
{
32
	git_object_free(g_object);
33
	g_object = NULL;
34

35
	cl_git_sandbox_cleanup();
36

37
	if (git_fs_path_isdir("alternative"))
38
		git_futils_rmdir_r("alternative", NULL, GIT_RMDIR_REMOVE_FILES);
39 40 41 42
}

void test_checkout_tree__cannot_checkout_a_non_treeish(void)
{
43
	/* blob */
44
	cl_git_pass(git_revparse_single(&g_object, g_repo, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd"));
45
	cl_git_fail(git_checkout_tree(g_repo, g_object, NULL));
46 47 48 49 50 51
}

void test_checkout_tree__can_checkout_a_subdirectory_from_a_commit(void)
{
	char *entries[] = { "ab/de/" };

52 53
	g_opts.paths.strings = entries;
	g_opts.paths.count = 1;
54

55
	cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
56

57
	cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
58

59
	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
60

61 62
	cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/2.txt"));
	cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/fgh/1.txt"));
63 64
}

65 66
void test_checkout_tree__can_checkout_and_remove_directory(void)
{
67
	cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
68

Dimitris Apostolou committed
69
	/* Checkout branch "subtrees" and update HEAD, so that HEAD matches the
70 71
	 * current working tree
	 */
72
	cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));
73
	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
74

75
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
76

77 78 79
	cl_assert_equal_i(true, git_fs_path_isdir("./testrepo/ab/"));
	cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/2.txt"));
	cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/fgh/1.txt"));
80

nulltoken committed
81 82 83
	git_object_free(g_object);
	g_object = NULL;

Dimitris Apostolou committed
84
	/* Checkout branch "master" and update HEAD, so that HEAD matches the
85 86
	 * current working tree
	 */
87
	cl_git_pass(git_revparse_single(&g_object, g_repo, "master"));
88 89
	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));

90
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master"));
91 92

	/* This directory should no longer exist */
93
	cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
94 95
}

96 97 98 99
void test_checkout_tree__can_checkout_a_subdirectory_from_a_subtree(void)
{
	char *entries[] = { "de/" };

100 101
	g_opts.paths.strings = entries;
	g_opts.paths.count = 1;
102

103
	cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees:ab"));
104

105
	cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/de/"));
106

107
	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
108

109 110
	cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/de/2.txt"));
	cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/de/fgh/1.txt"));
111
}
112

113
static void progress(const char *path, size_t cur, size_t tot, void *payload)
114
{
Ben Straub committed
115
	bool *was_called = (bool*)payload;
Ben Straub committed
116
	GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
Ben Straub committed
117
	*was_called = true;
118 119 120 121
}

void test_checkout_tree__calls_progress_callback(void)
{
Ben Straub committed
122
	bool was_called = 0;
123

124
	g_opts.progress_cb = progress;
Ben Straub committed
125
	g_opts.progress_payload = &was_called;
126

127
	cl_git_pass(git_revparse_single(&g_object, g_repo, "master"));
128

129
	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
130

Ben Straub committed
131
	cl_assert_equal_i(was_called, true);
132
}
133 134 135

void test_checkout_tree__doesnt_write_unrequested_files_to_worktree(void)
{
nulltoken committed
136 137 138 139
	git_oid master_oid;
	git_oid chomped_oid;
	git_commit* p_master_commit;
	git_commit* p_chomped_commit;
140
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
nulltoken committed
141

142 143
	git_oid__fromstr(&master_oid, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", GIT_OID_SHA1);
	git_oid__fromstr(&chomped_oid, "e90810b8df3e80c413d903f631643c716887138d", GIT_OID_SHA1);
nulltoken committed
144 145 146 147 148 149 150 151
	cl_git_pass(git_commit_lookup(&p_master_commit, g_repo, &master_oid));
	cl_git_pass(git_commit_lookup(&p_chomped_commit, g_repo, &chomped_oid));

	/* GIT_CHECKOUT_NONE should not add any file to the working tree from the
	 * index as it is supposed to be a dry run.
	 */
	opts.checkout_strategy = GIT_CHECKOUT_NONE;
	git_checkout_tree(g_repo, (git_object*)p_chomped_commit, &opts);
152
	cl_assert_equal_i(false, git_fs_path_isfile("testrepo/readme.txt"));
nulltoken committed
153 154 155

	git_commit_free(p_master_commit);
	git_commit_free(p_chomped_commit);
156
}
157 158 159

void test_checkout_tree__can_switch_branches(void)
{
160
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
161 162 163 164 165 166 167 168 169 170 171
	git_oid oid;
	git_object *obj = NULL;

	assert_on_branch(g_repo, "master");

	/* do first checkout with FORCE because we don't know if testrepo
	 * base data is clean for a checkout or not
	 */
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
172
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
173 174

	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
175
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
176

177 178 179 180
	cl_assert(git_fs_path_isfile("testrepo/README"));
	cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
	cl_assert(git_fs_path_isfile("testrepo/new.txt"));
	cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
181

182
	cl_assert(!git_fs_path_isdir("testrepo/ab"));
183 184 185 186 187 188 189 190 191

	assert_on_branch(g_repo, "dir");

	git_object_free(obj);

	/* do second checkout safe because we should be clean after first */
	opts.checkout_strategy = GIT_CHECKOUT_SAFE;

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees"));
192
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
193 194

	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
195
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
196

197 198 199 200 201 202 203
	cl_assert(git_fs_path_isfile("testrepo/README"));
	cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
	cl_assert(git_fs_path_isfile("testrepo/new.txt"));
	cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
	cl_assert(git_fs_path_isfile("testrepo/ab/c/3.txt"));
	cl_assert(git_fs_path_isfile("testrepo/ab/de/2.txt"));
	cl_assert(git_fs_path_isfile("testrepo/ab/de/fgh/1.txt"));
204

205
	cl_assert(!git_fs_path_isdir("testrepo/a"));
206 207 208 209 210 211 212 213

	assert_on_branch(g_repo, "subtrees");

	git_object_free(obj);
}

void test_checkout_tree__can_remove_untracked(void)
{
214
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
215 216 217 218

	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_REMOVE_UNTRACKED;

	cl_git_mkfile("testrepo/untracked_file", "as you wish");
219
	cl_assert(git_fs_path_isfile("testrepo/untracked_file"));
220 221 222

	cl_git_pass(git_checkout_head(g_repo, &opts));

223
	cl_assert(!git_fs_path_isfile("testrepo/untracked_file"));
224 225 226 227
}

void test_checkout_tree__can_remove_ignored(void)
{
228
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
229 230 231 232 233 234 235 236 237 238 239
	int ignored = 0;

	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_REMOVE_IGNORED;

	cl_git_mkfile("testrepo/ignored_file", "as you wish");

	cl_git_pass(git_ignore_add_rule(g_repo, "ignored_file\n"));

	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "ignored_file"));
	cl_assert_equal_i(1, ignored);

240
	cl_assert(git_fs_path_isfile("testrepo/ignored_file"));
241 242 243

	cl_git_pass(git_checkout_head(g_repo, &opts));

244
	cl_assert(!git_fs_path_isfile("testrepo/ignored_file"));
245 246
}

247
static int checkout_tree_with_blob_ignored_in_workdir(int strategy, bool isdir)
248 249 250
{
	git_oid oid;
	git_object *obj = NULL;
251
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
252 253 254 255 256 257 258 259 260 261
	int ignored = 0, error;

	assert_on_branch(g_repo, "master");

	/* do first checkout with FORCE because we don't know if testrepo
	 * base data is clean for a checkout or not
	 */
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
262
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
263 264

	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
265
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
266

267 268 269 270
	cl_assert(git_fs_path_isfile("testrepo/README"));
	cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
	cl_assert(git_fs_path_isfile("testrepo/new.txt"));
	cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
271

272
	cl_assert(!git_fs_path_isdir("testrepo/ab"));
273 274 275 276 277 278 279

	assert_on_branch(g_repo, "dir");

	git_object_free(obj);

	opts.checkout_strategy = strategy;

280 281 282 283 284 285 286 287
	if (isdir) {
		cl_must_pass(p_mkdir("testrepo/ab", 0777));
		cl_must_pass(p_mkdir("testrepo/ab/4.txt", 0777));

		cl_git_mkfile("testrepo/ab/4.txt/file1.txt", "as you wish");
		cl_git_mkfile("testrepo/ab/4.txt/file2.txt", "foo bar foo");
		cl_git_mkfile("testrepo/ab/4.txt/file3.txt", "inky blinky pinky clyde");

288
		cl_assert(git_fs_path_isdir("testrepo/ab/4.txt"));
289 290 291 292
	} else {
		cl_must_pass(p_mkdir("testrepo/ab", 0777));
		cl_git_mkfile("testrepo/ab/4.txt", "as you wish");

293
		cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
294
	}
295 296 297 298 299 300 301

	cl_git_pass(git_ignore_add_rule(g_repo, "ab/4.txt\n"));

	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "ab/4.txt"));
	cl_assert_equal_i(1, ignored);

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees"));
302
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
303 304 305 306 307 308 309 310 311 312 313 314 315

	error = git_checkout_tree(g_repo, obj, &opts);

	git_object_free(obj);

	return error;
}

void test_checkout_tree__conflict_on_ignored_when_not_overwriting(void)
{
	int error;

	cl_git_fail(error = checkout_tree_with_blob_ignored_in_workdir(
316
		GIT_CHECKOUT_SAFE | GIT_CHECKOUT_DONT_OVERWRITE_IGNORED, false));
317

318
	cl_assert_equal_i(GIT_ECONFLICT, error);
319 320 321 322
}

void test_checkout_tree__can_overwrite_ignored_by_default(void)
{
323
	cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE, false));
324

325
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
326

327
	cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
328 329 330 331

	assert_on_branch(g_repo, "subtrees");
}

332 333 334 335 336 337 338
void test_checkout_tree__conflict_on_ignored_folder_when_not_overwriting(void)
{
	int error;

	cl_git_fail(error = checkout_tree_with_blob_ignored_in_workdir(
		GIT_CHECKOUT_SAFE | GIT_CHECKOUT_DONT_OVERWRITE_IGNORED, true));

339
	cl_assert_equal_i(GIT_ECONFLICT, error);
340 341 342 343 344 345
}

void test_checkout_tree__can_overwrite_ignored_folder_by_default(void)
{
	cl_git_pass(checkout_tree_with_blob_ignored_in_workdir(GIT_CHECKOUT_SAFE, true));

346
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
347

348
	cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
349 350 351 352 353

	assert_on_branch(g_repo, "subtrees");

}

354 355
void test_checkout_tree__can_update_only(void)
{
356
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
357 358 359 360 361 362 363 364 365 366
	git_oid oid;
	git_object *obj = NULL;

	/* first let's get things into a known state - by checkout out the HEAD */

	assert_on_branch(g_repo, "master");

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
	cl_git_pass(git_checkout_head(g_repo, &opts));

367
	cl_assert(!git_fs_path_isdir("testrepo/a"));
368

369
	check_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n");
370 371 372 373 374 375

	/* now checkout branch but with update only */

	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_UPDATE_ONLY;

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
376
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
377 378

	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
379
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
380 381 382 383 384 385 386

	assert_on_branch(g_repo, "dir");

	/* this normally would have been created (which was tested separately in
	 * the test_checkout_tree__can_switch_branches test), but with
	 * UPDATE_ONLY it will not have been created.
	 */
387
	cl_assert(!git_fs_path_isdir("testrepo/a"));
388 389

	/* but this file still should have been updated */
390
	check_file_contents_nocr("testrepo/branch_file.txt", "hi\n");
391 392 393

	git_object_free(obj);
}
394 395 396 397 398 399 400 401 402 403

void test_checkout_tree__can_checkout_with_pattern(void)
{
	char *entries[] = { "[l-z]*.txt" };

	/* reset to beginning of history (i.e. just a README file) */

	g_opts.checkout_strategy =
		GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;

404
	cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479"));
405 406 407

	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
	cl_git_pass(
408
		git_repository_set_head_detached(g_repo, git_object_id(g_object)));
409 410 411 412

	git_object_free(g_object);
	g_object = NULL;

413 414 415 416
	cl_assert(git_fs_path_exists("testrepo/README"));
	cl_assert(!git_fs_path_exists("testrepo/branch_file.txt"));
	cl_assert(!git_fs_path_exists("testrepo/link_to_new.txt"));
	cl_assert(!git_fs_path_exists("testrepo/new.txt"));
417 418 419

	/* now to a narrow patterned checkout */

420
	g_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
421 422 423
	g_opts.paths.strings = entries;
	g_opts.paths.count = 1;

424
	cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master"));
425 426 427

	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));

428 429 430 431
	cl_assert(git_fs_path_exists("testrepo/README"));
	cl_assert(!git_fs_path_exists("testrepo/branch_file.txt"));
	cl_assert(git_fs_path_exists("testrepo/link_to_new.txt"));
	cl_assert(git_fs_path_exists("testrepo/new.txt"));
432 433
}

434 435 436 437 438 439 440 441 442 443 444 445 446 447
void test_checkout_tree__pathlist_checkout_ignores_non_matches(void)
{
	char *entries[] = { "branch_file.txt", "link_to_new.txt" };

	/* reset to beginning of history (i.e. just a README file) */

	g_opts.checkout_strategy =
		GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;

	cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master"));

	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master"));

448 449 450 451
	cl_assert(git_fs_path_exists("testrepo/README"));
	cl_assert(git_fs_path_exists("testrepo/branch_file.txt"));
	cl_assert(git_fs_path_exists("testrepo/link_to_new.txt"));
	cl_assert(git_fs_path_exists("testrepo/new.txt"));
452

453
	git_object_free(g_object);
454 455 456 457 458 459 460 461 462
	cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479"));

	g_opts.checkout_strategy =
		GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH;
	g_opts.paths.strings = entries;
	g_opts.paths.count = 2;

	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));

463 464 465 466
	cl_assert(git_fs_path_exists("testrepo/README"));
	cl_assert(!git_fs_path_exists("testrepo/branch_file.txt"));
	cl_assert(!git_fs_path_exists("testrepo/link_to_new.txt"));
	cl_assert(git_fs_path_exists("testrepo/new.txt"));
467 468
}

469 470 471 472 473 474 475 476 477
void test_checkout_tree__can_disable_pattern_match(void)
{
	char *entries[] = { "b*.txt" };

	/* reset to beginning of history (i.e. just a README file) */

	g_opts.checkout_strategy =
		GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;

478
	cl_git_pass(git_revparse_single(&g_object, g_repo, "8496071c1b46c854b31185ea97743be6a8774479"));
479 480 481

	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
	cl_git_pass(
482
		git_repository_set_head_detached(g_repo, git_object_id(g_object)));
483 484 485 486

	git_object_free(g_object);
	g_object = NULL;

487
	cl_assert(!git_fs_path_isfile("testrepo/branch_file.txt"));
488 489 490 491

	/* now to a narrow patterned checkout, but disable pattern */

	g_opts.checkout_strategy =
492 493
		GIT_CHECKOUT_SAFE |
		GIT_CHECKOUT_DISABLE_PATHSPEC_MATCH;
494 495 496
	g_opts.paths.strings = entries;
	g_opts.paths.count = 1;

497
	cl_git_pass(git_revparse_single(&g_object, g_repo, "refs/heads/master"));
498 499 500

	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));

501
	cl_assert(!git_fs_path_isfile("testrepo/branch_file.txt"));
502 503 504

	/* let's try that again, but allow the pattern match */

505
	g_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
506 507 508

	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));

509
	cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
510
}
511

512
static void assert_conflict(
513 514 515 516 517 518 519 520
	const char *entry_path,
	const char *new_content,
	const char *parent_sha,
	const char *commit_sha)
{
	git_index *index;
	git_object *hack_tree;
	git_reference *branch, *head;
521
	git_str file_path = GIT_STR_INIT;
522 523 524 525

	cl_git_pass(git_repository_index(&index, g_repo));

	/* Create a branch pointing at the parent */
526
	cl_git_pass(git_revparse_single(&g_object, g_repo, parent_sha));
527
	cl_git_pass(git_branch_create(&branch, g_repo,
528
		"potential_conflict", (git_commit *)g_object, 0));
529 530 531

	/* Make HEAD point to this branch */
	cl_git_pass(git_reference_symbolic_create(
532
		&head, g_repo, "HEAD", git_reference_name(branch), 1, NULL));
Carlos Martín Nieto committed
533 534
	git_reference_free(head);
	git_reference_free(branch);
535 536

	/* Checkout the parent */
537
	g_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
538 539
	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));

Dimitris Apostolou committed
540
	/* Hack-ishy workaround to ensure *all* the index entries
541 542
	 * match the content of the tree
	 */
543
	cl_git_pass(git_object_peel(&hack_tree, g_object, GIT_OBJECT_TREE));
544
	cl_git_pass(git_index_read_tree(index, (git_tree *)hack_tree));
545
	cl_git_pass(git_index_write(index));
546 547 548 549 550
	git_object_free(hack_tree);
	git_object_free(g_object);
	g_object = NULL;

	/* Create a conflicting file */
551 552 553
	cl_git_pass(git_str_joinpath(&file_path, "./testrepo", entry_path));
	cl_git_mkfile(git_str_cstr(&file_path), new_content);
	git_str_dispose(&file_path);
554 555

	/* Trying to checkout the original commit */
556
	cl_git_pass(git_revparse_single(&g_object, g_repo, commit_sha));
557

558
	g_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
559
	cl_assert_equal_i(
560
		GIT_ECONFLICT, git_checkout_tree(g_repo, g_object, &g_opts));
561 562

	/* Stage the conflicting change */
563
	cl_git_pass(git_index_add_bypath(index, entry_path));
564
	cl_git_pass(git_index_write(index));
Carlos Martín Nieto committed
565
	git_index_free(index);
566 567

	cl_assert_equal_i(
568
		GIT_ECONFLICT, git_checkout_tree(g_repo, g_object, &g_opts));
569 570
}

571
void test_checkout_tree__checking_out_a_conflicting_type_change_returns_ECONFLICT(void)
572 573 574 575 576 577 578 579 580
{
	/*
	 * 099faba adds a symlink named 'link_to_new.txt'
	 * a65fedf is the parent of 099faba
	 */

	assert_conflict("link_to_new.txt", "old.txt", "a65fedf", "099faba");
}

581
void test_checkout_tree__checking_out_a_conflicting_type_change_returns_ECONFLICT_2(void)
582 583 584 585 586 587 588 589 590
{
	/*
	 * cf80f8d adds a directory named 'a/'
	 * a4a7dce is the parent of cf80f8d
	 */

	assert_conflict("a", "hello\n", "a4a7dce", "cf80f8d");
}

591
void test_checkout_tree__checking_out_a_conflicting_content_change_returns_ECONFLICT(void)
592 593 594 595 596 597 598 599
{
	/*
	 * c47800c adds a symlink named 'branch_file.txt'
	 * 5b5b025 is the parent of 763d71a
	 */

	assert_conflict("branch_file.txt", "hello\n", "5b5b025", "c47800c");
}
600

601 602
void test_checkout_tree__donot_update_deleted_file_by_default(void)
{
603
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
604 605 606
	git_oid old_id, new_id;
	git_commit *old_commit = NULL, *new_commit = NULL;
	git_index *index = NULL;
607
	checkout_counts ct;
608 609 610

	opts.checkout_strategy = GIT_CHECKOUT_SAFE;

611 612 613 614 615
	memset(&ct, 0, sizeof(ct));
	opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
	opts.notify_cb = checkout_count_callback;
	opts.notify_payload = &ct;

616 617
	cl_git_pass(git_repository_index(&index, g_repo));

618
	cl_git_pass(git_oid__fromstr(&old_id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644", GIT_OID_SHA1));
619
	cl_git_pass(git_commit_lookup(&old_commit, g_repo, &old_id));
620
	cl_git_pass(git_reset(g_repo, (git_object *)old_commit, GIT_RESET_HARD, NULL));
621 622 623 624 625

	cl_git_pass(p_unlink("testrepo/branch_file.txt"));
	cl_git_pass(git_index_remove_bypath(index ,"branch_file.txt"));
	cl_git_pass(git_index_write(index));

626
	cl_assert(!git_fs_path_exists("testrepo/branch_file.txt"));
627

628
	cl_git_pass(git_oid__fromstr(&new_id, "099fabac3a9ea935598528c27f866e34089c2eff", GIT_OID_SHA1));
629
	cl_git_pass(git_commit_lookup(&new_commit, g_repo, &new_id));
630 631


632 633
	cl_git_fail(git_checkout_tree(g_repo, (git_object *)new_commit, &opts));

634 635 636
	cl_assert_equal_i(1, ct.n_conflicts);
	cl_assert_equal_i(1, ct.n_updates);

637 638 639 640 641
	git_commit_free(old_commit);
	git_commit_free(new_commit);
	git_index_free(index);
}

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
struct checkout_cancel_at {
	const char *filename;
	int error;
	int count;
};

static int checkout_cancel_cb(
	git_checkout_notify_t why,
	const char *path,
	const git_diff_file *b,
	const git_diff_file *t,
	const git_diff_file *w,
	void *payload)
{
	struct checkout_cancel_at *ca = payload;

	GIT_UNUSED(why); GIT_UNUSED(b); GIT_UNUSED(t); GIT_UNUSED(w);

	ca->count++;

	if (!strcmp(path, ca->filename))
		return ca->error;

	return 0;
}

void test_checkout_tree__can_cancel_checkout_from_notify(void)
{
	struct checkout_cancel_at ca;
671
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
672 673 674 675 676 677
	git_oid oid;
	git_object *obj = NULL;

	assert_on_branch(g_repo, "master");

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
678
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
679 680 681 682 683 684 685 686 687 688

	ca.filename = "new.txt";
	ca.error = -5555;
	ca.count = 0;

	opts.notify_flags = GIT_CHECKOUT_NOTIFY_UPDATED;
	opts.notify_cb = checkout_cancel_cb;
	opts.notify_payload = &ca;
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

689
	cl_assert(!git_fs_path_exists("testrepo/new.txt"));
690 691 692

	cl_git_fail_with(git_checkout_tree(g_repo, obj, &opts), -5555);

693
	cl_assert(!git_fs_path_exists("testrepo/new.txt"));
694 695 696 697

	/* on case-insensitive FS = a/b.txt, branch_file.txt, new.txt */
	/* on case-sensitive FS   = README, then above */

698
	if (git_fs_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */
699 700 701
		cl_assert_equal_i(3, ca.count);
	else
		cl_assert_equal_i(4, ca.count);
702 703 704 705 706 707 708 709

	/* and again with a different stopping point and return code */
	ca.filename = "README";
	ca.error = 123;
	ca.count = 0;

	cl_git_fail_with(git_checkout_tree(g_repo, obj, &opts), 123);

710
	cl_assert(!git_fs_path_exists("testrepo/new.txt"));
711

712
	if (git_fs_path_exists("testrepo/.git/CoNfIg")) /* case insensitive */
713 714 715
		cl_assert_equal_i(4, ca.count);
	else
		cl_assert_equal_i(1, ca.count);
716 717 718 719

	git_object_free(obj);
}

720 721 722
void test_checkout_tree__can_checkout_with_last_workdir_item_missing(void)
{
	git_index *index = NULL;
723
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
724 725 726
	git_oid tree_id, commit_id;
	git_tree *tree = NULL;
	git_commit *commit = NULL;
727

728
	git_repository_index(&index, g_repo);
729

730
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
731

732 733
	cl_git_pass(git_reference_name_to_id(&commit_id, g_repo, "refs/heads/master"));
	cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));
734

735
	cl_git_pass(git_checkout_tree(g_repo, (git_object *)commit, &opts));
736
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master"));
737

738 739
	cl_git_pass(p_mkdir("./testrepo/this-is-dir", 0777));
	cl_git_mkfile("./testrepo/this-is-dir/contained_file", "content\n");
740

741
	cl_git_pass(git_index_add_bypath(index, "this-is-dir/contained_file"));
742 743 744
	cl_git_pass(git_index_write(index));

	cl_git_pass(git_index_write_tree(&tree_id, index));
745
	cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
746

747
	cl_git_pass(p_unlink("./testrepo/this-is-dir/contained_file"));
748

749
	opts.checkout_strategy = GIT_CHECKOUT_SAFE;
750

751 752
	opts.checkout_strategy = 1;
	git_checkout_tree(g_repo, (git_object *)tree, &opts);
753

754 755 756 757
	git_tree_free(tree);
	git_commit_free(commit);
	git_index_free(index);
}
758 759 760

void test_checkout_tree__issue_1397(void)
{
761
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
762
	const char *partial_oid = "8a7ef04";
Russell Belfer committed
763 764 765
	git_object *tree = NULL;

	test_checkout_tree__cleanup(); /* cleanup default checkout */
766 767 768

	g_repo = cl_git_sandbox_init("issue_1397");

Russell Belfer committed
769
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
770

771
	cl_git_pass(git_revparse_single(&tree, g_repo, partial_oid));
772 773 774

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

Russell Belfer committed
775
	cl_git_pass(git_checkout_tree(g_repo, tree, &opts));
776

777
	check_file_contents("./issue_1397/crlf_file.txt", "first line\r\nsecond line\r\nboth with crlf");
778

Russell Belfer committed
779
	git_object_free(tree);
780
}
781 782 783

void test_checkout_tree__can_write_to_empty_dirs(void)
{
784
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
785 786 787 788 789 790 791 792 793 794 795 796 797
	git_oid oid;
	git_object *obj = NULL;

	assert_on_branch(g_repo, "master");

	cl_git_pass(p_mkdir("testrepo/a", 0777));

	/* do first checkout with FORCE because we don't know if testrepo
	 * base data is clean for a checkout or not
	 */
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
798
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
799 800 801

	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));

802
	cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
803 804 805

	git_object_free(obj);
}
806 807 808 809

void test_checkout_tree__fails_when_dir_in_use(void)
{
#ifdef GIT_WIN32
810
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
811 812 813 814 815 816
	git_oid oid;
	git_object *obj = NULL;

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
817
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
818 819 820

	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));

821
	cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
822 823 824 825 826 827

	git_object_free(obj);

	cl_git_pass(p_chdir("testrepo/a"));

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/master"));
828
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
829 830 831 832 833

	cl_git_fail(git_checkout_tree(g_repo, obj, &opts));

	cl_git_pass(p_chdir("../.."));

834
	cl_assert(git_fs_path_is_empty_dir("testrepo/a"));
835 836

	git_object_free(obj);
837 838 839 840 841 842
#endif
}

void test_checkout_tree__can_continue_when_dir_in_use(void)
{
#ifdef GIT_WIN32
843
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
844 845 846 847 848 849 850
	git_oid oid;
	git_object *obj = NULL;

	opts.checkout_strategy = GIT_CHECKOUT_FORCE |
		GIT_CHECKOUT_SKIP_LOCKED_DIRECTORIES;

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
851
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
852 853 854

	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));

855
	cl_assert(git_fs_path_isfile("testrepo/a/b.txt"));
856 857 858 859 860 861

	git_object_free(obj);

	cl_git_pass(p_chdir("testrepo/a"));

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/master"));
862
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
863 864 865 866 867

	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));

	cl_git_pass(p_chdir("../.."));

868
	cl_assert(git_fs_path_is_empty_dir("testrepo/a"));
869 870

	git_object_free(obj);
871 872
#endif
}
873 874 875

void test_checkout_tree__target_directory_from_bare(void)
{
876
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
877 878 879 880 881 882 883 884 885
	git_oid oid;
	checkout_counts cts;
	memset(&cts, 0, sizeof(cts));

	test_checkout_tree__cleanup(); /* cleanup default checkout */

	g_repo = cl_git_sandbox_init("testrepo.git");
	cl_assert(git_repository_is_bare(g_repo));

886 887
	opts.checkout_strategy = GIT_CHECKOUT_SAFE |
		GIT_CHECKOUT_RECREATE_MISSING;
888 889 890 891 892 893

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

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
894
	cl_git_pass(git_object_lookup(&g_object, g_repo, &oid, GIT_OBJECT_ANY));
895 896 897 898

	cl_git_fail(git_checkout_tree(g_repo, g_object, &opts));

	opts.target_directory = "alternative";
899
	cl_assert(!git_fs_path_isdir("alternative"));
900 901 902 903 904 905 906

	cl_git_pass(git_checkout_tree(g_repo, g_object, &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);

907 908 909
	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");
910 911 912 913

	cl_git_pass(git_futils_rmdir_r(
		"alternative", NULL, GIT_RMDIR_REMOVE_FILES));
}
914 915 916

void test_checkout_tree__extremely_long_file_name(void)
{
917
	/* A utf-8 string with 83 characters, but 249 bytes. */
918
	const char *longname = "\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97\xe5\x8f\x97";
919
	char path[1024] = {0};
920 921 922 923 924 925

	g_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
	cl_git_pass(git_revparse_single(&g_object, g_repo, "long-file-name"));
	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));

	sprintf(path, "testrepo/%s.txt", longname);
926
	cl_assert(git_fs_path_exists(path));
927 928 929 930

	git_object_free(g_object);
	cl_git_pass(git_revparse_single(&g_object, g_repo, "master"));
	cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
931
	cl_assert(!git_fs_path_exists(path));
932
}
933

934
static void create_conflict(const char *path)
935 936 937 938 939 940 941 942
{
	git_index *index;
	git_index_entry entry;

	cl_git_pass(git_repository_index(&index, g_repo));

	memset(&entry, 0x0, sizeof(git_index_entry));
	entry.mode = 0100644;
943
	GIT_INDEX_ENTRY_STAGE_SET(&entry, 1);
944
	git_oid__fromstr(&entry.id, "d427e0b2e138501a3d15cc376077a3631e15bd46", GIT_OID_SHA1);
945
	entry.path = path;
946 947
	cl_git_pass(git_index_add(index, &entry));

948
	GIT_INDEX_ENTRY_STAGE_SET(&entry, 2);
949
	git_oid__fromstr(&entry.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", GIT_OID_SHA1);
950 951
	cl_git_pass(git_index_add(index, &entry));

952
	GIT_INDEX_ENTRY_STAGE_SET(&entry, 3);
953
	git_oid__fromstr(&entry.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", GIT_OID_SHA1);
954 955
	cl_git_pass(git_index_add(index, &entry));

956
	cl_git_pass(git_index_write(index));
957 958 959 960 961
	git_index_free(index);
}

void test_checkout_tree__fails_when_conflicts_exist_in_index(void)
{
962
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
963 964 965 966 967 968
	git_oid oid;
	git_object *obj = NULL;

	opts.checkout_strategy = GIT_CHECKOUT_SAFE;

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
969
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
970

971
	create_conflict("conflicts.txt");
972 973 974 975 976

	cl_git_fail(git_checkout_tree(g_repo, obj, &opts));

	git_object_free(obj);
}
977 978 979 980 981

void test_checkout_tree__filemode_preserved_in_index(void)
{
	git_oid executable_oid;
	git_commit *commit;
982
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
983 984 985
	git_index *index;
	const git_index_entry *entry;

986 987
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

988 989
	cl_git_pass(git_repository_index(&index, g_repo));

990
	/* test a freshly added executable */
991
	cl_git_pass(git_oid__fromstr(&executable_oid, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6", GIT_OID_SHA1));
992 993 994 995
	cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));

	cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
	cl_assert(entry = git_index_get_bypath(index, "executable.txt", 0));
996
	cl_assert(GIT_PERMS_IS_EXEC(entry->mode));
997 998

	git_commit_free(commit);
999 1000 1001


	/* Now start with a commit which has a text file */
1002
	cl_git_pass(git_oid__fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9", GIT_OID_SHA1));
1003 1004 1005 1006
	cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));

	cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
	cl_assert(entry = git_index_get_bypath(index, "a/b.txt", 0));
1007
	cl_assert(!GIT_PERMS_IS_EXEC(entry->mode));
1008 1009 1010 1011 1012

	git_commit_free(commit);


	/* And then check out to a commit which converts the text file to an executable */
1013
	cl_git_pass(git_oid__fromstr(&executable_oid, "144344043ba4d4a405da03de3844aa829ae8be0e", GIT_OID_SHA1));
1014 1015 1016 1017
	cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));

	cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
	cl_assert(entry = git_index_get_bypath(index, "a/b.txt", 0));
1018
	cl_assert(GIT_PERMS_IS_EXEC(entry->mode));
1019 1020 1021 1022

	git_commit_free(commit);


1023
	/* Finally, check out the text file again and check that the exec bit is cleared */
1024
	cl_git_pass(git_oid__fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9", GIT_OID_SHA1));
1025 1026 1027 1028
	cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));

	cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
	cl_assert(entry = git_index_get_bypath(index, "a/b.txt", 0));
1029
	cl_assert(!GIT_PERMS_IS_EXEC(entry->mode));
1030 1031 1032 1033

	git_commit_free(commit);


1034 1035
	git_index_free(index);
}
1036

1037 1038
#ifndef GIT_WIN32
static mode_t read_filemode(const char *path)
1039
{
1040
	git_str fullpath = GIT_STR_INIT;
1041
	struct stat st;
1042
	mode_t result;
1043

1044
	git_str_joinpath(&fullpath, "testrepo", path);
1045
	cl_must_pass(p_stat(fullpath.ptr, &st));
1046

1047 1048
	result = GIT_PERMS_IS_EXEC(st.st_mode) ?
		GIT_FILEMODE_BLOB_EXECUTABLE : GIT_FILEMODE_BLOB;
1049

1050
	git_str_dispose(&fullpath);
1051 1052

	return result;
1053
}
1054
#endif
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065

void test_checkout_tree__filemode_preserved_in_workdir(void)
{
#ifndef GIT_WIN32
	git_oid executable_oid;
	git_commit *commit;
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	/* test a freshly added executable */
1066
	cl_git_pass(git_oid__fromstr(&executable_oid, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6", GIT_OID_SHA1));
1067 1068 1069
	cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));

	cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1070
	cl_assert(GIT_PERMS_IS_EXEC(read_filemode("executable.txt")));
1071 1072 1073 1074 1075

	git_commit_free(commit);


	/* Now start with a commit which has a text file */
1076
	cl_git_pass(git_oid__fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9", GIT_OID_SHA1));
1077 1078 1079
	cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));

	cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1080
	cl_assert(!GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
1081 1082 1083 1084 1085

	git_commit_free(commit);


	/* And then check out to a commit which converts the text file to an executable */
1086
	cl_git_pass(git_oid__fromstr(&executable_oid, "144344043ba4d4a405da03de3844aa829ae8be0e", GIT_OID_SHA1));
1087 1088 1089
	cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));

	cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1090
	cl_assert(GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
1091 1092 1093 1094 1095

	git_commit_free(commit);


	/* Finally, check out the text file again and check that the exec bit is cleared */
1096
	cl_git_pass(git_oid__fromstr(&executable_oid, "cf80f8de9f1185bf3a05f993f6121880dd0cfbc9", GIT_OID_SHA1));
1097 1098 1099
	cl_git_pass(git_commit_lookup(&commit, g_repo, &executable_oid));

	cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));
1100
	cl_assert(!GIT_PERMS_IS_EXEC(read_filemode("a/b.txt")));
1101 1102

	git_commit_free(commit);
1103 1104
#else
	cl_skip();
1105 1106 1107
#endif
}

1108 1109 1110 1111 1112 1113
void test_checkout_tree__removes_conflicts(void)
{
	git_oid commit_id;
	git_commit *commit;
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_index *index;
1114

1115
	cl_git_pass(git_oid__fromstr(&commit_id, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6", GIT_OID_SHA1));
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
	cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_remove(index, "executable.txt", 0));

	create_conflict("executable.txt");
	cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");

	create_conflict("other.txt");
	cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");

1131
	cl_git_pass(git_index_write(index));
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142

	cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));

	cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 1));
	cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 2));
	cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 3));

	cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 1));
	cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 2));
	cl_assert_equal_p(NULL, git_index_get_bypath(index, "other.txt", 3));

1143
	cl_assert(!git_fs_path_exists("testrepo/other.txt"));
1144

Carlos Martín Nieto committed
1145
	git_commit_free(commit);
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
	git_index_free(index);
}


void test_checkout_tree__removes_conflicts_only_by_pathscope(void)
{
	git_oid commit_id;
	git_commit *commit;
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_index *index;
	const char *path = "executable.txt";
1157

1158
	cl_git_pass(git_oid__fromstr(&commit_id, "afe4393b2b2a965f06acf2ca9658eaa01e0cd6b6", GIT_OID_SHA1));
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
	cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id));

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
	opts.paths.count = 1;
	opts.paths.strings = (char **)&path;

	cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_remove(index, "executable.txt", 0));

	create_conflict("executable.txt");
	cl_git_mkfile("testrepo/executable.txt", "This is the conflict file.\n");

	create_conflict("other.txt");
	cl_git_mkfile("testrepo/other.txt", "This is another conflict file.\n");

1176
	cl_git_pass(git_index_write(index));
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187

	cl_git_pass(git_checkout_tree(g_repo, (const git_object *)commit, &opts));

	cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 1));
	cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 2));
	cl_assert_equal_p(NULL, git_index_get_bypath(index, "executable.txt", 3));

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

1188
	cl_assert(git_fs_path_exists("testrepo/other.txt"));
1189

Carlos Martín Nieto committed
1190
	git_commit_free(commit);
1191 1192
	git_index_free(index);
}
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216

void test_checkout_tree__case_changing_rename(void)
{
	git_index *index;
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_oid master_id, dir_commit_id, tree_id, commit_id;
	git_commit *master_commit, *dir_commit;
	git_tree *tree;
	git_signature *signature;
	const git_index_entry *index_entry;
	bool case_sensitive;

	assert_on_branch(g_repo, "master");

	cl_git_pass(git_repository_index(&index, g_repo));

	/* Switch branches and perform a case-changing rename */

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	cl_git_pass(git_reference_name_to_id(&dir_commit_id, g_repo, "refs/heads/dir"));
	cl_git_pass(git_commit_lookup(&dir_commit, g_repo, &dir_commit_id));

	cl_git_pass(git_checkout_tree(g_repo, (git_object *)dir_commit, &opts));
1217
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));
1218

1219 1220
	cl_assert(git_fs_path_isfile("testrepo/README"));
	case_sensitive = !git_fs_path_isfile("testrepo/readme");
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239

	cl_assert(index_entry = git_index_get_bypath(index, "README", 0));
	cl_assert_equal_s("README", index_entry->path);

	cl_git_pass(git_index_remove_bypath(index, "README"));
	cl_git_pass(p_rename("testrepo/README", "testrepo/__readme__"));
	cl_git_pass(p_rename("testrepo/__readme__", "testrepo/readme"));
	cl_git_append2file("testrepo/readme", "An addendum...");
	cl_git_pass(git_index_add_bypath(index, "readme"));

	cl_git_pass(git_index_write(index));

	cl_git_pass(git_index_write_tree(&tree_id, index));
	cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));

	cl_git_pass(git_signature_new(&signature, "Renamer", "rename@contoso.com", time(NULL), 0));

	cl_git_pass(git_commit_create(&commit_id, g_repo, "refs/heads/dir", signature, signature, NULL, "case-changing rename", tree, 1, (const git_commit **)&dir_commit));

1240
	cl_assert(git_fs_path_isfile("testrepo/readme"));
1241
	if (case_sensitive)
1242
		cl_assert(!git_fs_path_isfile("testrepo/README"));
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253

	cl_assert(index_entry = git_index_get_bypath(index, "readme", 0));
	cl_assert_equal_s("readme", index_entry->path);

	/* Switching back to master should rename readme -> README */
	opts.checkout_strategy = GIT_CHECKOUT_SAFE;

	cl_git_pass(git_reference_name_to_id(&master_id, g_repo, "refs/heads/master"));
	cl_git_pass(git_commit_lookup(&master_commit, g_repo, &master_id));

	cl_git_pass(git_checkout_tree(g_repo, (git_object *)master_commit, &opts));
1254
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/master"));
1255

1256 1257
	assert_on_branch(g_repo, "master");

1258
	cl_assert(git_fs_path_isfile("testrepo/README"));
1259
	if (case_sensitive)
1260
		cl_assert(!git_fs_path_isfile("testrepo/readme"));
1261 1262 1263 1264

	cl_assert(index_entry = git_index_get_bypath(index, "README", 0));
	cl_assert_equal_s("README", index_entry->path);

1265
	git_index_free(index);
1266 1267 1268 1269 1270
	git_signature_free(signature);
	git_tree_free(tree);
	git_commit_free(dir_commit);
	git_commit_free(master_commit);
}
1271

1272
static void perfdata_cb(const git_checkout_perfdata *in, void *payload)
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
{
	memcpy(payload, in, sizeof(git_checkout_perfdata));
}

void test_checkout_tree__can_collect_perfdata(void)
{
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_oid oid;
	git_object *obj = NULL;
	git_checkout_perfdata perfdata = {0};

	opts.perfdata_cb = perfdata_cb;
	opts.perfdata_payload = &perfdata;

	assert_on_branch(g_repo, "master");
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
1291
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
1292 1293 1294 1295 1296 1297 1298 1299

	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));

	cl_assert(perfdata.mkdir_calls > 0);
	cl_assert(perfdata.stat_calls > 0);

	git_object_free(obj);
}
1300

1301
static void update_attr_callback(
1302 1303 1304 1305 1306
	const char *path,
	size_t completed_steps,
	size_t total_steps,
	void *payload)
{
1307 1308 1309 1310
	GIT_UNUSED(completed_steps);
	GIT_UNUSED(total_steps);
	GIT_UNUSED(payload);

1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
	if (path && strcmp(path, "ident1.txt") == 0)
		cl_git_write2file("testrepo/.gitattributes",
			"*.txt ident\n", 12, O_RDWR|O_CREAT, 0666);
}

void test_checkout_tree__caches_attributes_during_checkout(void)
{
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_oid oid;
	git_object *obj = NULL;
1321
	git_str ident1 = GIT_STR_INIT, ident2 = GIT_STR_INIT;
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
	char *ident_paths[] = { "ident1.txt", "ident2.txt" };

	opts.progress_cb = update_attr_callback;

	assert_on_branch(g_repo, "master");
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
	opts.paths.strings = ident_paths;
	opts.paths.count = 2;

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/ident"));
1332
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349

	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));

	cl_git_pass(git_futils_readbuffer(&ident1, "testrepo/ident1.txt"));
	cl_git_pass(git_futils_readbuffer(&ident2, "testrepo/ident2.txt"));

	cl_assert_equal_strn(ident1.ptr, "# $Id$", 6);
	cl_assert_equal_strn(ident2.ptr, "# $Id$", 6);

	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));

	cl_git_pass(git_futils_readbuffer(&ident1, "testrepo/ident1.txt"));
	cl_git_pass(git_futils_readbuffer(&ident2, "testrepo/ident2.txt"));

	cl_assert_equal_strn(ident1.ptr, "# $Id: ", 7);
	cl_assert_equal_strn(ident2.ptr, "# $Id: ", 7);

1350 1351
	git_str_dispose(&ident1);
	git_str_dispose(&ident2);
1352 1353
	git_object_free(obj);
}
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366

void test_checkout_tree__can_not_update_index(void)
{
	git_oid oid;
	git_object *head;
	unsigned int status;
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_index *index;

	opts.checkout_strategy |=
		GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_UPDATE_INDEX;

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
1367
	cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJECT_ANY));
1368

1369
	cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts));
1370

1371
	cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
1372 1373 1374 1375 1376

	cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));

	cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));

1377
	cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/2.txt"));
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
	cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
	cl_assert_equal_i(GIT_STATUS_WT_NEW, status);

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

	cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
	cl_assert_equal_i(GIT_STATUS_WT_NEW, status);

	git_object_free(head);
	git_index_free(index);
}

void test_checkout_tree__can_update_but_not_write_index(void)
{
	git_oid oid;
	git_object *head;
	unsigned int status;
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_index *index;
	git_repository *other;

	opts.checkout_strategy |=
		GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_WRITE_INDEX;

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
1404
	cl_git_pass(git_object_lookup(&head, g_repo, &oid, GIT_OBJECT_ANY));
1405

1406
	cl_git_pass(git_reset(g_repo, head, GIT_RESET_HARD, &g_opts));
1407

1408
	cl_assert_equal_i(false, git_fs_path_isdir("./testrepo/ab/"));
1409 1410 1411 1412 1413

	cl_git_pass(git_revparse_single(&g_object, g_repo, "subtrees"));

	cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));

1414
	cl_assert_equal_i(true, git_fs_path_isfile("./testrepo/ab/de/2.txt"));
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
	cl_git_pass(git_status_file(&status, g_repo, "ab/de/2.txt"));
	cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status);

	cl_git_pass(git_repository_open(&other, "testrepo"));
	cl_git_pass(git_status_file(&status, other, "ab/de/2.txt"));
	cl_assert_equal_i(GIT_STATUS_WT_NEW, status);
	git_repository_free(other);

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

	cl_git_pass(git_repository_open(&other, "testrepo"));
	cl_git_pass(git_status_file(&status, other, "ab/de/2.txt"));
	cl_assert_equal_i(GIT_STATUS_INDEX_NEW, status);
	git_repository_free(other);

	git_object_free(head);
	git_index_free(index);
}
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449

/* Emulate checking out in a repo created by clone --no-checkout,
 * which would not have written an index. */
void test_checkout_tree__safe_proceeds_if_no_index(void)
{
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_oid oid;
	git_object *obj = NULL;

	assert_on_branch(g_repo, "master");
	cl_must_pass(p_unlink("testrepo/.git/index"));

	/* do second checkout safe because we should be clean after first */
	opts.checkout_strategy = GIT_CHECKOUT_SAFE;

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/subtrees"));
1450
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
1451 1452

	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
1453
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/subtrees"));
1454

1455 1456 1457 1458 1459 1460 1461
	cl_assert(git_fs_path_isfile("testrepo/README"));
	cl_assert(git_fs_path_isfile("testrepo/branch_file.txt"));
	cl_assert(git_fs_path_isfile("testrepo/new.txt"));
	cl_assert(git_fs_path_isfile("testrepo/ab/4.txt"));
	cl_assert(git_fs_path_isfile("testrepo/ab/c/3.txt"));
	cl_assert(git_fs_path_isfile("testrepo/ab/de/2.txt"));
	cl_assert(git_fs_path_isfile("testrepo/ab/de/fgh/1.txt"));
1462

1463
	cl_assert(!git_fs_path_isdir("testrepo/a"));
1464 1465 1466 1467 1468 1469

	assert_on_branch(g_repo, "subtrees");

	git_object_free(obj);
}

1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
static int checkout_conflict_count_cb(
	git_checkout_notify_t why,
	const char *path,
	const git_diff_file *b,
	const git_diff_file *t,
	const git_diff_file *w,
	void *payload)
{
	size_t *n = payload;

	GIT_UNUSED(why);
	GIT_UNUSED(path);
	GIT_UNUSED(b);
	GIT_UNUSED(t);
	GIT_UNUSED(w);

	(*n)++;

	return 0;
}

/* A repo that has a HEAD (even a properly born HEAD that peels to
 * a commit) but no index should be treated as if it's an empty baseline
 */
void test_checkout_tree__baseline_is_empty_when_no_index(void)
{
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_reference *head;
	git_object *obj;
	size_t conflicts = 0;

	assert_on_branch(g_repo, "master");
1502

1503
	cl_git_pass(git_repository_head(&head, g_repo));
1504
	cl_git_pass(git_reference_peel(&obj, head, GIT_OBJECT_COMMIT));
1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524

	cl_git_pass(git_reset(g_repo, obj, GIT_RESET_HARD, NULL));

	cl_must_pass(p_unlink("testrepo/.git/index"));

	/* for a safe checkout, we should have checkout conflicts with
	 * the existing untracked files.
	 */
	opts.checkout_strategy &= ~GIT_CHECKOUT_FORCE;
	opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
	opts.notify_cb = checkout_conflict_count_cb;
	opts.notify_payload = &conflicts;

	cl_git_fail_with(GIT_ECONFLICT, git_checkout_tree(g_repo, obj, &opts));
	cl_assert_equal_i(4, conflicts);

	/* but force should succeed and update the index */
	opts.checkout_strategy |= GIT_CHECKOUT_FORCE;
	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));

1525
	assert_status_entrycount(g_repo, 0);
1526 1527 1528 1529 1530

	git_object_free(obj);
	git_reference_free(head);
}

1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
void test_checkout_tree__mode_change_is_force_updated(void)
{
	git_index *index;
	git_reference *head;
	git_object *obj;

	if (!cl_is_chmod_supported())
		clar__skip();

	assert_on_branch(g_repo, "master");
	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_repository_head(&head, g_repo));
1543
	cl_git_pass(git_reference_peel(&obj, head, GIT_OBJECT_COMMIT));
1544 1545

	cl_git_pass(git_reset(g_repo, obj, GIT_RESET_HARD, NULL));
1546
	assert_status_entrycount(g_repo, 0);
1547 1548 1549 1550

	/* update the mode on-disk */
	cl_must_pass(p_chmod("testrepo/README", 0755));

1551
	assert_status_entrycount(g_repo, 1);
1552
	cl_git_pass(git_checkout_tree(g_repo, obj, &g_opts));
1553
	assert_status_entrycount(g_repo, 0);
1554 1555 1556 1557 1558

	/* update the mode on-disk and in the index */
	cl_must_pass(p_chmod("testrepo/README", 0755));
	cl_must_pass(git_index_add_bypath(index, "README"));

1559
	cl_git_pass(git_index_write(index));
1560
	assert_status_entrycount(g_repo, 1);
1561

1562
	cl_git_pass(git_checkout_tree(g_repo, obj, &g_opts));
1563 1564
	cl_git_pass(git_index_write(index));

1565
	assert_status_entrycount(g_repo, 0);
1566 1567 1568 1569 1570 1571

	git_object_free(obj);
	git_reference_free(head);
	git_index_free(index);
}

1572 1573 1574 1575
void test_checkout_tree__nullopts(void)
{
	cl_git_pass(git_checkout_tree(g_repo, NULL, NULL));
}
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585

static void modify_index_ondisk(void)
{
	git_repository *other_repo;
	git_index *other_index;
	git_index_entry entry = {{0}};

	cl_git_pass(git_repository_open(&other_repo, git_repository_workdir(g_repo)));
	cl_git_pass(git_repository_index(&other_index, other_repo));

1586
	cl_git_pass(git_oid__fromstr(&entry.id, "1385f264afb75a56a5bec74243be9b367ba4ca08", GIT_OID_SHA1));
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
	entry.mode = 0100644;
	entry.path = "README";

	cl_git_pass(git_index_add(other_index, &entry));
	cl_git_pass(git_index_write(other_index));

	git_index_free(other_index);
	git_repository_free(other_repo);
}

static void modify_index_and_checkout_tree(git_checkout_options *opts)
{
	git_index *index;
	git_reference *head;
	git_object *obj;

	/* External changes to the index are maintained by default */
	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_repository_head(&head, g_repo));
1606
	cl_git_pass(git_reference_peel(&obj, head, GIT_OBJECT_COMMIT));
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639

	cl_git_pass(git_reset(g_repo, obj, GIT_RESET_HARD, NULL));
	assert_status_entrycount(g_repo, 0);

	modify_index_ondisk();

	/* The file in the index remains modified */
	cl_git_pass(git_checkout_tree(g_repo, obj, opts));

	git_object_free(obj);
	git_reference_free(head);
	git_index_free(index);
}

void test_checkout_tree__retains_external_index_changes(void)
{
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;

	opts.checkout_strategy = GIT_CHECKOUT_SAFE;

	modify_index_and_checkout_tree(&opts);
	assert_status_entrycount(g_repo, 1);
}

void test_checkout_tree__no_index_refresh(void)
{
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;

	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_NO_REFRESH;

	modify_index_and_checkout_tree(&opts);
	assert_status_entrycount(g_repo, 0);
}
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654

void test_checkout_tree__dry_run(void)
{
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	git_oid oid;
	git_object *obj = NULL;
	checkout_counts ct;

	/* first let's get things into a known state - by checkout out the HEAD */

	assert_on_branch(g_repo, "master");

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
	cl_git_pass(git_checkout_head(g_repo, &opts));

1655
	cl_assert(!git_fs_path_isdir("testrepo/a"));
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677

	check_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n");

	/* now checkout branch but with dry run enabled */

	memset(&ct, 0, sizeof(ct));
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_DRY_RUN;
	opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
	opts.notify_cb = checkout_count_callback;
	opts.notify_payload = &ct;

	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
	cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));

	cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/dir"));

	assert_on_branch(g_repo, "dir");

	/* these normally would have been created and updated, but with
	 * DRY_RUN they will be unchanged.
	 */
1678
	cl_assert(!git_fs_path_isdir("testrepo/a"));
1679 1680 1681 1682
	check_file_contents_nocr("testrepo/branch_file.txt", "hi\nbye!\n");

	/* check that notify callback was invoked */
	cl_assert_equal_i(ct.n_updates, 2);
1683

1684 1685
	git_object_free(obj);
}