]> git.ipfire.org Git - thirdparty/git.git/blobdiff - dir.c
Merge branch 'en/keep-cwd'
[thirdparty/git.git] / dir.c
diff --git a/dir.c b/dir.c
index 5aa6fbad0b76ad3ad026a8775761f9c7ea1776e8..c332fd0096cc3d3725e942fe72a934e2fce6b68a 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -3160,6 +3160,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) &&
@@ -3215,9 +3216,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.
@@ -3283,6 +3289,9 @@ int remove_path(const char *name)
                slash = dirs + (slash - name);
                do {
                        *slash = '\0';
+                       if (startup_info->original_cwd &&
+                           !strcmp(startup_info->original_cwd, dirs))
+                               break;
                } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));
                free(dirs);
        }