status.c 38.8 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "futils.h"
3
#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
static void assert_ignored_(
20 21
	bool expected, const char *filepath,
	const char *file, const char *func, int line)
22 23
{
	int is_ignored = 0;
24
	cl_git_expect(
25
		git_status_should_ignore(&is_ignored, g_repo, filepath), 0, file, func, line);
26 27
	clar__assert(
		(expected != 0) == (is_ignored != 0),
28
		file, func, line, "expected != is_ignored", filepath, 1);
29 30
}
#define assert_ignored(expected, filepath) \
31
	assert_ignored_(expected, filepath, __FILE__, __func__, __LINE__)
32
#define assert_is_ignored(filepath) \
33
	assert_ignored_(true, filepath, __FILE__, __func__, __LINE__)
34
#define refute_is_ignored(filepath) \
35
	assert_ignored_(false, filepath, __FILE__, __func__, __LINE__)
36

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

66 67
	g_repo = cl_git_sandbox_init("attr");

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

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


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

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

86 87 88 89 90 91
	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/");
92 93
}

94
void test_ignore_status__empty_repo_with_gitignore_rewrite(void)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
{
	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);

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

113 114 115 116 117 118 119 120 121 122
	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);

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

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

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

138
void test_ignore_status__ignore_pattern_contains_space(void)
139 140 141 142 143 144 145
{
	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");

146 147 148 149 150 151
	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);

152
	cl_git_pass(git_futils_mkdir_r("empty_standard_repo/foo", mode));
153 154 155 156 157
	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);
}
158

159
void test_ignore_status__ignore_pattern_ignorecase(void)
160 161 162 163 164 165 166 167 168 169 170
{
	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));
171
	ignore_case = (git_index_caps(index) & GIT_INDEX_CAPABILITY_IGNORE_CASE) != 0;
172 173 174 175 176
	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);
}
177

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

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	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);

197
	assert_is_ignored("ignore_me");
198

199 200 201 202 203 204 205 206 207
	/* 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.
208
	 */
209
	cl_git_pass(git_futils_mkdir_r(
210
		"empty_standard_repo/test/ignore_me", 0775));
211 212
	cl_git_mkfile(
		"empty_standard_repo/test/ignore_me/file", "I'm going to be ignored!");
213 214
	cl_git_mkfile(
		"empty_standard_repo/test/ignore_me/file2", "Me, too!");
215 216 217

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

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

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

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

231
	g_repo = cl_git_sandbox_init(reponame);
232 233

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

241 242 243 244 245 246 247 248 249 250 251 252 253
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
};

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

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

293
	memset(&counts, 0x0, sizeof(status_entry_counts));
294
	counts.expected_entry_count = 9;
295 296 297 298 299 300 301 302 303 304 305 306
	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);

307 308

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

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

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

323
void test_ignore_status__subdirectories_not_at_root(void)
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
{
	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,
	};

345
	make_test_data(test_repo_1, test_files_1);
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	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);
}

364
void test_ignore_status__leading_slash_ignores(void)
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
{
	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,
	};

386
	make_test_data(test_repo_1, test_files_1);
387

388
	cl_fake_home();
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 416
	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);
}

417
void test_ignore_status__multiple_leading_slash(void)
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
{
	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");
}

441
void test_ignore_status__contained_dir_with_matching_name(void)
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
{
	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");

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

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

481
void test_ignore_status__trailing_slash_star(void)
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
{
	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");
}

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

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

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

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

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

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

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

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

	cl_git_pass(git_ignore_clear_internal_rules(g_repo));

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

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

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

533
void test_ignore_status__add_internal_as_first_thing(void)
534 535 536 537 538 539 540
{
	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));

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

545
void test_ignore_status__internal_ignores_inside_deep_paths(void)
546 547 548 549 550 551 552
{
	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));

553 554 555 556 557 558 559 560 561 562
	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");
563
	/* pattern containing slash gets FNM_PATHNAME so all slashes must match */
564 565
	refute_is_ignored("and/this/is/deep");
	assert_is_ignored("this/is/deep/too");
566
	/* pattern containing slash gets FNM_PATHNAME so all slashes must match */
567 568 569 570 571 572
	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");
573
}
574

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

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

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

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

	cl_git_pass(git_ignore_clear_internal_rules(g_repo));

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

599
void test_ignore_status__filenames_with_special_prefixes_do_not_interfere_with_status_retrieval(void)
600 601 602 603 604 605 606 607 608 609 610 611 612
{
	status_entry_single st;
	char *test_cases[] = {
		"!file",
		"#blah",
		"[blah]",
		"[attr]",
		"[attr]blah",
		NULL
	};
	int i;

	for (i = 0; *(test_cases + i) != NULL; i++) {
613
		git_str file = GIT_STR_INIT;
614 615 616
		char *file_name = *(test_cases + i);
		git_repository *repo = cl_git_sandbox_init("empty_standard_repo");

617 618
		cl_git_pass(git_str_joinpath(&file, "empty_standard_repo", file_name));
		cl_git_mkfile(git_str_cstr(&file), "Please don't ignore me!");
619 620 621 622 623 624 625 626 627 628

		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();
629
		git_str_dispose(&file);
630 631
	}
}
632

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

	g_repo = cl_git_sandbox_init("empty_standard_repo");

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

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

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

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

	/* shouldn't have changed results from first couple either */
660 661
	refute_is_ignored("a/.gitignore");
	assert_is_ignored("a/ignoreme");
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 708

	/* 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);
	}
}

709 710 711 712 713 714 715 716 717
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 */
718
void test_ignore_status__more_breakage(void)
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 773
{
	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");
}

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

	make_test_data("empty_standard_repo", test_files);
	cl_git_mkfile(
		"empty_standard_repo/.gitignore",
787 788 789 790 791
		"top\n"
		"!top/mid/btm\n"
		"zoo/*\n"
		"!zoo/bar\n"
		"!zoo/foo/bar\n");
792 793 794 795 796 797 798
	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",
799
			"zoo/bar", "zoo/foo/bar",
800 801
		};
		static const unsigned int statuses[] = {
802 803
			GIT_STATUS_WT_NEW, GIT_STATUS_INDEX_NEW, GIT_STATUS_IGNORED,
			GIT_STATUS_WT_NEW, GIT_STATUS_IGNORED,
804 805 806
		};

		memset(&counts, 0x0, sizeof(status_entry_counts));
807
		counts.expected_entry_count = 5;
808 809 810 811 812 813 814 815 816 817 818 819 820
		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);
	}

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

826
void test_ignore_status__negative_ignores_in_slash_star(void)
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 862
{
	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);
}
863

864
void test_ignore_status__negative_ignores_without_trailing_slash_inside_ignores(void)
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 919
{
	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);
}

920
void test_ignore_status__negative_directory_ignores(void)
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 972
{
	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");
}

973
void test_ignore_status__unignore_entry_in_ignored_dir(void)
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
{
	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");
}

995
void test_ignore_status__do_not_unignore_basename_prefix(void)
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
{
	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");
}

1011
void test_ignore_status__filename_with_cr(void)
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 1042
{
	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);
}
1043

1044
void test_ignore_status__subdir_doesnt_match_above(void)
1045 1046 1047 1048 1049 1050 1051 1052
{
	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
1053
	git_config_free(cfg);
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	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);
}
1076

1077
void test_ignore_status__negate_exact_previous(void)
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
{
	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);
}
1088

1089
void test_ignore_status__negate_starstar(void)
1090 1091 1092 1093 1094 1095 1096 1097 1098
{
    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");

1099
    cl_git_pass(git_futils_mkdir_r("empty_standard_repo/code/projects/foo/bar/packages", 0777));
1100 1101 1102 1103 1104
    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);
}
1105

1106
void test_ignore_status__ignore_all_toplevel_dirs_include_files(void)
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
{
	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");
}

1131
void test_ignore_status__subdir_ignore_all_toplevel_dirs_include_files(void)
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
{
	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");
}

1156
void test_ignore_status__subdir_ignore_everything_except_certain_files(void)
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 1182
{
	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");
}
1183

1184
void test_ignore_status__deeper(void)
1185
{
1186 1187 1188 1189 1190 1191 1192
	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
	};
1193

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

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

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

1206
void test_ignore_status__unignored_dir_with_ignored_contents(void)
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
{
	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");
}

1224
void test_ignore_status__unignored_subdirs(void)
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
{
	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");
}
1241

1242
void test_ignore_status__skips_bom(void)
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
{
	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");
}
1264

1265
void test_ignore_status__leading_spaces_are_significant(void)
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
{
	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"
1279
		"# this is a comment\n"
1280 1281
		"b.test\n"
		"\tc.test\n"
1282
		" # not a comment\n"
1283 1284 1285 1286
		"d.test\n");

	refute_is_ignored("a.test");
	assert_is_ignored(" a.test");
1287
	refute_is_ignored("# this is a comment");
1288 1289 1290
	assert_is_ignored("b.test");
	refute_is_ignored("c.test");
	assert_is_ignored("\tc.test");
1291
	assert_is_ignored(" # not a comment");
1292 1293
	assert_is_ignored("d.test");
}
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337

void test_ignore_status__override_nested_wildcard_unignore(void)
{
	git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
	git_status_list *statuslist;
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
	const git_status_entry *status;

	cl_git_pass(git_futils_mkdir_r("empty_standard_repo/dir", 0777));
	cl_git_pass(git_futils_mkdir_r("empty_standard_repo/dir/subdir", 0777));
	cl_git_mkfile("empty_standard_repo/.gitignore", "a.test\n");
	cl_git_mkfile("empty_standard_repo/dir/.gitignore", "!*.test\n");
	cl_git_mkfile("empty_standard_repo/dir/subdir/.gitignore", "a.test\n");
	cl_git_mkfile("empty_standard_repo/dir/a.test", "pong");
	cl_git_mkfile("empty_standard_repo/dir/subdir/a.test", "pong");

	opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
	opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;

	cl_git_pass(git_status_list_new(&statuslist, repo, &opts));
	cl_assert_equal_sz(4, git_status_list_entrycount(statuslist));

	status = git_status_byindex(statuslist, 0);
	cl_assert(status != NULL);
	cl_assert_equal_s(".gitignore", status->index_to_workdir->old_file.path);
	cl_assert_equal_i(GIT_STATUS_WT_NEW, status->status);

	status = git_status_byindex(statuslist, 1);
	cl_assert(status != NULL);
	cl_assert_equal_s("dir/.gitignore", status->index_to_workdir->old_file.path);
	cl_assert_equal_i(GIT_STATUS_WT_NEW, status->status);

	status = git_status_byindex(statuslist, 2);
	cl_assert(status != NULL);
	cl_assert_equal_s("dir/a.test", status->index_to_workdir->old_file.path);
	cl_assert_equal_i(GIT_STATUS_WT_NEW, status->status);

	status = git_status_byindex(statuslist, 3);
	cl_assert(status != NULL);
	cl_assert_equal_s("dir/subdir/.gitignore", status->index_to_workdir->old_file.path);
	cl_assert_equal_i(GIT_STATUS_WT_NEW, status->status);

	git_status_list_free(statuslist);
}