]>
Commit | Line | Data |
---|---|---|
d4a1cab5 CW |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2006 Carl D. Worth | |
4 | # | |
5 | ||
5be60078 | 6 | test_description='Test of the various options to git rm.' |
d4a1cab5 | 7 | |
cbc75a12 | 8 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
9 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
10 | ||
d4a1cab5 CW |
11 | . ./test-lib.sh |
12 | ||
3844cdc8 | 13 | # Setup some files to be removed, some with funny characters |
b5368c23 RA |
14 | test_expect_success 'Initialize test directory' ' |
15 | touch -- foo bar baz "space embedded" -q && | |
16 | git add -- foo bar baz "space embedded" -q && | |
17 | git commit -m "add normal files" | |
18 | ' | |
56e78bfb | 19 | |
b5368c23 RA |
20 | if test_have_prereq !FUNNYNAMES |
21 | then | |
56e78bfb JS |
22 | say 'Your filesystem does not allow tabs in filenames.' |
23 | fi | |
24 | ||
b5368c23 RA |
25 | test_expect_success FUNNYNAMES 'add files with funny names' ' |
26 | touch -- "tab embedded" "newline${LF}embedded" && | |
27 | git add -- "tab embedded" "newline${LF}embedded" && | |
28 | git commit -m "add files with tabs and newlines" | |
29 | ' | |
30 | ||
31 | test_expect_success 'Pre-check that foo exists and is in index before git rm foo' ' | |
59a06e94 | 32 | test_path_is_file foo && |
b5368c23 RA |
33 | git ls-files --error-unmatch foo |
34 | ' | |
35 | ||
36 | test_expect_success 'Test that git rm foo succeeds' ' | |
37 | git rm --cached foo | |
38 | ' | |
39 | ||
40 | test_expect_success 'Test that git rm --cached foo succeeds if the index matches the file' ' | |
41 | echo content >foo && | |
42 | git add foo && | |
43 | git rm --cached foo | |
44 | ' | |
45 | ||
46 | test_expect_success 'Test that git rm --cached foo succeeds if the index matches the file' ' | |
47 | echo content >foo && | |
48 | git add foo && | |
49 | git commit -m foo && | |
50 | echo "other content" >foo && | |
51 | git rm --cached foo | |
52 | ' | |
53 | ||
54 | test_expect_success 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' ' | |
55 | echo content >foo && | |
56 | git add foo && | |
57 | git commit -m foo --allow-empty && | |
58 | echo "other content" >foo && | |
59 | git add foo && | |
60 | echo "yet another content" >foo && | |
61 | test_must_fail git rm --cached foo | |
62 | ' | |
63 | ||
64 | test_expect_success 'Test that git rm --cached -f foo works in case where --cached only did not' ' | |
65 | echo content >foo && | |
66 | git add foo && | |
67 | git commit -m foo --allow-empty && | |
68 | echo "other content" >foo && | |
69 | git add foo && | |
70 | echo "yet another content" >foo && | |
71 | git rm --cached -f foo | |
72 | ' | |
73 | ||
74 | test_expect_success 'Post-check that foo exists but is not in index after git rm foo' ' | |
59a06e94 | 75 | test_path_is_file foo && |
b5368c23 RA |
76 | test_must_fail git ls-files --error-unmatch foo |
77 | ' | |
78 | ||
79 | test_expect_success 'Pre-check that bar exists and is in index before "git rm bar"' ' | |
59a06e94 | 80 | test_path_is_file bar && |
b5368c23 RA |
81 | git ls-files --error-unmatch bar |
82 | ' | |
83 | ||
84 | test_expect_success 'Test that "git rm bar" succeeds' ' | |
85 | git rm bar | |
86 | ' | |
87 | ||
88 | test_expect_success 'Post-check that bar does not exist and is not in index after "git rm -f bar"' ' | |
59a06e94 | 89 | test_path_is_missing bar && |
b5368c23 RA |
90 | test_must_fail git ls-files --error-unmatch bar |
91 | ' | |
92 | ||
93 | test_expect_success 'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' ' | |
94 | git rm -- -q | |
95 | ' | |
96 | ||
97 | test_expect_success FUNNYNAMES 'Test that "git rm -f" succeeds with embedded space, tab, or newline characters.' ' | |
98 | git rm -f "space embedded" "tab embedded" "newline${LF}embedded" | |
99 | ' | |
3844cdc8 | 100 | |
fbbfc8a9 | 101 | test_expect_success SANITY 'Test that "git rm -f" fails if its rm fails' ' |
23739aa2 | 102 | test_when_finished "chmod 775 ." && |
56e78bfb | 103 | chmod a-w . && |
23739aa2 | 104 | test_must_fail git rm -f baz |
56e78bfb | 105 | ' |
3844cdc8 | 106 | |
b5368c23 RA |
107 | test_expect_success 'When the rm in "git rm -f" fails, it should not remove the file from the index' ' |
108 | git ls-files --error-unmatch baz | |
109 | ' | |
3844cdc8 | 110 | |
bb1faf0d SG |
111 | test_expect_success 'Remove nonexistent file with --ignore-unmatch' ' |
112 | git rm --ignore-unmatch nonexistent | |
113 | ' | |
114 | ||
b48caa20 | 115 | test_expect_success '"rm" command printed' ' |
9e189f1a | 116 | echo frotz >test-file && |
b48caa20 SG |
117 | git add test-file && |
118 | git commit -m "add file for rm test" && | |
0d913dfa DL |
119 | git rm test-file >rm-output.raw && |
120 | grep "^rm " rm-output.raw >rm-output && | |
121 | test_line_count = 1 rm-output && | |
122 | rm -f test-file rm-output.raw rm-output && | |
b48caa20 SG |
123 | git commit -m "remove file from rm test" |
124 | ' | |
125 | ||
126 | test_expect_success '"rm" command suppressed with --quiet' ' | |
9e189f1a | 127 | echo frotz >test-file && |
b48caa20 SG |
128 | git add test-file && |
129 | git commit -m "add file for rm --quiet test" && | |
9e189f1a SB |
130 | git rm --quiet test-file >rm-output && |
131 | test_must_be_empty rm-output && | |
b48caa20 SG |
132 | rm -f test-file rm-output && |
133 | git commit -m "remove file from rm --quiet test" | |
134 | ' | |
135 | ||
467e1b53 JH |
136 | # Now, failure cases. |
137 | test_expect_success 'Re-add foo and baz' ' | |
138 | git add foo baz && | |
139 | git ls-files --error-unmatch foo baz | |
140 | ' | |
141 | ||
142 | test_expect_success 'Modify foo -- rm should refuse' ' | |
143 | echo >>foo && | |
d492b31c | 144 | test_must_fail git rm foo baz && |
59a06e94 RA |
145 | test_path_is_file foo && |
146 | test_path_is_file baz && | |
467e1b53 JH |
147 | git ls-files --error-unmatch foo baz |
148 | ' | |
149 | ||
150 | test_expect_success 'Modified foo -- rm -f should work' ' | |
151 | git rm -f foo baz && | |
59a06e94 RA |
152 | test_path_is_missing foo && |
153 | test_path_is_missing baz && | |
d492b31c SB |
154 | test_must_fail git ls-files --error-unmatch foo && |
155 | test_must_fail git ls-files --error-unmatch bar | |
467e1b53 JH |
156 | ' |
157 | ||
158 | test_expect_success 'Re-add foo and baz for HEAD tests' ' | |
159 | echo frotz >foo && | |
160 | git checkout HEAD -- baz && | |
161 | git add foo baz && | |
162 | git ls-files --error-unmatch foo baz | |
163 | ' | |
164 | ||
165 | test_expect_success 'foo is different in index from HEAD -- rm should refuse' ' | |
d492b31c | 166 | test_must_fail git rm foo baz && |
59a06e94 RA |
167 | test_path_is_file foo && |
168 | test_path_is_file baz && | |
467e1b53 JH |
169 | git ls-files --error-unmatch foo baz |
170 | ' | |
171 | ||
172 | test_expect_success 'but with -f it should work.' ' | |
173 | git rm -f foo baz && | |
59a06e94 RA |
174 | test_path_is_missing foo && |
175 | test_path_is_missing baz && | |
8fb26872 | 176 | test_must_fail git ls-files --error-unmatch foo && |
d492b31c | 177 | test_must_fail git ls-files --error-unmatch baz |
467e1b53 JH |
178 | ' |
179 | ||
388b2acd JH |
180 | test_expect_success 'refuse to remove cached empty file with modifications' ' |
181 | >empty && | |
f55527f8 JK |
182 | git add empty && |
183 | echo content >empty && | |
184 | test_must_fail git rm --cached empty | |
185 | ' | |
186 | ||
187 | test_expect_success 'remove intent-to-add file without --force' ' | |
188 | echo content >intent-to-add && | |
99094a7a | 189 | git add -N intent-to-add && |
f55527f8 JK |
190 | git rm --cached intent-to-add |
191 | ' | |
192 | ||
467e1b53 JH |
193 | test_expect_success 'Recursive test setup' ' |
194 | mkdir -p frotz && | |
195 | echo qfwfq >frotz/nitfol && | |
196 | git add frotz && | |
197 | git commit -m "subdir test" | |
198 | ' | |
199 | ||
200 | test_expect_success 'Recursive without -r fails' ' | |
d492b31c | 201 | test_must_fail git rm frotz && |
59a06e94 RA |
202 | test_path_is_dir frotz && |
203 | test_path_is_file frotz/nitfol | |
467e1b53 JH |
204 | ' |
205 | ||
206 | test_expect_success 'Recursive with -r but dirty' ' | |
99094a7a | 207 | echo qfwfq >>frotz/nitfol && |
d492b31c | 208 | test_must_fail git rm -r frotz && |
59a06e94 RA |
209 | test_path_is_dir frotz && |
210 | test_path_is_file frotz/nitfol | |
467e1b53 JH |
211 | ' |
212 | ||
213 | test_expect_success 'Recursive with -r -f' ' | |
214 | git rm -f -r frotz && | |
59a06e94 RA |
215 | test_path_is_missing frotz/nitfol && |
216 | test_path_is_missing frotz | |
467e1b53 JH |
217 | ' |
218 | ||
41ac414e | 219 | test_expect_success 'Remove nonexistent file returns nonzero exit status' ' |
d492b31c | 220 | test_must_fail git rm nonexistent |
b48caa20 SG |
221 | ' |
222 | ||
4d264672 OM |
223 | test_expect_success 'Call "rm" from outside the work tree' ' |
224 | mkdir repo && | |
b5368c23 RA |
225 | ( |
226 | cd repo && | |
227 | git init && | |
228 | echo something >somefile && | |
229 | git add somefile && | |
230 | git commit -m "add a file" && | |
231 | ( | |
232 | cd .. && | |
233 | git --git-dir=repo/.git --work-tree=repo rm somefile | |
234 | ) && | |
235 | test_must_fail git ls-files --error-unmatch somefile | |
236 | ) | |
cced48a8 JS |
237 | ' |
238 | ||
239 | test_expect_success 'refresh index before checking if it is up-to-date' ' | |
cced48a8 | 240 | git reset --hard && |
0e496492 | 241 | test-tool chmtime -86400 frotz/nitfol && |
cced48a8 | 242 | git rm frotz/nitfol && |
59a06e94 | 243 | test_path_is_missing frotz/nitfol |
4d264672 OM |
244 | ' |
245 | ||
db89a82b | 246 | choke_git_rm_setup() { |
0693f9dd | 247 | git reset -q --hard && |
056f34bb | 248 | test_when_finished "rm -f .git/index.lock && git reset -q --hard" && |
0693f9dd | 249 | i=0 && |
ca6ba942 | 250 | hash=$(test_oid deadbeef) && |
0693f9dd JH |
251 | while test $i -lt 12000 |
252 | do | |
ca6ba942 | 253 | echo "100644 $hash 0 some-file-$i" |
b5368c23 | 254 | i=$(( $i + 1 )) |
db89a82b ÆAB |
255 | done | git update-index --index-info |
256 | } | |
257 | ||
258 | test_expect_success 'choking "git rm" should not let it die with cruft (induce SIGPIPE)' ' | |
259 | choke_git_rm_setup && | |
50cd31c6 | 260 | # git command is intentionally placed upstream of pipe to induce SIGPIPE |
056f34bb SG |
261 | git rm -n "some-file-*" | : && |
262 | test_path_is_missing .git/index.lock | |
0693f9dd JH |
263 | ' |
264 | ||
db89a82b ÆAB |
265 | |
266 | test_expect_success !MINGW 'choking "git rm" should not let it die with cruft (induce and check SIGPIPE)' ' | |
267 | choke_git_rm_setup && | |
268 | OUT=$( ((trap "" PIPE; git rm -n "some-file-*"; echo $? 1>&3) | :) 3>&1 ) && | |
269 | test_match_signal 13 "$OUT" && | |
270 | test_path_is_missing .git/index.lock | |
271 | ' | |
272 | ||
b2b1f615 JH |
273 | test_expect_success 'Resolving by removal is not a warning-worthy event' ' |
274 | git reset -q --hard && | |
275 | test_when_finished "rm -f .git/index.lock msg && git reset -q --hard" && | |
276 | blob=$(echo blob | git hash-object -w --stdin) && | |
277 | for stage in 1 2 3 | |
278 | do | |
279 | echo "100644 $blob $stage blob" | |
280 | done | git update-index --index-info && | |
281 | git rm blob >msg 2>&1 && | |
282 | test_i18ngrep ! "needs merge" msg && | |
283 | test_must_fail git ls-files -s --error-unmatch blob | |
284 | ' | |
285 | ||
3fc0d131 JK |
286 | test_expect_success 'rm removes subdirectories recursively' ' |
287 | mkdir -p dir/subdir/subsubdir && | |
288 | echo content >dir/subdir/subsubdir/file && | |
289 | git add dir/subdir/subsubdir/file && | |
290 | git rm -f dir/subdir/subsubdir/file && | |
59a06e94 | 291 | test_path_is_missing dir |
3fc0d131 JK |
292 | ' |
293 | ||
293ab15e | 294 | cat >expect <<EOF |
95c16418 | 295 | M .gitmodules |
293ab15e JL |
296 | D submod |
297 | EOF | |
298 | ||
299 | cat >expect.modified <<EOF | |
300 | M submod | |
301 | EOF | |
302 | ||
dd6962dd SB |
303 | cat >expect.modified_inside <<EOF |
304 | m submod | |
305 | EOF | |
306 | ||
307 | cat >expect.modified_untracked <<EOF | |
308 | ? submod | |
309 | EOF | |
310 | ||
95c16418 JL |
311 | cat >expect.cached <<EOF |
312 | D submod | |
313 | EOF | |
314 | ||
315 | cat >expect.both_deleted<<EOF | |
316 | D .gitmodules | |
317 | D submod | |
318 | EOF | |
319 | ||
293ab15e JL |
320 | test_expect_success 'rm removes empty submodules from work tree' ' |
321 | mkdir submod && | |
3b737381 DL |
322 | hash=$(git rev-parse HEAD) && |
323 | git update-index --add --cacheinfo 160000 "$hash" submod && | |
293ab15e JL |
324 | git config -f .gitmodules submodule.sub.url ./. && |
325 | git config -f .gitmodules submodule.sub.path submod && | |
326 | git submodule init && | |
327 | git add .gitmodules && | |
328 | git commit -m "add submodule" && | |
329 | git rm submod && | |
59a06e94 | 330 | test_path_is_missing submod && |
9e189f1a | 331 | git status -s -uno --ignore-submodules=none >actual && |
95c16418 JL |
332 | test_cmp expect actual && |
333 | test_must_fail git config -f .gitmodules submodule.sub.url && | |
334 | test_must_fail git config -f .gitmodules submodule.sub.path | |
293ab15e JL |
335 | ' |
336 | ||
95c16418 | 337 | test_expect_success 'rm removes removed submodule from index and .gitmodules' ' |
293ab15e JL |
338 | git reset --hard && |
339 | git submodule update && | |
340 | rm -rf submod && | |
341 | git rm submod && | |
9e189f1a | 342 | git status -s -uno --ignore-submodules=none >actual && |
95c16418 JL |
343 | test_cmp expect actual && |
344 | test_must_fail git config -f .gitmodules submodule.sub.url && | |
345 | test_must_fail git config -f .gitmodules submodule.sub.path | |
293ab15e JL |
346 | ' |
347 | ||
348 | test_expect_success 'rm removes work tree of unmodified submodules' ' | |
349 | git reset --hard && | |
350 | git submodule update && | |
351 | git rm submod && | |
59a06e94 | 352 | test_path_is_missing submod && |
9e189f1a | 353 | git status -s -uno --ignore-submodules=none >actual && |
95c16418 JL |
354 | test_cmp expect actual && |
355 | test_must_fail git config -f .gitmodules submodule.sub.url && | |
356 | test_must_fail git config -f .gitmodules submodule.sub.path | |
293ab15e JL |
357 | ' |
358 | ||
53e4c5dc JL |
359 | test_expect_success 'rm removes a submodule with a trailing /' ' |
360 | git reset --hard && | |
361 | git submodule update && | |
362 | git rm submod/ && | |
59a06e94 | 363 | test_path_is_missing submod && |
9e189f1a | 364 | git status -s -uno --ignore-submodules=none >actual && |
53e4c5dc JL |
365 | test_cmp expect actual |
366 | ' | |
367 | ||
368 | test_expect_success 'rm fails when given a file with a trailing /' ' | |
369 | test_must_fail git rm empty/ | |
370 | ' | |
371 | ||
372 | test_expect_success 'rm succeeds when given a directory with a trailing /' ' | |
373 | git rm -r frotz/ | |
374 | ' | |
375 | ||
293ab15e JL |
376 | test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' ' |
377 | git reset --hard && | |
378 | git submodule update && | |
9e189f1a | 379 | git -C submod checkout HEAD^ && |
293ab15e | 380 | test_must_fail git rm submod && |
59a06e94 RA |
381 | test_path_is_dir submod && |
382 | test_path_is_file submod/.git && | |
9e189f1a | 383 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
384 | test_cmp expect.modified actual && |
385 | git rm -f submod && | |
59a06e94 | 386 | test_path_is_missing submod && |
9e189f1a | 387 | git status -s -uno --ignore-submodules=none >actual && |
95c16418 JL |
388 | test_cmp expect actual && |
389 | test_must_fail git config -f .gitmodules submodule.sub.url && | |
390 | test_must_fail git config -f .gitmodules submodule.sub.path | |
391 | ' | |
392 | ||
393 | test_expect_success 'rm --cached leaves work tree of populated submodules and .gitmodules alone' ' | |
394 | git reset --hard && | |
395 | git submodule update && | |
396 | git rm --cached submod && | |
59a06e94 RA |
397 | test_path_is_dir submod && |
398 | test_path_is_file submod/.git && | |
95c16418 JL |
399 | git status -s -uno >actual && |
400 | test_cmp expect.cached actual && | |
401 | git config -f .gitmodules submodule.sub.url && | |
402 | git config -f .gitmodules submodule.sub.path | |
403 | ' | |
404 | ||
405 | test_expect_success 'rm --dry-run does not touch the submodule or .gitmodules' ' | |
406 | git reset --hard && | |
407 | git submodule update && | |
408 | git rm -n submod && | |
59a06e94 | 409 | test_path_is_file submod/.git && |
95c16418 JL |
410 | git diff-index --exit-code HEAD |
411 | ' | |
412 | ||
413 | test_expect_success 'rm does not complain when no .gitmodules file is found' ' | |
414 | git reset --hard && | |
415 | git submodule update && | |
416 | git rm .gitmodules && | |
417 | git rm submod >actual 2>actual.err && | |
ec10b018 | 418 | test_must_be_empty actual.err && |
59a06e94 RA |
419 | test_path_is_missing submod && |
420 | test_path_is_missing submod/.git && | |
95c16418 JL |
421 | git status -s -uno >actual && |
422 | test_cmp expect.both_deleted actual | |
423 | ' | |
424 | ||
425 | test_expect_success 'rm will error out on a modified .gitmodules file unless staged' ' | |
426 | git reset --hard && | |
427 | git submodule update && | |
428 | git config -f .gitmodules foo.bar true && | |
429 | test_must_fail git rm submod >actual 2>actual.err && | |
59a06e94 RA |
430 | test_file_not_empty actual.err && |
431 | test_path_is_dir submod && | |
432 | test_path_is_file submod/.git && | |
95c16418 JL |
433 | git diff-files --quiet -- submod && |
434 | git add .gitmodules && | |
435 | git rm submod >actual 2>actual.err && | |
ec10b018 | 436 | test_must_be_empty actual.err && |
59a06e94 RA |
437 | test_path_is_missing submod && |
438 | test_path_is_missing submod/.git && | |
95c16418 JL |
439 | git status -s -uno >actual && |
440 | test_cmp expect actual | |
441 | ' | |
7edee329 DT |
442 | test_expect_success 'rm will not error out on .gitmodules file with zero stat data' ' |
443 | git reset --hard && | |
444 | git submodule update && | |
445 | git read-tree HEAD && | |
446 | git rm submod && | |
447 | test_path_is_missing submod | |
448 | ' | |
95c16418 JL |
449 | |
450 | test_expect_success 'rm issues a warning when section is not found in .gitmodules' ' | |
451 | git reset --hard && | |
452 | git submodule update && | |
453 | git config -f .gitmodules --remove-section submodule.sub && | |
454 | git add .gitmodules && | |
455 | echo "warning: Could not find section in .gitmodules where path=submod" >expect.err && | |
456 | git rm submod >actual 2>actual.err && | |
1108cea7 | 457 | test_cmp expect.err actual.err && |
59a06e94 RA |
458 | test_path_is_missing submod && |
459 | test_path_is_missing submod/.git && | |
95c16418 | 460 | git status -s -uno >actual && |
293ab15e JL |
461 | test_cmp expect actual |
462 | ' | |
463 | ||
464 | test_expect_success 'rm of a populated submodule with modifications fails unless forced' ' | |
465 | git reset --hard && | |
466 | git submodule update && | |
9e189f1a | 467 | echo X >submod/empty && |
293ab15e | 468 | test_must_fail git rm submod && |
59a06e94 RA |
469 | test_path_is_dir submod && |
470 | test_path_is_file submod/.git && | |
9e189f1a | 471 | git status -s -uno --ignore-submodules=none >actual && |
dd6962dd | 472 | test_cmp expect.modified_inside actual && |
293ab15e | 473 | git rm -f submod && |
59a06e94 | 474 | test_path_is_missing submod && |
9e189f1a | 475 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
476 | test_cmp expect actual |
477 | ' | |
478 | ||
479 | test_expect_success 'rm of a populated submodule with untracked files fails unless forced' ' | |
480 | git reset --hard && | |
481 | git submodule update && | |
9e189f1a | 482 | echo X >submod/untracked && |
293ab15e | 483 | test_must_fail git rm submod && |
59a06e94 RA |
484 | test_path_is_dir submod && |
485 | test_path_is_file submod/.git && | |
9e189f1a | 486 | git status -s -uno --ignore-submodules=none >actual && |
dd6962dd | 487 | test_cmp expect.modified_untracked actual && |
293ab15e | 488 | git rm -f submod && |
59a06e94 | 489 | test_path_is_missing submod && |
9e189f1a | 490 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
491 | test_cmp expect actual |
492 | ' | |
493 | ||
494 | test_expect_success 'setup submodule conflict' ' | |
495 | git reset --hard && | |
496 | git submodule update && | |
497 | git checkout -b branch1 && | |
498 | echo 1 >nitfol && | |
499 | git add nitfol && | |
500 | git commit -m "added nitfol 1" && | |
cbc75a12 | 501 | git checkout -b branch2 main && |
293ab15e JL |
502 | echo 2 >nitfol && |
503 | git add nitfol && | |
504 | git commit -m "added nitfol 2" && | |
cbc75a12 | 505 | git checkout -b conflict1 main && |
9e189f1a SB |
506 | git -C submod fetch && |
507 | git -C submod checkout branch1 && | |
293ab15e JL |
508 | git add submod && |
509 | git commit -m "submod 1" && | |
cbc75a12 | 510 | git checkout -b conflict2 main && |
9e189f1a | 511 | git -C submod checkout branch2 && |
293ab15e JL |
512 | git add submod && |
513 | git commit -m "submod 2" | |
514 | ' | |
515 | ||
516 | cat >expect.conflict <<EOF | |
517 | UU submod | |
518 | EOF | |
519 | ||
520 | test_expect_success 'rm removes work tree of unmodified conflicted submodule' ' | |
521 | git checkout conflict1 && | |
522 | git reset --hard && | |
523 | git submodule update && | |
524 | test_must_fail git merge conflict2 && | |
525 | git rm submod && | |
59a06e94 | 526 | test_path_is_missing submod && |
9e189f1a | 527 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
528 | test_cmp expect actual |
529 | ' | |
530 | ||
531 | test_expect_success 'rm of a conflicted populated submodule with different HEAD fails unless forced' ' | |
532 | git checkout conflict1 && | |
533 | git reset --hard && | |
534 | git submodule update && | |
9e189f1a | 535 | git -C submod checkout HEAD^ && |
293ab15e JL |
536 | test_must_fail git merge conflict2 && |
537 | test_must_fail git rm submod && | |
59a06e94 RA |
538 | test_path_is_dir submod && |
539 | test_path_is_file submod/.git && | |
9e189f1a | 540 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
541 | test_cmp expect.conflict actual && |
542 | git rm -f submod && | |
59a06e94 | 543 | test_path_is_missing submod && |
9e189f1a | 544 | git status -s -uno --ignore-submodules=none >actual && |
95c16418 JL |
545 | test_cmp expect actual && |
546 | test_must_fail git config -f .gitmodules submodule.sub.url && | |
547 | test_must_fail git config -f .gitmodules submodule.sub.path | |
293ab15e JL |
548 | ' |
549 | ||
550 | test_expect_success 'rm of a conflicted populated submodule with modifications fails unless forced' ' | |
551 | git checkout conflict1 && | |
552 | git reset --hard && | |
553 | git submodule update && | |
9e189f1a | 554 | echo X >submod/empty && |
293ab15e JL |
555 | test_must_fail git merge conflict2 && |
556 | test_must_fail git rm submod && | |
59a06e94 RA |
557 | test_path_is_dir submod && |
558 | test_path_is_file submod/.git && | |
9e189f1a | 559 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
560 | test_cmp expect.conflict actual && |
561 | git rm -f submod && | |
59a06e94 | 562 | test_path_is_missing submod && |
9e189f1a | 563 | git status -s -uno --ignore-submodules=none >actual && |
95c16418 JL |
564 | test_cmp expect actual && |
565 | test_must_fail git config -f .gitmodules submodule.sub.url && | |
566 | test_must_fail git config -f .gitmodules submodule.sub.path | |
293ab15e JL |
567 | ' |
568 | ||
569 | test_expect_success 'rm of a conflicted populated submodule with untracked files fails unless forced' ' | |
570 | git checkout conflict1 && | |
571 | git reset --hard && | |
572 | git submodule update && | |
9e189f1a | 573 | echo X >submod/untracked && |
293ab15e JL |
574 | test_must_fail git merge conflict2 && |
575 | test_must_fail git rm submod && | |
59a06e94 RA |
576 | test_path_is_dir submod && |
577 | test_path_is_file submod/.git && | |
9e189f1a | 578 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
579 | test_cmp expect.conflict actual && |
580 | git rm -f submod && | |
59a06e94 | 581 | test_path_is_missing submod && |
9e189f1a | 582 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
583 | test_cmp expect actual |
584 | ' | |
585 | ||
586 | test_expect_success 'rm of a conflicted populated submodule with a .git directory fails even when forced' ' | |
587 | git checkout conflict1 && | |
588 | git reset --hard && | |
589 | git submodule update && | |
b5368c23 RA |
590 | ( |
591 | cd submod && | |
293ab15e | 592 | rm .git && |
2d3ac9ad | 593 | cp -R ../.git/modules/sub .git && |
293ab15e JL |
594 | GIT_WORK_TREE=. git config --unset core.worktree |
595 | ) && | |
596 | test_must_fail git merge conflict2 && | |
597 | test_must_fail git rm submod && | |
59a06e94 RA |
598 | test_path_is_dir submod && |
599 | test_path_is_dir submod/.git && | |
9e189f1a | 600 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
601 | test_cmp expect.conflict actual && |
602 | test_must_fail git rm -f submod && | |
59a06e94 RA |
603 | test_path_is_dir submod && |
604 | test_path_is_dir submod/.git && | |
9e189f1a | 605 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
606 | test_cmp expect.conflict actual && |
607 | git merge --abort && | |
608 | rm -rf submod | |
609 | ' | |
610 | ||
611 | test_expect_success 'rm of a conflicted unpopulated submodule succeeds' ' | |
612 | git checkout conflict1 && | |
613 | git reset --hard && | |
614 | test_must_fail git merge conflict2 && | |
615 | git rm submod && | |
59a06e94 | 616 | test_path_is_missing submod && |
9e189f1a | 617 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
618 | test_cmp expect actual |
619 | ' | |
620 | ||
55856a35 | 621 | test_expect_success 'rm of a populated submodule with a .git directory migrates git dir' ' |
cbc75a12 | 622 | git checkout -f main && |
293ab15e JL |
623 | git reset --hard && |
624 | git submodule update && | |
b5368c23 RA |
625 | ( |
626 | cd submod && | |
293ab15e | 627 | rm .git && |
2d3ac9ad | 628 | cp -R ../.git/modules/sub .git && |
55856a35 SB |
629 | GIT_WORK_TREE=. git config --unset core.worktree && |
630 | rm -r ../.git/modules/sub | |
293ab15e | 631 | ) && |
55856a35 | 632 | git rm submod 2>output.err && |
59a06e94 RA |
633 | test_path_is_missing submod && |
634 | test_path_is_missing submod/.git && | |
9e189f1a | 635 | git status -s -uno --ignore-submodules=none >actual && |
59a06e94 | 636 | test_file_not_empty actual && |
55856a35 | 637 | test_i18ngrep Migrating output.err |
293ab15e JL |
638 | ' |
639 | ||
640 | cat >expect.deepmodified <<EOF | |
641 | M submod/subsubmod | |
642 | EOF | |
643 | ||
644 | test_expect_success 'setup subsubmodule' ' | |
645 | git reset --hard && | |
646 | git submodule update && | |
b5368c23 RA |
647 | ( |
648 | cd submod && | |
3b737381 DL |
649 | hash=$(git rev-parse HEAD) && |
650 | git update-index --add --cacheinfo 160000 "$hash" subsubmod && | |
293ab15e JL |
651 | git config -f .gitmodules submodule.sub.url ../. && |
652 | git config -f .gitmodules submodule.sub.path subsubmod && | |
653 | git submodule init && | |
654 | git add .gitmodules && | |
655 | git commit -m "add subsubmodule" && | |
656 | git submodule update subsubmod | |
657 | ) && | |
658 | git commit -a -m "added deep submodule" | |
659 | ' | |
660 | ||
661 | test_expect_success 'rm recursively removes work tree of unmodified submodules' ' | |
662 | git rm submod && | |
59a06e94 | 663 | test_path_is_missing submod && |
9e189f1a | 664 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
665 | test_cmp expect actual |
666 | ' | |
667 | ||
668 | test_expect_success 'rm of a populated nested submodule with different nested HEAD fails unless forced' ' | |
669 | git reset --hard && | |
670 | git submodule update --recursive && | |
9e189f1a | 671 | git -C submod/subsubmod checkout HEAD^ && |
293ab15e | 672 | test_must_fail git rm submod && |
59a06e94 RA |
673 | test_path_is_dir submod && |
674 | test_path_is_file submod/.git && | |
9e189f1a | 675 | git status -s -uno --ignore-submodules=none >actual && |
dd6962dd | 676 | test_cmp expect.modified_inside actual && |
293ab15e | 677 | git rm -f submod && |
59a06e94 | 678 | test_path_is_missing submod && |
9e189f1a | 679 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
680 | test_cmp expect actual |
681 | ' | |
682 | ||
683 | test_expect_success 'rm of a populated nested submodule with nested modifications fails unless forced' ' | |
684 | git reset --hard && | |
685 | git submodule update --recursive && | |
9e189f1a | 686 | echo X >submod/subsubmod/empty && |
293ab15e | 687 | test_must_fail git rm submod && |
59a06e94 RA |
688 | test_path_is_dir submod && |
689 | test_path_is_file submod/.git && | |
9e189f1a | 690 | git status -s -uno --ignore-submodules=none >actual && |
dd6962dd | 691 | test_cmp expect.modified_inside actual && |
293ab15e | 692 | git rm -f submod && |
59a06e94 | 693 | test_path_is_missing submod && |
9e189f1a | 694 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
695 | test_cmp expect actual |
696 | ' | |
697 | ||
698 | test_expect_success 'rm of a populated nested submodule with nested untracked files fails unless forced' ' | |
699 | git reset --hard && | |
700 | git submodule update --recursive && | |
9e189f1a | 701 | echo X >submod/subsubmod/untracked && |
293ab15e | 702 | test_must_fail git rm submod && |
59a06e94 RA |
703 | test_path_is_dir submod && |
704 | test_path_is_file submod/.git && | |
9e189f1a | 705 | git status -s -uno --ignore-submodules=none >actual && |
40069d6e | 706 | test_cmp expect.modified_untracked actual && |
293ab15e | 707 | git rm -f submod && |
59a06e94 | 708 | test_path_is_missing submod && |
9e189f1a | 709 | git status -s -uno --ignore-submodules=none >actual && |
293ab15e JL |
710 | test_cmp expect actual |
711 | ' | |
712 | ||
70471ed9 | 713 | test_expect_success "rm absorbs submodule's nested .git directory" ' |
293ab15e JL |
714 | git reset --hard && |
715 | git submodule update --recursive && | |
b5368c23 RA |
716 | ( |
717 | cd submod/subsubmod && | |
293ab15e | 718 | rm .git && |
55856a35 | 719 | mv ../../.git/modules/sub/modules/sub .git && |
293ab15e JL |
720 | GIT_WORK_TREE=. git config --unset core.worktree |
721 | ) && | |
55856a35 | 722 | git rm submod 2>output.err && |
59a06e94 RA |
723 | test_path_is_missing submod && |
724 | test_path_is_missing submod/subsubmod/.git && | |
9e189f1a | 725 | git status -s -uno --ignore-submodules=none >actual && |
59a06e94 | 726 | test_file_not_empty actual && |
55856a35 | 727 | test_i18ngrep Migrating output.err |
293ab15e JL |
728 | ' |
729 | ||
bbad9f93 | 730 | test_expect_success 'checking out a commit after submodule removal needs manual updates' ' |
55856a35 | 731 | git commit -m "submodule removal" submod .gitmodules && |
bbad9f93 JL |
732 | git checkout HEAD^ && |
733 | git submodule update && | |
8954bd76 | 734 | git checkout -q HEAD^ && |
cbc75a12 | 735 | git checkout -q main 2>actual && |
0a288d1e | 736 | test_i18ngrep "^warning: unable to rmdir '\''submod'\'':" actual && |
bbad9f93 JL |
737 | git status -s submod >actual && |
738 | echo "?? submod/" >expected && | |
739 | test_cmp expected actual && | |
740 | rm -rf submod && | |
9e189f1a | 741 | git status -s -uno --ignore-submodules=none >actual && |
ec10b018 | 742 | test_must_be_empty actual |
bbad9f93 JL |
743 | ' |
744 | ||
9a6728d4 JK |
745 | test_expect_success 'rm of d/f when d has become a non-directory' ' |
746 | rm -rf d && | |
747 | mkdir d && | |
748 | >d/f && | |
749 | git add d && | |
750 | rm -rf d && | |
751 | >d && | |
752 | git rm d/f && | |
753 | test_must_fail git rev-parse --verify :d/f && | |
754 | test_path_is_file d | |
755 | ' | |
756 | ||
757 | test_expect_success SYMLINKS 'rm of d/f when d has become a dangling symlink' ' | |
758 | rm -rf d && | |
759 | mkdir d && | |
760 | >d/f && | |
761 | git add d && | |
762 | rm -rf d && | |
763 | ln -s nonexistent d && | |
764 | git rm d/f && | |
765 | test_must_fail git rev-parse --verify :d/f && | |
766 | test -h d && | |
767 | test_path_is_missing d | |
768 | ' | |
769 | ||
96ec8ee9 JK |
770 | test_expect_success 'rm of file when it has become a directory' ' |
771 | rm -rf d && | |
772 | >d && | |
773 | git add d && | |
774 | rm -f d && | |
775 | mkdir d && | |
776 | >d/f && | |
777 | test_must_fail git rm d && | |
778 | git rev-parse --verify :d && | |
779 | test_path_is_file d/f | |
780 | ' | |
781 | ||
03415ca8 JK |
782 | test_expect_success SYMLINKS 'rm across a symlinked leading path (no index)' ' |
783 | rm -rf d e && | |
784 | mkdir e && | |
785 | echo content >e/f && | |
786 | ln -s e d && | |
787 | git add -A e d && | |
788 | git commit -m "symlink d to e, e/f exists" && | |
789 | test_must_fail git rm d/f && | |
790 | git rev-parse --verify :d && | |
791 | git rev-parse --verify :e/f && | |
792 | test -h d && | |
793 | test_path_is_file e/f | |
794 | ' | |
795 | ||
796 | test_expect_failure SYMLINKS 'rm across a symlinked leading path (w/ index)' ' | |
797 | rm -rf d e && | |
798 | mkdir d && | |
799 | echo content >d/f && | |
800 | git add -A e d && | |
801 | git commit -m "d/f exists" && | |
802 | mv d e && | |
803 | ln -s e d && | |
804 | test_must_fail git rm d/f && | |
805 | git rev-parse --verify :d/f && | |
806 | test -h d && | |
807 | test_path_is_file e/f | |
808 | ' | |
809 | ||
914dc028 MLM |
810 | test_expect_success 'setup for testing rm messages' ' |
811 | >bar.txt && | |
812 | >foo.txt && | |
813 | git add bar.txt foo.txt | |
814 | ' | |
815 | ||
816 | test_expect_success 'rm files with different staged content' ' | |
817 | cat >expect <<-\EOF && | |
818 | error: the following files have staged content different from both the | |
819 | file and the HEAD: | |
820 | bar.txt | |
821 | foo.txt | |
822 | (use -f to force removal) | |
823 | EOF | |
824 | echo content1 >foo.txt && | |
825 | echo content1 >bar.txt && | |
826 | test_must_fail git rm foo.txt bar.txt 2>actual && | |
1108cea7 | 827 | test_cmp expect actual |
914dc028 MLM |
828 | ' |
829 | ||
7e309446 MLM |
830 | test_expect_success 'rm files with different staged content without hints' ' |
831 | cat >expect <<-\EOF && | |
832 | error: the following files have staged content different from both the | |
833 | file and the HEAD: | |
834 | bar.txt | |
835 | foo.txt | |
836 | EOF | |
837 | echo content2 >foo.txt && | |
838 | echo content2 >bar.txt && | |
839 | test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual && | |
1108cea7 | 840 | test_cmp expect actual |
7e309446 | 841 | ' |
914dc028 MLM |
842 | |
843 | test_expect_success 'rm file with local modification' ' | |
844 | cat >expect <<-\EOF && | |
845 | error: the following file has local modifications: | |
846 | foo.txt | |
847 | (use --cached to keep the file, or -f to force removal) | |
848 | EOF | |
849 | git commit -m "testing rm 3" && | |
850 | echo content3 >foo.txt && | |
851 | test_must_fail git rm foo.txt 2>actual && | |
1108cea7 | 852 | test_cmp expect actual |
914dc028 MLM |
853 | ' |
854 | ||
7e309446 MLM |
855 | test_expect_success 'rm file with local modification without hints' ' |
856 | cat >expect <<-\EOF && | |
857 | error: the following file has local modifications: | |
858 | bar.txt | |
859 | EOF | |
860 | echo content4 >bar.txt && | |
861 | test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual && | |
1108cea7 | 862 | test_cmp expect actual |
7e309446 | 863 | ' |
914dc028 MLM |
864 | |
865 | test_expect_success 'rm file with changes in the index' ' | |
866 | cat >expect <<-\EOF && | |
867 | error: the following file has changes staged in the index: | |
868 | foo.txt | |
869 | (use --cached to keep the file, or -f to force removal) | |
870 | EOF | |
871 | git reset --hard && | |
872 | echo content5 >foo.txt && | |
873 | git add foo.txt && | |
874 | test_must_fail git rm foo.txt 2>actual && | |
1108cea7 | 875 | test_cmp expect actual |
914dc028 MLM |
876 | ' |
877 | ||
7e309446 MLM |
878 | test_expect_success 'rm file with changes in the index without hints' ' |
879 | cat >expect <<-\EOF && | |
880 | error: the following file has changes staged in the index: | |
881 | foo.txt | |
882 | EOF | |
883 | test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual && | |
1108cea7 | 884 | test_cmp expect actual |
7e309446 | 885 | ' |
914dc028 MLM |
886 | |
887 | test_expect_success 'rm files with two different errors' ' | |
888 | cat >expect <<-\EOF && | |
889 | error: the following file has staged content different from both the | |
890 | file and the HEAD: | |
891 | foo1.txt | |
892 | (use -f to force removal) | |
893 | error: the following file has changes staged in the index: | |
894 | bar1.txt | |
895 | (use --cached to keep the file, or -f to force removal) | |
896 | EOF | |
897 | echo content >foo1.txt && | |
898 | git add foo1.txt && | |
899 | echo content6 >foo1.txt && | |
900 | echo content6 >bar1.txt && | |
901 | git add bar1.txt && | |
902 | test_must_fail git rm bar1.txt foo1.txt 2>actual && | |
1108cea7 | 903 | test_cmp expect actual |
914dc028 MLM |
904 | ' |
905 | ||
9e4e8a64 EX |
906 | test_expect_success 'rm empty string should fail' ' |
907 | test_must_fail git rm -rf "" | |
d426430e EX |
908 | ' |
909 | ||
d4a1cab5 | 910 | test_done |