Commit a7b4b639936709dbf53d09077b307edb4a633303

buddyspike 2019-08-24T12:14:31

ignore: correct handling of nested rules overriding wild card unignore problem: filesystem_iterator loads .gitignore files in top-down order. subsequently, ignore module evaluates them in the order they are loaded. this creates a problem if we have unignored a rule (using a wild card) in a sub dir and ignored it again in a level further below (see the test included in this patch). solution: process ignores in reverse order. closes #4963

diff --git a/src/ignore.c b/src/ignore.c
index c2d9944..f5fb507 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -444,7 +444,7 @@ static bool ignore_lookup_in_rules(
 int git_ignore__lookup(
 	int *out, git_ignores *ignores, const char *pathname, git_dir_flag dir_flag)
 {
-	unsigned int i;
+	size_t i;
 	git_attr_file *file;
 	git_attr_path path;
 
@@ -458,8 +458,11 @@ int git_ignore__lookup(
 	if (ignore_lookup_in_rules(out, ignores->ign_internal, &path))
 		goto cleanup;
 
-	/* next process files in the path */
-	git_vector_foreach(&ignores->ign_path, i, file) {
+	/* next process files in the path.
+	 * this process has to process ignores in reverse order
+	 * to ensure correct prioritization of rules
+	 */
+	git_vector_rforeach(&ignores->ign_path, i, file) {
 		if (ignore_lookup_in_rules(out, file, &path))
 			goto cleanup;
 	}
diff --git a/tests/ignore/status.c b/tests/ignore/status.c
index 6082a81..5e67c12 100644
--- a/tests/ignore/status.c
+++ b/tests/ignore/status.c
@@ -1290,3 +1290,47 @@ void test_ignore_status__leading_spaces_are_significant(void)
 	assert_is_ignored(" # not a comment");
 	assert_is_ignored("d.test");
 }
+
+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);
+}