Merge branch 'en/keep-cwd'

Many git commands that deal with working tree files try to remove a
directory that becomes empty (i.e. "git switch" from a branch that
has the directory to another branch that does not would attempt
remove all files in the directory and the directory itself).  This
drops users into an unfamiliar situation if the command was run in
a subdirectory that becomes subject to removal due to the command.
The commands have been taught to keep an empty directory if it is
the directory they were started in to avoid surprising users.

* en/keep-cwd:
  t2501: simplify the tests since we can now assume desired behavior
  dir: new flag to remove_dir_recurse() to spare the original_cwd
  dir: avoid incidentally removing the original_cwd in remove_path()
  stash: do not attempt to remove startup_info->original_cwd
  rebase: do not attempt to remove startup_info->original_cwd
  clean: do not attempt to remove startup_info->original_cwd
  symlinks: do not include startup_info->original_cwd in dir removal
  unpack-trees: add special cwd handling
  unpack-trees: refuse to remove startup_info->original_cwd
  setup: introduce startup_info->original_cwd
  t2501: add various tests for removing the current working directory
This commit is contained in:
Junio C Hamano
2022-01-05 14:01:28 -08:00
13 changed files with 442 additions and 22 deletions

15
dir.c
View File

@@ -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);
}