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 test_have_prereq
!MINGW
&& 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' '
100 test_when_finished "chmod 775 ." &&
102 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_must_be_empty rm-output &&
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.modified_inside
<<EOF
275 cat >expect.modified_untracked
<<EOF
279 cat >expect.cached
<<EOF
283 cat >expect.both_deleted
<<EOF
288 test_expect_success
'rm removes empty submodules from work tree' '
290 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) submod &&
291 git config -f .gitmodules submodule.sub.url ./. &&
292 git config -f .gitmodules submodule.sub.path submod &&
293 git submodule init &&
294 git add .gitmodules &&
295 git commit -m "add submodule" &&
298 git status -s -uno --ignore-submodules=none >actual &&
299 test_cmp expect actual &&
300 test_must_fail git config -f .gitmodules submodule.sub.url &&
301 test_must_fail git config -f .gitmodules submodule.sub.path
304 test_expect_success
'rm removes removed submodule from index and .gitmodules' '
306 git submodule update &&
309 git status -s -uno --ignore-submodules=none >actual &&
310 test_cmp expect actual &&
311 test_must_fail git config -f .gitmodules submodule.sub.url &&
312 test_must_fail git config -f .gitmodules submodule.sub.path
315 test_expect_success
'rm removes work tree of unmodified submodules' '
317 git submodule update &&
320 git status -s -uno --ignore-submodules=none >actual &&
321 test_cmp expect actual &&
322 test_must_fail git config -f .gitmodules submodule.sub.url &&
323 test_must_fail git config -f .gitmodules submodule.sub.path
326 test_expect_success
'rm removes a submodule with a trailing /' '
328 git submodule update &&
331 git status -s -uno --ignore-submodules=none >actual &&
332 test_cmp expect actual
335 test_expect_success
'rm fails when given a file with a trailing /' '
336 test_must_fail git rm empty/
339 test_expect_success
'rm succeeds when given a directory with a trailing /' '
343 test_expect_success
'rm of a populated submodule with different HEAD fails unless forced' '
345 git submodule update &&
346 git -C submod checkout HEAD^ &&
347 test_must_fail git rm submod &&
349 test -f submod/.git &&
350 git status -s -uno --ignore-submodules=none >actual &&
351 test_cmp expect.modified actual &&
354 git status -s -uno --ignore-submodules=none >actual &&
355 test_cmp expect actual &&
356 test_must_fail git config -f .gitmodules submodule.sub.url &&
357 test_must_fail git config -f .gitmodules submodule.sub.path
360 test_expect_success
'rm --cached leaves work tree of populated submodules and .gitmodules alone' '
362 git submodule update &&
363 git rm --cached submod &&
365 test -f submod/.git &&
366 git status -s -uno >actual &&
367 test_cmp expect.cached actual &&
368 git config -f .gitmodules submodule.sub.url &&
369 git config -f .gitmodules submodule.sub.path
372 test_expect_success
'rm --dry-run does not touch the submodule or .gitmodules' '
374 git submodule update &&
376 test -f submod/.git &&
377 git diff-index --exit-code HEAD
380 test_expect_success
'rm does not complain when no .gitmodules file is found' '
382 git submodule update &&
383 git rm .gitmodules &&
384 git rm submod >actual 2>actual.err &&
385 ! test -s actual.err &&
387 ! test -f submod/.git &&
388 git status -s -uno >actual &&
389 test_cmp expect.both_deleted actual
392 test_expect_success
'rm will error out on a modified .gitmodules file unless staged' '
394 git submodule update &&
395 git config -f .gitmodules foo.bar true &&
396 test_must_fail git rm submod >actual 2>actual.err &&
397 test -s actual.err &&
399 test -f submod/.git &&
400 git diff-files --quiet -- submod &&
401 git add .gitmodules &&
402 git rm submod >actual 2>actual.err &&
403 ! test -s actual.err &&
405 ! test -f submod/.git &&
406 git status -s -uno >actual &&
407 test_cmp expect actual
410 test_expect_success
'rm issues a warning when section is not found in .gitmodules' '
412 git submodule update &&
413 git config -f .gitmodules --remove-section submodule.sub &&
414 git add .gitmodules &&
415 echo "warning: Could not find section in .gitmodules where path=submod" >expect.err &&
416 git rm submod >actual 2>actual.err &&
417 test_i18ncmp expect.err actual.err &&
419 ! test -f submod/.git &&
420 git status -s -uno >actual &&
421 test_cmp expect actual
424 test_expect_success
'rm of a populated submodule with modifications fails unless forced' '
426 git submodule update &&
427 echo X >submod/empty &&
428 test_must_fail git rm submod &&
430 test -f submod/.git &&
431 git status -s -uno --ignore-submodules=none >actual &&
432 test_cmp expect.modified_inside actual &&
435 git status -s -uno --ignore-submodules=none >actual &&
436 test_cmp expect actual
439 test_expect_success
'rm of a populated submodule with untracked files fails unless forced' '
441 git submodule update &&
442 echo X >submod/untracked &&
443 test_must_fail git rm submod &&
445 test -f submod/.git &&
446 git status -s -uno --ignore-submodules=none >actual &&
447 test_cmp expect.modified_untracked actual &&
450 git status -s -uno --ignore-submodules=none >actual &&
451 test_cmp expect actual
454 test_expect_success
'setup submodule conflict' '
456 git submodule update &&
457 git checkout -b branch1 &&
460 git commit -m "added nitfol 1" &&
461 git checkout -b branch2 master &&
464 git commit -m "added nitfol 2" &&
465 git checkout -b conflict1 master &&
466 git -C submod fetch &&
467 git -C submod checkout branch1 &&
469 git commit -m "submod 1" &&
470 git checkout -b conflict2 master &&
471 git -C submod checkout branch2 &&
473 git commit -m "submod 2"
476 cat >expect.conflict
<<EOF
480 test_expect_success
'rm removes work tree of unmodified conflicted submodule' '
481 git checkout conflict1 &&
483 git submodule update &&
484 test_must_fail git merge conflict2 &&
487 git status -s -uno --ignore-submodules=none >actual &&
488 test_cmp expect actual
491 test_expect_success
'rm of a conflicted populated submodule with different HEAD fails unless forced' '
492 git checkout conflict1 &&
494 git submodule update &&
495 git -C submod checkout HEAD^ &&
496 test_must_fail git merge conflict2 &&
497 test_must_fail git rm submod &&
499 test -f submod/.git &&
500 git status -s -uno --ignore-submodules=none >actual &&
501 test_cmp expect.conflict actual &&
504 git status -s -uno --ignore-submodules=none >actual &&
505 test_cmp expect actual &&
506 test_must_fail git config -f .gitmodules submodule.sub.url &&
507 test_must_fail git config -f .gitmodules submodule.sub.path
510 test_expect_success
'rm of a conflicted populated submodule with modifications fails unless forced' '
511 git checkout conflict1 &&
513 git submodule update &&
514 echo X >submod/empty &&
515 test_must_fail git merge conflict2 &&
516 test_must_fail git rm submod &&
518 test -f submod/.git &&
519 git status -s -uno --ignore-submodules=none >actual &&
520 test_cmp expect.conflict actual &&
523 git status -s -uno --ignore-submodules=none >actual &&
524 test_cmp expect actual &&
525 test_must_fail git config -f .gitmodules submodule.sub.url &&
526 test_must_fail git config -f .gitmodules submodule.sub.path
529 test_expect_success
'rm of a conflicted populated submodule with untracked files fails unless forced' '
530 git checkout conflict1 &&
532 git submodule update &&
533 echo X >submod/untracked &&
534 test_must_fail git merge conflict2 &&
535 test_must_fail git rm submod &&
537 test -f submod/.git &&
538 git status -s -uno --ignore-submodules=none >actual &&
539 test_cmp expect.conflict actual &&
542 git status -s -uno --ignore-submodules=none >actual &&
543 test_cmp expect actual
546 test_expect_success
'rm of a conflicted populated submodule with a .git directory fails even when forced' '
547 git checkout conflict1 &&
549 git submodule update &&
552 cp -R ../.git/modules/sub .git &&
553 GIT_WORK_TREE=. git config --unset core.worktree
555 test_must_fail git merge conflict2 &&
556 test_must_fail git rm submod &&
558 test -d submod/.git &&
559 git status -s -uno --ignore-submodules=none >actual &&
560 test_cmp expect.conflict actual &&
561 test_must_fail git rm -f submod &&
563 test -d submod/.git &&
564 git status -s -uno --ignore-submodules=none >actual &&
565 test_cmp expect.conflict actual &&
570 test_expect_success
'rm of a conflicted unpopulated submodule succeeds' '
571 git checkout conflict1 &&
573 test_must_fail git merge conflict2 &&
576 git status -s -uno --ignore-submodules=none >actual &&
577 test_cmp expect actual
580 test_expect_success
'rm of a populated submodule with a .git directory migrates git dir' '
581 git checkout -f master &&
583 git submodule update &&
586 cp -R ../.git/modules/sub .git &&
587 GIT_WORK_TREE=. git config --unset core.worktree &&
588 rm -r ../.git/modules/sub
590 git rm submod 2>output.err &&
592 ! test -d submod/.git &&
593 git status -s -uno --ignore-submodules=none >actual &&
595 test_i18ngrep Migrating output.err
598 cat >expect.deepmodified
<<EOF
602 test_expect_success
'setup subsubmodule' '
604 git submodule update &&
606 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) subsubmod &&
607 git config -f .gitmodules submodule.sub.url ../. &&
608 git config -f .gitmodules submodule.sub.path subsubmod &&
609 git submodule init &&
610 git add .gitmodules &&
611 git commit -m "add subsubmodule" &&
612 git submodule update subsubmod
614 git commit -a -m "added deep submodule"
617 test_expect_success
'rm recursively removes work tree of unmodified submodules' '
620 git status -s -uno --ignore-submodules=none >actual &&
621 test_cmp expect actual
624 test_expect_success
'rm of a populated nested submodule with different nested HEAD fails unless forced' '
626 git submodule update --recursive &&
627 git -C submod/subsubmod checkout HEAD^ &&
628 test_must_fail git rm submod &&
630 test -f submod/.git &&
631 git status -s -uno --ignore-submodules=none >actual &&
632 test_cmp expect.modified_inside actual &&
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 nested modifications fails unless forced' '
641 git submodule update --recursive &&
642 echo X >submod/subsubmod/empty &&
643 test_must_fail git rm submod &&
645 test -f submod/.git &&
646 git status -s -uno --ignore-submodules=none >actual &&
647 test_cmp expect.modified_inside actual &&
650 git status -s -uno --ignore-submodules=none >actual &&
651 test_cmp expect actual
654 test_expect_success
'rm of a populated nested submodule with nested untracked files fails unless forced' '
656 git submodule update --recursive &&
657 echo X >submod/subsubmod/untracked &&
658 test_must_fail git rm submod &&
660 test -f submod/.git &&
661 git status -s -uno --ignore-submodules=none >actual &&
662 test_cmp expect.modified_untracked actual &&
665 git status -s -uno --ignore-submodules=none >actual &&
666 test_cmp expect actual
669 test_expect_success
"rm absorbs submodule's nested .git directory" '
671 git submodule update --recursive &&
672 (cd submod/subsubmod &&
674 mv ../../.git/modules/sub/modules/sub .git &&
675 GIT_WORK_TREE=. git config --unset core.worktree
677 git rm submod 2>output.err &&
679 ! test -d submod/subsubmod/.git &&
680 git status -s -uno --ignore-submodules=none >actual &&
682 test_i18ngrep Migrating output.err
685 test_expect_success
'checking out a commit after submodule removal needs manual updates' '
686 git commit -m "submodule removal" submod .gitmodules &&
687 git checkout HEAD^ &&
688 git submodule update &&
689 git checkout -q HEAD^ &&
690 git checkout -q master 2>actual &&
691 test_i18ngrep "^warning: unable to rmdir '\''submod'\'':" actual &&
692 git status -s submod >actual &&
693 echo "?? submod/" >expected &&
694 test_cmp expected actual &&
696 git status -s -uno --ignore-submodules=none >actual &&
700 test_expect_success
'rm of d/f when d has become a non-directory' '
708 test_must_fail git rev-parse --verify :d/f &&
712 test_expect_success SYMLINKS
'rm of d/f when d has become a dangling symlink' '
718 ln -s nonexistent d &&
720 test_must_fail git rev-parse --verify :d/f &&
722 test_path_is_missing d
725 test_expect_success
'rm of file when it has become a directory' '
732 test_must_fail git rm d &&
733 git rev-parse --verify :d &&
734 test_path_is_file d/f
737 test_expect_success SYMLINKS
'rm across a symlinked leading path (no index)' '
743 git commit -m "symlink d to e, e/f exists" &&
744 test_must_fail git rm d/f &&
745 git rev-parse --verify :d &&
746 git rev-parse --verify :e/f &&
748 test_path_is_file e/f
751 test_expect_failure SYMLINKS
'rm across a symlinked leading path (w/ index)' '
756 git commit -m "d/f exists" &&
759 test_must_fail git rm d/f &&
760 git rev-parse --verify :d/f &&
762 test_path_is_file e/f
765 test_expect_success
'setup for testing rm messages' '
768 git add bar.txt foo.txt
771 test_expect_success
'rm files with different staged content' '
772 cat >expect <<-\EOF &&
773 error: the following files have staged content different from both the
777 (use -f to force removal)
779 echo content1 >foo.txt &&
780 echo content1 >bar.txt &&
781 test_must_fail git rm foo.txt bar.txt 2>actual &&
782 test_i18ncmp expect actual
785 test_expect_success
'rm files with different staged content without hints' '
786 cat >expect <<-\EOF &&
787 error: the following files have staged content different from both the
792 echo content2 >foo.txt &&
793 echo content2 >bar.txt &&
794 test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual &&
795 test_i18ncmp expect actual
798 test_expect_success
'rm file with local modification' '
799 cat >expect <<-\EOF &&
800 error: the following file has local modifications:
802 (use --cached to keep the file, or -f to force removal)
804 git commit -m "testing rm 3" &&
805 echo content3 >foo.txt &&
806 test_must_fail git rm foo.txt 2>actual &&
807 test_i18ncmp expect actual
810 test_expect_success
'rm file with local modification without hints' '
811 cat >expect <<-\EOF &&
812 error: the following file has local modifications:
815 echo content4 >bar.txt &&
816 test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual &&
817 test_i18ncmp expect actual
820 test_expect_success
'rm file with changes in the index' '
821 cat >expect <<-\EOF &&
822 error: the following file has changes staged in the index:
824 (use --cached to keep the file, or -f to force removal)
827 echo content5 >foo.txt &&
829 test_must_fail git rm foo.txt 2>actual &&
830 test_i18ncmp expect actual
833 test_expect_success
'rm file with changes in the index without hints' '
834 cat >expect <<-\EOF &&
835 error: the following file has changes staged in the index:
838 test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual &&
839 test_i18ncmp expect actual
842 test_expect_success
'rm files with two different errors' '
843 cat >expect <<-\EOF &&
844 error: the following file has staged content different from both the
847 (use -f to force removal)
848 error: the following file has changes staged in the index:
850 (use --cached to keep the file, or -f to force removal)
852 echo content >foo1.txt &&
854 echo content6 >foo1.txt &&
855 echo content6 >bar1.txt &&
857 test_must_fail git rm bar1.txt foo1.txt 2>actual &&
858 test_i18ncmp expect actual
861 test_expect_success
'rm empty string should invoke warning' '
862 git rm -rf "" 2>output &&
863 test_i18ngrep "warning: empty strings" output