Merge pull request #891 from arrbee/internal-ignore-api API for managing in-memory ignore rules
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 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
diff --git a/include/git2.h b/include/git2.h
index 4016748..805044a 100644
--- a/include/git2.h
+++ b/include/git2.h
@@ -41,6 +41,7 @@
#include "git2/checkout.h"
#include "git2/attr.h"
+#include "git2/ignore.h"
#include "git2/branch.h"
#include "git2/refspec.h"
#include "git2/net.h"
diff --git a/include/git2/ignore.h b/include/git2/ignore.h
new file mode 100644
index 0000000..f7e04e8
--- /dev/null
+++ b/include/git2/ignore.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2012 the libgit2 contributors
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_git_ignore_h__
+#define INCLUDE_git_ignore_h__
+
+#include "common.h"
+#include "types.h"
+
+GIT_BEGIN_DECL
+
+/**
+ * Add ignore rules for a repository.
+ *
+ * Excludesfile rules (i.e. .gitignore rules) are generally read from
+ * .gitignore files in the repository tree or from a shared system file
+ * only if a "core.excludesfile" config value is set. The library also
+ * keeps a set of per-repository internal ignores that can be configured
+ * in-memory and will not persist. This function allows you to add to
+ * that internal rules list.
+ *
+ * Example usage:
+ *
+ * error = git_ignore_add(myrepo, "*.c\ndir/\nFile with space\n");
+ *
+ * This would add three rules to the ignores.
+ *
+ * @param repo The repository to add ignore rules to.
+ * @param rules Text of rules, a la the contents of a .gitignore file.
+ * It is okay to have multiple rules in the text; if so,
+ * each rule should be terminated with a newline.
+ * @return 0 on success
+ */
+GIT_EXTERN(int) git_ignore_add_rule(
+ git_repository *repo,
+ const char *rules);
+
+/**
+ * Clear ignore rules that were explicitly added.
+ *
+ * Clears the internal ignore rules that have been set up. This will not
+ * turn off the rules in .gitignore files that actually exist in the
+ * filesystem.
+ *
+ * @param repo The repository to remove ignore rules from.
+ * @return 0 on success
+ */
+GIT_EXTERN(int) git_ignore_clear_internal_rules(
+ git_repository *repo);
+
+/**
+ * Test if the ignore rules apply to a given path.
+ *
+ * This function simply checks the ignore rules to see if they would apply
+ * to the given file. This indicates if the file would be ignored regardless
+ * of whether the file is already in the index or commited to the repository.
+ *
+ * @param ignored boolean returning 0 if the file is not ignored, 1 if it is
+ * @param repo a repository object
+ * @param path the file to check ignores for, relative to the repo's workdir.
+ * @return 0 if ignore rules could be processed for the file (regardless
+ * of whether it exists or not), or an error < 0 if they could not.
+ */
+GIT_EXTERN(int) git_ignore_path_is_ignored(
+ int *ignored,
+ git_repository *repo,
+ const char *path);
+
+GIT_END_DECL
+
+#endif
diff --git a/src/attr.c b/src/attr.c
index de714a6..8a7ff28 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -612,6 +612,16 @@ int git_attr_cache__init(git_repository *repo)
if (ret < 0 && ret != GIT_ENOTFOUND)
return ret;
+ if (ret == GIT_ENOTFOUND) {
+ git_buf dflt = GIT_BUF_INIT;
+
+ ret = git_futils_find_global_file(&dflt, GIT_IGNORE_CONFIG_DEFAULT);
+ if (!ret)
+ cache->cfg_excl_file = git_buf_detach(&dflt);
+
+ git_buf_free(&dflt);
+ }
+
giterr_clear();
/* allocate hashtable for attribute and ignore file contents */
diff --git a/src/attr.h b/src/attr.h
index a35b116..78cfb57 100644
--- a/src/attr.h
+++ b/src/attr.h
@@ -12,6 +12,7 @@
#define GIT_ATTR_CONFIG "core.attributesfile"
#define GIT_IGNORE_CONFIG "core.excludesfile"
+#define GIT_IGNORE_CONFIG_DEFAULT ".config/git/ignore"
typedef struct {
int initialized;
diff --git a/src/ignore.c b/src/ignore.c
index 93d979f..1ac8afd 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -1,3 +1,4 @@
+#include "git2/ignore.h"
#include "ignore.h"
#include "path.h"
@@ -203,3 +204,51 @@ cleanup:
git_attr_path__free(&path);
return 0;
}
+
+int git_ignore_add_rule(
+ git_repository *repo,
+ const char *rules)
+{
+ int error;
+ git_attr_file *ign_internal;
+
+ error = git_attr_cache__internal_file(
+ repo, GIT_IGNORE_INTERNAL, &ign_internal);
+
+ if (!error && ign_internal != NULL)
+ error = parse_ignore_file(repo, rules, ign_internal);
+
+ return error;
+}
+
+int git_ignore_clear_internal_rules(
+ git_repository *repo)
+{
+ int error;
+ git_attr_file *ign_internal;
+
+ error = git_attr_cache__internal_file(
+ repo, GIT_IGNORE_INTERNAL, &ign_internal);
+
+ if (!error && ign_internal != NULL)
+ git_attr_file__clear_rules(ign_internal);
+
+ return error;
+}
+
+int git_ignore_path_is_ignored(
+ int *ignored,
+ git_repository *repo,
+ const char *path)
+{
+ int error;
+ git_ignores ignores;
+
+ if (git_ignore__for_path(repo, path, &ignores) < 0)
+ return -1;
+
+ error = git_ignore__lookup(&ignores, path, ignored);
+ git_ignore__free(&ignores);
+ return error;
+}
+
diff --git a/src/status.c b/src/status.c
index 8e46255..3d3d15d 100644
--- a/src/status.c
+++ b/src/status.c
@@ -243,14 +243,6 @@ int git_status_should_ignore(
git_repository *repo,
const char *path)
{
- int error;
- git_ignores ignores;
-
- if (git_ignore__for_path(repo, path, &ignores) < 0)
- return -1;
-
- error = git_ignore__lookup(&ignores, path, ignored);
- git_ignore__free(&ignores);
- return error;
+ return git_ignore_path_is_ignored(ignored, repo, path);
}
diff --git a/tests-clar/status/ignore.c b/tests-clar/status/ignore.c
index 0384306..9c6d7ee 100644
--- a/tests-clar/status/ignore.c
+++ b/tests-clar/status/ignore.c
@@ -139,9 +139,63 @@ void test_status_ignore__ignore_pattern_contains_space(void)
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_rewritefile("empty_standard_repo/.gitignore", "foo bar.txt\n");
+ 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);
+
cl_git_pass(git_futils_mkdir_r("empty_standard_repo/foo", NULL, mode));
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);
}
+
+void test_status_ignore__adding_internal_ignores(void)
+{
+ int ignored;
+
+ g_repo = cl_git_sandbox_init("empty_standard_repo");
+
+ cl_git_pass(git_status_should_ignore(&ignored, g_repo, "one.txt"));
+ cl_assert(!ignored);
+ cl_git_pass(git_status_should_ignore(&ignored, g_repo, "two.bar"));
+ cl_assert(!ignored);
+
+ cl_git_pass(git_ignore_add_rule(g_repo, "*.nomatch\n"));
+
+ cl_git_pass(git_status_should_ignore(&ignored, g_repo, "one.txt"));
+ cl_assert(!ignored);
+ cl_git_pass(git_status_should_ignore(&ignored, g_repo, "two.bar"));
+ cl_assert(!ignored);
+
+ cl_git_pass(git_ignore_add_rule(g_repo, "*.txt\n"));
+
+ cl_git_pass(git_status_should_ignore(&ignored, g_repo, "one.txt"));
+ cl_assert(ignored);
+ cl_git_pass(git_status_should_ignore(&ignored, g_repo, "two.bar"));
+ cl_assert(!ignored);
+
+ cl_git_pass(git_ignore_add_rule(g_repo, "*.bar\n"));
+
+ cl_git_pass(git_status_should_ignore(&ignored, g_repo, "one.txt"));
+ cl_assert(ignored);
+ cl_git_pass(git_status_should_ignore(&ignored, g_repo, "two.bar"));
+ cl_assert(ignored);
+
+ cl_git_pass(git_ignore_clear_internal_rules(g_repo));
+
+ cl_git_pass(git_status_should_ignore(&ignored, g_repo, "one.txt"));
+ cl_assert(!ignored);
+ cl_git_pass(git_status_should_ignore(&ignored, g_repo, "two.bar"));
+ cl_assert(!ignored);
+
+ cl_git_pass(git_ignore_add_rule(
+ g_repo, "multiple\n*.rules\n# comment line\n*.bar\n"));
+
+ cl_git_pass(git_status_should_ignore(&ignored, g_repo, "one.txt"));
+ cl_assert(!ignored);
+ cl_git_pass(git_status_should_ignore(&ignored, g_repo, "two.bar"));
+ cl_assert(ignored);
+}