index.c 14.1 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "checkout_helpers.h"
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

#include "git2/checkout.h"
#include "repository.h"

static git_repository *g_repo;

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

void test_checkout_index__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

void test_checkout_index__cannot_checkout_a_bare_repository(void)
{
	test_checkout_index__cleanup();

	g_repo = cl_git_sandbox_init("testrepo.git");

36
	cl_git_fail(git_checkout_index(g_repo, NULL, NULL));
37 38
}

39
void test_checkout_index__can_create_missing_files(void)
40
{
41 42
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;

43 44 45 46
	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"));

47
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
48

49
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
50 51 52 53 54 55

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

56 57
void test_checkout_index__can_remove_untracked_files(void)
{
58 59
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;

60 61 62 63 64 65
	git_futils_mkdir("./testrepo/dir/subdir/subsubdir", NULL, 0755, GIT_MKDIR_PATH);
	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"));

66
	opts.checkout_strategy =
67 68
		GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_REMOVE_UNTRACKED;

69
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
70 71 72 73

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

74 75
void test_checkout_index__honor_the_specified_pathspecs(void)
{
76
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
77 78
	char *entries[] = { "*.txt" };

79 80
	opts.paths.strings = entries;
	opts.paths.count = 1;
81 82 83 84 85

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

86
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
87

88
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

	cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
	test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
	test_file_contents("./testrepo/new.txt", "my new file\n");
}

static void set_config_entry_to(const char *entry_name, bool value)
{
	git_config *cfg;

	cl_git_pass(git_repository_config(&cfg, g_repo));
	cl_git_pass(git_config_set_bool(cfg, entry_name, value));

	git_config_free(cfg);
}

static void set_core_autocrlf_to(bool value)
{
	set_config_entry_to("core.autocrlf", value);
}

void test_checkout_index__honor_the_gitattributes_directives(void)
{
112
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
113 114 115 116 117 118 119
	const char *attributes =
		"branch_file.txt text eol=crlf\n"
		"new.txt text eol=lf\n";

	cl_git_mkfile("./testrepo/.gitattributes", attributes);
	set_core_autocrlf_to(false);

120
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
121

122
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
123 124 125 126 127 128 129 130 131

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

void test_checkout_index__honor_coreautocrlf_setting_set_to_true(void)
{
#ifdef GIT_WIN32
132
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
133 134 135 136 137
	const char *expected_readme_text = "hey there\r\n";

	cl_git_pass(p_unlink("./testrepo/.gitattributes"));
	set_core_autocrlf_to(true);

138
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
139

140
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
141 142 143 144 145 146 147 148 149 150 151 152

	test_file_contents("./testrepo/README", expected_readme_text);
#endif
}

static void set_repo_symlink_handling_cap_to(bool value)
{
	set_config_entry_to("core.symlinks", value);
}

void test_checkout_index__honor_coresymlinks_setting_set_to_true(void)
{
153 154
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;

155 156
	set_repo_symlink_handling_cap_to(true);

157
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
158

159
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

#ifdef GIT_WIN32
	test_file_contents("./testrepo/link_to_new.txt", "new.txt");
#else
	{
		char link_data[1024];
		size_t link_size = 1024;

		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");
		test_file_contents("./testrepo/link_to_new.txt", "my new file\n");
	}
#endif
}

void test_checkout_index__honor_coresymlinks_setting_set_to_false(void)
{
179 180
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;

181 182
	set_repo_symlink_handling_cap_to(false);

183
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
184

185
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
186 187 188 189

	test_file_contents("./testrepo/link_to_new.txt", "new.txt");
}

190
void test_checkout_index__donot_overwrite_modified_file_by_default(void)
191
{
192 193
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;

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

196 197 198
	/* set this up to not return an error code on conflicts, but it
	 * still will not have permission to overwrite anything...
	 */
199
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_ALLOW_CONFLICTS;
200

201
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
202 203 204 205

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

206
void test_checkout_index__can_overwrite_modified_file(void)
207
{
208 209
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;

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

212
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
213

214
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
215 216 217 218 219 220

	test_file_contents("./testrepo/new.txt", "my new file\n");
}

void test_checkout_index__options_disable_filters(void)
{
221 222
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;

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

225 226
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
	opts.disable_filters = false;
227

228
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
229 230 231 232 233

	test_file_contents("./testrepo/new.txt", "my new file\r\n");

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

234 235
	opts.disable_filters = true;
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
236 237 238 239 240 241 242

	test_file_contents("./testrepo/new.txt", "my new file\n");
}

void test_checkout_index__options_dir_modes(void)
{
#ifndef GIT_WIN32
243
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
244 245 246 247
	struct stat st;
	git_oid oid;
	git_commit *commit;

248
	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
249 250 251 252
	cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));

	reset_index_to_treeish((git_object *)commit);

253 254
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
	opts.dir_mode = 0701;
255

256
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

	cl_git_pass(p_stat("./testrepo/a", &st));
	cl_assert_equal_i(st.st_mode & 0777, 0701);

	/* File-mode test, since we're on the 'dir' branch */
	cl_git_pass(p_stat("./testrepo/a/b.txt", &st));
	cl_assert_equal_i(st.st_mode & 0777, 0755);

	git_commit_free(commit);
#endif
}

void test_checkout_index__options_override_file_modes(void)
{
#ifndef GIT_WIN32
272
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
273 274
	struct stat st;

275 276
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
	opts.file_mode = 0700;
277

278
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
279 280 281 282 283 284 285 286

	cl_git_pass(p_stat("./testrepo/new.txt", &st));
	cl_assert_equal_i(st.st_mode & 0777, 0700);
#endif
}

void test_checkout_index__options_open_flags(void)
{
287 288
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;

289 290
	cl_git_mkfile("./testrepo/new.txt", "hi\n");

291 292
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
	opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND;
293

294 295
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
296 297 298

	test_file_contents("./testrepo/new.txt", "hi\nmy new file\n");
}
299

300
struct notify_data {
301 302 303 304
	const char *file;
	const char *sha;
};

305 306 307 308 309 310
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,
311 312
	void *payload)
{
313
	struct notify_data *expectations = (struct notify_data *)payload;
314

315
	GIT_UNUSED(workdir);
316

317 318 319 320
	cl_assert_equal_i(GIT_CHECKOUT_NOTIFY_CONFLICT, why);
	cl_assert_equal_s(expectations->file, path);
	cl_assert_equal_i(0, git_oid_streq(&baseline->oid, expectations->sha));
	cl_assert_equal_i(0, git_oid_streq(&target->oid, expectations->sha));
321 322 323 324 325 326

	return 0;
}

void test_checkout_index__can_notify_of_skipped_files(void)
{
327
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
328
	struct notify_data data;
329 330 331 332 333 334 335 336 337 338 339 340

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

341
	opts.checkout_strategy =
342
		GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_ALLOW_CONFLICTS;
343 344 345
	opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
	opts.notify_cb = test_checkout_notify_cb;
	opts.notify_payload = &data;
346

347
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
348 349
}

350
static int dont_notify_cb(
351 352 353 354 355
	git_checkout_notify_t why,
	const char *path,
	const git_diff_file *baseline,
	const git_diff_file *target,
	const git_diff_file *workdir,
356 357
	void *payload)
{
358 359 360 361 362
	GIT_UNUSED(why);
	GIT_UNUSED(path);
	GIT_UNUSED(baseline);
	GIT_UNUSED(target);
	GIT_UNUSED(workdir);
363 364 365 366 367 368 369 370 371
	GIT_UNUSED(payload);

	cl_assert(false);

	return 0;
}

void test_checkout_index__wont_notify_of_expected_line_ending_changes(void)
{
372 373
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;

374 375
	cl_git_pass(p_unlink("./testrepo/.gitattributes"));
	set_core_autocrlf_to(true);
376

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

379
	opts.checkout_strategy =
380
		GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_ALLOW_CONFLICTS;
381 382 383
	opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
	opts.notify_cb = dont_notify_cb;
	opts.notify_payload = NULL;
384

385
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
386 387
}

388 389
static void checkout_progress_counter(
	const char *path, size_t cur, size_t tot, void *payload)
390
{
Ben Straub committed
391
	GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
392
	(*(int *)payload)++;
393 394 395 396
}

void test_checkout_index__calls_progress_callback(void)
{
397
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
398 399
	int calls = 0;

400 401 402
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
	opts.progress_cb = checkout_progress_counter;
	opts.progress_payload = &calls;
403

404
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
405
	cl_assert(calls > 0);
406
}
407 408 409

void test_checkout_index__can_overcome_name_clashes(void)
{
410
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
411 412 413 414 415 416 417 418 419
	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");

420 421
	cl_git_pass(git_index_add_bypath(index, "path0"));
	cl_git_pass(git_index_add_bypath(index, "path1/file1"));
422

423 424 425 426 427 428 429 430
	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");

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

434
	opts.checkout_strategy =
435
		GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_ALLOW_CONFLICTS;
436
	cl_git_pass(git_checkout_index(g_repo, index, &opts));
437 438 439 440

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

441 442
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
	cl_git_pass(git_checkout_index(g_repo, index, &opts));
443 444 445 446

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

447 448
	git_index_free(index);
}
449 450 451

void test_checkout_index__validates_struct_version(void)
{
452
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
453 454
	const git_error *err;

455 456
	opts.version = 1024;
	cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
457 458 459 460

	err = giterr_last();
	cl_assert_equal_i(err->klass, GITERR_INVALID);

461
	opts.version = 0;
462
	giterr_clear();
463
	cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
464 465 466 467

	err = giterr_last();
	cl_assert_equal_i(err->klass, GITERR_INVALID);
}
468 469 470 471 472

void test_checkout_index__can_update_prefixed_files(void)
{
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;

473 474 475 476
	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"));

477 478 479 480 481 482 483
	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));

484 485
	opts.checkout_strategy =
		GIT_CHECKOUT_SAFE_CREATE | GIT_CHECKOUT_REMOVE_UNTRACKED;
486 487 488

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

489 490 491 492 493 494
	/* remove untracked will remove the .gitattributes file before the blobs
	 * were created, so they will have had crlf filtering applied on Windows
	 */
	test_file_contents_nocr("./testrepo/README", "hey there\n");
	test_file_contents_nocr("./testrepo/branch_file.txt", "hi\nbye!\n");
	test_file_contents_nocr("./testrepo/new.txt", "my new file\n");
495 496 497 498 499 500

	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"));
}
501 502 503 504 505 506 507 508 509 510

void test_checkout_index__can_checkout_a_newly_initialized_repository(void)
{
	test_checkout_index__cleanup();

	g_repo = cl_git_sandbox_init("empty_standard_repo");
	cl_git_remove_placeholders(git_repository_path(g_repo), "dummy-marker.txt");

	cl_git_pass(git_checkout_index(g_repo, NULL, NULL));
}