symlinks: do not include startup_info->original_cwd in dir removal

symlinks has a pair of schedule_dir_for_removal() and remove_scheduled_dirs() functions that ensure that directories made empty by removing other files also themselves get removed. However, we want to exclude startup_info->original_cwd and leave it around. This avoids the user getting confused by subsequent git commands (and non-git commands) that would otherwise report confusing messages about being unable to read the current working directory. Acked-by: Derrick Stolee <stolee@gmail.com> Acked-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Dec 9, 2021 at 05:08 UTC 00fcce285db3db48f85730a183421fdb488c14cc
2 files changed +12 -6
symlinks.c
+7 -1
@@ -279,7 +279,9 @@ static void do_remove_scheduled_dirs(int new_len)
279 {
280 while (removal.len > new_len) {
281 removal.buf[removal.len] = '\0';
282 - if (rmdir(removal.buf))
282 + if ((startup_info->original_cwd &&
283 + !strcmp(removal.buf, startup_info->original_cwd)) ||
284 + rmdir(removal.buf))
285 break;
286 do {
287 removal.len--;
@@ -293,6 +295,10 @@ void schedule_dir_for_removal(const char *name, int len)
295 {
296 int match_len, last_slash, i, previous_slash;
297
298 + if (startup_info->original_cwd &&
299 + !strcmp(name, startup_info->original_cwd))
300 + return; /* Do not remove the current working directory */
301 +
302 match_len = last_slash = i =
303 longest_path_match(name, len, removal.buf, removal.len,
304 &previous_slash);
t/t2501-cwd-empty.sh
+5 -5
@@ -109,7 +109,7 @@ test_required_dir_removal () {
109 }
110
111 test_expect_success 'checkout does not clean cwd incidentally' '
112 - test_incidental_dir_removal failure git checkout init
112 + test_incidental_dir_removal success git checkout init
113 '
114
115 test_expect_success 'checkout fails if cwd needs to be removed' '
@@ -117,7 +117,7 @@ test_expect_success 'checkout fails if cwd needs to be removed' '
117 '
118
119 test_expect_success 'reset --hard does not clean cwd incidentally' '
120 - test_incidental_dir_removal failure git reset --hard init
120 + test_incidental_dir_removal success git reset --hard init
121 '
122
123 test_expect_success 'reset --hard fails if cwd needs to be removed' '
@@ -125,7 +125,7 @@ test_expect_success 'reset --hard fails if cwd needs to be removed' '
125 '
126
127 test_expect_success 'merge does not clean cwd incidentally' '
128 - test_incidental_dir_removal failure git merge reverted
128 + test_incidental_dir_removal success git merge reverted
129 '
130
131 # This file uses some simple merges where
@@ -158,7 +158,7 @@ test_expect_success 'merge fails if cwd needs to be removed' '
158 '
159
160 test_expect_success 'cherry-pick does not clean cwd incidentally' '
161 - test_incidental_dir_removal failure git cherry-pick reverted
161 + test_incidental_dir_removal success git cherry-pick reverted
162 '
163
164 test_expect_success 'cherry-pick fails if cwd needs to be removed' '
@@ -174,7 +174,7 @@ test_expect_success 'rebase fails if cwd needs to be removed' '
174 '
175
176 test_expect_success 'revert does not clean cwd incidentally' '
177 - test_incidental_dir_removal failure git revert HEAD
177 + test_incidental_dir_removal success git revert HEAD
178 '
179
180 test_expect_success 'revert fails if cwd needs to be removed' '