Merge branch 'ab/detox-gettext-tests'
[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 test_expect_success 'choking "git rm" should not let it die with cruft' '
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         # git command is intentionally placed upstream of pipe to induce SIGPIPE
257         git rm -n "some-file-*" | : &&
258         test_path_is_missing .git/index.lock
259 '
260
261 test_expect_success 'Resolving by removal is not a warning-worthy event' '
262         git reset -q --hard &&
263         test_when_finished "rm -f .git/index.lock msg && git reset -q --hard" &&
264         blob=$(echo blob | git hash-object -w --stdin) &&
265         for stage in 1 2 3
266         do
267                 echo "100644 $blob $stage       blob"
268         done | git update-index --index-info &&
269         git rm blob >msg 2>&1 &&
270         test_i18ngrep ! "needs merge" msg &&
271         test_must_fail git ls-files -s --error-unmatch blob
272 '
273
274 test_expect_success 'rm removes subdirectories recursively' '
275         mkdir -p dir/subdir/subsubdir &&
276         echo content >dir/subdir/subsubdir/file &&
277         git add dir/subdir/subsubdir/file &&
278         git rm -f dir/subdir/subsubdir/file &&
279         test_path_is_missing dir
280 '
281
282 cat >expect <<EOF
283 M  .gitmodules
284 D  submod
285 EOF
286
287 cat >expect.modified <<EOF
288  M submod
289 EOF
290
291 cat >expect.modified_inside <<EOF
292  m submod
293 EOF
294
295 cat >expect.modified_untracked <<EOF
296  ? submod
297 EOF
298
299 cat >expect.cached <<EOF
300 D  submod
301 EOF
302
303 cat >expect.both_deleted<<EOF
304 D  .gitmodules
305 D  submod
306 EOF
307
308 test_expect_success 'rm removes empty submodules from work tree' '
309         mkdir submod &&
310         hash=$(git rev-parse HEAD) &&
311         git update-index --add --cacheinfo 160000 "$hash" submod &&
312         git config -f .gitmodules submodule.sub.url ./. &&
313         git config -f .gitmodules submodule.sub.path submod &&
314         git submodule init &&
315         git add .gitmodules &&
316         git commit -m "add submodule" &&
317         git rm submod &&
318         test_path_is_missing submod &&
319         git status -s -uno --ignore-submodules=none >actual &&
320         test_cmp expect actual &&
321         test_must_fail git config -f .gitmodules submodule.sub.url &&
322         test_must_fail git config -f .gitmodules submodule.sub.path
323 '
324
325 test_expect_success 'rm removes removed submodule from index and .gitmodules' '
326         git reset --hard &&
327         git submodule update &&
328         rm -rf submod &&
329         git rm submod &&
330         git status -s -uno --ignore-submodules=none >actual &&
331         test_cmp expect actual &&
332         test_must_fail git config -f .gitmodules submodule.sub.url &&
333         test_must_fail git config -f .gitmodules submodule.sub.path
334 '
335
336 test_expect_success 'rm removes work tree of unmodified submodules' '
337         git reset --hard &&
338         git submodule update &&
339         git rm submod &&
340         test_path_is_missing submod &&
341         git status -s -uno --ignore-submodules=none >actual &&
342         test_cmp expect actual &&
343         test_must_fail git config -f .gitmodules submodule.sub.url &&
344         test_must_fail git config -f .gitmodules submodule.sub.path
345 '
346
347 test_expect_success 'rm removes a submodule with a trailing /' '
348         git reset --hard &&
349         git submodule update &&
350         git rm submod/ &&
351         test_path_is_missing submod &&
352         git status -s -uno --ignore-submodules=none >actual &&
353         test_cmp expect actual
354 '
355
356 test_expect_success 'rm fails when given a file with a trailing /' '
357         test_must_fail git rm empty/
358 '
359
360 test_expect_success 'rm succeeds when given a directory with a trailing /' '
361         git rm -r frotz/
362 '
363
364 test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '
365         git reset --hard &&
366         git submodule update &&
367         git -C submod checkout HEAD^ &&
368         test_must_fail git rm submod &&
369         test_path_is_dir submod &&
370         test_path_is_file submod/.git &&
371         git status -s -uno --ignore-submodules=none >actual &&
372         test_cmp expect.modified actual &&
373         git rm -f submod &&
374         test_path_is_missing submod &&
375         git status -s -uno --ignore-submodules=none >actual &&
376         test_cmp expect actual &&
377         test_must_fail git config -f .gitmodules submodule.sub.url &&
378         test_must_fail git config -f .gitmodules submodule.sub.path
379 '
380
381 test_expect_success 'rm --cached leaves work tree of populated submodules and .gitmodules alone' '
382         git reset --hard &&
383         git submodule update &&
384         git rm --cached submod &&
385         test_path_is_dir submod &&
386         test_path_is_file submod/.git &&
387         git status -s -uno >actual &&
388         test_cmp expect.cached actual &&
389         git config -f .gitmodules submodule.sub.url &&
390         git config -f .gitmodules submodule.sub.path
391 '
392
393 test_expect_success 'rm --dry-run does not touch the submodule or .gitmodules' '
394         git reset --hard &&
395         git submodule update &&
396         git rm -n submod &&
397         test_path_is_file submod/.git &&
398         git diff-index --exit-code HEAD
399 '
400
401 test_expect_success 'rm does not complain when no .gitmodules file is found' '
402         git reset --hard &&
403         git submodule update &&
404         git rm .gitmodules &&
405         git rm submod >actual 2>actual.err &&
406         test_must_be_empty actual.err &&
407         test_path_is_missing submod &&
408         test_path_is_missing submod/.git &&
409         git status -s -uno >actual &&
410         test_cmp expect.both_deleted actual
411 '
412
413 test_expect_success 'rm will error out on a modified .gitmodules file unless staged' '
414         git reset --hard &&
415         git submodule update &&
416         git config -f .gitmodules foo.bar true &&
417         test_must_fail git rm submod >actual 2>actual.err &&
418         test_file_not_empty actual.err &&
419         test_path_is_dir submod &&
420         test_path_is_file submod/.git &&
421         git diff-files --quiet -- submod &&
422         git add .gitmodules &&
423         git rm submod >actual 2>actual.err &&
424         test_must_be_empty actual.err &&
425         test_path_is_missing submod &&
426         test_path_is_missing submod/.git &&
427         git status -s -uno >actual &&
428         test_cmp expect actual
429 '
430 test_expect_success 'rm will not error out on .gitmodules file with zero stat data' '
431         git reset --hard &&
432         git submodule update &&
433         git read-tree HEAD &&
434         git rm submod &&
435         test_path_is_missing submod
436 '
437
438 test_expect_success 'rm issues a warning when section is not found in .gitmodules' '
439         git reset --hard &&
440         git submodule update &&
441         git config -f .gitmodules --remove-section submodule.sub &&
442         git add .gitmodules &&
443         echo "warning: Could not find section in .gitmodules where path=submod" >expect.err &&
444         git rm submod >actual 2>actual.err &&
445         test_i18ncmp expect.err actual.err &&
446         test_path_is_missing submod &&
447         test_path_is_missing submod/.git &&
448         git status -s -uno >actual &&
449         test_cmp expect actual
450 '
451
452 test_expect_success 'rm of a populated submodule with modifications fails unless forced' '
453         git reset --hard &&
454         git submodule update &&
455         echo X >submod/empty &&
456         test_must_fail git rm submod &&
457         test_path_is_dir submod &&
458         test_path_is_file submod/.git &&
459         git status -s -uno --ignore-submodules=none >actual &&
460         test_cmp expect.modified_inside actual &&
461         git rm -f submod &&
462         test_path_is_missing submod &&
463         git status -s -uno --ignore-submodules=none >actual &&
464         test_cmp expect actual
465 '
466
467 test_expect_success 'rm of a populated submodule with untracked files fails unless forced' '
468         git reset --hard &&
469         git submodule update &&
470         echo X >submod/untracked &&
471         test_must_fail git rm submod &&
472         test_path_is_dir submod &&
473         test_path_is_file submod/.git &&
474         git status -s -uno --ignore-submodules=none >actual &&
475         test_cmp expect.modified_untracked actual &&
476         git rm -f submod &&
477         test_path_is_missing submod &&
478         git status -s -uno --ignore-submodules=none >actual &&
479         test_cmp expect actual
480 '
481
482 test_expect_success 'setup submodule conflict' '
483         git reset --hard &&
484         git submodule update &&
485         git checkout -b branch1 &&
486         echo 1 >nitfol &&
487         git add nitfol &&
488         git commit -m "added nitfol 1" &&
489         git checkout -b branch2 main &&
490         echo 2 >nitfol &&
491         git add nitfol &&
492         git commit -m "added nitfol 2" &&
493         git checkout -b conflict1 main &&
494         git -C submod fetch &&
495         git -C submod checkout branch1 &&
496         git add submod &&
497         git commit -m "submod 1" &&
498         git checkout -b conflict2 main &&
499         git -C submod checkout branch2 &&
500         git add submod &&
501         git commit -m "submod 2"
502 '
503
504 cat >expect.conflict <<EOF
505 UU submod
506 EOF
507
508 test_expect_success 'rm removes work tree of unmodified conflicted submodule' '
509         git checkout conflict1 &&
510         git reset --hard &&
511         git submodule update &&
512         test_must_fail git merge conflict2 &&
513         git rm submod &&
514         test_path_is_missing submod &&
515         git status -s -uno --ignore-submodules=none >actual &&
516         test_cmp expect actual
517 '
518
519 test_expect_success 'rm of a conflicted populated submodule with different HEAD fails unless forced' '
520         git checkout conflict1 &&
521         git reset --hard &&
522         git submodule update &&
523         git -C submod checkout HEAD^ &&
524         test_must_fail git merge conflict2 &&
525         test_must_fail git rm submod &&
526         test_path_is_dir submod &&
527         test_path_is_file submod/.git &&
528         git status -s -uno --ignore-submodules=none >actual &&
529         test_cmp expect.conflict actual &&
530         git rm -f submod &&
531         test_path_is_missing submod &&
532         git status -s -uno --ignore-submodules=none >actual &&
533         test_cmp expect actual &&
534         test_must_fail git config -f .gitmodules submodule.sub.url &&
535         test_must_fail git config -f .gitmodules submodule.sub.path
536 '
537
538 test_expect_success 'rm of a conflicted populated submodule with modifications fails unless forced' '
539         git checkout conflict1 &&
540         git reset --hard &&
541         git submodule update &&
542         echo X >submod/empty &&
543         test_must_fail git merge conflict2 &&
544         test_must_fail git rm submod &&
545         test_path_is_dir submod &&
546         test_path_is_file submod/.git &&
547         git status -s -uno --ignore-submodules=none >actual &&
548         test_cmp expect.conflict actual &&
549         git rm -f submod &&
550         test_path_is_missing submod &&
551         git status -s -uno --ignore-submodules=none >actual &&
552         test_cmp expect actual &&
553         test_must_fail git config -f .gitmodules submodule.sub.url &&
554         test_must_fail git config -f .gitmodules submodule.sub.path
555 '
556
557 test_expect_success 'rm of a conflicted populated submodule with untracked files fails unless forced' '
558         git checkout conflict1 &&
559         git reset --hard &&
560         git submodule update &&
561         echo X >submod/untracked &&
562         test_must_fail git merge conflict2 &&
563         test_must_fail git rm submod &&
564         test_path_is_dir submod &&
565         test_path_is_file submod/.git &&
566         git status -s -uno --ignore-submodules=none >actual &&
567         test_cmp expect.conflict actual &&
568         git rm -f submod &&
569         test_path_is_missing submod &&
570         git status -s -uno --ignore-submodules=none >actual &&
571         test_cmp expect actual
572 '
573
574 test_expect_success 'rm of a conflicted populated submodule with a .git directory fails even when forced' '
575         git checkout conflict1 &&
576         git reset --hard &&
577         git submodule update &&
578         (
579                 cd submod &&
580                 rm .git &&
581                 cp -R ../.git/modules/sub .git &&
582                 GIT_WORK_TREE=. git config --unset core.worktree
583         ) &&
584         test_must_fail git merge conflict2 &&
585         test_must_fail git rm submod &&
586         test_path_is_dir submod &&
587         test_path_is_dir submod/.git &&
588         git status -s -uno --ignore-submodules=none >actual &&
589         test_cmp expect.conflict actual &&
590         test_must_fail git rm -f submod &&
591         test_path_is_dir submod &&
592         test_path_is_dir submod/.git &&
593         git status -s -uno --ignore-submodules=none >actual &&
594         test_cmp expect.conflict actual &&
595         git merge --abort &&
596         rm -rf submod
597 '
598
599 test_expect_success 'rm of a conflicted unpopulated submodule succeeds' '
600         git checkout conflict1 &&
601         git reset --hard &&
602         test_must_fail git merge conflict2 &&
603         git rm submod &&
604         test_path_is_missing submod &&
605         git status -s -uno --ignore-submodules=none >actual &&
606         test_cmp expect actual
607 '
608
609 test_expect_success 'rm of a populated submodule with a .git directory migrates git dir' '
610         git checkout -f main &&
611         git reset --hard &&
612         git submodule update &&
613         (
614                 cd submod &&
615                 rm .git &&
616                 cp -R ../.git/modules/sub .git &&
617                 GIT_WORK_TREE=. git config --unset core.worktree &&
618                 rm -r ../.git/modules/sub
619         ) &&
620         git rm submod 2>output.err &&
621         test_path_is_missing submod &&
622         test_path_is_missing submod/.git &&
623         git status -s -uno --ignore-submodules=none >actual &&
624         test_file_not_empty actual &&
625         test_i18ngrep Migrating output.err
626 '
627
628 cat >expect.deepmodified <<EOF
629  M submod/subsubmod
630 EOF
631
632 test_expect_success 'setup subsubmodule' '
633         git reset --hard &&
634         git submodule update &&
635         (
636                 cd submod &&
637                 hash=$(git rev-parse HEAD) &&
638                 git update-index --add --cacheinfo 160000 "$hash" subsubmod &&
639                 git config -f .gitmodules submodule.sub.url ../. &&
640                 git config -f .gitmodules submodule.sub.path subsubmod &&
641                 git submodule init &&
642                 git add .gitmodules &&
643                 git commit -m "add subsubmodule" &&
644                 git submodule update subsubmod
645         ) &&
646         git commit -a -m "added deep submodule"
647 '
648
649 test_expect_success 'rm recursively removes work tree of unmodified submodules' '
650         git rm submod &&
651         test_path_is_missing submod &&
652         git status -s -uno --ignore-submodules=none >actual &&
653         test_cmp expect actual
654 '
655
656 test_expect_success 'rm of a populated nested submodule with different nested HEAD fails unless forced' '
657         git reset --hard &&
658         git submodule update --recursive &&
659         git -C submod/subsubmod checkout HEAD^ &&
660         test_must_fail git rm submod &&
661         test_path_is_dir submod &&
662         test_path_is_file submod/.git &&
663         git status -s -uno --ignore-submodules=none >actual &&
664         test_cmp expect.modified_inside actual &&
665         git rm -f submod &&
666         test_path_is_missing submod &&
667         git status -s -uno --ignore-submodules=none >actual &&
668         test_cmp expect actual
669 '
670
671 test_expect_success 'rm of a populated nested submodule with nested modifications fails unless forced' '
672         git reset --hard &&
673         git submodule update --recursive &&
674         echo X >submod/subsubmod/empty &&
675         test_must_fail git rm submod &&
676         test_path_is_dir submod &&
677         test_path_is_file submod/.git &&
678         git status -s -uno --ignore-submodules=none >actual &&
679         test_cmp expect.modified_inside actual &&
680         git rm -f submod &&
681         test_path_is_missing submod &&
682         git status -s -uno --ignore-submodules=none >actual &&
683         test_cmp expect actual
684 '
685
686 test_expect_success 'rm of a populated nested submodule with nested untracked files fails unless forced' '
687         git reset --hard &&
688         git submodule update --recursive &&
689         echo X >submod/subsubmod/untracked &&
690         test_must_fail git rm submod &&
691         test_path_is_dir submod &&
692         test_path_is_file submod/.git &&
693         git status -s -uno --ignore-submodules=none >actual &&
694         test_cmp expect.modified_untracked actual &&
695         git rm -f submod &&
696         test_path_is_missing submod &&
697         git status -s -uno --ignore-submodules=none >actual &&
698         test_cmp expect actual
699 '
700
701 test_expect_success "rm absorbs submodule's nested .git directory" '
702         git reset --hard &&
703         git submodule update --recursive &&
704         (
705                 cd submod/subsubmod &&
706                 rm .git &&
707                 mv ../../.git/modules/sub/modules/sub .git &&
708                 GIT_WORK_TREE=. git config --unset core.worktree
709         ) &&
710         git rm submod 2>output.err &&
711         test_path_is_missing submod &&
712         test_path_is_missing submod/subsubmod/.git &&
713         git status -s -uno --ignore-submodules=none >actual &&
714         test_file_not_empty actual &&
715         test_i18ngrep Migrating output.err
716 '
717
718 test_expect_success 'checking out a commit after submodule removal needs manual updates' '
719         git commit -m "submodule removal" submod .gitmodules &&
720         git checkout HEAD^ &&
721         git submodule update &&
722         git checkout -q HEAD^ &&
723         git checkout -q main 2>actual &&
724         test_i18ngrep "^warning: unable to rmdir '\''submod'\'':" actual &&
725         git status -s submod >actual &&
726         echo "?? submod/" >expected &&
727         test_cmp expected actual &&
728         rm -rf submod &&
729         git status -s -uno --ignore-submodules=none >actual &&
730         test_must_be_empty actual
731 '
732
733 test_expect_success 'rm of d/f when d has become a non-directory' '
734         rm -rf d &&
735         mkdir d &&
736         >d/f &&
737         git add d &&
738         rm -rf d &&
739         >d &&
740         git rm d/f &&
741         test_must_fail git rev-parse --verify :d/f &&
742         test_path_is_file d
743 '
744
745 test_expect_success SYMLINKS 'rm of d/f when d has become a dangling symlink' '
746         rm -rf d &&
747         mkdir d &&
748         >d/f &&
749         git add d &&
750         rm -rf d &&
751         ln -s nonexistent d &&
752         git rm d/f &&
753         test_must_fail git rev-parse --verify :d/f &&
754         test -h d &&
755         test_path_is_missing d
756 '
757
758 test_expect_success 'rm of file when it has become a directory' '
759         rm -rf d &&
760         >d &&
761         git add d &&
762         rm -f d &&
763         mkdir d &&
764         >d/f &&
765         test_must_fail git rm d &&
766         git rev-parse --verify :d &&
767         test_path_is_file d/f
768 '
769
770 test_expect_success SYMLINKS 'rm across a symlinked leading path (no index)' '
771         rm -rf d e &&
772         mkdir e &&
773         echo content >e/f &&
774         ln -s e d &&
775         git add -A e d &&
776         git commit -m "symlink d to e, e/f exists" &&
777         test_must_fail git rm d/f &&
778         git rev-parse --verify :d &&
779         git rev-parse --verify :e/f &&
780         test -h d &&
781         test_path_is_file e/f
782 '
783
784 test_expect_failure SYMLINKS 'rm across a symlinked leading path (w/ index)' '
785         rm -rf d e &&
786         mkdir d &&
787         echo content >d/f &&
788         git add -A e d &&
789         git commit -m "d/f exists" &&
790         mv d e &&
791         ln -s e d &&
792         test_must_fail git rm d/f &&
793         git rev-parse --verify :d/f &&
794         test -h d &&
795         test_path_is_file e/f
796 '
797
798 test_expect_success 'setup for testing rm messages' '
799         >bar.txt &&
800         >foo.txt &&
801         git add bar.txt foo.txt
802 '
803
804 test_expect_success 'rm files with different staged content' '
805         cat >expect <<-\EOF &&
806         error: the following files have staged content different from both the
807         file and the HEAD:
808             bar.txt
809             foo.txt
810         (use -f to force removal)
811         EOF
812         echo content1 >foo.txt &&
813         echo content1 >bar.txt &&
814         test_must_fail git rm foo.txt bar.txt 2>actual &&
815         test_i18ncmp expect actual
816 '
817
818 test_expect_success 'rm files with different staged content without hints' '
819         cat >expect <<-\EOF &&
820         error: the following files have staged content different from both the
821         file and the HEAD:
822             bar.txt
823             foo.txt
824         EOF
825         echo content2 >foo.txt &&
826         echo content2 >bar.txt &&
827         test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual &&
828         test_i18ncmp expect actual
829 '
830
831 test_expect_success 'rm file with local modification' '
832         cat >expect <<-\EOF &&
833         error: the following file has local modifications:
834             foo.txt
835         (use --cached to keep the file, or -f to force removal)
836         EOF
837         git commit -m "testing rm 3" &&
838         echo content3 >foo.txt &&
839         test_must_fail git rm foo.txt 2>actual &&
840         test_i18ncmp expect actual
841 '
842
843 test_expect_success 'rm file with local modification without hints' '
844         cat >expect <<-\EOF &&
845         error: the following file has local modifications:
846             bar.txt
847         EOF
848         echo content4 >bar.txt &&
849         test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual &&
850         test_i18ncmp expect actual
851 '
852
853 test_expect_success 'rm file with changes in the index' '
854         cat >expect <<-\EOF &&
855         error: the following file has changes staged in the index:
856             foo.txt
857         (use --cached to keep the file, or -f to force removal)
858         EOF
859         git reset --hard &&
860         echo content5 >foo.txt &&
861         git add foo.txt &&
862         test_must_fail git rm foo.txt 2>actual &&
863         test_i18ncmp expect actual
864 '
865
866 test_expect_success 'rm file with changes in the index without hints' '
867         cat >expect <<-\EOF &&
868         error: the following file has changes staged in the index:
869             foo.txt
870         EOF
871         test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual &&
872         test_i18ncmp expect actual
873 '
874
875 test_expect_success 'rm files with two different errors' '
876         cat >expect <<-\EOF &&
877         error: the following file has staged content different from both the
878         file and the HEAD:
879             foo1.txt
880         (use -f to force removal)
881         error: the following file has changes staged in the index:
882             bar1.txt
883         (use --cached to keep the file, or -f to force removal)
884         EOF
885         echo content >foo1.txt &&
886         git add foo1.txt &&
887         echo content6 >foo1.txt &&
888         echo content6 >bar1.txt &&
889         git add bar1.txt &&
890         test_must_fail git rm bar1.txt foo1.txt 2>actual &&
891         test_i18ncmp expect actual
892 '
893
894 test_expect_success 'rm empty string should fail' '
895         test_must_fail git rm -rf ""
896 '
897
898 test_done