@@ -3327,6 +3327,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);
}
@@ -504,7 +504,11 @@ int get_sparse_checkout_patterns(struct pattern_list *pl);
*/
int remove_dir_recursively(struct strbuf *path, int flag);
-/* tries to remove the path with empty directories along it, ignores ENOENT */
+/*
+ * Tries to remove the path, along with leading empty directories so long as
+ * those empty directories are not startup_info->original_cwd. Ignores
+ * ENOENT.
+ */
int remove_path(const char *path);
int fspathcmp(const char *a, const char *b);
@@ -191,7 +191,7 @@ test_expect_success 'revert fails if cwd needs to be removed' '
test_path_is_dir dirORfile
'
-test_expect_failure 'rm does not remove cwd incidentally' '
+test_expect_success 'rm does not remove cwd incidentally' '
test_when_finished "git reset --hard" &&
git checkout foo/bar/baz &&
@@ -205,7 +205,7 @@ test_expect_failure 'rm does not remove cwd incidentally' '
test_path_is_dir foo
'
-test_expect_failure 'apply does not remove cwd incidentally' '
+test_expect_success 'apply does not remove cwd incidentally' '
test_when_finished "git reset --hard" &&
git checkout foo/bar/baz &&