ignore.c 10.9 KB
Newer Older
1
#include "clar_libgit2.h"
2 3
#include "posix.h"
#include "path.h"
4
#include "fileops.h"
5 6 7 8 9

static git_repository *g_repo = NULL;

void test_attr_ignore__initialize(void)
{
10
	g_repo = cl_git_sandbox_init("attr");
11 12 13 14
}

void test_attr_ignore__cleanup(void)
{
15 16
	cl_git_sandbox_cleanup();
	g_repo = NULL;
17 18
}

19
static void assert_is_ignored_(
20
	bool expected, const char *filepath, const char *file, int line)
21
{
22
	int is_ignored = 0;
23

24
	cl_git_expect(
25
		git_ignore_path_is_ignored(&is_ignored, g_repo, filepath), 0, file, line);
26 27 28 29

	clar__assert_equal(
		file, line, "expected != is_ignored", 1, "%d",
		(int)(expected != 0), (int)(is_ignored != 0));
30
}
31 32
#define assert_is_ignored(expected, filepath) \
	assert_is_ignored_(expected, filepath, __FILE__, __LINE__)
33 34 35

void test_attr_ignore__honor_temporary_rules(void)
{
36
	cl_git_rewritefile("attr/.gitignore", "/NewFolder\n/NewFolder/NewFolder");
37

38 39 40 41 42 43
	assert_is_ignored(false, "File.txt");
	assert_is_ignored(true, "NewFolder");
	assert_is_ignored(true, "NewFolder/NewFolder");
	assert_is_ignored(true, "NewFolder/NewFolder/File.txt");
}

44 45 46 47 48 49 50 51
void test_attr_ignore__allow_root(void)
{
	cl_git_rewritefile("attr/.gitignore", "/");

	assert_is_ignored(false, "File.txt");
	assert_is_ignored(false, "NewFolder");
	assert_is_ignored(false, "NewFolder/NewFolder");
	assert_is_ignored(false, "NewFolder/NewFolder/File.txt");
52 53 54 55 56 57 58 59 60 61
}

void test_attr_ignore__ignore_space(void)
{
	cl_git_rewritefile("attr/.gitignore", "/\n\n/NewFolder \n/NewFolder/NewFolder");

	assert_is_ignored(false, "File.txt");
	assert_is_ignored(true, "NewFolder");
	assert_is_ignored(true, "NewFolder/NewFolder");
	assert_is_ignored(true, "NewFolder/NewFolder/File.txt");
62 63
}

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
void test_attr_ignore__ignore_dir(void)
{
	cl_git_rewritefile("attr/.gitignore", "dir/\n");

	assert_is_ignored(true, "dir");
	assert_is_ignored(true, "dir/file");
}

void test_attr_ignore__ignore_dir_with_trailing_space(void)
{
	cl_git_rewritefile("attr/.gitignore", "dir/ \n");

	assert_is_ignored(true, "dir");
	assert_is_ignored(true, "dir/file");
}

80 81 82 83 84 85 86 87 88 89
void test_attr_ignore__ignore_root(void)
{
	cl_git_rewritefile("attr/.gitignore", "/\n\n/NewFolder\n/NewFolder/NewFolder");

	assert_is_ignored(false, "File.txt");
	assert_is_ignored(true, "NewFolder");
	assert_is_ignored(true, "NewFolder/NewFolder");
	assert_is_ignored(true, "NewFolder/NewFolder/File.txt");
}

90 91 92 93 94 95 96 97 98 99 100
void test_attr_ignore__full_paths(void)
{
	cl_git_rewritefile("attr/.gitignore", "Folder/*/Contained");

	assert_is_ignored(true, "Folder/Middle/Contained");
	assert_is_ignored(false, "Folder/Middle/More/More/Contained");

	cl_git_rewritefile("attr/.gitignore", "Folder/**/Contained");

	assert_is_ignored(true, "Folder/Middle/Contained");
	assert_is_ignored(true, "Folder/Middle/More/More/Contained");
101 102 103 104 105 106 107

	cl_git_rewritefile("attr/.gitignore", "Folder/**/Contained/*/Child");

	assert_is_ignored(true, "Folder/Middle/Contained/Happy/Child");
	assert_is_ignored(false, "Folder/Middle/Contained/Not/Happy/Child");
	assert_is_ignored(true, "Folder/Middle/More/More/Contained/Happy/Child");
	assert_is_ignored(false, "Folder/Middle/More/More/Contained/Not/Happy/Child");
108 109
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
void test_attr_ignore__more_starstar_cases(void)
{
	cl_must_pass(p_unlink("attr/.gitignore"));
	cl_git_mkfile(
		"attr/dir/.gitignore",
		"sub/**/*.html\n");

	assert_is_ignored(false, "aaa.html");
	assert_is_ignored(false, "dir");
	assert_is_ignored(false, "dir/sub");
	assert_is_ignored(true,  "dir/sub/sub2/aaa.html");
	assert_is_ignored(true,  "dir/sub/aaa.html");
	assert_is_ignored(false, "dir/aaa.html");
	assert_is_ignored(false, "sub");
	assert_is_ignored(false, "sub/aaa.html");
	assert_is_ignored(false, "sub/sub2/aaa.html");
}

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
void test_attr_ignore__leading_stars(void)
{
	cl_git_rewritefile(
		"attr/.gitignore",
		"*/onestar\n"
		"**/twostars\n"
		"*/parent1/kid1/*\n"
		"**/parent2/kid2/*\n");

	assert_is_ignored(true, "dir1/onestar");
	assert_is_ignored(true, "dir1/onestar/child"); /* in ignored dir */
	assert_is_ignored(false, "dir1/dir2/onestar");

	assert_is_ignored(true, "dir1/twostars");
	assert_is_ignored(true, "dir1/twostars/child"); /* in ignored dir */
	assert_is_ignored(true, "dir1/dir2/twostars");
	assert_is_ignored(true, "dir1/dir2/twostars/child"); /* in ignored dir */
	assert_is_ignored(true, "dir1/dir2/dir3/twostars");

	assert_is_ignored(true, "dir1/parent1/kid1/file");
	assert_is_ignored(true, "dir1/parent1/kid1/file/inside/parent");
	assert_is_ignored(false, "dir1/dir2/parent1/kid1/file");
	assert_is_ignored(false, "dir1/parent1/file");
	assert_is_ignored(false, "dir1/kid1/file");

	assert_is_ignored(true, "dir1/parent2/kid2/file");
	assert_is_ignored(true, "dir1/parent2/kid2/file/inside/parent");
	assert_is_ignored(true, "dir1/dir2/parent2/kid2/file");
	assert_is_ignored(true, "dir1/dir2/dir3/parent2/kid2/file");
	assert_is_ignored(false, "dir1/parent2/file");
	assert_is_ignored(false, "dir1/kid2/file");
}
160

161 162
void test_attr_ignore__globs_and_path_delimiters(void)
{
163 164 165 166 167 168 169 170 171 172
	cl_git_rewritefile("attr/.gitignore", "foo/bar/**");
	assert_is_ignored(true, "foo/bar/baz");
	assert_is_ignored(true, "foo/bar/baz/quux");

	cl_git_rewritefile("attr/.gitignore", "_*/");
	assert_is_ignored(true, "sub/_test/a/file");
	assert_is_ignored(false, "test_folder/file");
	assert_is_ignored(true, "_test/file");
	assert_is_ignored(true, "_test/a/file");

173
	cl_git_rewritefile("attr/.gitignore", "**/_*/");
174
	assert_is_ignored(true, "sub/_test/a/file");
175 176 177 178 179 180
	assert_is_ignored(false, "test_folder/file");
	assert_is_ignored(true, "_test/file");
	assert_is_ignored(true, "_test/a/file");

	cl_git_rewritefile("attr/.gitignore", "**/_*/foo/bar/*ux");

181
	assert_is_ignored(true, "sub/_test/foo/bar/qux/file");
182 183 184 185 186
	assert_is_ignored(true, "_test/foo/bar/qux/file");
	assert_is_ignored(true, "_test/foo/bar/crux/file");
	assert_is_ignored(false, "_test/foo/bar/code/file");
}

187 188 189 190 191 192 193 194 195 196 197 198
void test_attr_ignore__skip_gitignore_directory(void)
{
	cl_git_rewritefile("attr/.git/info/exclude", "/NewFolder\n/NewFolder/NewFolder");
	p_unlink("attr/.gitignore");
	cl_assert(!git_path_exists("attr/.gitignore"));
	p_mkdir("attr/.gitignore", 0777);
	cl_git_mkfile("attr/.gitignore/garbage.txt", "new_file\n");

	assert_is_ignored(false, "File.txt");
	assert_is_ignored(true, "NewFolder");
	assert_is_ignored(true, "NewFolder/NewFolder");
	assert_is_ignored(true, "NewFolder/NewFolder/File.txt");
199
}
200

201 202 203 204 205 206 207 208 209 210 211 212 213 214
void test_attr_ignore__subdirectory_gitignore(void)
{
	p_unlink("attr/.gitignore");
	cl_assert(!git_path_exists("attr/.gitignore"));
	cl_git_mkfile(
		"attr/.gitignore",
		"file1\n");
	p_mkdir("attr/dir", 0777);
	cl_git_mkfile(
		"attr/dir/.gitignore",
		"file2/\n");

	assert_is_ignored(true, "file1");
	assert_is_ignored(true, "dir/file1");
215
	assert_is_ignored(true, "dir/file2/actual_file");  /* in ignored dir */
216 217 218
	assert_is_ignored(false, "dir/file3");
}

219 220 221 222 223 224
void test_attr_ignore__expand_tilde_to_homedir(void)
{
	git_config *cfg;

	assert_is_ignored(false, "example.global_with_tilde");

225
	cl_fake_home();
226

227 228
	/* construct fake home with fake global excludes */
	cl_git_mkfile("home/globalexclude", "# found me\n*.global_with_tilde\n");
229 230

	cl_git_pass(git_repository_config(&cfg, g_repo));
231
	cl_git_pass(git_config_set_string(cfg, "core.excludesfile", "~/globalexclude"));
232 233 234 235 236 237 238 239
	git_config_free(cfg);

	git_attr_cache_flush(g_repo); /* must reset to pick up change */

	assert_is_ignored(true, "example.global_with_tilde");

	cl_git_pass(git_futils_rmdir_r("home", NULL, GIT_RMDIR_REMOVE_FILES));

240
	cl_fake_home_cleanup(NULL);
241

242 243 244
	git_attr_cache_flush(g_repo); /* must reset to pick up change */

	assert_is_ignored(false, "example.global_with_tilde");
245
}
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275

/* Ensure that the .gitignore in the subdirectory only affects
 * items in the subdirectory. */
void test_attr_ignore__gitignore_in_subdir(void)
{
	cl_git_rmfile("attr/.gitignore");

	cl_must_pass(p_mkdir("attr/dir1", 0777));
	cl_must_pass(p_mkdir("attr/dir1/dir2", 0777));
	cl_must_pass(p_mkdir("attr/dir1/dir2/dir3", 0777));

	cl_git_mkfile("attr/dir1/dir2/dir3/.gitignore", "dir1/\ndir1/subdir/");

	assert_is_ignored(false, "dir1/file");
	assert_is_ignored(false, "dir1/dir2/file");
	assert_is_ignored(false, "dir1/dir2/dir3/file");
	assert_is_ignored(true,  "dir1/dir2/dir3/dir1/file");
	assert_is_ignored(true,  "dir1/dir2/dir3/dir1/subdir/foo");

	if (cl_repo_get_bool(g_repo, "core.ignorecase")) {
		cl_git_mkfile("attr/dir1/dir2/dir3/.gitignore", "DiR1/\nDiR1/subdir/\n");

		assert_is_ignored(false, "dir1/file");
		assert_is_ignored(false, "dir1/dir2/file");
		assert_is_ignored(false, "dir1/dir2/dir3/file");
		assert_is_ignored(true,  "dir1/dir2/dir3/dir1/file");
		assert_is_ignored(true,  "dir1/dir2/dir3/dir1/subdir/foo");
	}
}

276 277
/* Ensure that files do not match folder cases */
void test_attr_ignore__dont_ignore_files_for_folder(void)
278 279 280
{
	cl_git_rmfile("attr/.gitignore");

281
	cl_git_mkfile("attr/dir/.gitignore", "test/\n");
282

283 284
	/* Create "test" as a file; ensure it is not ignored. */
	cl_git_mkfile("attr/dir/test", "This is a file.");
285

286 287 288
	assert_is_ignored(false, "dir/test");
	if (cl_repo_get_bool(g_repo, "core.ignorecase"))
		assert_is_ignored(false, "dir/TeSt");
289

290 291 292 293 294 295 296
	/* Create "test" as a directory; ensure it is ignored. */
	cl_git_rmfile("attr/dir/test");
	cl_must_pass(p_mkdir("attr/dir/test", 0777));

	assert_is_ignored(true, "dir/test");
	if (cl_repo_get_bool(g_repo, "core.ignorecase"))
		assert_is_ignored(true, "dir/TeSt");
297

298 299 300 301 302 303 304 305 306
	/* Remove "test" entirely; ensure it is not ignored.
	 * (As it doesn't exist, it is not a directory.)
	 */
	cl_must_pass(p_rmdir("attr/dir/test"));

	assert_is_ignored(false, "dir/test");
	if (cl_repo_get_bool(g_repo, "core.ignorecase"))
		assert_is_ignored(false, "dir/TeSt");
}
307 308 309 310 311 312 313 314 315 316 317 318 319

void test_attr_ignore__symlink_to_outside(void)
{
#ifdef GIT_WIN32
	cl_skip();
#endif

	cl_git_rewritefile("attr/.gitignore", "symlink\n");
	cl_git_mkfile("target", "target");
	cl_git_pass(p_symlink("../target", "attr/symlink"));
	assert_is_ignored(true, "symlink");
	assert_is_ignored(true, "lala/../symlink");
}
320 321 322 323 324 325 326 327 328 329 330 331

void test_attr_ignore__test(void)
{
	cl_git_rewritefile("attr/.gitignore",
		"/*/\n"
		"!/src\n");
	assert_is_ignored(false, "src/foo.c");
	assert_is_ignored(false, "src/foo/foo.c");
	assert_is_ignored(false, "README.md");
	assert_is_ignored(true, "dist/foo.o");
	assert_is_ignored(true, "bin/foo");
}
332 333 334 335 336 337 338 339 340

void test_attr_ignore__unignore_dir_succeeds(void)
{
	cl_git_rewritefile("attr/.gitignore",
		"*.c\n"
		"!src/*.c\n");
	assert_is_ignored(false, "src/foo.c");
	assert_is_ignored(true, "src/foo/foo.c");
}
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

void test_attr_ignore__case_insensitive_unignores_previous_rule(void)
{
	git_config *cfg;

	cl_git_rewritefile("attr/.gitignore",
		"/case\n"
		"!/Case/\n");

	cl_git_pass(git_repository_config(&cfg, g_repo));
	cl_git_pass(git_config_set_bool(cfg, "core.ignorecase", true));

	cl_must_pass(p_mkdir("attr/case", 0755));
	cl_git_mkfile("attr/case/file", "content");

	assert_is_ignored(false, "case/file");
}

void test_attr_ignore__case_sensitive_unignore_does_nothing(void)
{
	git_config *cfg;

	cl_git_rewritefile("attr/.gitignore",
		"/case\n"
		"!/Case/\n");

	cl_git_pass(git_repository_config(&cfg, g_repo));
	cl_git_pass(git_config_set_bool(cfg, "core.ignorecase", false));

	cl_must_pass(p_mkdir("attr/case", 0755));
	cl_git_mkfile("attr/case/file", "content");

	assert_is_ignored(true, "case/file");
}