Commit 02df42dd by Russell Belfer

Set up default internal ignores

This adds "." ".." and ".git" to the internal ignores list by
default - asking about paths with these files will always say
that they are ignored.
parent cfeef7ce
...@@ -41,9 +41,10 @@ GIT_EXTERN(int) git_ignore_add_rule( ...@@ -41,9 +41,10 @@ GIT_EXTERN(int) git_ignore_add_rule(
/** /**
* Clear ignore rules that were explicitly added. * Clear ignore rules that were explicitly added.
* *
* Clears the internal ignore rules that have been set up. This will not * Resets to the default internal ignore rules. This will not turn off
* turn off the rules in .gitignore files that actually exist in the * rules in .gitignore files that actually exist in the filesystem.
* filesystem. *
* The default internal ignores ignore ".", ".." and ".git" entries.
* *
* @param repo The repository to remove ignore rules from. * @param repo The repository to remove ignore rules from.
* @return 0 on success * @return 0 on success
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#define GIT_IGNORE_FILE_INREPO "info/exclude" #define GIT_IGNORE_FILE_INREPO "info/exclude"
#define GIT_IGNORE_FILE ".gitignore" #define GIT_IGNORE_FILE ".gitignore"
#define GIT_IGNORE_DEFAULT_RULES ".\n..\n.git\n"
static int parse_ignore_file( static int parse_ignore_file(
git_repository *repo, void *parsedata, const char *buffer, git_attr_file *ignores) git_repository *repo, void *parsedata, const char *buffer, git_attr_file *ignores)
{ {
...@@ -88,6 +90,19 @@ static int push_one_ignore(void *ref, git_buf *path) ...@@ -88,6 +90,19 @@ static int push_one_ignore(void *ref, git_buf *path)
return push_ignore_file(ign->repo, ign, &ign->ign_path, path->ptr, GIT_IGNORE_FILE); return push_ignore_file(ign->repo, ign, &ign->ign_path, path->ptr, GIT_IGNORE_FILE);
} }
static int get_internal_ignores(git_attr_file **ign, git_repository *repo)
{
int error;
if (!(error = git_attr_cache__init(repo)))
error = git_attr_cache__internal_file(repo, GIT_IGNORE_INTERNAL, ign);
if (!error && !(*ign)->rules.length)
error = parse_ignore_file(repo, NULL, GIT_IGNORE_DEFAULT_RULES, *ign);
return error;
}
int git_ignore__for_path( int git_ignore__for_path(
git_repository *repo, git_repository *repo,
const char *path, const char *path,
...@@ -129,8 +144,7 @@ int git_ignore__for_path( ...@@ -129,8 +144,7 @@ int git_ignore__for_path(
goto cleanup; goto cleanup;
/* set up internals */ /* set up internals */
error = git_attr_cache__internal_file( error = get_internal_ignores(&ignores->ign_internal, repo);
repo, GIT_IGNORE_INTERNAL, &ignores->ign_internal);
if (error < 0) if (error < 0)
goto cleanup; goto cleanup;
...@@ -239,16 +253,6 @@ cleanup: ...@@ -239,16 +253,6 @@ cleanup:
return 0; return 0;
} }
static int get_internal_ignores(git_attr_file **ign, git_repository *repo)
{
int error;
if (!(error = git_attr_cache__init(repo)))
error = git_attr_cache__internal_file(repo, GIT_IGNORE_INTERNAL, ign);
return error;
}
int git_ignore_add_rule( int git_ignore_add_rule(
git_repository *repo, git_repository *repo,
const char *rules) const char *rules)
...@@ -268,9 +272,13 @@ int git_ignore_clear_internal_rules( ...@@ -268,9 +272,13 @@ int git_ignore_clear_internal_rules(
int error; int error;
git_attr_file *ign_internal; git_attr_file *ign_internal;
if (!(error = get_internal_ignores(&ign_internal, repo))) if (!(error = get_internal_ignores(&ign_internal, repo))) {
git_attr_file__clear_rules(ign_internal); git_attr_file__clear_rules(ign_internal);
return parse_ignore_file(
repo, NULL, GIT_IGNORE_DEFAULT_RULES, ign_internal);
}
return error; return error;
} }
......
...@@ -341,3 +341,41 @@ void test_status_ignore__internal_ignores_inside_deep_paths(void) ...@@ -341,3 +341,41 @@ void test_status_ignore__internal_ignores_inside_deep_paths(void)
cl_git_pass(git_status_should_ignore(&ignored, g_repo, "xthis/is/deep")); cl_git_pass(git_status_should_ignore(&ignored, g_repo, "xthis/is/deep"));
cl_assert(!ignored); cl_assert(!ignored);
} }
void test_status_ignore__automatically_ignore_bad_files(void)
{
int ignored;
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_pass(git_status_should_ignore(&ignored, g_repo, ".git"));
cl_assert(ignored);
cl_git_pass(git_status_should_ignore(&ignored, g_repo, "this/file/."));
cl_assert(ignored);
cl_git_pass(git_status_should_ignore(&ignored, g_repo, "path/../funky"));
cl_assert(ignored);
cl_git_pass(git_status_should_ignore(&ignored, g_repo, "path/whatever.c"));
cl_assert(!ignored);
cl_git_pass(git_ignore_add_rule(g_repo, "*.c\n"));
cl_git_pass(git_status_should_ignore(&ignored, g_repo, ".git"));
cl_assert(ignored);
cl_git_pass(git_status_should_ignore(&ignored, g_repo, "this/file/."));
cl_assert(ignored);
cl_git_pass(git_status_should_ignore(&ignored, g_repo, "path/../funky"));
cl_assert(ignored);
cl_git_pass(git_status_should_ignore(&ignored, g_repo, "path/whatever.c"));
cl_assert(ignored);
cl_git_pass(git_ignore_clear_internal_rules(g_repo));
cl_git_pass(git_status_should_ignore(&ignored, g_repo, ".git"));
cl_assert(ignored);
cl_git_pass(git_status_should_ignore(&ignored, g_repo, "this/file/."));
cl_assert(ignored);
cl_git_pass(git_status_should_ignore(&ignored, g_repo, "path/../funky"));
cl_assert(ignored);
cl_git_pass(git_status_should_ignore(&ignored, g_repo, "path/whatever.c"));
cl_assert(!ignored);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment