Skip to content

Commit c844a80

Browse files
committed
remove_dir_recursively(): Add flag for skipping removal of toplevel dir
Add the REMOVE_DIR_KEEP_TOPLEVEL flag to remove_dir_recursively() for deleting everything inside the given directory, but _not_ the given directory itself. Note that this does not pass the REMOVE_DIR_KEEP_NESTED_GIT flag, if set, to the recursive invocations of remove_dir_recursively(). It is likely to be a a bug that has been present since REMOVE_DIR_KEEP_NESTED_GIT was introduced (a0f4afb), but this commit keeps the same behaviour for now. Signed-off-by: Johan Herland <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0dbe659 commit c844a80

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

dir.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,16 +1178,22 @@ int remove_dir_recursively(struct strbuf *path, int flag)
11781178
struct dirent *e;
11791179
int ret = 0, original_len = path->len, len;
11801180
int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
1181+
int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);
11811182
unsigned char submodule_head[20];
11821183

11831184
if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
11841185
!resolve_gitlink_ref(path->buf, "HEAD", submodule_head))
11851186
/* Do not descend and nuke a nested git work tree. */
11861187
return 0;
11871188

1189+
flag &= ~(REMOVE_DIR_KEEP_TOPLEVEL|REMOVE_DIR_KEEP_NESTED_GIT);
11881190
dir = opendir(path->buf);
1189-
if (!dir)
1190-
return rmdir(path->buf);
1191+
if (!dir) {
1192+
if (!keep_toplevel)
1193+
return rmdir(path->buf);
1194+
else
1195+
return -1;
1196+
}
11911197
if (path->buf[original_len - 1] != '/')
11921198
strbuf_addch(path, '/');
11931199

@@ -1202,7 +1208,7 @@ int remove_dir_recursively(struct strbuf *path, int flag)
12021208
if (lstat(path->buf, &st))
12031209
; /* fall thru */
12041210
else if (S_ISDIR(st.st_mode)) {
1205-
if (!remove_dir_recursively(path, only_empty))
1211+
if (!remove_dir_recursively(path, flag))
12061212
continue; /* happy */
12071213
} else if (!only_empty && !unlink(path->buf))
12081214
continue; /* happy, too */
@@ -1214,7 +1220,7 @@ int remove_dir_recursively(struct strbuf *path, int flag)
12141220
closedir(dir);
12151221

12161222
strbuf_setlen(path, original_len);
1217-
if (!ret)
1223+
if (!ret && !keep_toplevel)
12181224
ret = rmdir(path->buf);
12191225
return ret;
12201226
}

dir.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ extern void setup_standard_excludes(struct dir_struct *dir);
102102

103103
#define REMOVE_DIR_EMPTY_ONLY 01
104104
#define REMOVE_DIR_KEEP_NESTED_GIT 02
105+
#define REMOVE_DIR_KEEP_TOPLEVEL 04
105106
extern int remove_dir_recursively(struct strbuf *path, int flag);
106107

107108
/* tries to remove the path with empty directories along it, ignores ENOENT */

0 commit comments

Comments
 (0)