Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='Test handling of the current working directory becoming empty' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | test_expect_success setup ' |
| 8 | test_commit init && |
| 9 | |
| 10 | git branch fd_conflict && |
| 11 | |
| 12 | mkdir -p foo/bar && |
| 13 | test_commit foo/bar/baz && |
| 14 | |
| 15 | git revert HEAD && |
| 16 | git tag reverted && |
| 17 | |
| 18 | git checkout fd_conflict && |
| 19 | mkdir dirORfile && |
| 20 | test_commit dirORfile/foo && |
| 21 | |
| 22 | git rm -r dirORfile && |
| 23 | echo not-a-directory >dirORfile && |
| 24 | git add dirORfile && |
| 25 | git commit -m dirORfile && |
| 26 | |
| 27 | git switch -c df_conflict HEAD~1 && |
| 28 | test_commit random_file && |
| 29 | |
| 30 | git switch -c undo_fd_conflict fd_conflict && |
| 31 | git revert HEAD |
| 32 | ' |
| 33 | |
| 34 | test_incidental_dir_removal () { |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 35 | test_when_finished "git reset --hard" && |
| 36 | |
| 37 | git checkout foo/bar/baz^{commit} && |
| 38 | test_path_is_dir foo/bar && |
| 39 | |
| 40 | ( |
| 41 | cd foo && |
| 42 | "$@" && |
| 43 | |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 44 | # Make sure foo still exists, and commands needing it work |
| 45 | test-tool getcwd && |
| 46 | git status --porcelain |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 47 | ) && |
| 48 | test_path_is_missing foo/bar/baz && |
| 49 | test_path_is_missing foo/bar && |
| 50 | |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 51 | test_path_is_dir foo |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | test_required_dir_removal () { |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 55 | git checkout df_conflict^{commit} && |
| 56 | test_when_finished "git clean -fdx" && |
| 57 | |
| 58 | ( |
| 59 | cd dirORfile && |
| 60 | |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 61 | # Ensure command refuses to run |
| 62 | test_must_fail "$@" 2>../error && |
| 63 | grep "Refusing to remove.*current working directory" ../error && |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 64 | |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 65 | # ...and that the index and working tree are left clean |
| 66 | git diff --exit-code HEAD && |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 67 | |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 68 | # Ensure that getcwd and git status do not error out (which |
| 69 | # they might if the current working directory had been removed) |
| 70 | test-tool getcwd && |
| 71 | git status --porcelain |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 72 | ) && |
| 73 | |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 74 | test_path_is_dir dirORfile |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | test_expect_success 'checkout does not clean cwd incidentally' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 78 | test_incidental_dir_removal git checkout init |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 79 | ' |
| 80 | |
| 81 | test_expect_success 'checkout fails if cwd needs to be removed' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 82 | test_required_dir_removal git checkout fd_conflict |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 83 | ' |
| 84 | |
| 85 | test_expect_success 'reset --hard does not clean cwd incidentally' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 86 | test_incidental_dir_removal git reset --hard init |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 87 | ' |
| 88 | |
| 89 | test_expect_success 'reset --hard fails if cwd needs to be removed' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 90 | test_required_dir_removal git reset --hard fd_conflict |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 91 | ' |
| 92 | |
| 93 | test_expect_success 'merge does not clean cwd incidentally' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 94 | test_incidental_dir_removal git merge reverted |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 95 | ' |
| 96 | |
| 97 | # This file uses some simple merges where |
| 98 | # Base: 'dirORfile/' exists |
| 99 | # Side1: random other file changed |
| 100 | # Side2: 'dirORfile/' removed, 'dirORfile' added |
| 101 | # this should resolve cleanly, but merge-recursive throws merge conflicts |
| 102 | # because it's dumb. Add a special test for checking merge-recursive (and |
| 103 | # merge-ort), then after this just hard require ort for all remaining tests. |
| 104 | # |
| 105 | test_expect_success 'merge fails if cwd needs to be removed; recursive friendly' ' |
| 106 | git checkout foo/bar/baz && |
| 107 | test_when_finished "git clean -fdx" && |
| 108 | |
| 109 | mkdir dirORfile && |
| 110 | ( |
| 111 | cd dirORfile && |
| 112 | |
Elijah Newren | b817e54 | 2021-12-09 05:08:27 +0000 | [diff] [blame] | 113 | test_must_fail git merge fd_conflict 2>../error |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 114 | ) && |
| 115 | |
Elijah Newren | b817e54 | 2021-12-09 05:08:27 +0000 | [diff] [blame] | 116 | test_path_is_dir dirORfile && |
| 117 | grep "Refusing to remove the current working directory" error |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 118 | ' |
| 119 | |
| 120 | GIT_TEST_MERGE_ALGORITHM=ort |
| 121 | |
| 122 | test_expect_success 'merge fails if cwd needs to be removed' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 123 | test_required_dir_removal git merge fd_conflict |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 124 | ' |
| 125 | |
| 126 | test_expect_success 'cherry-pick does not clean cwd incidentally' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 127 | test_incidental_dir_removal git cherry-pick reverted |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 128 | ' |
| 129 | |
| 130 | test_expect_success 'cherry-pick fails if cwd needs to be removed' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 131 | test_required_dir_removal git cherry-pick fd_conflict |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 132 | ' |
| 133 | |
| 134 | test_expect_success 'rebase does not clean cwd incidentally' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 135 | test_incidental_dir_removal git rebase reverted |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 136 | ' |
| 137 | |
| 138 | test_expect_success 'rebase fails if cwd needs to be removed' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 139 | test_required_dir_removal git rebase fd_conflict |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 140 | ' |
| 141 | |
| 142 | test_expect_success 'revert does not clean cwd incidentally' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 143 | test_incidental_dir_removal git revert HEAD |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 144 | ' |
| 145 | |
| 146 | test_expect_success 'revert fails if cwd needs to be removed' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 147 | test_required_dir_removal git revert undo_fd_conflict |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 148 | ' |
| 149 | |
| 150 | test_expect_success 'rm does not clean cwd incidentally' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 151 | test_incidental_dir_removal git rm bar/baz.t |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 152 | ' |
| 153 | |
| 154 | test_expect_success 'apply does not remove cwd incidentally' ' |
| 155 | git diff HEAD HEAD~1 >patch && |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 156 | test_incidental_dir_removal git apply ../patch |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 157 | ' |
| 158 | |
| 159 | test_incidental_untracked_dir_removal () { |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 160 | test_when_finished "git reset --hard" && |
| 161 | |
| 162 | git checkout foo/bar/baz^{commit} && |
| 163 | mkdir -p untracked && |
| 164 | mkdir empty |
| 165 | >untracked/random && |
| 166 | |
| 167 | ( |
| 168 | cd untracked && |
| 169 | "$@" && |
| 170 | |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 171 | # Make sure untracked still exists, and commands needing it work |
| 172 | test-tool getcwd && |
| 173 | git status --porcelain |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 174 | ) && |
| 175 | test_path_is_missing empty && |
| 176 | test_path_is_missing untracked/random && |
| 177 | |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 178 | test_path_is_dir untracked |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | test_expect_success 'clean does not remove cwd incidentally' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 182 | test_incidental_untracked_dir_removal \ |
Elijah Newren | c65744e | 2021-12-09 05:08:30 +0000 | [diff] [blame] | 183 | git -C .. clean -fd -e warnings . >warnings && |
| 184 | grep "Refusing to remove current working directory" warnings |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 185 | ' |
| 186 | |
| 187 | test_expect_success 'stash does not remove cwd incidentally' ' |
Elijah Newren | 324b170 | 2021-12-09 05:08:35 +0000 | [diff] [blame] | 188 | test_incidental_untracked_dir_removal \ |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 189 | git stash --include-untracked |
| 190 | ' |
| 191 | |
| 192 | test_expect_success '`rm -rf dir` only removes a subset of dir' ' |
| 193 | test_when_finished "rm -rf a/" && |
| 194 | |
| 195 | mkdir -p a/b/c && |
| 196 | >a/b/c/untracked && |
| 197 | >a/b/c/tracked && |
| 198 | git add a/b/c/tracked && |
| 199 | |
| 200 | ( |
| 201 | cd a/b && |
| 202 | git rm -rf ../b |
| 203 | ) && |
| 204 | |
| 205 | test_path_is_dir a/b && |
| 206 | test_path_is_missing a/b/c/tracked && |
| 207 | test_path_is_file a/b/c/untracked |
| 208 | ' |
| 209 | |
| 210 | test_expect_success '`rm -rf dir` even with only tracked files will remove something else' ' |
| 211 | test_when_finished "rm -rf a/" && |
| 212 | |
| 213 | mkdir -p a/b/c && |
| 214 | >a/b/c/tracked && |
| 215 | git add a/b/c/tracked && |
| 216 | |
| 217 | ( |
| 218 | cd a/b && |
| 219 | git rm -rf ../b |
| 220 | ) && |
| 221 | |
| 222 | test_path_is_missing a/b/c/tracked && |
Elijah Newren | 63bbe8b | 2021-12-09 05:08:33 +0000 | [diff] [blame] | 223 | test_path_is_missing a/b/c && |
| 224 | test_path_is_dir a/b |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 225 | ' |
| 226 | |
| 227 | test_expect_success 'git version continues working from a deleted dir' ' |
| 228 | mkdir tmp && |
| 229 | ( |
| 230 | cd tmp && |
| 231 | rm -rf ../tmp && |
| 232 | git version |
| 233 | ) |
| 234 | ' |
| 235 | |
| 236 | test_submodule_removal () { |
| 237 | path_status=$1 && |
| 238 | shift && |
| 239 | |
| 240 | test_status= |
| 241 | test "$path_status" = dir && test_status=test_must_fail |
| 242 | |
Elijah Newren | 8a0d52d | 2021-12-09 05:08:25 +0000 | [diff] [blame] | 243 | test_when_finished "git reset --hard HEAD~1" && |
| 244 | test_when_finished "rm -rf .git/modules/my_submodule" && |
| 245 | |
| 246 | git checkout foo/bar/baz && |
| 247 | |
| 248 | git init my_submodule && |
| 249 | touch my_submodule/file && |
| 250 | git -C my_submodule add file && |
| 251 | git -C my_submodule commit -m "initial commit" && |
| 252 | git submodule add ./my_submodule && |
| 253 | git commit -m "Add the submodule" && |
| 254 | |
| 255 | ( |
| 256 | cd my_submodule && |
| 257 | $test_status "$@" |
| 258 | ) && |
| 259 | |
| 260 | test_path_is_${path_status} my_submodule |
| 261 | } |
| 262 | |
| 263 | test_expect_success 'rm -r with -C leaves submodule if cwd inside' ' |
| 264 | test_submodule_removal dir git -C .. rm -r my_submodule/ |
| 265 | ' |
| 266 | |
| 267 | test_expect_success 'rm -r leaves submodule if cwd inside' ' |
| 268 | test_submodule_removal dir \ |
| 269 | git --git-dir=../.git --work-tree=.. rm -r ../my_submodule/ |
| 270 | ' |
| 271 | |
| 272 | test_expect_success 'rm -rf removes submodule even if cwd inside' ' |
| 273 | test_submodule_removal missing \ |
| 274 | git --git-dir=../.git --work-tree=.. rm -rf ../my_submodule/ |
| 275 | ' |
| 276 | |
| 277 | test_done |