3 # Copyright (c) 2006 Carl D. Worth
6 test_description='Test of the various options to git rm.'
10 # Setup some files to be removed, some with funny characters
12 'Initialize test directory' \
13 "touch -- foo bar baz 'space embedded' -q &&
14 git add -- foo bar baz 'space embedded' -q &&
15 git commit -m 'add normal files'"
17 if touch -- 'tab embedded' 'newline
20 test_set_prereq FUNNYNAMES
22 say 'Your filesystem does not allow tabs in filenames.'
25 test_expect_success FUNNYNAMES 'add files with funny names' "
26 git add -- 'tab embedded' 'newline
28 git commit -m 'add files with tabs and newlines'
32 'Pre-check that foo exists and is in index before git rm foo' \
33 '[ -f foo ] && git ls-files --error-unmatch foo'
36 'Test that git rm foo succeeds' \
40 'Test that git rm --cached foo succeeds if the index matches the file' \
46 'Test that git rm --cached foo succeeds if the index matches the file' \
50 echo "other content" >foo &&
54 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' '
57 git commit -m foo --allow-empty &&
58 echo "other content" >foo &&
60 echo "yet another content" >foo &&
61 test_must_fail git rm --cached foo
65 'Test that git rm --cached -f foo works in case where --cached only did not' \
68 git commit -m foo --allow-empty &&
69 echo "other content" >foo &&
71 echo "yet another content" >foo &&
72 git rm --cached -f foo'
75 'Post-check that foo exists but is not in index after git rm foo' \
76 '[ -f foo ] && test_must_fail git ls-files --error-unmatch foo'
79 'Pre-check that bar exists and is in index before "git rm bar"' \
80 '[ -f bar ] && git ls-files --error-unmatch bar'
83 'Test that "git rm bar" succeeds' \
87 'Post-check that bar does not exist and is not in index after "git rm -f bar"' \
88 '! [ -f bar ] && test_must_fail git ls-files --error-unmatch bar'
91 'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' \
94 test_expect_success FUNNYNAMES \
95 "Test that \"git rm -f\" succeeds with embedded space, tab, or newline characters." \
96 "git rm -f 'space embedded' 'tab embedded' 'newline
99 test_expect_success SANITY 'Test that "git rm -f" fails if its rm fails' '
101 test_must_fail git rm -f baz &&
105 test_expect_success \
106 'When the rm in "git rm -f" fails, it should not remove the file from the index' \
107 'git ls-files --error-unmatch baz'
109 test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
110 git rm --ignore-unmatch nonexistent
113 test_expect_success '"rm" command printed' '
114 echo frotz > test-file &&
116 git commit -m "add file for rm test" &&
117 git rm test-file > rm-output &&
118 test $(grep "^rm " rm-output | wc -l) = 1 &&
119 rm -f test-file rm-output &&
120 git commit -m "remove file from rm test"
123 test_expect_success '"rm" command suppressed with --quiet' '
124 echo frotz > test-file &&
126 git commit -m "add file for rm --quiet test" &&
127 git rm --quiet test-file > rm-output &&
128 test $(wc -l < rm-output) = 0 &&
129 rm -f test-file rm-output &&
130 git commit -m "remove file from rm --quiet test"
133 # Now, failure cases.
134 test_expect_success 'Re-add foo and baz' '
136 git ls-files --error-unmatch foo baz
139 test_expect_success 'Modify foo -- rm should refuse' '
141 test_must_fail git rm foo baz &&
144 git ls-files --error-unmatch foo baz
147 test_expect_success 'Modified foo -- rm -f should work' '
151 test_must_fail git ls-files --error-unmatch foo &&
152 test_must_fail git ls-files --error-unmatch bar
155 test_expect_success 'Re-add foo and baz for HEAD tests' '
157 git checkout HEAD -- baz &&
159 git ls-files --error-unmatch foo baz
162 test_expect_success 'foo is different in index from HEAD -- rm should refuse' '
163 test_must_fail git rm foo baz &&
166 git ls-files --error-unmatch foo baz
169 test_expect_success 'but with -f it should work.' '
173 test_must_fail git ls-files --error-unmatch foo &&
174 test_must_fail git ls-files --error-unmatch baz
177 test_expect_success 'refuse to remove cached empty file with modifications' '
180 echo content >empty &&
181 test_must_fail git rm --cached empty
184 test_expect_success 'remove intent-to-add file without --force' '
185 echo content >intent-to-add &&
186 git add -N intent-to-add &&
187 git rm --cached intent-to-add
190 test_expect_success 'Recursive test setup' '
192 echo qfwfq >frotz/nitfol &&
194 git commit -m "subdir test"
197 test_expect_success 'Recursive without -r fails' '
198 test_must_fail git rm frotz &&
203 test_expect_success 'Recursive with -r but dirty' '
204 echo qfwfq >>frotz/nitfol &&
205 test_must_fail git rm -r frotz &&
210 test_expect_success 'Recursive with -r -f' '
211 git rm -f -r frotz &&
212 ! test -f frotz/nitfol &&
216 test_expect_success 'Remove nonexistent file returns nonzero exit status' '
217 test_must_fail git rm nonexistent
220 test_expect_success 'Call "rm" from outside the work tree' '
224 echo something > somefile &&
226 git commit -m "add a file" &&
228 git --git-dir=repo/.git --work-tree=repo rm somefile) &&
229 test_must_fail git ls-files --error-unmatch somefile)
232 test_expect_success 'refresh index before checking if it is up-to-date' '
235 test-chmtime -86400 frotz/nitfol &&
236 git rm frotz/nitfol &&
237 test ! -f frotz/nitfol
241 test_expect_success 'choking "git rm" should not let it die with cruft' '
242 git reset -q --hard &&
243 test_when_finished "rm -f .git/index.lock && git reset -q --hard" &&
245 while test $i -lt 12000
247 echo "100644 1234567890123456789012345678901234567890 0 some-file-$i"
249 done | git update-index --index-info &&
250 git rm -n "some-file-*" | : &&
251 test_path_is_missing .git/index.lock
254 test_expect_success 'rm removes subdirectories recursively' '
255 mkdir -p dir/subdir/subsubdir &&
256 echo content >dir/subdir/subsubdir/file &&
257 git add dir/subdir/subsubdir/file &&
258 git rm -f dir/subdir/subsubdir/file &&
267 cat >expect.modified <<EOF
271 cat >expect.cached <<EOF
275 cat >expect.both_deleted<<EOF
280 test_expect_success 'rm removes empty submodules from work tree' '
282 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) submod &&
283 git config -f .gitmodules submodule.sub.url ./. &&
284 git config -f .gitmodules submodule.sub.path submod &&
285 git submodule init &&
286 git add .gitmodules &&
287 git commit -m "add submodule" &&
290 git status -s -uno --ignore-submodules=none > actual &&
291 test_cmp expect actual &&
292 test_must_fail git config -f .gitmodules submodule.sub.url &&
293 test_must_fail git config -f .gitmodules submodule.sub.path
296 test_expect_success 'rm removes removed submodule from index and .gitmodules' '
298 git submodule update &&
301 git status -s -uno --ignore-submodules=none > actual &&
302 test_cmp expect actual &&
303 test_must_fail git config -f .gitmodules submodule.sub.url &&
304 test_must_fail git config -f .gitmodules submodule.sub.path
307 test_expect_success 'rm removes work tree of unmodified submodules' '
309 git submodule update &&
312 git status -s -uno --ignore-submodules=none > actual &&
313 test_cmp expect actual &&
314 test_must_fail git config -f .gitmodules submodule.sub.url &&
315 test_must_fail git config -f .gitmodules submodule.sub.path
318 test_expect_success 'rm removes a submodule with a trailing /' '
320 git submodule update &&
323 git status -s -uno --ignore-submodules=none > actual &&
324 test_cmp expect actual
327 test_expect_success 'rm fails when given a file with a trailing /' '
328 test_must_fail git rm empty/
331 test_expect_success 'rm succeeds when given a directory with a trailing /' '
335 test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '
337 git submodule update &&
341 test_must_fail git rm submod &&
343 test -f submod/.git &&
344 git status -s -uno --ignore-submodules=none > actual &&
345 test_cmp expect.modified actual &&
348 git status -s -uno --ignore-submodules=none > actual &&
349 test_cmp expect actual &&
350 test_must_fail git config -f .gitmodules submodule.sub.url &&
351 test_must_fail git config -f .gitmodules submodule.sub.path
354 test_expect_success 'rm --cached leaves work tree of populated submodules and .gitmodules alone' '
356 git submodule update &&
357 git rm --cached submod &&
359 test -f submod/.git &&
360 git status -s -uno >actual &&
361 test_cmp expect.cached actual &&
362 git config -f .gitmodules submodule.sub.url &&
363 git config -f .gitmodules submodule.sub.path
366 test_expect_success 'rm --dry-run does not touch the submodule or .gitmodules' '
368 git submodule update &&
370 test -f submod/.git &&
371 git diff-index --exit-code HEAD
374 test_expect_success 'rm does not complain when no .gitmodules file is found' '
376 git submodule update &&
377 git rm .gitmodules &&
378 git rm submod >actual 2>actual.err &&
379 ! test -s actual.err &&
381 ! test -f submod/.git &&
382 git status -s -uno >actual &&
383 test_cmp expect.both_deleted actual
386 test_expect_success 'rm will error out on a modified .gitmodules file unless staged' '
388 git submodule update &&
389 git config -f .gitmodules foo.bar true &&
390 test_must_fail git rm submod >actual 2>actual.err &&
391 test -s actual.err &&
393 test -f submod/.git &&
394 git diff-files --quiet -- submod &&
395 git add .gitmodules &&
396 git rm submod >actual 2>actual.err &&
397 ! test -s actual.err &&
399 ! test -f submod/.git &&
400 git status -s -uno >actual &&
401 test_cmp expect actual
404 test_expect_success 'rm issues a warning when section is not found in .gitmodules' '
406 git submodule update &&
407 git config -f .gitmodules --remove-section submodule.sub &&
408 git add .gitmodules &&
409 echo "warning: Could not find section in .gitmodules where path=submod" >expect.err &&
410 git rm submod >actual 2>actual.err &&
411 test_i18ncmp expect.err actual.err &&
413 ! test -f submod/.git &&
414 git status -s -uno >actual &&
415 test_cmp expect actual
418 test_expect_success 'rm of a populated submodule with modifications fails unless forced' '
420 git submodule update &&
424 test_must_fail git rm submod &&
426 test -f submod/.git &&
427 git status -s -uno --ignore-submodules=none > actual &&
428 test_cmp expect.modified actual &&
431 git status -s -uno --ignore-submodules=none > actual &&
432 test_cmp expect actual
435 test_expect_success 'rm of a populated submodule with untracked files fails unless forced' '
437 git submodule update &&
441 test_must_fail git rm submod &&
443 test -f submod/.git &&
444 git status -s -uno --ignore-submodules=none > actual &&
445 test_cmp expect.modified actual &&
448 git status -s -uno --ignore-submodules=none > actual &&
449 test_cmp expect actual
452 test_expect_success 'setup submodule conflict' '
454 git submodule update &&
455 git checkout -b branch1 &&
458 git commit -m "added nitfol 1" &&
459 git checkout -b branch2 master &&
462 git commit -m "added nitfol 2" &&
463 git checkout -b conflict1 master &&
469 git commit -m "submod 1" &&
470 git checkout -b conflict2 master &&
475 git commit -m "submod 2"
478 cat >expect.conflict <<EOF
482 test_expect_success 'rm removes work tree of unmodified conflicted submodule' '
483 git checkout conflict1 &&
485 git submodule update &&
486 test_must_fail git merge conflict2 &&
489 git status -s -uno --ignore-submodules=none > actual &&
490 test_cmp expect actual
493 test_expect_success 'rm of a conflicted populated submodule with different HEAD fails unless forced' '
494 git checkout conflict1 &&
496 git submodule update &&
500 test_must_fail git merge conflict2 &&
501 test_must_fail git rm submod &&
503 test -f submod/.git &&
504 git status -s -uno --ignore-submodules=none > actual &&
505 test_cmp expect.conflict actual &&
508 git status -s -uno --ignore-submodules=none > actual &&
509 test_cmp expect actual &&
510 test_must_fail git config -f .gitmodules submodule.sub.url &&
511 test_must_fail git config -f .gitmodules submodule.sub.path
514 test_expect_success 'rm of a conflicted populated submodule with modifications fails unless forced' '
515 git checkout conflict1 &&
517 git submodule update &&
521 test_must_fail git merge conflict2 &&
522 test_must_fail git rm submod &&
524 test -f submod/.git &&
525 git status -s -uno --ignore-submodules=none > actual &&
526 test_cmp expect.conflict actual &&
529 git status -s -uno --ignore-submodules=none > actual &&
530 test_cmp expect actual &&
531 test_must_fail git config -f .gitmodules submodule.sub.url &&
532 test_must_fail git config -f .gitmodules submodule.sub.path
535 test_expect_success 'rm of a conflicted populated submodule with untracked files fails unless forced' '
536 git checkout conflict1 &&
538 git submodule update &&
542 test_must_fail git merge conflict2 &&
543 test_must_fail git rm submod &&
545 test -f submod/.git &&
546 git status -s -uno --ignore-submodules=none > actual &&
547 test_cmp expect.conflict actual &&
550 git status -s -uno --ignore-submodules=none > actual &&
551 test_cmp expect actual
554 test_expect_success 'rm of a conflicted populated submodule with a .git directory fails even when forced' '
555 git checkout conflict1 &&
557 git submodule update &&
560 cp -R ../.git/modules/sub .git &&
561 GIT_WORK_TREE=. git config --unset core.worktree
563 test_must_fail git merge conflict2 &&
564 test_must_fail git rm submod &&
566 test -d submod/.git &&
567 git status -s -uno --ignore-submodules=none > actual &&
568 test_cmp expect.conflict actual &&
569 test_must_fail git rm -f submod &&
571 test -d submod/.git &&
572 git status -s -uno --ignore-submodules=none > actual &&
573 test_cmp expect.conflict actual &&
578 test_expect_success 'rm of a conflicted unpopulated submodule succeeds' '
579 git checkout conflict1 &&
581 test_must_fail git merge conflict2 &&
584 git status -s -uno --ignore-submodules=none > actual &&
585 test_cmp expect actual
588 test_expect_success 'rm of a populated submodule with a .git directory fails even when forced' '
589 git checkout -f master &&
591 git submodule update &&
594 cp -R ../.git/modules/sub .git &&
595 GIT_WORK_TREE=. git config --unset core.worktree
597 test_must_fail git rm submod &&
599 test -d submod/.git &&
600 git status -s -uno --ignore-submodules=none > actual &&
602 test_must_fail git rm -f submod &&
604 test -d submod/.git &&
605 git status -s -uno --ignore-submodules=none > actual &&
610 cat >expect.deepmodified <<EOF
614 test_expect_success 'setup subsubmodule' '
616 git submodule update &&
618 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) subsubmod &&
619 git config -f .gitmodules submodule.sub.url ../. &&
620 git config -f .gitmodules submodule.sub.path subsubmod &&
621 git submodule init &&
622 git add .gitmodules &&
623 git commit -m "add subsubmodule" &&
624 git submodule update subsubmod
626 git commit -a -m "added deep submodule"
629 test_expect_success 'rm recursively removes work tree of unmodified submodules' '
632 git status -s -uno --ignore-submodules=none > actual &&
633 test_cmp expect actual
636 test_expect_success 'rm of a populated nested submodule with different nested HEAD fails unless forced' '
638 git submodule update --recursive &&
639 (cd submod/subsubmod &&
642 test_must_fail git rm submod &&
644 test -f submod/.git &&
645 git status -s -uno --ignore-submodules=none > actual &&
646 test_cmp expect.modified actual &&
649 git status -s -uno --ignore-submodules=none > actual &&
650 test_cmp expect actual
653 test_expect_success 'rm of a populated nested submodule with nested modifications fails unless forced' '
655 git submodule update --recursive &&
656 (cd submod/subsubmod &&
659 test_must_fail git rm submod &&
661 test -f submod/.git &&
662 git status -s -uno --ignore-submodules=none > actual &&
663 test_cmp expect.modified actual &&
666 git status -s -uno --ignore-submodules=none > actual &&
667 test_cmp expect actual
670 test_expect_success 'rm of a populated nested submodule with nested untracked files fails unless forced' '
672 git submodule update --recursive &&
673 (cd submod/subsubmod &&
676 test_must_fail git rm submod &&
678 test -f submod/.git &&
679 git status -s -uno --ignore-submodules=none > actual &&
680 test_cmp expect.modified actual &&
683 git status -s -uno --ignore-submodules=none > actual &&
684 test_cmp expect actual
687 test_expect_success 'rm of a populated nested submodule with a nested .git directory fails even when forced' '
689 git submodule update --recursive &&
690 (cd submod/subsubmod &&
692 cp -R ../../.git/modules/sub/modules/sub .git &&
693 GIT_WORK_TREE=. git config --unset core.worktree
695 test_must_fail git rm submod &&
697 test -d submod/subsubmod/.git &&
698 git status -s -uno --ignore-submodules=none > actual &&
700 test_must_fail git rm -f submod &&
702 test -d submod/subsubmod/.git &&
703 git status -s -uno --ignore-submodules=none > actual &&
708 test_expect_success 'checking out a commit after submodule removal needs manual updates' '
709 git commit -m "submodule removal" submod &&
710 git checkout HEAD^ &&
711 git submodule update &&
712 git checkout -q HEAD^ 2>actual &&
713 git checkout -q master 2>actual &&
714 test_i18ngrep "^warning: unable to rmdir submod:" actual &&
715 git status -s submod >actual &&
716 echo "?? submod/" >expected &&
717 test_cmp expected actual &&
719 git status -s -uno --ignore-submodules=none > actual &&
723 test_expect_success 'rm of d/f when d has become a non-directory' '
731 test_must_fail git rev-parse --verify :d/f &&
735 test_expect_success SYMLINKS 'rm of d/f when d has become a dangling symlink' '
741 ln -s nonexistent d &&
743 test_must_fail git rev-parse --verify :d/f &&
745 test_path_is_missing d
748 test_expect_success 'rm of file when it has become a directory' '
755 test_must_fail git rm d &&
756 git rev-parse --verify :d &&
757 test_path_is_file d/f
760 test_expect_success SYMLINKS 'rm across a symlinked leading path (no index)' '
766 git commit -m "symlink d to e, e/f exists" &&
767 test_must_fail git rm d/f &&
768 git rev-parse --verify :d &&
769 git rev-parse --verify :e/f &&
771 test_path_is_file e/f
774 test_expect_failure SYMLINKS 'rm across a symlinked leading path (w/ index)' '
779 git commit -m "d/f exists" &&
782 test_must_fail git rm d/f &&
783 git rev-parse --verify :d/f &&
785 test_path_is_file e/f
788 test_expect_success 'setup for testing rm messages' '
791 git add bar.txt foo.txt
794 test_expect_success 'rm files with different staged content' '
795 cat >expect <<-\EOF &&
796 error: the following files have staged content different from both the
800 (use -f to force removal)
802 echo content1 >foo.txt &&
803 echo content1 >bar.txt &&
804 test_must_fail git rm foo.txt bar.txt 2>actual &&
805 test_i18ncmp expect actual
808 test_expect_success 'rm files with different staged content without hints' '
809 cat >expect <<-\EOF &&
810 error: the following files have staged content different from both the
815 echo content2 >foo.txt &&
816 echo content2 >bar.txt &&
817 test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual &&
818 test_i18ncmp expect actual
821 test_expect_success 'rm file with local modification' '
822 cat >expect <<-\EOF &&
823 error: the following file has local modifications:
825 (use --cached to keep the file, or -f to force removal)
827 git commit -m "testing rm 3" &&
828 echo content3 >foo.txt &&
829 test_must_fail git rm foo.txt 2>actual &&
830 test_i18ncmp expect actual
833 test_expect_success 'rm file with local modification without hints' '
834 cat >expect <<-\EOF &&
835 error: the following file has local modifications:
838 echo content4 >bar.txt &&
839 test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual &&
840 test_i18ncmp expect actual
843 test_expect_success 'rm file with changes in the index' '
844 cat >expect <<-\EOF &&
845 error: the following file has changes staged in the index:
847 (use --cached to keep the file, or -f to force removal)
850 echo content5 >foo.txt &&
852 test_must_fail git rm foo.txt 2>actual &&
853 test_i18ncmp expect actual
856 test_expect_success 'rm file with changes in the index without hints' '
857 cat >expect <<-\EOF &&
858 error: the following file has changes staged in the index:
861 test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual &&
862 test_i18ncmp expect actual
865 test_expect_success 'rm files with two different errors' '
866 cat >expect <<-\EOF &&
867 error: the following file has staged content different from both the
870 (use -f to force removal)
871 error: the following file has changes staged in the index:
873 (use --cached to keep the file, or -f to force removal)
875 echo content >foo1.txt &&
877 echo content6 >foo1.txt &&
878 echo content6 >bar1.txt &&
880 test_must_fail git rm bar1.txt foo1.txt 2>actual &&
881 test_i18ncmp expect actual