summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
authorElijah Newren <newren@gmail.com>2021-12-09 05:08:34 +0000
committerJunio C Hamano <gitster@pobox.com>2021-12-09 13:33:13 -0800
commit580a5d7f75fc7b6c4c369ef429742d9d417acddd (patch)
treedb3789482029387549d85a8976ebb612e181e81d /dir.c
parent63bbe8beb78ee8af5a7faeee4be747a82d8e2dc7 (diff)
downloadgit-580a5d7f75fc7b6c4c369ef429742d9d417acddd.tar.gz
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>
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/dir.c b/dir.c
index 97d6b71c87..52064345a6 100644
--- a/dir.c
+++ b/dir.c
@@ -3204,6 +3204,7 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
int ret = 0, original_len = path->len, len, kept_down = 0;
int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);
+ int purge_original_cwd = (flag & REMOVE_DIR_PURGE_ORIGINAL_CWD);
struct object_id submodule_head;
if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
@@ -3259,9 +3260,14 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
closedir(dir);
strbuf_setlen(path, original_len);
- if (!ret && !keep_toplevel && !kept_down)
- ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1;
- else if (kept_up)
+ if (!ret && !keep_toplevel && !kept_down) {
+ if (!purge_original_cwd &&
+ startup_info->original_cwd &&
+ !strcmp(startup_info->original_cwd, path->buf))
+ ret = -1; /* Do not remove current working directory */
+ else
+ ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1;
+ } else if (kept_up)
/*
* report the uplevel that it is not an error that we
* did not rmdir() our directory.