There is an implicit assumption that a directory containing only untracked and ignored files should itself be considered untracked. This makes sense in use cases where we're asking if a directory should be added to the git database, but not when we're asking if a directory can be safely removed from the working tree; as a result, clean -d would assume that an "untracked" directory containing ignored files could be deleted. To get around this, we teach clean -d to collect ignored files and skip over so-called "untracked" directories if they contain any ignored files (while still removing the untracked contents of such dirs). This also fixes the known breakage in t7300 since clean -d now skips untracked directories containing ignored files. Signed-off-by: Samuel Lijin <sxlijin@xxxxxxxxx> --- builtin/clean.c | 38 +++++++++++++++++++++++++++++++++++++- t/t7300-clean.sh | 2 +- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/builtin/clean.c b/builtin/clean.c index d861f836a..0d490d76e 100644 --- a/builtin/clean.c +++ b/builtin/clean.c @@ -859,7 +859,7 @@ static void interactive_main_loop(void) int cmd_clean(int argc, const char **argv, const char *prefix) { - int i, res; + int i, j, res; int dry_run = 0, remove_directories = 0, quiet = 0, ignored = 0; int ignored_only = 0, config_set = 0, errors = 0, gone = 1; int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT; @@ -911,6 +911,9 @@ int cmd_clean(int argc, const char **argv, const char *prefix) " refusing to clean")); } + if (remove_directories) + dir.flags |= DIR_SHOW_IGNORED_TOO | DIR_KEEP_UNTRACKED_CONTENTS; + if (force > 1) rm_flags = 0; @@ -932,12 +935,39 @@ int cmd_clean(int argc, const char **argv, const char *prefix) fill_directory(&dir, &pathspec); + for (j = i = 0; i < dir.nr;) { + for (; + j < dir.ignored_nr && + 0 <= cmp_dir_entry(&dir.entries[i], &dir.ignored[j]); + j++); + + if ((j < dir.ignored_nr) && + check_dir_entry_contains(dir.entries[i], dir.ignored[j])) { + /* skip any dir.entries which contains a dir.ignored */ + free(dir.entries[i]); + dir.entries[i++] = NULL; + } else { + /* prune the contents of a dir.entries which will be removed */ + struct dir_entry *ent = dir.entries[i++]; + for (; + i < dir.nr && + check_dir_entry_contains(ent, dir.entries[i]); + i++) { + free(dir.entries[i]); + dir.entries[i] = NULL; + } + } + } + for (i = 0; i < dir.nr; i++) { struct dir_entry *ent = dir.entries[i]; int matches = 0; struct stat st; const char *rel; + if (!ent) + continue; + if (!cache_name_is_other(ent->name, ent->len)) continue; @@ -958,6 +988,12 @@ int cmd_clean(int argc, const char **argv, const char *prefix) string_list_append(&del_list, rel); } + for (i = 0; i < dir.nr; i++) + free(dir.entries[i]); + + for (i = 0; i < dir.ignored_nr; i++) + free(dir.ignored[i]); + if (interactive && del_list.nr > 0) interactive_main_loop(); diff --git a/t/t7300-clean.sh b/t/t7300-clean.sh index 3a2d709c2..7b36954d6 100755 --- a/t/t7300-clean.sh +++ b/t/t7300-clean.sh @@ -653,7 +653,7 @@ test_expect_success 'git clean -d respects pathspecs (pathspec is prefix of dir) test_path_is_dir foobar ' -test_expect_failure 'git clean -d skips untracked dirs containing ignored files' ' +test_expect_success 'git clean -d skips untracked dirs containing ignored files' ' echo /foo/bar >.gitignore && echo ignoreme >>.gitignore && rm -rf foo && -- 2.13.0