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' '
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' \
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 &&
244 while test $i -lt 12000
246 echo "100644 $_z40 0 some-file-$i"
248 done | git update-index --index-info &&
249 git rm -n "some-file-*" | :;
250 test -f .git/index.lock
252 rm -f .git/index.lock
257 test_expect_success
'rm removes subdirectories recursively' '
258 mkdir -p dir/subdir/subsubdir &&
259 echo content >dir/subdir/subsubdir/file &&
260 git add dir/subdir/subsubdir/file &&
261 git rm -f dir/subdir/subsubdir/file &&
270 cat >expect.modified
<<EOF
274 cat >expect.cached
<<EOF
278 cat >expect.both_deleted
<<EOF
283 test_expect_success
'rm removes empty submodules from work tree' '
285 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) submod &&
286 git config -f .gitmodules submodule.sub.url ./. &&
287 git config -f .gitmodules submodule.sub.path submod &&
288 git submodule init &&
289 git add .gitmodules &&
290 git commit -m "add submodule" &&
293 git status -s -uno --ignore-submodules=none > actual &&
294 test_cmp expect actual &&
295 test_must_fail git config -f .gitmodules submodule.sub.url &&
296 test_must_fail git config -f .gitmodules submodule.sub.path
299 test_expect_success
'rm removes removed submodule from index and .gitmodules' '
301 git submodule update &&
304 git status -s -uno --ignore-submodules=none > actual &&
305 test_cmp expect actual &&
306 test_must_fail git config -f .gitmodules submodule.sub.url &&
307 test_must_fail git config -f .gitmodules submodule.sub.path
310 test_expect_success
'rm removes work tree of unmodified submodules' '
312 git submodule update &&
315 git status -s -uno --ignore-submodules=none > actual &&
316 test_cmp expect actual &&
317 test_must_fail git config -f .gitmodules submodule.sub.url &&
318 test_must_fail git config -f .gitmodules submodule.sub.path
321 test_expect_success
'rm removes a submodule with a trailing /' '
323 git submodule update &&
326 git status -s -uno --ignore-submodules=none > actual &&
327 test_cmp expect actual
330 test_expect_success
'rm fails when given a file with a trailing /' '
331 test_must_fail git rm empty/
334 test_expect_success
'rm succeeds when given a directory with a trailing /' '
338 test_expect_success
'rm of a populated submodule with different HEAD fails unless forced' '
340 git submodule update &&
344 test_must_fail git rm submod &&
346 test -f submod/.git &&
347 git status -s -uno --ignore-submodules=none > actual &&
348 test_cmp expect.modified actual &&
351 git status -s -uno --ignore-submodules=none > actual &&
352 test_cmp expect actual &&
353 test_must_fail git config -f .gitmodules submodule.sub.url &&
354 test_must_fail git config -f .gitmodules submodule.sub.path
357 test_expect_success
'rm --cached leaves work tree of populated submodules and .gitmodules alone' '
359 git submodule update &&
360 git rm --cached submod &&
362 test -f submod/.git &&
363 git status -s -uno >actual &&
364 test_cmp expect.cached actual &&
365 git config -f .gitmodules submodule.sub.url &&
366 git config -f .gitmodules submodule.sub.path
369 test_expect_success
'rm --dry-run does not touch the submodule or .gitmodules' '
371 git submodule update &&
373 test -f submod/.git &&
374 git diff-index --exit-code HEAD
377 test_expect_success
'rm does not complain when no .gitmodules file is found' '
379 git submodule update &&
380 git rm .gitmodules &&
381 git rm submod >actual 2>actual.err &&
382 ! test -s actual.err &&
384 ! test -f submod/.git &&
385 git status -s -uno >actual &&
386 test_cmp expect.both_deleted actual
389 test_expect_success
'rm will error out on a modified .gitmodules file unless staged' '
391 git submodule update &&
392 git config -f .gitmodules foo.bar true &&
393 test_must_fail git rm submod >actual 2>actual.err &&
394 test -s actual.err &&
396 test -f submod/.git &&
397 git diff-files --quiet -- submod &&
398 git add .gitmodules &&
399 git rm submod >actual 2>actual.err &&
400 ! test -s actual.err &&
402 ! test -f submod/.git &&
403 git status -s -uno >actual &&
404 test_cmp expect actual
407 test_expect_success
'rm issues a warning when section is not found in .gitmodules' '
409 git submodule update &&
410 git config -f .gitmodules --remove-section submodule.sub &&
411 git add .gitmodules &&
412 echo "warning: Could not find section in .gitmodules where path=submod" >expect.err &&
413 git rm submod >actual 2>actual.err &&
414 test_i18ncmp expect.err actual.err &&
416 ! test -f submod/.git &&
417 git status -s -uno >actual &&
418 test_cmp expect actual
421 test_expect_success
'rm of a populated submodule with modifications fails unless forced' '
423 git submodule update &&
427 test_must_fail git rm submod &&
429 test -f submod/.git &&
430 git status -s -uno --ignore-submodules=none > actual &&
431 test_cmp expect.modified actual &&
434 git status -s -uno --ignore-submodules=none > actual &&
435 test_cmp expect actual
438 test_expect_success
'rm of a populated submodule with untracked files fails unless forced' '
440 git submodule update &&
444 test_must_fail git rm submod &&
446 test -f submod/.git &&
447 git status -s -uno --ignore-submodules=none > actual &&
448 test_cmp expect.modified actual &&
451 git status -s -uno --ignore-submodules=none > actual &&
452 test_cmp expect actual
455 test_expect_success
'setup submodule conflict' '
457 git submodule update &&
458 git checkout -b branch1 &&
461 git commit -m "added nitfol 1" &&
462 git checkout -b branch2 master &&
465 git commit -m "added nitfol 2" &&
466 git checkout -b conflict1 master &&
472 git commit -m "submod 1" &&
473 git checkout -b conflict2 master &&
478 git commit -m "submod 2"
481 cat >expect.conflict
<<EOF
485 test_expect_success
'rm removes work tree of unmodified conflicted submodule' '
486 git checkout conflict1 &&
488 git submodule update &&
489 test_must_fail git merge conflict2 &&
492 git status -s -uno --ignore-submodules=none > actual &&
493 test_cmp expect actual
496 test_expect_success
'rm of a conflicted populated submodule with different HEAD fails unless forced' '
497 git checkout conflict1 &&
499 git submodule update &&
503 test_must_fail git merge conflict2 &&
504 test_must_fail git rm submod &&
506 test -f submod/.git &&
507 git status -s -uno --ignore-submodules=none > actual &&
508 test_cmp expect.conflict actual &&
511 git status -s -uno --ignore-submodules=none > actual &&
512 test_cmp expect actual &&
513 test_must_fail git config -f .gitmodules submodule.sub.url &&
514 test_must_fail git config -f .gitmodules submodule.sub.path
517 test_expect_success
'rm of a conflicted populated submodule with modifications fails unless forced' '
518 git checkout conflict1 &&
520 git submodule update &&
524 test_must_fail git merge conflict2 &&
525 test_must_fail git rm submod &&
527 test -f submod/.git &&
528 git status -s -uno --ignore-submodules=none > actual &&
529 test_cmp expect.conflict actual &&
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
538 test_expect_success
'rm of a conflicted populated submodule with untracked files fails unless forced' '
539 git checkout conflict1 &&
541 git submodule update &&
545 test_must_fail git merge conflict2 &&
546 test_must_fail git rm submod &&
548 test -f submod/.git &&
549 git status -s -uno --ignore-submodules=none > actual &&
550 test_cmp expect.conflict actual &&
553 git status -s -uno --ignore-submodules=none > actual &&
554 test_cmp expect actual
557 test_expect_success
'rm of a conflicted populated submodule with a .git directory fails even when forced' '
558 git checkout conflict1 &&
560 git submodule update &&
563 cp -R ../.git/modules/sub .git &&
564 GIT_WORK_TREE=. git config --unset core.worktree
566 test_must_fail git merge conflict2 &&
567 test_must_fail git rm submod &&
569 test -d submod/.git &&
570 git status -s -uno --ignore-submodules=none > actual &&
571 test_cmp expect.conflict actual &&
572 test_must_fail git rm -f submod &&
574 test -d submod/.git &&
575 git status -s -uno --ignore-submodules=none > actual &&
576 test_cmp expect.conflict actual &&
581 test_expect_success
'rm of a conflicted unpopulated submodule succeeds' '
582 git checkout conflict1 &&
584 test_must_fail git merge conflict2 &&
587 git status -s -uno --ignore-submodules=none > actual &&
588 test_cmp expect actual
591 test_expect_success
'rm of a populated submodule with a .git directory fails even when forced' '
592 git checkout -f master &&
594 git submodule update &&
597 cp -R ../.git/modules/sub .git &&
598 GIT_WORK_TREE=. git config --unset core.worktree
600 test_must_fail git rm submod &&
602 test -d submod/.git &&
603 git status -s -uno --ignore-submodules=none > actual &&
605 test_must_fail git rm -f submod &&
607 test -d submod/.git &&
608 git status -s -uno --ignore-submodules=none > actual &&
613 cat >expect.deepmodified
<<EOF
617 test_expect_success
'setup subsubmodule' '
619 git submodule update &&
621 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) subsubmod &&
622 git config -f .gitmodules submodule.sub.url ../. &&
623 git config -f .gitmodules submodule.sub.path subsubmod &&
624 git submodule init &&
625 git add .gitmodules &&
626 git commit -m "add subsubmodule" &&
627 git submodule update subsubmod
629 git commit -a -m "added deep submodule"
632 test_expect_success
'rm recursively removes work tree of unmodified submodules' '
635 git status -s -uno --ignore-submodules=none > actual &&
636 test_cmp expect actual
639 test_expect_success
'rm of a populated nested submodule with different nested HEAD fails unless forced' '
641 git submodule update --recursive &&
642 (cd submod/subsubmod &&
645 test_must_fail git rm submod &&
647 test -f submod/.git &&
648 git status -s -uno --ignore-submodules=none > actual &&
649 test_cmp expect.modified actual &&
652 git status -s -uno --ignore-submodules=none > actual &&
653 test_cmp expect actual
656 test_expect_success
'rm of a populated nested submodule with nested modifications fails unless forced' '
658 git submodule update --recursive &&
659 (cd submod/subsubmod &&
662 test_must_fail git rm submod &&
664 test -f submod/.git &&
665 git status -s -uno --ignore-submodules=none > actual &&
666 test_cmp expect.modified actual &&
669 git status -s -uno --ignore-submodules=none > actual &&
670 test_cmp expect actual
673 test_expect_success
'rm of a populated nested submodule with nested untracked files fails unless forced' '
675 git submodule update --recursive &&
676 (cd submod/subsubmod &&
679 test_must_fail git rm submod &&
681 test -f submod/.git &&
682 git status -s -uno --ignore-submodules=none > actual &&
683 test_cmp expect.modified actual &&
686 git status -s -uno --ignore-submodules=none > actual &&
687 test_cmp expect actual
690 test_expect_success
'rm of a populated nested submodule with a nested .git directory fails even when forced' '
692 git submodule update --recursive &&
693 (cd submod/subsubmod &&
695 cp -R ../../.git/modules/sub/modules/sub .git &&
696 GIT_WORK_TREE=. git config --unset core.worktree
698 test_must_fail git rm submod &&
700 test -d submod/subsubmod/.git &&
701 git status -s -uno --ignore-submodules=none > actual &&
703 test_must_fail git rm -f submod &&
705 test -d submod/subsubmod/.git &&
706 git status -s -uno --ignore-submodules=none > actual &&
711 test_expect_success
'rm of d/f when d has become a non-directory' '
719 test_must_fail git rev-parse --verify :d/f &&
723 test_expect_success SYMLINKS
'rm of d/f when d has become a dangling symlink' '
729 ln -s nonexistent d &&
731 test_must_fail git rev-parse --verify :d/f &&
733 test_path_is_missing d
736 test_expect_success
'rm of file when it has become a directory' '
743 test_must_fail git rm d &&
744 git rev-parse --verify :d &&
745 test_path_is_file d/f
748 test_expect_success SYMLINKS
'rm across a symlinked leading path (no index)' '
754 git commit -m "symlink d to e, e/f exists" &&
755 test_must_fail git rm d/f &&
756 git rev-parse --verify :d &&
757 git rev-parse --verify :e/f &&
759 test_path_is_file e/f
762 test_expect_failure SYMLINKS
'rm across a symlinked leading path (w/ index)' '
767 git commit -m "d/f exists" &&
770 test_must_fail git rm d/f &&
771 git rev-parse --verify :d/f &&
773 test_path_is_file e/f
776 test_expect_success
'setup for testing rm messages' '
779 git add bar.txt foo.txt
782 test_expect_success
'rm files with different staged content' '
783 cat >expect <<-\EOF &&
784 error: the following files have staged content different from both the
788 (use -f to force removal)
790 echo content1 >foo.txt &&
791 echo content1 >bar.txt &&
792 test_must_fail git rm foo.txt bar.txt 2>actual &&
793 test_i18ncmp expect actual
796 test_expect_success
'rm files with different staged content without hints' '
797 cat >expect <<-\EOF &&
798 error: the following files have staged content different from both the
803 echo content2 >foo.txt &&
804 echo content2 >bar.txt &&
805 test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual &&
806 test_i18ncmp expect actual
809 test_expect_success
'rm file with local modification' '
810 cat >expect <<-\EOF &&
811 error: the following file has local modifications:
813 (use --cached to keep the file, or -f to force removal)
815 git commit -m "testing rm 3" &&
816 echo content3 >foo.txt &&
817 test_must_fail git rm foo.txt 2>actual &&
818 test_i18ncmp expect actual
821 test_expect_success
'rm file with local modification without hints' '
822 cat >expect <<-\EOF &&
823 error: the following file has local modifications:
826 echo content4 >bar.txt &&
827 test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual &&
828 test_i18ncmp expect actual
831 test_expect_success
'rm file with changes in the index' '
832 cat >expect <<-\EOF &&
833 error: the following file has changes staged in the index:
835 (use --cached to keep the file, or -f to force removal)
838 echo content5 >foo.txt &&
840 test_must_fail git rm foo.txt 2>actual &&
841 test_i18ncmp expect actual
844 test_expect_success
'rm file with changes in the index without hints' '
845 cat >expect <<-\EOF &&
846 error: the following file has changes staged in the index:
849 test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual &&
850 test_i18ncmp expect actual
853 test_expect_success
'rm files with two different errors' '
854 cat >expect <<-\EOF &&
855 error: the following file has staged content different from both the
858 (use -f to force removal)
859 error: the following file has changes staged in the index:
861 (use --cached to keep the file, or -f to force removal)
863 echo content >foo1.txt &&
865 echo content6 >foo1.txt &&
866 echo content6 >bar1.txt &&
868 test_must_fail git rm bar1.txt foo1.txt 2>actual &&
869 test_i18ncmp expect actual