mirror of
https://github.com/git/git.git
synced 2026-01-10 18:20:27 +00:00
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:
15
dir.c
15
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user