Recursing into ignored dirs for diff and status This implements working versions of GIT_DIFF_RECURSE_IGNORED_DIRS and GIT_STATUS_OPT_RECURSE_IGNORED_DIRS along with some tests for the newly available behaviors. This is not turned on by default for status, but can be accessed via the options to the extended version of the command.
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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
diff --git a/include/git2/diff.h b/include/git2/diff.h
index e49e6e5..d9ceadf 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -123,7 +123,7 @@ typedef enum {
* will be marked with only a single entry in the diff list; this flag
* adds all files under the directory as IGNORED entries, too.
*/
- GIT_DIFF_RECURSE_IGNORED_DIRS = (1 << 10),
+ GIT_DIFF_RECURSE_IGNORED_DIRS = (1 << 18),
} git_diff_option_t;
/**
diff --git a/include/git2/status.h b/include/git2/status.h
index d0c4a49..fa62820 100644
--- a/include/git2/status.h
+++ b/include/git2/status.h
@@ -127,18 +127,22 @@ typedef enum {
* will.
* - GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH indicates that the given path
* will be treated as a literal path, and not as a pathspec.
+ * - GIT_STATUS_OPT_RECURSE_IGNORED_DIRS indicates that the contents of
+ * ignored directories should be included in the status. This is like
+ * doing `git ls-files -o -i --exclude-standard` with core git.
*
* Calling `git_status_foreach()` is like calling the extended version
* with: GIT_STATUS_OPT_INCLUDE_IGNORED, GIT_STATUS_OPT_INCLUDE_UNTRACKED,
* and GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS.
*/
typedef enum {
- GIT_STATUS_OPT_INCLUDE_UNTRACKED = (1 << 0),
- GIT_STATUS_OPT_INCLUDE_IGNORED = (1 << 1),
- GIT_STATUS_OPT_INCLUDE_UNMODIFIED = (1 << 2),
- GIT_STATUS_OPT_EXCLUDE_SUBMODULES = (1 << 3),
- GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS = (1 << 4),
- GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH = (1 << 5),
+ GIT_STATUS_OPT_INCLUDE_UNTRACKED = (1u << 0),
+ GIT_STATUS_OPT_INCLUDE_IGNORED = (1u << 1),
+ GIT_STATUS_OPT_INCLUDE_UNMODIFIED = (1u << 2),
+ GIT_STATUS_OPT_EXCLUDE_SUBMODULES = (1u << 3),
+ GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS = (1u << 4),
+ GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH = (1u << 5),
+ GIT_STATUS_OPT_RECURSE_IGNORED_DIRS = (1u << 6),
} git_status_opt_t;
/**
diff --git a/src/diff.c b/src/diff.c
index fb69f89..11ffc48 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -12,6 +12,9 @@
#include "filter.h"
#include "pathspec.h"
+#define DIFF_FLAG_IS_SET(DIFF,FLAG) (((DIFF)->opts.flags & (FLAG)) != 0)
+#define DIFF_FLAG_ISNT_SET(DIFF,FLAG) (((DIFF)->opts.flags & (FLAG)) == 0)
+
static git_diff_delta *diff_delta__alloc(
git_diff_list *diff,
git_delta_t status,
@@ -29,7 +32,7 @@ static git_diff_delta *diff_delta__alloc(
delta->new_file.path = delta->old_file.path;
- if (diff->opts.flags & GIT_DIFF_REVERSE) {
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
switch (status) {
case GIT_DELTA_ADDED: status = GIT_DELTA_DELETED; break;
case GIT_DELTA_DELETED: status = GIT_DELTA_ADDED; break;
@@ -63,17 +66,18 @@ static int diff_delta__from_one(
int notify_res;
if (status == GIT_DELTA_IGNORED &&
- (diff->opts.flags & GIT_DIFF_INCLUDE_IGNORED) == 0)
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_IGNORED))
return 0;
if (status == GIT_DELTA_UNTRACKED &&
- (diff->opts.flags & GIT_DIFF_INCLUDE_UNTRACKED) == 0)
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNTRACKED))
return 0;
if (!git_pathspec_match_path(
&diff->pathspec, entry->path,
- (diff->opts.flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH) != 0,
- (diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0, &matched_pathspec))
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH),
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_DELTAS_ARE_ICASE),
+ &matched_pathspec))
return 0;
delta = diff_delta__alloc(diff, status, entry->path);
@@ -124,10 +128,10 @@ static int diff_delta__from_two(
int notify_res;
if (status == GIT_DELTA_UNMODIFIED &&
- (diff->opts.flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0)
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED))
return 0;
- if ((diff->opts.flags & GIT_DIFF_REVERSE) != 0) {
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
uint32_t temp_mode = old_mode;
const git_index_entry *temp_entry = old_entry;
old_entry = new_entry;
@@ -149,7 +153,7 @@ static int diff_delta__from_two(
delta->new_file.mode = new_mode;
if (new_oid) {
- if ((diff->opts.flags & GIT_DIFF_REVERSE) != 0)
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE))
git_oid_cpy(&delta->old_file.oid, new_oid);
else
git_oid_cpy(&delta->new_file.oid, new_oid);
@@ -316,14 +320,14 @@ static git_diff_list *git_diff_list_alloc(
if (!diff->opts.old_prefix || !diff->opts.new_prefix)
goto fail;
- if (diff->opts.flags & GIT_DIFF_REVERSE) {
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_REVERSE)) {
const char *swap = diff->opts.old_prefix;
diff->opts.old_prefix = diff->opts.new_prefix;
diff->opts.new_prefix = swap;
}
/* INCLUDE_TYPECHANGE_TREES implies INCLUDE_TYPECHANGE */
- if (diff->opts.flags & GIT_DIFF_INCLUDE_TYPECHANGE_TREES)
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES))
diff->opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE;
return diff;
@@ -452,8 +456,9 @@ static int maybe_modified(
if (!git_pathspec_match_path(
&diff->pathspec, oitem->path,
- (diff->opts.flags & GIT_DIFF_DISABLE_PATHSPEC_MATCH) != 0,
- (diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) != 0, &matched_pathspec))
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_DISABLE_PATHSPEC_MATCH),
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_DELTAS_ARE_ICASE),
+ &matched_pathspec))
return 0;
/* on platforms with no symlinks, preserve mode of existing symlinks */
@@ -478,7 +483,7 @@ static int maybe_modified(
/* if basic type of file changed, then split into delete and add */
else if (GIT_MODE_TYPE(omode) != GIT_MODE_TYPE(nmode)) {
- if ((diff->opts.flags & GIT_DIFF_INCLUDE_TYPECHANGE) != 0)
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE))
status = GIT_DELTA_TYPECHANGE;
else {
if (diff_delta__from_one(diff, GIT_DELTA_DELETED, oitem) < 0 ||
@@ -515,7 +520,7 @@ static int maybe_modified(
int err;
git_submodule *sub;
- if ((diff->opts.flags & GIT_DIFF_IGNORE_SUBMODULES) != 0)
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_IGNORE_SUBMODULES))
status = GIT_DELTA_UNMODIFIED;
else if ((err = git_submodule_lookup(&sub, diff->repo, nitem->path)) < 0) {
if (err == GIT_EEXISTS)
@@ -626,7 +631,7 @@ int git_diff__from_iterators(
if (!diff || diff_list_init_from_iterators(diff, old_iter, new_iter) < 0)
goto fail;
- if (diff->opts.flags & GIT_DIFF_DELTAS_ARE_ICASE) {
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_DELTAS_ARE_ICASE)) {
if (git_iterator_set_ignore_case(old_iter, true) < 0 ||
git_iterator_set_ignore_case(new_iter, true) < 0)
goto fail;
@@ -648,7 +653,7 @@ int git_diff__from_iterators(
/* if we are generating TYPECHANGE records then check for that
* instead of just generating a DELETE record
*/
- if ((diff->opts.flags & GIT_DIFF_INCLUDE_TYPECHANGE_TREES) != 0 &&
+ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
entry_is_prefixed(diff, nitem, oitem))
{
/* this entry has become a tree! convert to TYPECHANGE */
@@ -663,7 +668,7 @@ int git_diff__from_iterators(
* Unless RECURSE_UNTRACKED_DIRS is set, skip over them...
*/
if (S_ISDIR(nitem->mode) &&
- !(diff->opts.flags & GIT_DIFF_RECURSE_UNTRACKED_DIRS))
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS))
{
if (git_iterator_advance(&nitem, new_iter) < 0)
goto fail;
@@ -691,20 +696,22 @@ int git_diff__from_iterators(
* it or if the user requested the contents of untracked
* directories and it is not under an ignored directory.
*/
- bool recurse_untracked =
+ bool recurse_into_dir =
(delta_type == GIT_DELTA_UNTRACKED &&
- (diff->opts.flags & GIT_DIFF_RECURSE_UNTRACKED_DIRS) != 0);
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_UNTRACKED_DIRS)) ||
+ (delta_type == GIT_DELTA_IGNORED &&
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS));
/* do not advance into directories that contain a .git file */
- if (!contains_oitem && recurse_untracked) {
+ if (!contains_oitem && recurse_into_dir) {
git_buf *full = NULL;
if (git_iterator_current_workdir_path(&full, new_iter) < 0)
goto fail;
if (git_path_contains_dir(full, DOT_GIT))
- recurse_untracked = false;
+ recurse_into_dir = false;
}
- if (contains_oitem || recurse_untracked) {
+ if (contains_oitem || recurse_into_dir) {
/* if this directory is ignored, remember it as the
* "ignore_prefix" for processing contained items
*/
@@ -744,7 +751,8 @@ int git_diff__from_iterators(
* checked before container directory exclusions are used to
* skip the file.
*/
- else if (delta_type == GIT_DELTA_IGNORED) {
+ else if (delta_type == GIT_DELTA_IGNORED &&
+ DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_RECURSE_IGNORED_DIRS)) {
if (git_iterator_advance(&nitem, new_iter) < 0)
goto fail;
continue; /* ignored parent directory, so skip completely */
@@ -763,7 +771,7 @@ int git_diff__from_iterators(
* instead of just generating an ADDED/UNTRACKED record
*/
if (delta_type != GIT_DELTA_IGNORED &&
- (diff->opts.flags & GIT_DIFF_INCLUDE_TYPECHANGE_TREES) != 0 &&
+ DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES) &&
contains_oitem)
{
/* this entry was prefixed with a tree - make TYPECHANGE */
diff --git a/src/status.c b/src/status.c
index 282cb39..7f84235 100644
--- a/src/status.c
+++ b/src/status.c
@@ -142,7 +142,8 @@ int git_status_foreach_ext(
diffopt.flags = diffopt.flags | GIT_DIFF_RECURSE_UNTRACKED_DIRS;
if ((opts->flags & GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH) != 0)
diffopt.flags = diffopt.flags | GIT_DIFF_DISABLE_PATHSPEC_MATCH;
- /* TODO: support EXCLUDE_SUBMODULES flag */
+ if ((opts->flags & GIT_STATUS_OPT_RECURSE_IGNORED_DIRS) != 0)
+ diffopt.flags = diffopt.flags | GIT_DIFF_RECURSE_IGNORED_DIRS;
if (show != GIT_STATUS_SHOW_WORKDIR_ONLY &&
(err = git_diff_tree_to_index(&idx2head, repo, head, NULL, &diffopt)) < 0)
diff --git a/tests-clar/status/ignore.c b/tests-clar/status/ignore.c
index e2e4aaf..d40af7e 100644
--- a/tests-clar/status/ignore.c
+++ b/tests-clar/status/ignore.c
@@ -199,7 +199,6 @@ void test_status_ignore__subdirectories(void)
cl_git_pass(git_status_should_ignore(&ignored, g_repo, "ignore_me"));
cl_assert(ignored);
-
/* So, interestingly, as per the comment in diff_from_iterators() the
* following file is ignored, but in a way so that it does not show up
* in status even if INCLUDE_IGNORED is used. This actually matches
@@ -207,11 +206,12 @@ void test_status_ignore__subdirectories(void)
* status -uall --ignored" then the following file and directory will
* not show up in the output at all.
*/
-
- cl_git_pass(
- git_futils_mkdir_r("empty_standard_repo/test/ignore_me", NULL, 0775));
+ cl_git_pass(git_futils_mkdir_r(
+ "empty_standard_repo/test/ignore_me", NULL, 0775));
cl_git_mkfile(
"empty_standard_repo/test/ignore_me/file", "I'm going to be ignored!");
+ cl_git_mkfile(
+ "empty_standard_repo/test/ignore_me/file2", "Me, too!");
memset(&st, 0, sizeof(st));
cl_git_pass(git_status_foreach(g_repo, cb_status__single, &st));
@@ -225,6 +225,61 @@ void test_status_ignore__subdirectories(void)
cl_assert(ignored);
}
+void test_status_ignore__subdirectories_recursion(void)
+{
+ /* Let's try again with recursing into ignored dirs turned on */
+ git_status_options opts = GIT_STATUS_OPTIONS_INIT;
+ status_entry_counts counts;
+ static const char *paths[] = {
+ ".gitignore",
+ "ignore_me",
+ "test/ignore_me/and_me/file",
+ "test/ignore_me/file",
+ "test/ignore_me/file2",
+ };
+ static const unsigned int statuses[] = {
+ GIT_STATUS_WT_NEW,
+ GIT_STATUS_IGNORED,
+ GIT_STATUS_IGNORED,
+ GIT_STATUS_IGNORED,
+ GIT_STATUS_IGNORED,
+ };
+
+ opts.flags = GIT_STATUS_OPT_INCLUDE_IGNORED |
+ GIT_STATUS_OPT_RECURSE_IGNORED_DIRS |
+ GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+ GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
+
+ g_repo = cl_git_sandbox_init("empty_standard_repo");
+
+ cl_git_rewritefile("empty_standard_repo/.gitignore", "ignore_me\n");
+
+ cl_git_mkfile(
+ "empty_standard_repo/ignore_me", "I'm going to be ignored!");
+ cl_git_pass(git_futils_mkdir_r(
+ "empty_standard_repo/test/ignore_me", NULL, 0775));
+ cl_git_mkfile(
+ "empty_standard_repo/test/ignore_me/file", "I'm going to be ignored!");
+ cl_git_mkfile(
+ "empty_standard_repo/test/ignore_me/file2", "Me, too!");
+ cl_git_pass(git_futils_mkdir_r(
+ "empty_standard_repo/test/ignore_me/and_me", NULL, 0775));
+ cl_git_mkfile(
+ "empty_standard_repo/test/ignore_me/and_me/file", "Deeply ignored");
+
+ memset(&counts, 0x0, sizeof(status_entry_counts));
+ counts.expected_entry_count = 5;
+ counts.expected_paths = paths;
+ counts.expected_statuses = statuses;
+
+ cl_git_pass(git_status_foreach_ext(
+ g_repo, &opts, cb_status__normal, &counts));
+
+ cl_assert_equal_i(counts.expected_entry_count, counts.entry_count);
+ cl_assert_equal_i(0, counts.wrong_status_flags_count);
+ cl_assert_equal_i(0, counts.wrong_sorted_path);
+}
+
void test_status_ignore__adding_internal_ignores(void)
{
int ignored;