]> git.ipfire.org Git - thirdparty/git.git/blob - t/t3600-rm.sh
Merge branch 'en/fetch-negotiation-default-fix'
[thirdparty/git.git] / t / t3600-rm.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Carl D. Worth
4 #
5
6 test_description='Test of the various options to git rm.'
7
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
11 . ./test-lib.sh
12
13 # Setup some files to be removed, some with funny characters
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 '
19
20 if test_have_prereq !FUNNYNAMES
21 then
22 say 'Your filesystem does not allow tabs in filenames.'
23 fi
24
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' '
32 test_path_is_file foo &&
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' '
75 test_path_is_file foo &&
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"' '
80 test_path_is_file bar &&
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"' '
89 test_path_is_missing bar &&
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 '
100
101 test_expect_success SANITY 'Test that "git rm -f" fails if its rm fails' '
102 test_when_finished "chmod 775 ." &&
103 chmod a-w . &&
104 test_must_fail git rm -f baz
105 '
106
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 '
110
111 test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
112 git rm --ignore-unmatch nonexistent
113 '
114
115 test_expect_success '"rm" command printed' '
116 echo frotz >test-file &&
117 git add test-file &&
118 git commit -m "add file for rm test" &&
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 &&
123 git commit -m "remove file from rm test"
124 '
125
126 test_expect_success '"rm" command suppressed with --quiet' '
127 echo frotz >test-file &&
128 git add test-file &&
129 git commit -m "add file for rm --quiet test" &&
130 git rm --quiet test-file >rm-output &&
131 test_must_be_empty rm-output &&
132 rm -f test-file rm-output &&
133 git commit -m "remove file from rm --quiet test"
134 '
135
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 &&
144 test_must_fail git rm foo baz &&
145 test_path_is_file foo &&
146 test_path_is_file baz &&
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 &&
152 test_path_is_missing foo &&
153 test_path_is_missing baz &&
154 test_must_fail git ls-files --error-unmatch foo &&
155 test_must_fail git ls-files --error-unmatch bar
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' '
166 test_must_fail git rm foo baz &&
167 test_path_is_file foo &&
168 test_path_is_file baz &&
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 &&
174 test_path_is_missing foo &&
175 test_path_is_missing baz &&
176 test_must_fail git ls-files --error-unmatch foo &&
177 test_must_fail git ls-files --error-unmatch baz
178 '
179
180 test_expect_success 'refuse to remove cached empty file with modifications' '
181 >empty &&
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 &&
189 git add -N intent-to-add &&
190 git rm --cached intent-to-add
191 '
192
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' '
201 test_must_fail git rm frotz &&
202 test_path_is_dir frotz &&
203 test_path_is_file frotz/nitfol
204 '
205
206 test_expect_success 'Recursive with -r but dirty' '
207 echo qfwfq >>frotz/nitfol &&
208 test_must_fail git rm -r frotz &&
209 test_path_is_dir frotz &&
210 test_path_is_file frotz/nitfol
211 '
212
213 test_expect_success 'Recursive with -r -f' '
214 git rm -f -r frotz &&
215 test_path_is_missing frotz/nitfol &&
216 test_path_is_missing frotz
217 '
218
219 test_expect_success 'Remove nonexistent file returns nonzero exit status' '
220 test_must_fail git rm nonexistent
221 '
222
223 test_expect_success 'Call "rm" from outside the work tree' '
224 mkdir repo &&
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 )
237 '
238
239 test_expect_success 'refresh index before checking if it is up-to-date' '
240 git reset --hard &&
241 test-tool chmtime -86400 frotz/nitfol &&
242 git rm frotz/nitfol &&
243 test_path_is_missing frotz/nitfol
244 '
245
246 choke_git_rm_setup() {
247 git reset -q --hard &&
248 test_when_finished "rm -f .git/index.lock && git reset -q --hard" &&
249 i=0 &&
250 hash=$(test_oid deadbeef) &&
251 while test $i -lt 12000
252 do
253 echo "100644 $hash 0 some-file-$i"
254 i=$(( $i + 1 ))
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 &&
260 # git command is intentionally placed upstream of pipe to induce SIGPIPE
261 git rm -n "some-file-*" | : &&
262 test_path_is_missing .git/index.lock
263 '
264
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
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 printf "100644 $blob %d\tblob\n" 1 2 3 | git update-index --index-info &&
278 git rm blob >msg 2>&1 &&
279 test_i18ngrep ! "needs merge" msg &&
280 test_must_fail git ls-files -s --error-unmatch blob
281 '
282
283 test_expect_success 'rm removes subdirectories recursively' '
284 mkdir -p dir/subdir/subsubdir &&
285 echo content >dir/subdir/subsubdir/file &&
286 git add dir/subdir/subsubdir/file &&
287 git rm -f dir/subdir/subsubdir/file &&
288 test_path_is_missing dir
289 '
290
291 cat >expect <<EOF
292 M .gitmodules
293 D submod
294 EOF
295
296 cat >expect.modified <<EOF
297 M submod
298 EOF
299
300 cat >expect.modified_inside <<EOF
301 m submod
302 EOF
303
304 cat >expect.modified_untracked <<EOF
305 ? submod
306 EOF
307
308 cat >expect.cached <<EOF
309 D submod
310 EOF
311
312 cat >expect.both_deleted<<EOF
313 D .gitmodules
314 D submod
315 EOF
316
317 test_expect_success 'rm removes empty submodules from work tree' '
318 mkdir submod &&
319 hash=$(git rev-parse HEAD) &&
320 git update-index --add --cacheinfo 160000 "$hash" submod &&
321 git config -f .gitmodules submodule.sub.url ./. &&
322 git config -f .gitmodules submodule.sub.path submod &&
323 git submodule init &&
324 git add .gitmodules &&
325 git commit -m "add submodule" &&
326 git rm submod &&
327 test_path_is_missing submod &&
328 git status -s -uno --ignore-submodules=none >actual &&
329 test_cmp expect actual &&
330 test_must_fail git config -f .gitmodules submodule.sub.url &&
331 test_must_fail git config -f .gitmodules submodule.sub.path
332 '
333
334 test_expect_success 'rm removes removed submodule from index and .gitmodules' '
335 git reset --hard &&
336 git submodule update &&
337 rm -rf submod &&
338 git rm submod &&
339 git status -s -uno --ignore-submodules=none >actual &&
340 test_cmp expect actual &&
341 test_must_fail git config -f .gitmodules submodule.sub.url &&
342 test_must_fail git config -f .gitmodules submodule.sub.path
343 '
344
345 test_expect_success 'rm removes work tree of unmodified submodules' '
346 git reset --hard &&
347 git submodule update &&
348 git rm submod &&
349 test_path_is_missing submod &&
350 git status -s -uno --ignore-submodules=none >actual &&
351 test_cmp expect actual &&
352 test_must_fail git config -f .gitmodules submodule.sub.url &&
353 test_must_fail git config -f .gitmodules submodule.sub.path
354 '
355
356 test_expect_success 'rm removes a submodule with a trailing /' '
357 git reset --hard &&
358 git submodule update &&
359 git rm submod/ &&
360 test_path_is_missing submod &&
361 git status -s -uno --ignore-submodules=none >actual &&
362 test_cmp expect actual
363 '
364
365 test_expect_success 'rm fails when given a file with a trailing /' '
366 test_must_fail git rm empty/
367 '
368
369 test_expect_success 'rm succeeds when given a directory with a trailing /' '
370 git rm -r frotz/
371 '
372
373 test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '
374 git reset --hard &&
375 git submodule update &&
376 git -C submod checkout HEAD^ &&
377 test_must_fail git rm submod &&
378 test_path_is_dir submod &&
379 test_path_is_file submod/.git &&
380 git status -s -uno --ignore-submodules=none >actual &&
381 test_cmp expect.modified actual &&
382 git rm -f submod &&
383 test_path_is_missing submod &&
384 git status -s -uno --ignore-submodules=none >actual &&
385 test_cmp expect actual &&
386 test_must_fail git config -f .gitmodules submodule.sub.url &&
387 test_must_fail git config -f .gitmodules submodule.sub.path
388 '
389
390 test_expect_success 'rm --cached leaves work tree of populated submodules and .gitmodules alone' '
391 git reset --hard &&
392 git submodule update &&
393 git rm --cached submod &&
394 test_path_is_dir submod &&
395 test_path_is_file submod/.git &&
396 git status -s -uno >actual &&
397 test_cmp expect.cached actual &&
398 git config -f .gitmodules submodule.sub.url &&
399 git config -f .gitmodules submodule.sub.path
400 '
401
402 test_expect_success 'rm --dry-run does not touch the submodule or .gitmodules' '
403 git reset --hard &&
404 git submodule update &&
405 git rm -n submod &&
406 test_path_is_file submod/.git &&
407 git diff-index --exit-code HEAD
408 '
409
410 test_expect_success 'rm does not complain when no .gitmodules file is found' '
411 git reset --hard &&
412 git submodule update &&
413 git rm .gitmodules &&
414 git rm submod >actual 2>actual.err &&
415 test_must_be_empty actual.err &&
416 test_path_is_missing submod &&
417 test_path_is_missing submod/.git &&
418 git status -s -uno >actual &&
419 test_cmp expect.both_deleted actual
420 '
421
422 test_expect_success 'rm will error out on a modified .gitmodules file unless staged' '
423 git reset --hard &&
424 git submodule update &&
425 git config -f .gitmodules foo.bar true &&
426 test_must_fail git rm submod >actual 2>actual.err &&
427 test_file_not_empty actual.err &&
428 test_path_is_dir submod &&
429 test_path_is_file submod/.git &&
430 git diff-files --quiet -- submod &&
431 git add .gitmodules &&
432 git rm submod >actual 2>actual.err &&
433 test_must_be_empty actual.err &&
434 test_path_is_missing submod &&
435 test_path_is_missing submod/.git &&
436 git status -s -uno >actual &&
437 test_cmp expect actual
438 '
439 test_expect_success 'rm will not error out on .gitmodules file with zero stat data' '
440 git reset --hard &&
441 git submodule update &&
442 git read-tree HEAD &&
443 git rm submod &&
444 test_path_is_missing submod
445 '
446
447 test_expect_success 'rm issues a warning when section is not found in .gitmodules' '
448 git reset --hard &&
449 git submodule update &&
450 git config -f .gitmodules --remove-section submodule.sub &&
451 git add .gitmodules &&
452 echo "warning: Could not find section in .gitmodules where path=submod" >expect.err &&
453 git rm submod >actual 2>actual.err &&
454 test_cmp expect.err actual.err &&
455 test_path_is_missing submod &&
456 test_path_is_missing submod/.git &&
457 git status -s -uno >actual &&
458 test_cmp expect actual
459 '
460
461 test_expect_success 'rm of a populated submodule with modifications fails unless forced' '
462 git reset --hard &&
463 git submodule update &&
464 echo X >submod/empty &&
465 test_must_fail git rm submod &&
466 test_path_is_dir submod &&
467 test_path_is_file submod/.git &&
468 git status -s -uno --ignore-submodules=none >actual &&
469 test_cmp expect.modified_inside actual &&
470 git rm -f submod &&
471 test_path_is_missing submod &&
472 git status -s -uno --ignore-submodules=none >actual &&
473 test_cmp expect actual
474 '
475
476 test_expect_success 'rm of a populated submodule with untracked files fails unless forced' '
477 git reset --hard &&
478 git submodule update &&
479 echo X >submod/untracked &&
480 test_must_fail git rm submod &&
481 test_path_is_dir submod &&
482 test_path_is_file submod/.git &&
483 git status -s -uno --ignore-submodules=none >actual &&
484 test_cmp expect.modified_untracked actual &&
485 git rm -f submod &&
486 test_path_is_missing submod &&
487 git status -s -uno --ignore-submodules=none >actual &&
488 test_cmp expect actual
489 '
490
491 test_expect_success 'setup submodule conflict' '
492 git reset --hard &&
493 git submodule update &&
494 git checkout -b branch1 &&
495 echo 1 >nitfol &&
496 git add nitfol &&
497 git commit -m "added nitfol 1" &&
498 git checkout -b branch2 main &&
499 echo 2 >nitfol &&
500 git add nitfol &&
501 git commit -m "added nitfol 2" &&
502 git checkout -b conflict1 main &&
503 git -C submod fetch &&
504 git -C submod checkout branch1 &&
505 git add submod &&
506 git commit -m "submod 1" &&
507 git checkout -b conflict2 main &&
508 git -C submod checkout branch2 &&
509 git add submod &&
510 git commit -m "submod 2"
511 '
512
513 cat >expect.conflict <<EOF
514 UU submod
515 EOF
516
517 test_expect_success 'rm removes work tree of unmodified conflicted submodule' '
518 git checkout conflict1 &&
519 git reset --hard &&
520 git submodule update &&
521 test_must_fail git merge conflict2 &&
522 git rm submod &&
523 test_path_is_missing submod &&
524 git status -s -uno --ignore-submodules=none >actual &&
525 test_cmp expect actual
526 '
527
528 test_expect_success 'rm of a conflicted populated submodule with different HEAD fails unless forced' '
529 git checkout conflict1 &&
530 git reset --hard &&
531 git submodule update &&
532 git -C submod checkout HEAD^ &&
533 test_must_fail git merge conflict2 &&
534 test_must_fail git rm submod &&
535 test_path_is_dir submod &&
536 test_path_is_file submod/.git &&
537 git status -s -uno --ignore-submodules=none >actual &&
538 test_cmp expect.conflict actual &&
539 git rm -f submod &&
540 test_path_is_missing submod &&
541 git status -s -uno --ignore-submodules=none >actual &&
542 test_cmp expect actual &&
543 test_must_fail git config -f .gitmodules submodule.sub.url &&
544 test_must_fail git config -f .gitmodules submodule.sub.path
545 '
546
547 test_expect_success 'rm of a conflicted populated submodule with modifications fails unless forced' '
548 git checkout conflict1 &&
549 git reset --hard &&
550 git submodule update &&
551 echo X >submod/empty &&
552 test_must_fail git merge conflict2 &&
553 test_must_fail git rm submod &&
554 test_path_is_dir submod &&
555 test_path_is_file submod/.git &&
556 git status -s -uno --ignore-submodules=none >actual &&
557 test_cmp expect.conflict actual &&
558 git rm -f submod &&
559 test_path_is_missing submod &&
560 git status -s -uno --ignore-submodules=none >actual &&
561 test_cmp expect actual &&
562 test_must_fail git config -f .gitmodules submodule.sub.url &&
563 test_must_fail git config -f .gitmodules submodule.sub.path
564 '
565
566 test_expect_success 'rm of a conflicted populated submodule with untracked files fails unless forced' '
567 git checkout conflict1 &&
568 git reset --hard &&
569 git submodule update &&
570 echo X >submod/untracked &&
571 test_must_fail git merge conflict2 &&
572 test_must_fail git rm submod &&
573 test_path_is_dir submod &&
574 test_path_is_file submod/.git &&
575 git status -s -uno --ignore-submodules=none >actual &&
576 test_cmp expect.conflict actual &&
577 git rm -f submod &&
578 test_path_is_missing submod &&
579 git status -s -uno --ignore-submodules=none >actual &&
580 test_cmp expect actual
581 '
582
583 test_expect_success 'rm of a conflicted populated submodule with a .git directory fails even when forced' '
584 git checkout conflict1 &&
585 git reset --hard &&
586 git submodule update &&
587 (
588 cd submod &&
589 rm .git &&
590 cp -R ../.git/modules/sub .git &&
591 GIT_WORK_TREE=. git config --unset core.worktree
592 ) &&
593 test_must_fail git merge conflict2 &&
594 test_must_fail git rm submod &&
595 test_path_is_dir submod &&
596 test_path_is_dir submod/.git &&
597 git status -s -uno --ignore-submodules=none >actual &&
598 test_cmp expect.conflict actual &&
599 test_must_fail git rm -f submod &&
600 test_path_is_dir submod &&
601 test_path_is_dir submod/.git &&
602 git status -s -uno --ignore-submodules=none >actual &&
603 test_cmp expect.conflict actual &&
604 git merge --abort &&
605 rm -rf submod
606 '
607
608 test_expect_success 'rm of a conflicted unpopulated submodule succeeds' '
609 git checkout conflict1 &&
610 git reset --hard &&
611 test_must_fail git merge conflict2 &&
612 git rm submod &&
613 test_path_is_missing submod &&
614 git status -s -uno --ignore-submodules=none >actual &&
615 test_cmp expect actual
616 '
617
618 test_expect_success 'rm of a populated submodule with a .git directory migrates git dir' '
619 git checkout -f main &&
620 git reset --hard &&
621 git submodule update &&
622 (
623 cd submod &&
624 rm .git &&
625 cp -R ../.git/modules/sub .git &&
626 GIT_WORK_TREE=. git config --unset core.worktree &&
627 rm -r ../.git/modules/sub
628 ) &&
629 git rm submod 2>output.err &&
630 test_path_is_missing submod &&
631 test_path_is_missing submod/.git &&
632 git status -s -uno --ignore-submodules=none >actual &&
633 test_file_not_empty actual &&
634 test_i18ngrep Migrating output.err
635 '
636
637 cat >expect.deepmodified <<EOF
638 M submod/subsubmod
639 EOF
640
641 test_expect_success 'setup subsubmodule' '
642 git reset --hard &&
643 git submodule update &&
644 (
645 cd submod &&
646 hash=$(git rev-parse HEAD) &&
647 git update-index --add --cacheinfo 160000 "$hash" subsubmod &&
648 git config -f .gitmodules submodule.sub.url ../. &&
649 git config -f .gitmodules submodule.sub.path subsubmod &&
650 git submodule init &&
651 git add .gitmodules &&
652 git commit -m "add subsubmodule" &&
653 git submodule update subsubmod
654 ) &&
655 git commit -a -m "added deep submodule"
656 '
657
658 test_expect_success 'rm recursively removes work tree of unmodified submodules' '
659 git rm submod &&
660 test_path_is_missing submod &&
661 git status -s -uno --ignore-submodules=none >actual &&
662 test_cmp expect actual
663 '
664
665 test_expect_success 'rm of a populated nested submodule with different nested HEAD fails unless forced' '
666 git reset --hard &&
667 git submodule update --recursive &&
668 git -C submod/subsubmod checkout HEAD^ &&
669 test_must_fail git rm submod &&
670 test_path_is_dir submod &&
671 test_path_is_file submod/.git &&
672 git status -s -uno --ignore-submodules=none >actual &&
673 test_cmp expect.modified_inside actual &&
674 git rm -f submod &&
675 test_path_is_missing submod &&
676 git status -s -uno --ignore-submodules=none >actual &&
677 test_cmp expect actual
678 '
679
680 test_expect_success 'rm of a populated nested submodule with nested modifications fails unless forced' '
681 git reset --hard &&
682 git submodule update --recursive &&
683 echo X >submod/subsubmod/empty &&
684 test_must_fail git rm submod &&
685 test_path_is_dir submod &&
686 test_path_is_file submod/.git &&
687 git status -s -uno --ignore-submodules=none >actual &&
688 test_cmp expect.modified_inside actual &&
689 git rm -f submod &&
690 test_path_is_missing submod &&
691 git status -s -uno --ignore-submodules=none >actual &&
692 test_cmp expect actual
693 '
694
695 test_expect_success 'rm of a populated nested submodule with nested untracked files fails unless forced' '
696 git reset --hard &&
697 git submodule update --recursive &&
698 echo X >submod/subsubmod/untracked &&
699 test_must_fail git rm submod &&
700 test_path_is_dir submod &&
701 test_path_is_file submod/.git &&
702 git status -s -uno --ignore-submodules=none >actual &&
703 test_cmp expect.modified_untracked actual &&
704 git rm -f submod &&
705 test_path_is_missing submod &&
706 git status -s -uno --ignore-submodules=none >actual &&
707 test_cmp expect actual
708 '
709
710 test_expect_success "rm absorbs submodule's nested .git directory" '
711 git reset --hard &&
712 git submodule update --recursive &&
713 (
714 cd submod/subsubmod &&
715 rm .git &&
716 mv ../../.git/modules/sub/modules/sub .git &&
717 GIT_WORK_TREE=. git config --unset core.worktree
718 ) &&
719 git rm submod 2>output.err &&
720 test_path_is_missing submod &&
721 test_path_is_missing submod/subsubmod/.git &&
722 git status -s -uno --ignore-submodules=none >actual &&
723 test_file_not_empty actual &&
724 test_i18ngrep Migrating output.err
725 '
726
727 test_expect_success 'checking out a commit after submodule removal needs manual updates' '
728 git commit -m "submodule removal" submod .gitmodules &&
729 git checkout HEAD^ &&
730 git submodule update &&
731 git checkout -q HEAD^ &&
732 git checkout -q main 2>actual &&
733 test_i18ngrep "^warning: unable to rmdir '\''submod'\'':" actual &&
734 git status -s submod >actual &&
735 echo "?? submod/" >expected &&
736 test_cmp expected actual &&
737 rm -rf submod &&
738 git status -s -uno --ignore-submodules=none >actual &&
739 test_must_be_empty actual
740 '
741
742 test_expect_success 'rm of d/f when d has become a non-directory' '
743 rm -rf d &&
744 mkdir d &&
745 >d/f &&
746 git add d &&
747 rm -rf d &&
748 >d &&
749 git rm d/f &&
750 test_must_fail git rev-parse --verify :d/f &&
751 test_path_is_file d
752 '
753
754 test_expect_success SYMLINKS 'rm of d/f when d has become a dangling symlink' '
755 rm -rf d &&
756 mkdir d &&
757 >d/f &&
758 git add d &&
759 rm -rf d &&
760 ln -s nonexistent d &&
761 git rm d/f &&
762 test_must_fail git rev-parse --verify :d/f &&
763 test -h d &&
764 test_path_is_missing d
765 '
766
767 test_expect_success 'rm of file when it has become a directory' '
768 rm -rf d &&
769 >d &&
770 git add d &&
771 rm -f d &&
772 mkdir d &&
773 >d/f &&
774 test_must_fail git rm d &&
775 git rev-parse --verify :d &&
776 test_path_is_file d/f
777 '
778
779 test_expect_success SYMLINKS 'rm across a symlinked leading path (no index)' '
780 rm -rf d e &&
781 mkdir e &&
782 echo content >e/f &&
783 ln -s e d &&
784 git add -A e d &&
785 git commit -m "symlink d to e, e/f exists" &&
786 test_must_fail git rm d/f &&
787 git rev-parse --verify :d &&
788 git rev-parse --verify :e/f &&
789 test -h d &&
790 test_path_is_file e/f
791 '
792
793 test_expect_failure SYMLINKS 'rm across a symlinked leading path (w/ index)' '
794 rm -rf d e &&
795 mkdir d &&
796 echo content >d/f &&
797 git add -A e d &&
798 git commit -m "d/f exists" &&
799 mv d e &&
800 ln -s e d &&
801 test_must_fail git rm d/f &&
802 git rev-parse --verify :d/f &&
803 test -h d &&
804 test_path_is_file e/f
805 '
806
807 test_expect_success 'setup for testing rm messages' '
808 >bar.txt &&
809 >foo.txt &&
810 git add bar.txt foo.txt
811 '
812
813 test_expect_success 'rm files with different staged content' '
814 cat >expect <<-\EOF &&
815 error: the following files have staged content different from both the
816 file and the HEAD:
817 bar.txt
818 foo.txt
819 (use -f to force removal)
820 EOF
821 echo content1 >foo.txt &&
822 echo content1 >bar.txt &&
823 test_must_fail git rm foo.txt bar.txt 2>actual &&
824 test_cmp expect actual
825 '
826
827 test_expect_success 'rm files with different staged content without hints' '
828 cat >expect <<-\EOF &&
829 error: the following files have staged content different from both the
830 file and the HEAD:
831 bar.txt
832 foo.txt
833 EOF
834 echo content2 >foo.txt &&
835 echo content2 >bar.txt &&
836 test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual &&
837 test_cmp expect actual
838 '
839
840 test_expect_success 'rm file with local modification' '
841 cat >expect <<-\EOF &&
842 error: the following file has local modifications:
843 foo.txt
844 (use --cached to keep the file, or -f to force removal)
845 EOF
846 git commit -m "testing rm 3" &&
847 echo content3 >foo.txt &&
848 test_must_fail git rm foo.txt 2>actual &&
849 test_cmp expect actual
850 '
851
852 test_expect_success 'rm file with local modification without hints' '
853 cat >expect <<-\EOF &&
854 error: the following file has local modifications:
855 bar.txt
856 EOF
857 echo content4 >bar.txt &&
858 test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual &&
859 test_cmp expect actual
860 '
861
862 test_expect_success 'rm file with changes in the index' '
863 cat >expect <<-\EOF &&
864 error: the following file has changes staged in the index:
865 foo.txt
866 (use --cached to keep the file, or -f to force removal)
867 EOF
868 git reset --hard &&
869 echo content5 >foo.txt &&
870 git add foo.txt &&
871 test_must_fail git rm foo.txt 2>actual &&
872 test_cmp expect actual
873 '
874
875 test_expect_success 'rm file with changes in the index without hints' '
876 cat >expect <<-\EOF &&
877 error: the following file has changes staged in the index:
878 foo.txt
879 EOF
880 test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual &&
881 test_cmp expect actual
882 '
883
884 test_expect_success 'rm files with two different errors' '
885 cat >expect <<-\EOF &&
886 error: the following file has staged content different from both the
887 file and the HEAD:
888 foo1.txt
889 (use -f to force removal)
890 error: the following file has changes staged in the index:
891 bar1.txt
892 (use --cached to keep the file, or -f to force removal)
893 EOF
894 echo content >foo1.txt &&
895 git add foo1.txt &&
896 echo content6 >foo1.txt &&
897 echo content6 >bar1.txt &&
898 git add bar1.txt &&
899 test_must_fail git rm bar1.txt foo1.txt 2>actual &&
900 test_cmp expect actual
901 '
902
903 test_expect_success 'rm empty string should fail' '
904 test_must_fail git rm -rf ""
905 '
906
907 test_done