Patch cleanup for merge After reviewing the gitignore support with Vicent, we came up with a list of minor cleanups to prepare for merge, including: * checking git_repository_config error returns * renaming git_ignore_is_ignored and moving to status.h * fixing next_line skipping to include \r skips * commenting on where ignores are and are not included
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
diff --git a/include/git2/index.h b/include/git2/index.h
index 627d6c4..5018c89 100644
--- a/include/git2/index.h
+++ b/include/git2/index.h
@@ -169,6 +169,10 @@ GIT_EXTERN(void) git_index_uniq(git_index *index);
*
* This method will fail in bare index instances.
*
+ * This forces the file to be added to the index, not looking
+ * at gitignore rules. Those rules can be evaluated through
+ * the git_status APIs (in status.h) before calling this.
+ *
* @param index an existing index object
* @param path filename to add
* @param stage stage for the entry
diff --git a/include/git2/status.h b/include/git2/status.h
index 0f2b63d..c0f38c5 100644
--- a/include/git2/status.h
+++ b/include/git2/status.h
@@ -58,6 +58,22 @@ GIT_EXTERN(int) git_status_foreach(git_repository *repo, int (*callback)(const c
*/
GIT_EXTERN(int) git_status_file(unsigned int *status_flags, git_repository *repo, const char *path);
+/**
+ * Test if the ignore rules apply to a given file.
+ *
+ * This function simply checks the ignore rules to see if they would apply
+ * to the given file. Unlike git_status_file(), this indicates if the file
+ * would be ignored regardless of whether the file is already in the index
+ * or in the repository.
+ *
+ * @param repo a repository object
+ * @param path the file to check ignores for, rooted at the repo's workdir
+ * @param ignored boolean returning 0 if the file is not ignored, 1 if it is
+ * @return GIT_SUCCESS if the 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_status_should_ignore(git_repository *repo, const char *path, int *ignored);
+
/** @} */
GIT_END_DECL
#endif
diff --git a/src/attr.c b/src/attr.c
index cbc2a5b..dc42379 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -3,11 +3,6 @@
#include "config.h"
#include <ctype.h>
-#define GIT_ATTR_FILE_INREPO "info/attributes"
-#define GIT_ATTR_FILE ".gitattributes"
-#define GIT_ATTR_FILE_SYSTEM "gitattributes"
-#define GIT_ATTR_CONFIG "core.attributesfile"
-
static int collect_attr_files(
git_repository *repo, const char *path, git_vector *files);
@@ -304,7 +299,7 @@ static int collect_attr_files(
if (error < GIT_SUCCESS)
goto cleanup;
- if (git_repository_config(&cfg, repo) == GIT_SUCCESS) {
+ if ((error = git_repository_config(&cfg, repo)) == GIT_SUCCESS) {
const char *core_attribs = NULL;
git_config_get_string(cfg, GIT_ATTR_CONFIG, &core_attribs);
git_clearerror(); /* don't care if attributesfile is not set */
diff --git a/src/attr_file.h b/src/attr_file.h
index 86836b5..7190c4c 100644
--- a/src/attr_file.h
+++ b/src/attr_file.h
@@ -11,6 +11,11 @@
#include "vector.h"
#include "hashtable.h"
+#define GIT_ATTR_FILE ".gitattributes"
+#define GIT_ATTR_FILE_INREPO "info/attributes"
+#define GIT_ATTR_FILE_SYSTEM "gitattributes"
+#define GIT_ATTR_CONFIG "core.attributesfile"
+
#define GIT_ATTR_FNMATCH_NEGATIVE (1U << 0)
#define GIT_ATTR_FNMATCH_DIRECTORY (1U << 1)
#define GIT_ATTR_FNMATCH_FULLPATH (1U << 2)
diff --git a/src/ignore.c b/src/ignore.c
index cdc3eda..1040574 100644
--- a/src/ignore.c
+++ b/src/ignore.c
@@ -106,7 +106,7 @@ int git_ignore__for_path(git_repository *repo, const char *path, git_vector *sta
goto cleanup;
/* load core.excludesfile */
- if (git_repository_config(&cfg, repo) == GIT_SUCCESS) {
+ if ((error = git_repository_config(&cfg, repo)) == GIT_SUCCESS) {
const char *core_ignore;
error = git_config_get_string(cfg, GIT_IGNORE_CONFIG, &core_ignore);
if (error == GIT_SUCCESS && core_ignore != NULL)
@@ -157,18 +157,3 @@ found:
return error;
}
-
-
-int git_ignore_is_ignored(git_repository *repo, const char *path, int *ignored)
-{
- int error;
- git_vector ignores = GIT_VECTOR_INIT;
-
- if ((error = git_ignore__for_path(repo, path, &ignores)) == GIT_SUCCESS)
- error = git_ignore__lookup(&ignores, path, ignored);
-
- git_ignore__free(&ignores);
-
- return error;
-}
-
diff --git a/src/ignore.h b/src/ignore.h
index a6e6a1a..2954445 100644
--- a/src/ignore.h
+++ b/src/ignore.h
@@ -14,6 +14,4 @@ extern int git_ignore__for_path(git_repository *repo, const char *path, git_vect
extern void git_ignore__free(git_vector *stack);
extern int git_ignore__lookup(git_vector *stack, const char *path, int *ignored);
-extern int git_ignore_is_ignored(git_repository *repo, const char *path, int *ignored);
-
#endif
diff --git a/src/status.c b/src/status.c
index 72ee7b0..3ead15a 100644
--- a/src/status.c
+++ b/src/status.c
@@ -761,3 +761,18 @@ static int alphasorted_futils_direach(
git_vector_free(&entry_names);
return error;
}
+
+
+int git_status_should_ignore(git_repository *repo, const char *path, int *ignored)
+{
+ int error;
+ git_vector ignores = GIT_VECTOR_INIT;
+
+ if ((error = git_ignore__for_path(repo, path, &ignores)) == GIT_SUCCESS)
+ error = git_ignore__lookup(&ignores, path, ignored);
+
+ git_ignore__free(&ignores);
+
+ return error;
+}
+
diff --git a/src/util.h b/src/util.h
index bd76a26..6c929cf 100644
--- a/src/util.h
+++ b/src/util.h
@@ -105,7 +105,7 @@ extern void git__strtolower(char *str);
GIT_INLINE(const char *) git__next_line(const char *s)
{
while (*s && *s != '\n') s++;
- while (*s == '\n') s++;
+ while (*s == '\n' || *s == '\r') s++;
return s;
}
diff --git a/tests-clay/status/worktree.c b/tests-clay/status/worktree.c
index af6f005..2183649 100644
--- a/tests-clay/status/worktree.c
+++ b/tests-clay/status/worktree.c
@@ -142,13 +142,13 @@ void test_status_worktree__ignores(void)
int i, ignored;
for (i = 0; i < (int)entry_count0; i++) {
- cl_git_pass(git_ignore_is_ignored(_repository, entry_paths0[i], &ignored));
+ cl_git_pass(git_status_should_ignore(_repository, entry_paths0[i], &ignored));
cl_assert(ignored == (entry_statuses0[i] == GIT_STATUS_IGNORED));
}
- cl_git_pass(git_ignore_is_ignored(_repository, "nonexistent_file", &ignored));
+ cl_git_pass(git_status_should_ignore(_repository, "nonexistent_file", &ignored));
cl_assert(!ignored);
- cl_git_pass(git_ignore_is_ignored(_repository, "ignored_nonexistent_file", &ignored));
+ cl_git_pass(git_status_should_ignore(_repository, "ignored_nonexistent_file", &ignored));
cl_assert(ignored);
}