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 !FUNNYNAMES; then
18 say 'Your filesystem does not allow tabs in filenames.'
21 test_expect_success FUNNYNAMES 'add files with funny names' "
22 touch -- 'tab embedded' 'newline
24 git add -- 'tab embedded' 'newline
26 git commit -m 'add files with tabs and newlines'
30 'Pre-check that foo exists and is in index before git rm foo' \
31 '[ -f foo ] && git ls-files --error-unmatch foo'
34 'Test that git rm foo succeeds' \
38 'Test that git rm --cached foo succeeds if the index matches the file' \
44 'Test that git rm --cached foo succeeds if the index matches the file' \
48 echo "other content" >foo &&
52 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' '
55 git commit -m foo --allow-empty &&
56 echo "other content" >foo &&
58 echo "yet another content" >foo &&
59 test_must_fail git rm --cached foo
63 'Test that git rm --cached -f foo works in case where --cached only did not' \
66 git commit -m foo --allow-empty &&
67 echo "other content" >foo &&
69 echo "yet another content" >foo &&
70 git rm --cached -f foo'
73 'Post-check that foo exists but is not in index after git rm foo' \
74 '[ -f foo ] && test_must_fail git ls-files --error-unmatch foo'
77 'Pre-check that bar exists and is in index before "git rm bar"' \
78 '[ -f bar ] && git ls-files --error-unmatch bar'
81 'Test that "git rm bar" succeeds' \
85 'Post-check that bar does not exist and is not in index after "git rm -f bar"' \
86 '! [ -f bar ] && test_must_fail git ls-files --error-unmatch bar'
89 'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' \
92 test_expect_success FUNNYNAMES \
93 "Test that \"git rm -f\" succeeds with embedded space, tab, or newline characters." \
94 "git rm -f 'space embedded' 'tab embedded' 'newline
97 test_expect_success SANITY 'Test that "git rm -f" fails if its rm fails' '
98 test_when_finished "chmod 775 ." &&
100 test_must_fail git rm -f baz
103 test_expect_success \
104 'When the rm in "git rm -f" fails, it should not remove the file from the index' \
105 'git ls-files --error-unmatch baz'
107 test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
108 git rm --ignore-unmatch nonexistent
111 test_expect_success '"rm" command printed' '
112 echo frotz >test-file &&
114 git commit -m "add file for rm test" &&
115 git rm test-file >rm-output &&
116 test $(grep "^rm " rm-output | wc -l) = 1 &&
117 rm -f test-file rm-output &&
118 git commit -m "remove file from rm test"
121 test_expect_success '"rm" command suppressed with --quiet' '
122 echo frotz >test-file &&
124 git commit -m "add file for rm --quiet test" &&
125 git rm --quiet test-file >rm-output &&
126 test_must_be_empty rm-output &&
127 rm -f test-file rm-output &&
128 git commit -m "remove file from rm --quiet test"
131 # Now, failure cases.
132 test_expect_success 'Re-add foo and baz' '
134 git ls-files --error-unmatch foo baz
137 test_expect_success 'Modify foo -- rm should refuse' '
139 test_must_fail git rm foo baz &&
142 git ls-files --error-unmatch foo baz
145 test_expect_success 'Modified foo -- rm -f should work' '
149 test_must_fail git ls-files --error-unmatch foo &&
150 test_must_fail git ls-files --error-unmatch bar
153 test_expect_success 'Re-add foo and baz for HEAD tests' '
155 git checkout HEAD -- baz &&
157 git ls-files --error-unmatch foo baz
160 test_expect_success 'foo is different in index from HEAD -- rm should refuse' '
161 test_must_fail git rm foo baz &&
164 git ls-files --error-unmatch foo baz
167 test_expect_success 'but with -f it should work.' '
171 test_must_fail git ls-files --error-unmatch foo &&
172 test_must_fail git ls-files --error-unmatch baz
175 test_expect_success 'refuse to remove cached empty file with modifications' '
178 echo content >empty &&
179 test_must_fail git rm --cached empty
182 test_expect_success 'remove intent-to-add file without --force' '
183 echo content >intent-to-add &&
184 git add -N intent-to-add &&
185 git rm --cached intent-to-add
188 test_expect_success 'Recursive test setup' '
190 echo qfwfq >frotz/nitfol &&
192 git commit -m "subdir test"
195 test_expect_success 'Recursive without -r fails' '
196 test_must_fail git rm frotz &&
201 test_expect_success 'Recursive with -r but dirty' '
202 echo qfwfq >>frotz/nitfol &&
203 test_must_fail git rm -r frotz &&
208 test_expect_success 'Recursive with -r -f' '
209 git rm -f -r frotz &&
210 ! test -f frotz/nitfol &&
214 test_expect_success 'Remove nonexistent file returns nonzero exit status' '
215 test_must_fail git rm nonexistent
218 test_expect_success 'Call "rm" from outside the work tree' '
222 echo something >somefile &&
224 git commit -m "add a file" &&
226 git --git-dir=repo/.git --work-tree=repo rm somefile) &&
227 test_must_fail git ls-files --error-unmatch somefile)
230 test_expect_success 'refresh index before checking if it is up-to-date' '
233 test-tool chmtime -86400 frotz/nitfol &&
234 git rm frotz/nitfol &&
235 test ! -f frotz/nitfol
239 test_expect_success 'choking "git rm" should not let it die with cruft' '
240 git reset -q --hard &&
241 test_when_finished "rm -f .git/index.lock && git reset -q --hard" &&
243 while test $i -lt 12000
245 echo "100644 1234567890123456789012345678901234567890 0 some-file-$i"
247 done | git update-index --index-info &&
248 git rm -n "some-file-*" | : &&
249 test_path_is_missing .git/index.lock
252 test_expect_success 'rm removes subdirectories recursively' '
253 mkdir -p dir/subdir/subsubdir &&
254 echo content >dir/subdir/subsubdir/file &&
255 git add dir/subdir/subsubdir/file &&
256 git rm -f dir/subdir/subsubdir/file &&
265 cat >expect.modified <<EOF
269 cat >expect.modified_inside <<EOF
273 cat >expect.modified_untracked <<EOF
277 cat >expect.cached <<EOF
281 cat >expect.both_deleted<<EOF
286 test_expect_success 'rm removes empty submodules from work tree' '
288 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) submod &&
289 git config -f .gitmodules submodule.sub.url ./. &&
290 git config -f .gitmodules submodule.sub.path submod &&
291 git submodule init &&
292 git add .gitmodules &&
293 git commit -m "add submodule" &&
296 git status -s -uno --ignore-submodules=none >actual &&
297 test_cmp expect actual &&
298 test_must_fail git config -f .gitmodules submodule.sub.url &&
299 test_must_fail git config -f .gitmodules submodule.sub.path
302 test_expect_success 'rm removes removed submodule from index and .gitmodules' '
304 git submodule update &&
307 git status -s -uno --ignore-submodules=none >actual &&
308 test_cmp expect actual &&
309 test_must_fail git config -f .gitmodules submodule.sub.url &&
310 test_must_fail git config -f .gitmodules submodule.sub.path
313 test_expect_success 'rm removes work tree of unmodified submodules' '
315 git submodule update &&
318 git status -s -uno --ignore-submodules=none >actual &&
319 test_cmp expect actual &&
320 test_must_fail git config -f .gitmodules submodule.sub.url &&
321 test_must_fail git config -f .gitmodules submodule.sub.path
324 test_expect_success 'rm removes a submodule with a trailing /' '
326 git submodule update &&
329 git status -s -uno --ignore-submodules=none >actual &&
330 test_cmp expect actual
333 test_expect_success 'rm fails when given a file with a trailing /' '
334 test_must_fail git rm empty/
337 test_expect_success 'rm succeeds when given a directory with a trailing /' '
341 test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '
343 git submodule update &&
344 git -C submod checkout HEAD^ &&
345 test_must_fail git rm submod &&
347 test -f submod/.git &&
348 git status -s -uno --ignore-submodules=none >actual &&
349 test_cmp expect.modified actual &&
352 git status -s -uno --ignore-submodules=none >actual &&
353 test_cmp expect actual &&
354 test_must_fail git config -f .gitmodules submodule.sub.url &&
355 test_must_fail git config -f .gitmodules submodule.sub.path
358 test_expect_success 'rm --cached leaves work tree of populated submodules and .gitmodules alone' '
360 git submodule update &&
361 git rm --cached submod &&
363 test -f submod/.git &&
364 git status -s -uno >actual &&
365 test_cmp expect.cached actual &&
366 git config -f .gitmodules submodule.sub.url &&
367 git config -f .gitmodules submodule.sub.path
370 test_expect_success 'rm --dry-run does not touch the submodule or .gitmodules' '
372 git submodule update &&
374 test -f submod/.git &&
375 git diff-index --exit-code HEAD
378 test_expect_success 'rm does not complain when no .gitmodules file is found' '
380 git submodule update &&
381 git rm .gitmodules &&
382 git rm submod >actual 2>actual.err &&
383 test_must_be_empty actual.err &&
385 ! test -f submod/.git &&
386 git status -s -uno >actual &&
387 test_cmp expect.both_deleted actual
390 test_expect_success 'rm will error out on a modified .gitmodules file unless staged' '
392 git submodule update &&
393 git config -f .gitmodules foo.bar true &&
394 test_must_fail git rm submod >actual 2>actual.err &&
395 test -s actual.err &&
397 test -f submod/.git &&
398 git diff-files --quiet -- submod &&
399 git add .gitmodules &&
400 git rm submod >actual 2>actual.err &&
401 test_must_be_empty actual.err &&
403 ! test -f submod/.git &&
404 git status -s -uno >actual &&
405 test_cmp expect actual
408 test_expect_success 'rm issues a warning when section is not found in .gitmodules' '
410 git submodule update &&
411 git config -f .gitmodules --remove-section submodule.sub &&
412 git add .gitmodules &&
413 echo "warning: Could not find section in .gitmodules where path=submod" >expect.err &&
414 git rm submod >actual 2>actual.err &&
415 test_i18ncmp expect.err actual.err &&
417 ! test -f submod/.git &&
418 git status -s -uno >actual &&
419 test_cmp expect actual
422 test_expect_success 'rm of a populated submodule with modifications fails unless forced' '
424 git submodule update &&
425 echo X >submod/empty &&
426 test_must_fail git rm submod &&
428 test -f submod/.git &&
429 git status -s -uno --ignore-submodules=none >actual &&
430 test_cmp expect.modified_inside actual &&
433 git status -s -uno --ignore-submodules=none >actual &&
434 test_cmp expect actual
437 test_expect_success 'rm of a populated submodule with untracked files fails unless forced' '
439 git submodule update &&
440 echo X >submod/untracked &&
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_untracked 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 &&
464 git -C submod fetch &&
465 git -C submod checkout branch1 &&
467 git commit -m "submod 1" &&
468 git checkout -b conflict2 master &&
469 git -C submod checkout branch2 &&
471 git commit -m "submod 2"
474 cat >expect.conflict <<EOF
478 test_expect_success 'rm removes work tree of unmodified conflicted submodule' '
479 git checkout conflict1 &&
481 git submodule update &&
482 test_must_fail git merge conflict2 &&
485 git status -s -uno --ignore-submodules=none >actual &&
486 test_cmp expect actual
489 test_expect_success 'rm of a conflicted populated submodule with different HEAD fails unless forced' '
490 git checkout conflict1 &&
492 git submodule update &&
493 git -C submod checkout HEAD^ &&
494 test_must_fail git merge conflict2 &&
495 test_must_fail git rm submod &&
497 test -f submod/.git &&
498 git status -s -uno --ignore-submodules=none >actual &&
499 test_cmp expect.conflict actual &&
502 git status -s -uno --ignore-submodules=none >actual &&
503 test_cmp expect actual &&
504 test_must_fail git config -f .gitmodules submodule.sub.url &&
505 test_must_fail git config -f .gitmodules submodule.sub.path
508 test_expect_success 'rm of a conflicted populated submodule with modifications fails unless forced' '
509 git checkout conflict1 &&
511 git submodule update &&
512 echo X >submod/empty &&
513 test_must_fail git merge conflict2 &&
514 test_must_fail git rm submod &&
516 test -f submod/.git &&
517 git status -s -uno --ignore-submodules=none >actual &&
518 test_cmp expect.conflict actual &&
521 git status -s -uno --ignore-submodules=none >actual &&
522 test_cmp expect actual &&
523 test_must_fail git config -f .gitmodules submodule.sub.url &&
524 test_must_fail git config -f .gitmodules submodule.sub.path
527 test_expect_success 'rm of a conflicted populated submodule with untracked files fails unless forced' '
528 git checkout conflict1 &&
530 git submodule update &&
531 echo X >submod/untracked &&
532 test_must_fail git merge conflict2 &&
533 test_must_fail git rm submod &&
535 test -f submod/.git &&
536 git status -s -uno --ignore-submodules=none >actual &&
537 test_cmp expect.conflict actual &&
540 git status -s -uno --ignore-submodules=none >actual &&
541 test_cmp expect actual
544 test_expect_success 'rm of a conflicted populated submodule with a .git directory fails even when forced' '
545 git checkout conflict1 &&
547 git submodule update &&
550 cp -R ../.git/modules/sub .git &&
551 GIT_WORK_TREE=. git config --unset core.worktree
553 test_must_fail git merge conflict2 &&
554 test_must_fail git rm submod &&
556 test -d submod/.git &&
557 git status -s -uno --ignore-submodules=none >actual &&
558 test_cmp expect.conflict actual &&
559 test_must_fail git rm -f submod &&
561 test -d submod/.git &&
562 git status -s -uno --ignore-submodules=none >actual &&
563 test_cmp expect.conflict actual &&
568 test_expect_success 'rm of a conflicted unpopulated submodule succeeds' '
569 git checkout conflict1 &&
571 test_must_fail git merge conflict2 &&
574 git status -s -uno --ignore-submodules=none >actual &&
575 test_cmp expect actual
578 test_expect_success 'rm of a populated submodule with a .git directory migrates git dir' '
579 git checkout -f master &&
581 git submodule update &&
584 cp -R ../.git/modules/sub .git &&
585 GIT_WORK_TREE=. git config --unset core.worktree &&
586 rm -r ../.git/modules/sub
588 git rm submod 2>output.err &&
590 ! test -d submod/.git &&
591 git status -s -uno --ignore-submodules=none >actual &&
593 test_i18ngrep Migrating output.err
596 cat >expect.deepmodified <<EOF
600 test_expect_success 'setup subsubmodule' '
602 git submodule update &&
604 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) subsubmod &&
605 git config -f .gitmodules submodule.sub.url ../. &&
606 git config -f .gitmodules submodule.sub.path subsubmod &&
607 git submodule init &&
608 git add .gitmodules &&
609 git commit -m "add subsubmodule" &&
610 git submodule update subsubmod
612 git commit -a -m "added deep submodule"
615 test_expect_success 'rm recursively removes work tree of unmodified submodules' '
618 git status -s -uno --ignore-submodules=none >actual &&
619 test_cmp expect actual
622 test_expect_success 'rm of a populated nested submodule with different nested HEAD fails unless forced' '
624 git submodule update --recursive &&
625 git -C submod/subsubmod checkout HEAD^ &&
626 test_must_fail git rm submod &&
628 test -f submod/.git &&
629 git status -s -uno --ignore-submodules=none >actual &&
630 test_cmp expect.modified_inside actual &&
633 git status -s -uno --ignore-submodules=none >actual &&
634 test_cmp expect actual
637 test_expect_success 'rm of a populated nested submodule with nested modifications fails unless forced' '
639 git submodule update --recursive &&
640 echo X >submod/subsubmod/empty &&
641 test_must_fail git rm submod &&
643 test -f submod/.git &&
644 git status -s -uno --ignore-submodules=none >actual &&
645 test_cmp expect.modified_inside actual &&
648 git status -s -uno --ignore-submodules=none >actual &&
649 test_cmp expect actual
652 test_expect_success 'rm of a populated nested submodule with nested untracked files fails unless forced' '
654 git submodule update --recursive &&
655 echo X >submod/subsubmod/untracked &&
656 test_must_fail git rm submod &&
658 test -f submod/.git &&
659 git status -s -uno --ignore-submodules=none >actual &&
660 test_cmp expect.modified_untracked actual &&
663 git status -s -uno --ignore-submodules=none >actual &&
664 test_cmp expect actual
667 test_expect_success "rm absorbs submodule's nested .git directory" '
669 git submodule update --recursive &&
670 (cd submod/subsubmod &&
672 mv ../../.git/modules/sub/modules/sub .git &&
673 GIT_WORK_TREE=. git config --unset core.worktree
675 git rm submod 2>output.err &&
677 ! test -d submod/subsubmod/.git &&
678 git status -s -uno --ignore-submodules=none >actual &&
680 test_i18ngrep Migrating output.err
683 test_expect_success 'checking out a commit after submodule removal needs manual updates' '
684 git commit -m "submodule removal" submod .gitmodules &&
685 git checkout HEAD^ &&
686 git submodule update &&
687 git checkout -q HEAD^ &&
688 git checkout -q master 2>actual &&
689 test_i18ngrep "^warning: unable to rmdir '\''submod'\'':" actual &&
690 git status -s submod >actual &&
691 echo "?? submod/" >expected &&
692 test_cmp expected actual &&
694 git status -s -uno --ignore-submodules=none >actual &&
695 test_must_be_empty actual
698 test_expect_success 'rm of d/f when d has become a non-directory' '
706 test_must_fail git rev-parse --verify :d/f &&
710 test_expect_success SYMLINKS 'rm of d/f when d has become a dangling symlink' '
716 ln -s nonexistent d &&
718 test_must_fail git rev-parse --verify :d/f &&
720 test_path_is_missing d
723 test_expect_success 'rm of file when it has become a directory' '
730 test_must_fail git rm d &&
731 git rev-parse --verify :d &&
732 test_path_is_file d/f
735 test_expect_success SYMLINKS 'rm across a symlinked leading path (no index)' '
741 git commit -m "symlink d to e, e/f exists" &&
742 test_must_fail git rm d/f &&
743 git rev-parse --verify :d &&
744 git rev-parse --verify :e/f &&
746 test_path_is_file e/f
749 test_expect_failure SYMLINKS 'rm across a symlinked leading path (w/ index)' '
754 git commit -m "d/f exists" &&
757 test_must_fail git rm d/f &&
758 git rev-parse --verify :d/f &&
760 test_path_is_file e/f
763 test_expect_success 'setup for testing rm messages' '
766 git add bar.txt foo.txt
769 test_expect_success 'rm files with different staged content' '
770 cat >expect <<-\EOF &&
771 error: the following files have staged content different from both the
775 (use -f to force removal)
777 echo content1 >foo.txt &&
778 echo content1 >bar.txt &&
779 test_must_fail git rm foo.txt bar.txt 2>actual &&
780 test_i18ncmp expect actual
783 test_expect_success 'rm files with different staged content without hints' '
784 cat >expect <<-\EOF &&
785 error: the following files have staged content different from both the
790 echo content2 >foo.txt &&
791 echo content2 >bar.txt &&
792 test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual &&
793 test_i18ncmp expect actual
796 test_expect_success 'rm file with local modification' '
797 cat >expect <<-\EOF &&
798 error: the following file has local modifications:
800 (use --cached to keep the file, or -f to force removal)
802 git commit -m "testing rm 3" &&
803 echo content3 >foo.txt &&
804 test_must_fail git rm foo.txt 2>actual &&
805 test_i18ncmp expect actual
808 test_expect_success 'rm file with local modification without hints' '
809 cat >expect <<-\EOF &&
810 error: the following file has local modifications:
813 echo content4 >bar.txt &&
814 test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual &&
815 test_i18ncmp expect actual
818 test_expect_success 'rm file with changes in the index' '
819 cat >expect <<-\EOF &&
820 error: the following file has changes staged in the index:
822 (use --cached to keep the file, or -f to force removal)
825 echo content5 >foo.txt &&
827 test_must_fail git rm foo.txt 2>actual &&
828 test_i18ncmp expect actual
831 test_expect_success 'rm file with changes in the index without hints' '
832 cat >expect <<-\EOF &&
833 error: the following file has changes staged in the index:
836 test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual &&
837 test_i18ncmp expect actual
840 test_expect_success 'rm files with two different errors' '
841 cat >expect <<-\EOF &&
842 error: the following file has staged content different from both the
845 (use -f to force removal)
846 error: the following file has changes staged in the index:
848 (use --cached to keep the file, or -f to force removal)
850 echo content >foo1.txt &&
852 echo content6 >foo1.txt &&
853 echo content6 >bar1.txt &&
855 test_must_fail git rm bar1.txt foo1.txt 2>actual &&
856 test_i18ncmp expect actual
859 test_expect_success 'rm empty string should fail' '
860 test_must_fail git rm -rf ""