status.c 37 KB
Newer Older
1
#include "clar_libgit2.h"
2 3
#include "fileops.h"
#include "git2/attr.h"
4
#include "ignore.h"
5
#include "attr.h"
6
#include "status/status_helpers.h"
7 8 9

static git_repository *g_repo = NULL;

10
void test_ignore_status__initialize(void)
11 12 13
{
}

14
void test_ignore_status__cleanup(void)
15
{
16
	cl_git_sandbox_cleanup();
17 18
}

19 20 21 22
static void assert_ignored_(
	bool expected, const char *filepath, const char *file, int line)
{
	int is_ignored = 0;
23
	cl_git_expect(
24
		git_status_should_ignore(&is_ignored, g_repo, filepath), 0, file, line);
25 26 27 28 29 30 31 32 33 34 35
	clar__assert(
		(expected != 0) == (is_ignored != 0),
		file, line, "expected != is_ignored", filepath, 1);
}
#define assert_ignored(expected, filepath) \
	assert_ignored_(expected, filepath, __FILE__, __LINE__)
#define assert_is_ignored(filepath) \
	assert_ignored_(true, filepath, __FILE__, __LINE__)
#define refute_is_ignored(filepath) \
	assert_ignored_(false, filepath, __FILE__, __LINE__)

36
void test_ignore_status__0(void)
37 38 39 40 41
{
	struct {
		const char *path;
		int expected;
	} test_cases[] = {
42
		/* pattern "ign" from .gitignore */
43 44
		{ "file", 0 },
		{ "ign", 1 },
45
		{ "sub", 0 },
46 47
		{ "sub/file", 0 },
		{ "sub/ign", 1 },
48 49 50 51
		{ "sub/ign/file", 1 },
		{ "sub/ign/sub", 1 },
		{ "sub/ign/sub/file", 1 },
		{ "sub/sub", 0 },
52 53
		{ "sub/sub/file", 0 },
		{ "sub/sub/ign", 1 },
54
		{ "sub/sub/sub", 0 },
55 56 57 58 59
		/* pattern "dir/" from .gitignore */
		{ "dir", 1 },
		{ "dir/", 1 },
		{ "sub/dir", 1 },
		{ "sub/dir/", 1 },
60
		{ "sub/dir/file", 1 }, /* contained in ignored parent */
61
		{ "sub/sub/dir", 0 }, /* dir is not actually a dir, but a file */
62 63 64
		{ NULL, 0 }
	}, *one_test;

65 66
	g_repo = cl_git_sandbox_init("attr");

67 68
	for (one_test = test_cases; one_test->path != NULL; one_test++)
		assert_ignored(one_test->expected, one_test->path);
69 70

	/* confirm that ignore files were cached */
71 72 73 74
	cl_assert(git_attr_cache__is_cached(
		g_repo, GIT_ATTR_FILE__FROM_FILE, ".git/info/exclude"));
	cl_assert(git_attr_cache__is_cached(
		g_repo, GIT_ATTR_FILE__FROM_FILE, ".gitignore"));
75
}
76 77


78
void test_ignore_status__1(void)
79
{
80 81
	g_repo = cl_git_sandbox_init("attr");

82 83 84
	cl_git_rewritefile("attr/.gitignore", "/*.txt\n/dir/\n");
	git_attr_cache_flush(g_repo);

85 86 87 88 89 90
	assert_is_ignored("root_test4.txt");
	refute_is_ignored("sub/subdir_test2.txt");
	assert_is_ignored("dir");
	assert_is_ignored("dir/");
	refute_is_ignored("sub/dir");
	refute_is_ignored("sub/dir/");
91 92
}

93
void test_ignore_status__empty_repo_with_gitignore_rewrite(void)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
{
	status_entry_single st;

	g_repo = cl_git_sandbox_init("empty_standard_repo");

	cl_git_mkfile(
		"empty_standard_repo/look-ma.txt", "I'm going to be ignored!");

	memset(&st, 0, sizeof(st));
	cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st));
	cl_assert(st.count == 1);
	cl_assert(st.status == GIT_STATUS_WT_NEW);

	cl_git_pass(git_status_file(&st.status, g_repo, "look-ma.txt"));
	cl_assert(st.status == GIT_STATUS_WT_NEW);

110
	refute_is_ignored("look-ma.txt");
111

112 113 114 115 116 117 118 119 120 121
	cl_git_rewritefile("empty_standard_repo/.gitignore", "*.nomatch\n");

	memset(&st, 0, sizeof(st));
	cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st));
	cl_assert(st.count == 2);
	cl_assert(st.status == GIT_STATUS_WT_NEW);

	cl_git_pass(git_status_file(&st.status, g_repo, "look-ma.txt"));
	cl_assert(st.status == GIT_STATUS_WT_NEW);

122
	refute_is_ignored("look-ma.txt");
123 124 125 126 127 128 129 130 131 132 133

	cl_git_rewritefile("empty_standard_repo/.gitignore", "*.txt\n");

	memset(&st, 0, sizeof(st));
	cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st));
	cl_assert(st.count == 2);
	cl_assert(st.status == GIT_STATUS_IGNORED);

	cl_git_pass(git_status_file(&st.status, g_repo, "look-ma.txt"));
	cl_assert(st.status == GIT_STATUS_IGNORED);

134
	assert_is_ignored("look-ma.txt");
135 136
}

137
void test_ignore_status__ignore_pattern_contains_space(void)
138 139 140 141 142 143 144
{
	unsigned int flags;
	const mode_t mode = 0777;

	g_repo = cl_git_sandbox_init("empty_standard_repo");
	cl_git_rewritefile("empty_standard_repo/.gitignore", "foo bar.txt\n");

145 146 147 148 149 150
	cl_git_mkfile(
		"empty_standard_repo/foo bar.txt", "I'm going to be ignored!");

	cl_git_pass(git_status_file(&flags, g_repo, "foo bar.txt"));
	cl_assert(flags == GIT_STATUS_IGNORED);

151
	cl_git_pass(git_futils_mkdir_r("empty_standard_repo/foo", mode));
152 153 154 155 156
	cl_git_mkfile("empty_standard_repo/foo/look-ma.txt", "I'm not going to be ignored!");

	cl_git_pass(git_status_file(&flags, g_repo, "foo/look-ma.txt"));
	cl_assert(flags == GIT_STATUS_WT_NEW);
}
157

158
void test_ignore_status__ignore_pattern_ignorecase(void)
159 160 161 162 163 164 165 166 167 168 169
{
	unsigned int flags;
	bool ignore_case;
	git_index *index;

	g_repo = cl_git_sandbox_init("empty_standard_repo");
	cl_git_rewritefile("empty_standard_repo/.gitignore", "a.txt\n");

	cl_git_mkfile("empty_standard_repo/A.txt", "Differs in case");

	cl_git_pass(git_repository_index(&index, g_repo));
170
	ignore_case = (git_index_caps(index) & GIT_INDEX_CAPABILITY_IGNORE_CASE) != 0;
171 172 173 174 175
	git_index_free(index);

	cl_git_pass(git_status_file(&flags, g_repo, "A.txt"));
	cl_assert(flags == ignore_case ? GIT_STATUS_IGNORED : GIT_STATUS_WT_NEW);
}
176

177
void test_ignore_status__subdirectories(void)
178 179
{
	status_entry_single st;
180

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	g_repo = cl_git_sandbox_init("empty_standard_repo");

	cl_git_mkfile(
		"empty_standard_repo/ignore_me", "I'm going to be ignored!");

	cl_git_rewritefile("empty_standard_repo/.gitignore", "ignore_me\n");

	memset(&st, 0, sizeof(st));
	cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st));
	cl_assert_equal_i(2, st.count);
	cl_assert(st.status == GIT_STATUS_IGNORED);

	cl_git_pass(git_status_file(&st.status, g_repo, "ignore_me"));
	cl_assert(st.status == GIT_STATUS_IGNORED);

196
	assert_is_ignored("ignore_me");
197

198 199 200 201 202 203 204 205 206
	/* I've changed libgit2 so that the behavior here now differs from
	 * core git but seems to make more sense.  In core git, the following
	 * items are skipped completed, even if --ignored is passed to status.
	 * It you mirror these steps and run "git status -uall --ignored" then
	 * you will not see "test/ignore_me/" in the results.
	 *
	 * However, we had a couple reports of this as a bug, plus there is a
	 * similar circumstance where we were differing for core git when you
	 * used a rooted path for an ignore, so I changed this behavior.
207
	 */
208
	cl_git_pass(git_futils_mkdir_r(
209
		"empty_standard_repo/test/ignore_me", 0775));
210 211
	cl_git_mkfile(
		"empty_standard_repo/test/ignore_me/file", "I'm going to be ignored!");
212 213
	cl_git_mkfile(
		"empty_standard_repo/test/ignore_me/file2", "Me, too!");
214 215 216

	memset(&st, 0, sizeof(st));
	cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st));
217
	cl_assert_equal_i(3, st.count);
218 219 220 221

	cl_git_pass(git_status_file(&st.status, g_repo, "test/ignore_me/file"));
	cl_assert(st.status == GIT_STATUS_IGNORED);

222
	assert_is_ignored("test/ignore_me/file");
223 224
}

225
static void make_test_data(const char *reponame, const char **files)
226 227
{
	const char **scan;
228
	size_t repolen = strlen(reponame) + 1;
229

230
	g_repo = cl_git_sandbox_init(reponame);
231 232

	for (scan = files; *scan != NULL; ++scan) {
233
		cl_git_pass(git_futils_mkdir_relative(
234
			*scan + repolen, reponame,
235
			0777, GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST, NULL));
236 237 238 239
		cl_git_mkfile(*scan, "contents");
	}
}

240 241 242 243 244 245 246 247 248 249 250 251 252
static const char *test_repo_1 = "empty_standard_repo";
static const char *test_files_1[] = {
	"empty_standard_repo/dir/a/ignore_me",
	"empty_standard_repo/dir/b/ignore_me",
	"empty_standard_repo/dir/ignore_me",
	"empty_standard_repo/ignore_also/file",
	"empty_standard_repo/ignore_me",
	"empty_standard_repo/test/ignore_me/file",
	"empty_standard_repo/test/ignore_me/file2",
	"empty_standard_repo/test/ignore_me/and_me/file",
	NULL
};

253
void test_ignore_status__subdirectories_recursion(void)
254 255 256 257
{
	/* Let's try again with recursing into ignored dirs turned on */
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
	status_entry_counts counts;
258
	static const char *paths_r[] = {
259
		".gitignore",
260 261 262
		"dir/a/ignore_me",
		"dir/b/ignore_me",
		"dir/ignore_me",
263
		"ignore_also/file",
264 265 266 267 268
		"ignore_me",
		"test/ignore_me/and_me/file",
		"test/ignore_me/file",
		"test/ignore_me/file2",
	};
269
	static const unsigned int statuses_r[] = {
270 271 272
		GIT_STATUS_WT_NEW,  GIT_STATUS_IGNORED, GIT_STATUS_IGNORED,
		GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED,
		GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED,
273 274 275
	};
	static const char *paths_nr[] = {
		".gitignore",
276 277 278
		"dir/a/ignore_me",
		"dir/b/ignore_me",
		"dir/ignore_me",
279 280 281 282 283 284
		"ignore_also/",
		"ignore_me",
		"test/ignore_me/",
	};
	static const unsigned int statuses_nr[] = {
		GIT_STATUS_WT_NEW,
285 286
		GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED,
		GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED,
287 288
	};

289
	make_test_data(test_repo_1, test_files_1);
290
	cl_git_rewritefile("empty_standard_repo/.gitignore", "ignore_me\n/ignore_also\n");
291

292
	memset(&counts, 0x0, sizeof(status_entry_counts));
293
	counts.expected_entry_count = 9;
294 295 296 297 298 299 300 301 302 303 304 305
	counts.expected_paths = paths_r;
	counts.expected_statuses = statuses_r;

	opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_RECURSE_IGNORED_DIRS;

	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__normal, &counts));

	cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
	cl_assert_equal_i(0, counts.wrong_status_flags_count);
	cl_assert_equal_i(0, counts.wrong_sorted_path);

306 307

	memset(&counts, 0x0, sizeof(status_entry_counts));
308
	counts.expected_entry_count = 7;
309 310 311 312
	counts.expected_paths = paths_nr;
	counts.expected_statuses = statuses_nr;

	opts.flags = GIT_STATUS_OPT_DEFAULTS;
313 314 315 316 317 318 319 320 321

	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__normal, &counts));

	cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
	cl_assert_equal_i(0, counts.wrong_status_flags_count);
	cl_assert_equal_i(0, counts.wrong_sorted_path);
}

322
void test_ignore_status__subdirectories_not_at_root(void)
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
{
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
	status_entry_counts counts;
	static const char *paths_1[] = {
		"dir/.gitignore",
		"dir/a/ignore_me",
		"dir/b/ignore_me",
		"dir/ignore_me",
		"ignore_also/file",
		"ignore_me",
		"test/.gitignore",
		"test/ignore_me/and_me/file",
		"test/ignore_me/file",
		"test/ignore_me/file2",
	};
	static const unsigned int statuses_1[] = {
		GIT_STATUS_WT_NEW,  GIT_STATUS_IGNORED, GIT_STATUS_IGNORED,
		GIT_STATUS_IGNORED, GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW,
		GIT_STATUS_WT_NEW, GIT_STATUS_IGNORED, GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW,
	};

344
	make_test_data(test_repo_1, test_files_1);
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	cl_git_rewritefile("empty_standard_repo/dir/.gitignore", "ignore_me\n/ignore_also\n");
	cl_git_rewritefile("empty_standard_repo/test/.gitignore", "and_me\n");

	memset(&counts, 0x0, sizeof(status_entry_counts));
	counts.expected_entry_count = 10;
	counts.expected_paths = paths_1;
	counts.expected_statuses = statuses_1;

	opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_RECURSE_IGNORED_DIRS;

	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__normal, &counts));

	cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
	cl_assert_equal_i(0, counts.wrong_status_flags_count);
	cl_assert_equal_i(0, counts.wrong_sorted_path);
}

363
void test_ignore_status__leading_slash_ignores(void)
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
{
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
	status_entry_counts counts;
	static const char *paths_2[] = {
		"dir/.gitignore",
		"dir/a/ignore_me",
		"dir/b/ignore_me",
		"dir/ignore_me",
		"ignore_also/file",
		"ignore_me",
		"test/.gitignore",
		"test/ignore_me/and_me/file",
		"test/ignore_me/file",
		"test/ignore_me/file2",
	};
	static const unsigned int statuses_2[] = {
		GIT_STATUS_WT_NEW,  GIT_STATUS_WT_NEW,  GIT_STATUS_WT_NEW,
		GIT_STATUS_IGNORED, GIT_STATUS_IGNORED, GIT_STATUS_IGNORED,
		GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW, GIT_STATUS_WT_NEW,
	};

385
	make_test_data(test_repo_1, test_files_1);
386

387
	cl_fake_home();
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	cl_git_mkfile("home/.gitignore", "/ignore_me\n");
	{
		git_config *cfg;
		cl_git_pass(git_repository_config(&cfg, g_repo));
		cl_git_pass(git_config_set_string(
			cfg, "core.excludesfile", "~/.gitignore"));
		git_config_free(cfg);
	}

	cl_git_rewritefile("empty_standard_repo/.git/info/exclude", "/ignore_also\n");
	cl_git_rewritefile("empty_standard_repo/dir/.gitignore", "/ignore_me\n");
	cl_git_rewritefile("empty_standard_repo/test/.gitignore", "/and_me\n");

	memset(&counts, 0x0, sizeof(status_entry_counts));
	counts.expected_entry_count = 10;
	counts.expected_paths = paths_2;
	counts.expected_statuses = statuses_2;

	opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_RECURSE_IGNORED_DIRS;

	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__normal, &counts));

	cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
	cl_assert_equal_i(0, counts.wrong_status_flags_count);
	cl_assert_equal_i(0, counts.wrong_sorted_path);
}

416
void test_ignore_status__multiple_leading_slash(void)
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
{
	static const char *test_files[] = {
		"empty_standard_repo/a.test",
		"empty_standard_repo/b.test",
		"empty_standard_repo/c.test",
		"empty_standard_repo/d.test",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/.gitignore",
		"a.test\n"
		"/b.test\n"
		"//c.test\n"
		"///d.test\n");

	assert_is_ignored("a.test");
	assert_is_ignored("b.test");
	refute_is_ignored("c.test");
	refute_is_ignored("d.test");
}

440
void test_ignore_status__contained_dir_with_matching_name(void)
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
{
	static const char *test_files[] = {
		"empty_standard_repo/subdir_match/aaa/subdir_match/file",
		"empty_standard_repo/subdir_match/zzz_ignoreme",
		NULL
	};
	static const char *expected_paths[] = {
		"subdir_match/.gitignore",
		"subdir_match/aaa/subdir_match/file",
		"subdir_match/zzz_ignoreme",
	};
	static const unsigned int expected_statuses[] = {
		GIT_STATUS_WT_NEW,  GIT_STATUS_WT_NEW,  GIT_STATUS_IGNORED
	};
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
	status_entry_counts counts;

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/subdir_match/.gitignore", "*_ignoreme\n");

462 463
	refute_is_ignored("subdir_match/aaa/subdir_match/file");
	assert_is_ignored("subdir_match/zzz_ignoreme");
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

	memset(&counts, 0x0, sizeof(status_entry_counts));
	counts.expected_entry_count = 3;
	counts.expected_paths = expected_paths;
	counts.expected_statuses = expected_statuses;

	opts.flags = GIT_STATUS_OPT_DEFAULTS | GIT_STATUS_OPT_RECURSE_IGNORED_DIRS;

	cl_git_pass(git_status_foreach_ext(
		g_repo, &opts, cb_status__normal, &counts));

	cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
	cl_assert_equal_i(0, counts.wrong_status_flags_count);
	cl_assert_equal_i(0, counts.wrong_sorted_path);
}

480
void test_ignore_status__trailing_slash_star(void)
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
{
	static const char *test_files[] = {
		"empty_standard_repo/file",
		"empty_standard_repo/subdir/file",
		"empty_standard_repo/subdir/sub2/sub3/file",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/subdir/.gitignore", "/**/*\n");

	refute_is_ignored("file");
	assert_is_ignored("subdir/sub2/sub3/file");
	assert_is_ignored("subdir/file");
}

498
void test_ignore_status__adding_internal_ignores(void)
499 500 501
{
	g_repo = cl_git_sandbox_init("empty_standard_repo");

502 503
	refute_is_ignored("one.txt");
	refute_is_ignored("two.bar");
504 505 506

	cl_git_pass(git_ignore_add_rule(g_repo, "*.nomatch\n"));

507 508
	refute_is_ignored("one.txt");
	refute_is_ignored("two.bar");
509 510 511

	cl_git_pass(git_ignore_add_rule(g_repo, "*.txt\n"));

512 513
	assert_is_ignored("one.txt");
	refute_is_ignored("two.bar");
514 515 516

	cl_git_pass(git_ignore_add_rule(g_repo, "*.bar\n"));

517 518
	assert_is_ignored("one.txt");
	assert_is_ignored("two.bar");
519 520 521

	cl_git_pass(git_ignore_clear_internal_rules(g_repo));

522 523
	refute_is_ignored("one.txt");
	refute_is_ignored("two.bar");
524 525 526 527

	cl_git_pass(git_ignore_add_rule(
		g_repo, "multiple\n*.rules\n# comment line\n*.bar\n"));

528 529
	refute_is_ignored("one.txt");
	assert_is_ignored("two.bar");
530
}
531

532
void test_ignore_status__add_internal_as_first_thing(void)
533 534 535 536 537 538 539
{
	const char *add_me = "\n#################\n## Eclipse\n#################\n\n*.pydevproject\n.project\n.metadata\nbin/\ntmp/\n*.tmp\n\n";

	g_repo = cl_git_sandbox_init("empty_standard_repo");

	cl_git_pass(git_ignore_add_rule(g_repo, add_me));

540 541
	assert_is_ignored("one.tmp");
	refute_is_ignored("two.bar");
542
}
543

544
void test_ignore_status__internal_ignores_inside_deep_paths(void)
545 546 547 548 549 550 551
{
	const char *add_me = "Debug\nthis/is/deep\npatterned*/dir\n";

	g_repo = cl_git_sandbox_init("empty_standard_repo");

	cl_git_pass(git_ignore_add_rule(g_repo, add_me));

552 553 554 555 556 557 558 559 560 561
	assert_is_ignored("Debug");
	assert_is_ignored("and/Debug");
	assert_is_ignored("really/Debug/this/file");
	assert_is_ignored("Debug/what/I/say");

	refute_is_ignored("and/NoDebug");
	refute_is_ignored("NoDebug/this");
	refute_is_ignored("please/NoDebug/this");

	assert_is_ignored("this/is/deep");
562
	/* pattern containing slash gets FNM_PATHNAME so all slashes must match */
563 564
	refute_is_ignored("and/this/is/deep");
	assert_is_ignored("this/is/deep/too");
565
	/* pattern containing slash gets FNM_PATHNAME so all slashes must match */
566 567 568 569 570 571
	refute_is_ignored("but/this/is/deep/and/ignored");

	refute_is_ignored("this/is/not/deep");
	refute_is_ignored("is/this/not/as/deep");
	refute_is_ignored("this/is/deepish");
	refute_is_ignored("xthis/is/deep");
572
}
573

574
void test_ignore_status__automatically_ignore_bad_files(void)
575 576 577
{
	g_repo = cl_git_sandbox_init("empty_standard_repo");

578 579 580 581
	assert_is_ignored(".git");
	assert_is_ignored("this/file/.");
	assert_is_ignored("path/../funky");
	refute_is_ignored("path/whatever.c");
582 583 584

	cl_git_pass(git_ignore_add_rule(g_repo, "*.c\n"));

585 586 587 588
	assert_is_ignored(".git");
	assert_is_ignored("this/file/.");
	assert_is_ignored("path/../funky");
	assert_is_ignored("path/whatever.c");
589 590 591

	cl_git_pass(git_ignore_clear_internal_rules(g_repo));

592 593 594 595
	assert_is_ignored(".git");
	assert_is_ignored("this/file/.");
	assert_is_ignored("path/../funky");
	refute_is_ignored("path/whatever.c");
596
}
597

598
void test_ignore_status__filenames_with_special_prefixes_do_not_interfere_with_status_retrieval(void)
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
{
	status_entry_single st;
	char *test_cases[] = {
		"!file",
		"#blah",
		"[blah]",
		"[attr]",
		"[attr]blah",
		NULL
	};
	int i;

	for (i = 0; *(test_cases + i) != NULL; i++) {
		git_buf file = GIT_BUF_INIT;
		char *file_name = *(test_cases + i);
		git_repository *repo = cl_git_sandbox_init("empty_standard_repo");

		cl_git_pass(git_buf_joinpath(&file, "empty_standard_repo", file_name));
		cl_git_mkfile(git_buf_cstr(&file), "Please don't ignore me!");

		memset(&st, 0, sizeof(st));
		cl_git_pass(git_status_foreach(repo, cb_status__single, &st));
		cl_assert(st.count == 1);
		cl_assert(st.status == GIT_STATUS_WT_NEW);

		cl_git_pass(git_status_file(&st.status, repo, file_name));
		cl_assert(st.status == GIT_STATUS_WT_NEW);

		cl_git_sandbox_cleanup();
628
		git_buf_dispose(&file);
629 630
	}
}
631

632
void test_ignore_status__issue_1766_negated_ignores(void)
633 634 635 636 637 638
{
	unsigned int status;

	g_repo = cl_git_sandbox_init("empty_standard_repo");

	cl_git_pass(git_futils_mkdir_r(
639
		"empty_standard_repo/a", 0775));
640 641 642 643 644
	cl_git_mkfile(
		"empty_standard_repo/a/.gitignore", "*\n!.gitignore\n");
	cl_git_mkfile(
		"empty_standard_repo/a/ignoreme", "I should be ignored\n");

645 646
	refute_is_ignored("a/.gitignore");
	assert_is_ignored("a/ignoreme");
647 648

	cl_git_pass(git_futils_mkdir_r(
649
		"empty_standard_repo/b", 0775));
650 651 652 653 654
	cl_git_mkfile(
		"empty_standard_repo/b/.gitignore", "*\n!.gitignore\n");
	cl_git_mkfile(
		"empty_standard_repo/b/ignoreme", "I should be ignored\n");

655 656
	refute_is_ignored("b/.gitignore");
	assert_is_ignored("b/ignoreme");
657 658

	/* shouldn't have changed results from first couple either */
659 660
	refute_is_ignored("a/.gitignore");
	assert_is_ignored("a/ignoreme");
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707

	/* status should find the two ignore files and nothing else */

	cl_git_pass(git_status_file(&status, g_repo, "a/.gitignore"));
	cl_assert_equal_i(GIT_STATUS_WT_NEW, (int)status);

	cl_git_pass(git_status_file(&status, g_repo, "a/ignoreme"));
	cl_assert_equal_i(GIT_STATUS_IGNORED, (int)status);

	cl_git_pass(git_status_file(&status, g_repo, "b/.gitignore"));
	cl_assert_equal_i(GIT_STATUS_WT_NEW, (int)status);

	cl_git_pass(git_status_file(&status, g_repo, "b/ignoreme"));
	cl_assert_equal_i(GIT_STATUS_IGNORED, (int)status);

	{
		git_status_options opts = GIT_STATUS_OPTIONS_INIT;
		status_entry_counts counts;
		static const char *paths[] = {
			"a/.gitignore",
			"a/ignoreme",
			"b/.gitignore",
			"b/ignoreme",
		};
		static const unsigned int statuses[] = {
			GIT_STATUS_WT_NEW,
			GIT_STATUS_IGNORED,
			GIT_STATUS_WT_NEW,
			GIT_STATUS_IGNORED,
		};

		memset(&counts, 0x0, sizeof(status_entry_counts));
		counts.expected_entry_count = 4;
		counts.expected_paths = paths;
		counts.expected_statuses = statuses;

		opts.flags = GIT_STATUS_OPT_DEFAULTS;

		cl_git_pass(git_status_foreach_ext(
			g_repo, &opts, cb_status__normal, &counts));

		cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
		cl_assert_equal_i(0, counts.wrong_status_flags_count);
		cl_assert_equal_i(0, counts.wrong_sorted_path);
	}
}

708 709 710 711 712 713 714 715 716
static void add_one_to_index(const char *file)
{
	git_index *index;
	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_add_bypath(index, file));
	git_index_free(index);
}

/* Some further broken scenarios that have been reported */
717
void test_ignore_status__more_breakage(void)
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
{
	static const char *test_files[] = {
		"empty_standard_repo/d1/pfx-d2/d3/d4/d5/tracked",
		"empty_standard_repo/d1/pfx-d2/d3/d4/d5/untracked",
		"empty_standard_repo/d1/pfx-d2/d3/d4/untracked",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/.gitignore",
		"/d1/pfx-*\n"
		"!/d1/pfx-d2/\n"
		"/d1/pfx-d2/*\n"
		"!/d1/pfx-d2/d3/\n"
		"/d1/pfx-d2/d3/*\n"
		"!/d1/pfx-d2/d3/d4/\n");
	add_one_to_index("d1/pfx-d2/d3/d4/d5/tracked");

	{
		git_status_options opts = GIT_STATUS_OPTIONS_INIT;
		status_entry_counts counts;
		static const char *files[] = {
			".gitignore",
			"d1/pfx-d2/d3/d4/d5/tracked",
			"d1/pfx-d2/d3/d4/d5/untracked",
			"d1/pfx-d2/d3/d4/untracked",
		};
		static const unsigned int statuses[] = {
			GIT_STATUS_WT_NEW,
			GIT_STATUS_INDEX_NEW,
			GIT_STATUS_WT_NEW,
			GIT_STATUS_WT_NEW,
		};

		memset(&counts, 0x0, sizeof(status_entry_counts));
		counts.expected_entry_count = 4;
		counts.expected_paths = files;
		counts.expected_statuses = statuses;
		opts.flags = GIT_STATUS_OPT_DEFAULTS |
			GIT_STATUS_OPT_INCLUDE_IGNORED |
			GIT_STATUS_OPT_RECURSE_IGNORED_DIRS;
		cl_git_pass(git_status_foreach_ext(
			g_repo, &opts, cb_status__normal, &counts));

		cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
		cl_assert_equal_i(0, counts.wrong_status_flags_count);
		cl_assert_equal_i(0, counts.wrong_sorted_path);
	}

	refute_is_ignored("d1/pfx-d2/d3/d4/d5/tracked");
	refute_is_ignored("d1/pfx-d2/d3/d4/d5/untracked");
	refute_is_ignored("d1/pfx-d2/d3/d4/untracked");
}

773
void test_ignore_status__negative_ignores_inside_ignores(void)
774 775 776 777
{
	static const char *test_files[] = {
		"empty_standard_repo/top/mid/btm/tracked",
		"empty_standard_repo/top/mid/btm/untracked",
778 779
		"empty_standard_repo/zoo/bar",
		"empty_standard_repo/zoo/foo/bar",
780 781 782 783 784 785
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/.gitignore",
786 787 788 789 790
		"top\n"
		"!top/mid/btm\n"
		"zoo/*\n"
		"!zoo/bar\n"
		"!zoo/foo/bar\n");
791 792 793 794 795 796 797
	add_one_to_index("top/mid/btm/tracked");

	{
		git_status_options opts = GIT_STATUS_OPTIONS_INIT;
		status_entry_counts counts;
		static const char *files[] = {
			".gitignore", "top/mid/btm/tracked", "top/mid/btm/untracked",
798
			"zoo/bar", "zoo/foo/bar",
799 800
		};
		static const unsigned int statuses[] = {
801 802
			GIT_STATUS_WT_NEW, GIT_STATUS_INDEX_NEW, GIT_STATUS_IGNORED,
			GIT_STATUS_WT_NEW, GIT_STATUS_IGNORED,
803 804 805
		};

		memset(&counts, 0x0, sizeof(status_entry_counts));
806
		counts.expected_entry_count = 5;
807 808 809 810 811 812 813 814 815 816 817 818 819
		counts.expected_paths = files;
		counts.expected_statuses = statuses;
		opts.flags = GIT_STATUS_OPT_DEFAULTS |
			GIT_STATUS_OPT_INCLUDE_IGNORED |
			GIT_STATUS_OPT_RECURSE_IGNORED_DIRS;
		cl_git_pass(git_status_foreach_ext(
			g_repo, &opts, cb_status__normal, &counts));

		cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
		cl_assert_equal_i(0, counts.wrong_status_flags_count);
		cl_assert_equal_i(0, counts.wrong_sorted_path);
	}

820 821 822
	assert_is_ignored("top/mid/btm/tracked");
	assert_is_ignored("top/mid/btm/untracked");
	refute_is_ignored("foo/bar");
823
}
824

825
void test_ignore_status__negative_ignores_in_slash_star(void)
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
{
	git_status_options status_opts = GIT_STATUS_OPTIONS_INIT;
	git_status_list *list;
	int found_look_ma = 0, found_what_about = 0;
	size_t i;
	static const char *test_files[] = {
		"empty_standard_repo/bin/look-ma.txt",
		"empty_standard_repo/bin/what-about-me.txt",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/.gitignore",
		"bin/*\n"
		"!bin/w*\n");

	assert_is_ignored("bin/look-ma.txt");
	refute_is_ignored("bin/what-about-me.txt");

	status_opts.flags = GIT_STATUS_OPT_DEFAULTS;
	cl_git_pass(git_status_list_new(&list, g_repo, &status_opts));
	for (i = 0; i < git_status_list_entrycount(list); i++) {
		const git_status_entry *entry = git_status_byindex(list, i);

		if (!strcmp("bin/look-ma.txt", entry->index_to_workdir->new_file.path))
			found_look_ma = 1;

		if (!strcmp("bin/what-about-me.txt", entry->index_to_workdir->new_file.path))
			found_what_about = 1;
	}
	git_status_list_free(list);

	cl_assert(found_look_ma);
	cl_assert(found_what_about);
}
862

863
void test_ignore_status__negative_ignores_without_trailing_slash_inside_ignores(void)
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
{
	git_status_options status_opts = GIT_STATUS_OPTIONS_INIT;
	git_status_list *list;
	int found_parent_file = 0, found_parent_child1_file = 0, found_parent_child2_file = 0;
	size_t i;
	static const char *test_files[] = {
		"empty_standard_repo/parent/file.txt",
		"empty_standard_repo/parent/force.txt",
		"empty_standard_repo/parent/child1/file.txt",
		"empty_standard_repo/parent/child2/file.txt",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/.gitignore",
		"parent/*\n"
		"!parent/force.txt\n"
		"!parent/child1\n"
		"!parent/child2/\n");

	add_one_to_index("parent/force.txt");

	assert_is_ignored("parent/file.txt");
	refute_is_ignored("parent/force.txt");
	refute_is_ignored("parent/child1/file.txt");
	refute_is_ignored("parent/child2/file.txt");

	status_opts.flags = GIT_STATUS_OPT_DEFAULTS;
	cl_git_pass(git_status_list_new(&list, g_repo, &status_opts));
	for (i = 0; i < git_status_list_entrycount(list); i++) {
		const git_status_entry *entry = git_status_byindex(list, i);

		if (!entry->index_to_workdir)
			continue;

		if (!strcmp("parent/file.txt", entry->index_to_workdir->new_file.path))
			found_parent_file = 1;

		if (!strcmp("parent/force.txt", entry->index_to_workdir->new_file.path))
			found_parent_file = 1;

		if (!strcmp("parent/child1/file.txt", entry->index_to_workdir->new_file.path))
			found_parent_child1_file = 1;

		if (!strcmp("parent/child2/file.txt", entry->index_to_workdir->new_file.path))
			found_parent_child2_file = 1;
	}
	git_status_list_free(list);

	cl_assert(found_parent_file);
	cl_assert(found_parent_child1_file);
	cl_assert(found_parent_child2_file);
}

919
void test_ignore_status__negative_directory_ignores(void)
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
{
	static const char *test_files[] = {
		"empty_standard_repo/parent/child1/bar.txt",
		"empty_standard_repo/parent/child2/bar.txt",
		"empty_standard_repo/parent/child3/foo.txt",
		"empty_standard_repo/parent/child4/bar.txt",
		"empty_standard_repo/parent/nested/child5/bar.txt",
		"empty_standard_repo/parent/nested/child6/bar.txt",
		"empty_standard_repo/parent/nested/child7/bar.txt",
		"empty_standard_repo/padded_parent/child8/bar.txt",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/.gitignore",
		"foo.txt\n"
		"parent/child1\n"
		"parent/child2\n"
		"parent/child4\n"
		"parent/nested/child5\n"
		"nested/child6\n"
		"nested/child7\n"
		"padded_parent/child8\n"
		/* test simple exact match */
		"!parent/child1\n"
		/* test negating file without negating dir */
		"!parent/child2/bar.txt\n"
		/* test negative pattern on dir with its content
		 * being ignored */
		"!parent/child3\n"
		/* test with partial match at end */
		"!child4\n"
		/* test with partial match with '/' at end */
		"!nested/child5\n"
		/* test with complete match */
		"!nested/child6\n"
		/* test with trailing '/' */
		"!child7/\n"
		/* test with partial dir match */
		"!_parent/child8\n");

	refute_is_ignored("parent/child1/bar.txt");
	assert_is_ignored("parent/child2/bar.txt");
	assert_is_ignored("parent/child3/foo.txt");
	refute_is_ignored("parent/child4/bar.txt");
	assert_is_ignored("parent/nested/child5/bar.txt");
	refute_is_ignored("parent/nested/child6/bar.txt");
	refute_is_ignored("parent/nested/child7/bar.txt");
	assert_is_ignored("padded_parent/child8/bar.txt");
}

972
void test_ignore_status__unignore_entry_in_ignored_dir(void)
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
{
	static const char *test_files[] = {
		"empty_standard_repo/bar.txt",
		"empty_standard_repo/parent/bar.txt",
		"empty_standard_repo/parent/child/bar.txt",
		"empty_standard_repo/nested/parent/child/bar.txt",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/.gitignore",
		"bar.txt\n"
		"!parent/child/bar.txt\n");

	assert_is_ignored("bar.txt");
	assert_is_ignored("parent/bar.txt");
	refute_is_ignored("parent/child/bar.txt");
	assert_is_ignored("nested/parent/child/bar.txt");
}

994
void test_ignore_status__do_not_unignore_basename_prefix(void)
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
{
	static const char *test_files[] = {
		"empty_standard_repo/foo_bar.txt",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/.gitignore",
		"foo_bar.txt\n"
		"!bar.txt\n");

	assert_is_ignored("foo_bar.txt");
}

1010
void test_ignore_status__filename_with_cr(void)
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
{
	int ignored;

	g_repo = cl_git_sandbox_init("empty_standard_repo");
	cl_git_mkfile("empty_standard_repo/.gitignore", "Icon\r\r\n");

	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Icon\r"));
	cl_assert_equal_i(1, ignored);

	cl_git_mkfile("empty_standard_repo/.gitignore", "Ico\rn\n");
	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Ico\rn"));
	cl_assert_equal_i(1, ignored);

	cl_git_mkfile("empty_standard_repo/.gitignore", "Ico\rn\r\n");
	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Ico\rn"));
	cl_assert_equal_i(1, ignored);
	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Ico\rn\r"));
	cl_assert_equal_i(0, ignored);

	cl_git_mkfile("empty_standard_repo/.gitignore", "Ico\rn\r\r\n");
	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Ico\rn\r"));
	cl_assert_equal_i(1, ignored);
	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Icon\r"));
	cl_assert_equal_i(0, ignored);

	cl_git_mkfile("empty_standard_repo/.gitignore", "Icon\r\n");
	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Icon\r"));
	cl_assert_equal_i(0, ignored);
	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "Icon"));
	cl_assert_equal_i(1, ignored);
}
1042

1043
void test_ignore_status__subdir_doesnt_match_above(void)
1044 1045 1046 1047 1048 1049 1050 1051
{
	int ignored, icase = 0, error;
	git_config *cfg;

	g_repo = cl_git_sandbox_init("empty_standard_repo");

	cl_git_pass(git_repository_config_snapshot(&cfg, g_repo));
	error = git_config_get_bool(&icase, cfg, "core.ignorecase");
Carlos Martín Nieto committed
1052
	git_config_free(cfg);
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
	if (error == GIT_ENOTFOUND)
		error = 0;

	cl_git_pass(error);

	cl_git_pass(p_mkdir("empty_standard_repo/src", 0777));
	cl_git_pass(p_mkdir("empty_standard_repo/src/src", 0777));
	cl_git_mkfile("empty_standard_repo/src/.gitignore", "src\n");
	cl_git_mkfile("empty_standard_repo/.gitignore", "");

	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "src/test.txt"));
	cl_assert_equal_i(0, ignored);
	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "src/src/test.txt"));
	cl_assert_equal_i(1, ignored);
	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "src/foo/test.txt"));
	cl_assert_equal_i(0, ignored);

	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "SRC/src/test.txt"));
	cl_assert_equal_i(icase, ignored);
	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "src/SRC/test.txt"));
	cl_assert_equal_i(icase, ignored);
}
1075

1076
void test_ignore_status__negate_exact_previous(void)
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
{
	int ignored;

	g_repo = cl_git_sandbox_init("empty_standard_repo");

	cl_git_mkfile("empty_standard_repo/.gitignore", "*.com\ntags\n!tags/\n.buildpath");
	cl_git_mkfile("empty_standard_repo/.buildpath", "");
	cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, ".buildpath"));
	cl_assert_equal_i(1, ignored);
}
1087

1088
void test_ignore_status__negate_starstar(void)
1089 1090 1091 1092 1093 1094 1095 1096 1097
{
    int ignored;

    g_repo = cl_git_sandbox_init("empty_standard_repo");

    cl_git_mkfile("empty_standard_repo/.gitignore",
              "code/projects/**/packages/*\n"
              "!code/projects/**/packages/repositories.config");

1098
    cl_git_pass(git_futils_mkdir_r("empty_standard_repo/code/projects/foo/bar/packages", 0777));
1099 1100 1101 1102 1103
    cl_git_mkfile("empty_standard_repo/code/projects/foo/bar/packages/repositories.config", "");

    cl_git_pass(git_ignore_path_is_ignored(&ignored, g_repo, "code/projects/foo/bar/packages/repositories.config"));
    cl_assert_equal_i(0, ignored);
}
1104

1105
void test_ignore_status__ignore_all_toplevel_dirs_include_files(void)
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
{
	static const char *test_files[] = {
		"empty_standard_repo/README.md",
		"empty_standard_repo/src/main.c",
		"empty_standard_repo/src/foo/foo.c",
		"empty_standard_repo/dist/foo.o",
		"empty_standard_repo/dist/main.o",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/.gitignore",
		"/*/\n"
		"!/src\n");

	assert_is_ignored("dist/foo.o");
	assert_is_ignored("dist/main.o");

	refute_is_ignored("README.md");
	refute_is_ignored("src/foo.c");
	refute_is_ignored("src/foo/foo.c");
}

1130
void test_ignore_status__subdir_ignore_all_toplevel_dirs_include_files(void)
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
{
	static const char *test_files[] = {
		"empty_standard_repo/project/README.md",
		"empty_standard_repo/project/src/main.c",
		"empty_standard_repo/project/src/foo/foo.c",
		"empty_standard_repo/project/dist/foo.o",
		"empty_standard_repo/project/dist/main.o",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/project/.gitignore",
		"/*/\n"
		"!/src\n");

	assert_is_ignored("project/dist/foo.o");
	assert_is_ignored("project/dist/main.o");

	refute_is_ignored("project/src/foo.c");
	refute_is_ignored("project/src/foo/foo.c");
	refute_is_ignored("project/README.md");
}

1155
void test_ignore_status__subdir_ignore_everything_except_certain_files(void)
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
{
	static const char *test_files[] = {
		"empty_standard_repo/project/README.md",
		"empty_standard_repo/project/some_file",
		"empty_standard_repo/project/src/main.c",
		"empty_standard_repo/project/src/foo/foo.c",
		"empty_standard_repo/project/dist/foo.o",
		"empty_standard_repo/project/dist/main.o",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/project/.gitignore",
		"/*\n"
		"!/src\n"
		"!README.md\n");

	assert_is_ignored("project/some_file");
	assert_is_ignored("project/dist/foo.o");
	assert_is_ignored("project/dist/main.o");

	refute_is_ignored("project/README.md");
	refute_is_ignored("project/src/foo.c");
	refute_is_ignored("project/src/foo/foo.c");
}
1182

1183
void test_ignore_status__deeper(void)
1184
{
1185 1186 1187 1188 1189 1190 1191
	const char *test_files[] = {
		"empty_standard_repo/foo.data",
		"empty_standard_repo/bar.data",
		"empty_standard_repo/dont_ignore/foo.data",
		"empty_standard_repo/dont_ignore/bar.data",
		NULL
	};
1192

1193 1194 1195 1196
	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile("empty_standard_repo/.gitignore",
		"*.data\n"
		"!dont_ignore/*.data\n");
1197

1198 1199
	assert_is_ignored("foo.data");
	assert_is_ignored("bar.data");
1200

1201 1202
	refute_is_ignored("dont_ignore/foo.data");
	refute_is_ignored("dont_ignore/bar.data");
1203
}
1204

1205
void test_ignore_status__unignored_dir_with_ignored_contents(void)
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
{
	static const char *test_files[] = {
		"empty_standard_repo/dir/a.test",
		"empty_standard_repo/dir/subdir/a.test",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/.gitignore",
		"*.test\n"
		"!dir/*\n");

	refute_is_ignored("dir/a.test");
	assert_is_ignored("dir/subdir/a.test");
}

1223
void test_ignore_status__unignored_subdirs(void)
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
{
	static const char *test_files[] = {
		"empty_standard_repo/dir/a.test",
		"empty_standard_repo/dir/subdir/a.test",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/.gitignore",
		"dir/*\n"
		"!dir/*/\n");

	assert_is_ignored("dir/a.test");
	refute_is_ignored("dir/subdir/a.test");
}
1240

1241
void test_ignore_status__skips_bom(void)
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
{
	static const char *test_files[] = {
		"empty_standard_repo/a.test",
		"empty_standard_repo/b.test",
		"empty_standard_repo/c.test",
		"empty_standard_repo/foo.txt",
		"empty_standard_repo/bar.txt",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/.gitignore",
		"\xEF\xBB\xBF*.test\n");

	assert_is_ignored("a.test");
	assert_is_ignored("b.test");
	assert_is_ignored("c.test");
	refute_is_ignored("foo.txt");
	refute_is_ignored("bar.txt");
}
1263

1264
void test_ignore_status__leading_spaces_are_significant(void)
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
{
	static const char *test_files[] = {
		"empty_standard_repo/a.test",
		"empty_standard_repo/b.test",
		"empty_standard_repo/c.test",
		"empty_standard_repo/d.test",
		NULL
	};

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/.gitignore",
		" a.test\n"
1278
		"# this is a comment\n"
1279 1280
		"b.test\n"
		"\tc.test\n"
1281
		" # not a comment\n"
1282 1283 1284 1285
		"d.test\n");

	refute_is_ignored("a.test");
	assert_is_ignored(" a.test");
1286
	refute_is_ignored("# this is a comment");
1287 1288 1289
	assert_is_ignored("b.test");
	refute_is_ignored("c.test");
	assert_is_ignored("\tc.test");
1290
	assert_is_ignored(" # not a comment");
1291 1292
	assert_is_ignored("d.test");
}