dir: new flag to remove_dir_recurse() to spare the original_cwd

remove_dir_recurse(), and its non-static wrapper called remove_dir_recursively(), both take flags for modifying its behavior. As with the previous commits, we would generally like to protect the original_cwd, but we want to forced user commands (e.g. 'git rm -rf ...') or other special cases to remove it. Add a flag for this purpose. After reading through every caller of remove_dir_recursively() in the current codebase, there was only one that should be adjusted and that one only in a very unusual circumstance. Add a pair of new testcases to highlight that very specific case involving submodules && --git-dir && --work-tree. Acked-by: Derrick Stolee <stolee@gmail.com> Acked-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Dec 9, 2021 at 05:08 UTC 580a5d7f75fc7b6c4c369ef429742d9d417acddd
4 files changed +14 -9
builtin/rm.c
+2 -1
@@ -399,12 +399,13 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
399 if (!index_only) {
400 int removed = 0, gitmodules_modified = 0;
401 struct strbuf buf = STRBUF_INIT;
402 + int flag = force ? REMOVE_DIR_PURGE_ORIGINAL_CWD : 0;
403 for (i = 0; i < list.nr; i++) {
404 const char *path = list.entry[i].name;
405 if (list.entry[i].is_submodule) {
406 strbuf_reset(&buf);
407 strbuf_addstr(&buf, path);
407 - if (remove_dir_recursively(&buf, 0))
408 + if (remove_dir_recursively(&buf, flag))
409 die(_("could not remove '%s'"), path);
410
411 removed = 1;
dir.c
+9 -3
@@ -3204,6 +3204,7 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
3204 int ret = 0, original_len = path->len, len, kept_down = 0;
3205 int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
3206 int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);
3207 + int purge_original_cwd = (flag & REMOVE_DIR_PURGE_ORIGINAL_CWD);
3208 struct object_id submodule_head;
3209
3210 if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
@@ -3259,9 +3260,14 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
3260 closedir(dir);
3261
3262 strbuf_setlen(path, original_len);
3262 - if (!ret && !keep_toplevel && !kept_down)
3263 - ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1;
3264 - else if (kept_up)
3263 + if (!ret && !keep_toplevel && !kept_down) {
3264 + if (!purge_original_cwd &&
3265 + startup_info->original_cwd &&
3266 + !strcmp(startup_info->original_cwd, path->buf))
3267 + ret = -1; /* Do not remove current working directory */
3268 + else
3269 + ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1;
3270 + } else if (kept_up)
3271 /*
3272 * report the uplevel that it is not an error that we
3273 * did not rmdir() our directory.
dir.h
+3
@@ -495,6 +495,9 @@ int get_sparse_checkout_patterns(struct pattern_list *pl);
495 /* Remove the contents of path, but leave path itself. */
496 #define REMOVE_DIR_KEEP_TOPLEVEL 04
497
498 +/* Remove the_original_cwd too */
499 +#define REMOVE_DIR_PURGE_ORIGINAL_CWD 0x08
500 +
501 /*
502 * Remove path and its contents, recursively. flags is a combination
503 * of the above REMOVE_DIR_* constants. Return 0 on success.
t/t2501-cwd-empty.sh
-5
@@ -291,11 +291,6 @@ test_submodule_removal () {
291 test_status=
292 test "$path_status" = dir && test_status=test_must_fail
293
294 - # Actually, while path_status=dir && test_status=test_must_fail
295 - # reflect our desired behavior, current behavior is:
296 - path_status=missing
297 - test_status=
298 -
294 test_when_finished "git reset --hard HEAD~1" &&
295 test_when_finished "rm -rf .git/modules/my_submodule" &&
296