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' '
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_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.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 &&
338 git -C submod checkout HEAD^ &&
339 test_must_fail git rm submod &&
341 test -f submod/.git &&
342 git status -s -uno --ignore-submodules=none >actual &&
343 test_cmp expect.modified actual &&
346 git status -s -uno --ignore-submodules=none >actual &&
347 test_cmp expect actual &&
348 test_must_fail git config -f .gitmodules submodule.sub.url &&
349 test_must_fail git config -f .gitmodules submodule.sub.path
352 test_expect_success 'rm --cached leaves work tree of populated submodules and .gitmodules alone' '
354 git submodule update &&
355 git rm --cached submod &&
357 test -f submod/.git &&
358 git status -s -uno >actual &&
359 test_cmp expect.cached actual &&
360 git config -f .gitmodules submodule.sub.url &&
361 git config -f .gitmodules submodule.sub.path
364 test_expect_success 'rm --dry-run does not touch the submodule or .gitmodules' '
366 git submodule update &&
368 test -f submod/.git &&
369 git diff-index --exit-code HEAD
372 test_expect_success 'rm does not complain when no .gitmodules file is found' '
374 git submodule update &&
375 git rm .gitmodules &&
376 git rm submod >actual 2>actual.err &&
377 ! test -s actual.err &&
379 ! test -f submod/.git &&
380 git status -s -uno >actual &&
381 test_cmp expect.both_deleted actual
384 test_expect_success 'rm will error out on a modified .gitmodules file unless staged' '
386 git submodule update &&
387 git config -f .gitmodules foo.bar true &&
388 test_must_fail git rm submod >actual 2>actual.err &&
389 test -s actual.err &&
391 test -f submod/.git &&
392 git diff-files --quiet -- submod &&
393 git add .gitmodules &&
394 git rm submod >actual 2>actual.err &&
395 ! test -s actual.err &&
397 ! test -f submod/.git &&
398 git status -s -uno >actual &&
399 test_cmp expect actual
402 test_expect_success 'rm issues a warning when section is not found in .gitmodules' '
404 git submodule update &&
405 git config -f .gitmodules --remove-section submodule.sub &&
406 git add .gitmodules &&
407 echo "warning: Could not find section in .gitmodules where path=submod" >expect.err &&
408 git rm submod >actual 2>actual.err &&
409 test_i18ncmp expect.err actual.err &&
411 ! test -f submod/.git &&
412 git status -s -uno >actual &&
413 test_cmp expect actual
416 test_expect_success 'rm of a populated submodule with modifications fails unless forced' '
418 git submodule update &&
419 echo X >submod/empty &&
420 test_must_fail git rm submod &&
422 test -f submod/.git &&
423 git status -s -uno --ignore-submodules=none >actual &&
424 test_cmp expect.modified actual &&
427 git status -s -uno --ignore-submodules=none >actual &&
428 test_cmp expect actual
431 test_expect_success 'rm of a populated submodule with untracked files fails unless forced' '
433 git submodule update &&
434 echo X >submod/untracked &&
435 test_must_fail git rm submod &&
437 test -f submod/.git &&
438 git status -s -uno --ignore-submodules=none >actual &&
439 test_cmp expect.modified actual &&
442 git status -s -uno --ignore-submodules=none >actual &&
443 test_cmp expect actual
446 test_expect_success 'setup submodule conflict' '
448 git submodule update &&
449 git checkout -b branch1 &&
452 git commit -m "added nitfol 1" &&
453 git checkout -b branch2 master &&
456 git commit -m "added nitfol 2" &&
457 git checkout -b conflict1 master &&
458 git -C submod fetch &&
459 git -C submod checkout branch1 &&
461 git commit -m "submod 1" &&
462 git checkout -b conflict2 master &&
463 git -C submod checkout branch2 &&
465 git commit -m "submod 2"
468 cat >expect.conflict <<EOF
472 test_expect_success 'rm removes work tree of unmodified conflicted submodule' '
473 git checkout conflict1 &&
475 git submodule update &&
476 test_must_fail git merge conflict2 &&
479 git status -s -uno --ignore-submodules=none >actual &&
480 test_cmp expect actual
483 test_expect_success 'rm of a conflicted populated submodule with different HEAD fails unless forced' '
484 git checkout conflict1 &&
486 git submodule update &&
487 git -C submod checkout HEAD^ &&
488 test_must_fail git merge conflict2 &&
489 test_must_fail git rm submod &&
491 test -f submod/.git &&
492 git status -s -uno --ignore-submodules=none >actual &&
493 test_cmp expect.conflict actual &&
496 git status -s -uno --ignore-submodules=none >actual &&
497 test_cmp expect actual &&
498 test_must_fail git config -f .gitmodules submodule.sub.url &&
499 test_must_fail git config -f .gitmodules submodule.sub.path
502 test_expect_success 'rm of a conflicted populated submodule with modifications fails unless forced' '
503 git checkout conflict1 &&
505 git submodule update &&
506 echo X >submod/empty &&
507 test_must_fail git merge conflict2 &&
508 test_must_fail git rm submod &&
510 test -f submod/.git &&
511 git status -s -uno --ignore-submodules=none >actual &&
512 test_cmp expect.conflict actual &&
515 git status -s -uno --ignore-submodules=none >actual &&
516 test_cmp expect actual &&
517 test_must_fail git config -f .gitmodules submodule.sub.url &&
518 test_must_fail git config -f .gitmodules submodule.sub.path
521 test_expect_success 'rm of a conflicted populated submodule with untracked files fails unless forced' '
522 git checkout conflict1 &&
524 git submodule update &&
525 echo X >submod/untracked &&
526 test_must_fail git merge conflict2 &&
527 test_must_fail git rm submod &&
529 test -f submod/.git &&
530 git status -s -uno --ignore-submodules=none >actual &&
531 test_cmp expect.conflict actual &&
534 git status -s -uno --ignore-submodules=none >actual &&
535 test_cmp expect actual
538 test_expect_success 'rm of a conflicted populated submodule with a .git directory fails even when forced' '
539 git checkout conflict1 &&
541 git submodule update &&
544 cp -R ../.git/modules/sub .git &&
545 GIT_WORK_TREE=. git config --unset core.worktree
547 test_must_fail git merge conflict2 &&
548 test_must_fail git rm submod &&
550 test -d submod/.git &&
551 git status -s -uno --ignore-submodules=none >actual &&
552 test_cmp expect.conflict actual &&
553 test_must_fail git rm -f submod &&
555 test -d submod/.git &&
556 git status -s -uno --ignore-submodules=none >actual &&
557 test_cmp expect.conflict actual &&
562 test_expect_success 'rm of a conflicted unpopulated submodule succeeds' '
563 git checkout conflict1 &&
565 test_must_fail git merge conflict2 &&
568 git status -s -uno --ignore-submodules=none >actual &&
569 test_cmp expect actual
572 test_expect_success 'rm of a populated submodule with a .git directory migrates git dir' '
573 git checkout -f master &&
575 git submodule update &&
578 cp -R ../.git/modules/sub .git &&
579 GIT_WORK_TREE=. git config --unset core.worktree &&
580 rm -r ../.git/modules/sub
582 git rm submod 2>output.err &&
584 ! test -d submod/.git &&
585 git status -s -uno --ignore-submodules=none >actual &&
587 test_i18ngrep Migrating output.err
590 cat >expect.deepmodified <<EOF
594 test_expect_success 'setup subsubmodule' '
596 git submodule update &&
598 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) subsubmod &&
599 git config -f .gitmodules submodule.sub.url ../. &&
600 git config -f .gitmodules submodule.sub.path subsubmod &&
601 git submodule init &&
602 git add .gitmodules &&
603 git commit -m "add subsubmodule" &&
604 git submodule update subsubmod
606 git commit -a -m "added deep submodule"
609 test_expect_success 'rm recursively removes work tree of unmodified submodules' '
612 git status -s -uno --ignore-submodules=none >actual &&
613 test_cmp expect actual
616 test_expect_success 'rm of a populated nested submodule with different nested HEAD fails unless forced' '
618 git submodule update --recursive &&
619 git -C submod/subsubmod checkout HEAD^ &&
620 test_must_fail git rm submod &&
622 test -f submod/.git &&
623 git status -s -uno --ignore-submodules=none >actual &&
624 test_cmp expect.modified actual &&
627 git status -s -uno --ignore-submodules=none >actual &&
628 test_cmp expect actual
631 test_expect_success 'rm of a populated nested submodule with nested modifications fails unless forced' '
633 git submodule update --recursive &&
634 echo X >submod/subsubmod/empty &&
635 test_must_fail git rm submod &&
637 test -f submod/.git &&
638 git status -s -uno --ignore-submodules=none >actual &&
639 test_cmp expect.modified actual &&
642 git status -s -uno --ignore-submodules=none >actual &&
643 test_cmp expect actual
646 test_expect_success 'rm of a populated nested submodule with nested untracked files fails unless forced' '
648 git submodule update --recursive &&
649 echo X >submod/subsubmod/untracked &&
650 test_must_fail git rm submod &&
652 test -f submod/.git &&
653 git status -s -uno --ignore-submodules=none >actual &&
654 test_cmp expect.modified actual &&
657 git status -s -uno --ignore-submodules=none >actual &&
658 test_cmp expect actual
661 test_expect_success 'rm of a populated nested submodule with a nested .git directory fails even when forced' '
663 git submodule update --recursive &&
664 (cd submod/subsubmod &&
666 mv ../../.git/modules/sub/modules/sub .git &&
667 GIT_WORK_TREE=. git config --unset core.worktree
669 git rm submod 2>output.err &&
671 ! test -d submod/subsubmod/.git &&
672 git status -s -uno --ignore-submodules=none >actual &&
674 test_i18ngrep Migrating output.err
677 test_expect_success 'checking out a commit after submodule removal needs manual updates' '
678 git commit -m "submodule removal" submod .gitmodules &&
679 git checkout HEAD^ &&
680 git submodule update &&
681 git checkout -q HEAD^ &&
682 git checkout -q master 2>actual &&
683 test_i18ngrep "^warning: unable to rmdir submod:" actual &&
684 git status -s submod >actual &&
685 echo "?? submod/" >expected &&
686 test_cmp expected actual &&
688 git status -s -uno --ignore-submodules=none >actual &&
692 test_expect_success 'rm of d/f when d has become a non-directory' '
700 test_must_fail git rev-parse --verify :d/f &&
704 test_expect_success SYMLINKS 'rm of d/f when d has become a dangling symlink' '
710 ln -s nonexistent d &&
712 test_must_fail git rev-parse --verify :d/f &&
714 test_path_is_missing d
717 test_expect_success 'rm of file when it has become a directory' '
724 test_must_fail git rm d &&
725 git rev-parse --verify :d &&
726 test_path_is_file d/f
729 test_expect_success SYMLINKS 'rm across a symlinked leading path (no index)' '
735 git commit -m "symlink d to e, e/f exists" &&
736 test_must_fail git rm d/f &&
737 git rev-parse --verify :d &&
738 git rev-parse --verify :e/f &&
740 test_path_is_file e/f
743 test_expect_failure SYMLINKS 'rm across a symlinked leading path (w/ index)' '
748 git commit -m "d/f exists" &&
751 test_must_fail git rm d/f &&
752 git rev-parse --verify :d/f &&
754 test_path_is_file e/f
757 test_expect_success 'setup for testing rm messages' '
760 git add bar.txt foo.txt
763 test_expect_success 'rm files with different staged content' '
764 cat >expect <<-\EOF &&
765 error: the following files have staged content different from both the
769 (use -f to force removal)
771 echo content1 >foo.txt &&
772 echo content1 >bar.txt &&
773 test_must_fail git rm foo.txt bar.txt 2>actual &&
774 test_i18ncmp expect actual
777 test_expect_success 'rm files with different staged content without hints' '
778 cat >expect <<-\EOF &&
779 error: the following files have staged content different from both the
784 echo content2 >foo.txt &&
785 echo content2 >bar.txt &&
786 test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual &&
787 test_i18ncmp expect actual
790 test_expect_success 'rm file with local modification' '
791 cat >expect <<-\EOF &&
792 error: the following file has local modifications:
794 (use --cached to keep the file, or -f to force removal)
796 git commit -m "testing rm 3" &&
797 echo content3 >foo.txt &&
798 test_must_fail git rm foo.txt 2>actual &&
799 test_i18ncmp expect actual
802 test_expect_success 'rm file with local modification without hints' '
803 cat >expect <<-\EOF &&
804 error: the following file has local modifications:
807 echo content4 >bar.txt &&
808 test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual &&
809 test_i18ncmp expect actual
812 test_expect_success 'rm file with changes in the index' '
813 cat >expect <<-\EOF &&
814 error: the following file has changes staged in the index:
816 (use --cached to keep the file, or -f to force removal)
819 echo content5 >foo.txt &&
821 test_must_fail git rm foo.txt 2>actual &&
822 test_i18ncmp expect actual
825 test_expect_success 'rm file with changes in the index without hints' '
826 cat >expect <<-\EOF &&
827 error: the following file has changes staged in the index:
830 test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual &&
831 test_i18ncmp expect actual
834 test_expect_success 'rm files with two different errors' '
835 cat >expect <<-\EOF &&
836 error: the following file has staged content different from both the
839 (use -f to force removal)
840 error: the following file has changes staged in the index:
842 (use --cached to keep the file, or -f to force removal)
844 echo content >foo1.txt &&
846 echo content6 >foo1.txt &&
847 echo content6 >bar1.txt &&
849 test_must_fail git rm bar1.txt foo1.txt 2>actual &&
850 test_i18ncmp expect actual
853 test_expect_success 'rm empty string should invoke warning' '
854 git rm -rf "" 2>output &&
855 test_i18ngrep "warning: empty strings" output