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
11 test_expect_success 'Initialize test directory' '
12 touch -- foo bar baz "space embedded" -q &&
13 git add -- foo bar baz "space embedded" -q &&
14 git commit -m "add normal files"
17 if test_have_prereq !FUNNYNAMES
19 say 'Your filesystem does not allow tabs in filenames.'
22 test_expect_success FUNNYNAMES 'add files with funny names' '
23 touch -- "tab embedded" "newline${LF}embedded" &&
24 git add -- "tab embedded" "newline${LF}embedded" &&
25 git commit -m "add files with tabs and newlines"
28 test_expect_success 'Pre-check that foo exists and is in index before git rm foo' '
29 test_path_is_file foo &&
30 git ls-files --error-unmatch foo
33 test_expect_success 'Test that git rm foo succeeds' '
37 test_expect_success 'Test that git rm --cached foo succeeds if the index matches the file' '
43 test_expect_success 'Test that git rm --cached foo succeeds if the index matches the file' '
47 echo "other content" >foo &&
51 test_expect_success 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' '
54 git commit -m foo --allow-empty &&
55 echo "other content" >foo &&
57 echo "yet another content" >foo &&
58 test_must_fail git rm --cached foo
61 test_expect_success 'Test that git rm --cached -f foo works in case where --cached only did not' '
64 git commit -m foo --allow-empty &&
65 echo "other content" >foo &&
67 echo "yet another content" >foo &&
68 git rm --cached -f foo
71 test_expect_success 'Post-check that foo exists but is not in index after git rm foo' '
72 test_path_is_file foo &&
73 test_must_fail git ls-files --error-unmatch foo
76 test_expect_success 'Pre-check that bar exists and is in index before "git rm bar"' '
77 test_path_is_file bar &&
78 git ls-files --error-unmatch bar
81 test_expect_success 'Test that "git rm bar" succeeds' '
85 test_expect_success 'Post-check that bar does not exist and is not in index after "git rm -f bar"' '
86 test_path_is_missing bar &&
87 test_must_fail git ls-files --error-unmatch bar
90 test_expect_success 'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' '
94 test_expect_success FUNNYNAMES 'Test that "git rm -f" succeeds with embedded space, tab, or newline characters.' '
95 git rm -f "space embedded" "tab embedded" "newline${LF}embedded"
98 test_expect_success SANITY 'Test that "git rm -f" fails if its rm fails' '
99 test_when_finished "chmod 775 ." &&
101 test_must_fail git rm -f baz
104 test_expect_success 'When the rm in "git rm -f" fails, it should not remove the file from the index' '
105 git ls-files --error-unmatch baz
108 test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
109 git rm --ignore-unmatch nonexistent
112 test_expect_success '"rm" command printed' '
113 echo frotz >test-file &&
115 git commit -m "add file for rm test" &&
116 git rm test-file >rm-output &&
117 test $(grep "^rm " rm-output | wc -l) = 1 &&
118 rm -f test-file rm-output &&
119 git commit -m "remove file from rm test"
122 test_expect_success '"rm" command suppressed with --quiet' '
123 echo frotz >test-file &&
125 git commit -m "add file for rm --quiet test" &&
126 git rm --quiet test-file >rm-output &&
127 test_must_be_empty rm-output &&
128 rm -f test-file rm-output &&
129 git commit -m "remove file from rm --quiet test"
132 # Now, failure cases.
133 test_expect_success 'Re-add foo and baz' '
135 git ls-files --error-unmatch foo baz
138 test_expect_success 'Modify foo -- rm should refuse' '
140 test_must_fail git rm foo baz &&
141 test_path_is_file foo &&
142 test_path_is_file baz &&
143 git ls-files --error-unmatch foo baz
146 test_expect_success 'Modified foo -- rm -f should work' '
148 test_path_is_missing foo &&
149 test_path_is_missing baz &&
150 test_must_fail git ls-files --error-unmatch foo &&
151 test_must_fail git ls-files --error-unmatch bar
154 test_expect_success 'Re-add foo and baz for HEAD tests' '
156 git checkout HEAD -- baz &&
158 git ls-files --error-unmatch foo baz
161 test_expect_success 'foo is different in index from HEAD -- rm should refuse' '
162 test_must_fail git rm foo baz &&
163 test_path_is_file foo &&
164 test_path_is_file baz &&
165 git ls-files --error-unmatch foo baz
168 test_expect_success 'but with -f it should work.' '
170 test_path_is_missing foo &&
171 test_path_is_missing baz &&
172 test_must_fail git ls-files --error-unmatch foo &&
173 test_must_fail git ls-files --error-unmatch baz
176 test_expect_success 'refuse to remove cached empty file with modifications' '
179 echo content >empty &&
180 test_must_fail git rm --cached empty
183 test_expect_success 'remove intent-to-add file without --force' '
184 echo content >intent-to-add &&
185 git add -N intent-to-add &&
186 git rm --cached intent-to-add
189 test_expect_success 'Recursive test setup' '
191 echo qfwfq >frotz/nitfol &&
193 git commit -m "subdir test"
196 test_expect_success 'Recursive without -r fails' '
197 test_must_fail git rm frotz &&
198 test_path_is_dir frotz &&
199 test_path_is_file frotz/nitfol
202 test_expect_success 'Recursive with -r but dirty' '
203 echo qfwfq >>frotz/nitfol &&
204 test_must_fail git rm -r frotz &&
205 test_path_is_dir frotz &&
206 test_path_is_file frotz/nitfol
209 test_expect_success 'Recursive with -r -f' '
210 git rm -f -r frotz &&
211 test_path_is_missing frotz/nitfol &&
212 test_path_is_missing frotz
215 test_expect_success 'Remove nonexistent file returns nonzero exit status' '
216 test_must_fail git rm nonexistent
219 test_expect_success 'Call "rm" from outside the work tree' '
224 echo something >somefile &&
226 git commit -m "add a file" &&
229 git --git-dir=repo/.git --work-tree=repo rm somefile
231 test_must_fail git ls-files --error-unmatch somefile
235 test_expect_success 'refresh index before checking if it is up-to-date' '
237 test-tool chmtime -86400 frotz/nitfol &&
238 git rm frotz/nitfol &&
239 test_path_is_missing frotz/nitfol
242 test_expect_success 'choking "git rm" should not let it die with cruft' '
243 git reset -q --hard &&
244 test_when_finished "rm -f .git/index.lock && git reset -q --hard" &&
246 while test $i -lt 12000
248 echo "100644 1234567890123456789012345678901234567890 0 some-file-$i"
250 done | git update-index --index-info &&
251 git rm -n "some-file-*" | : &&
252 test_path_is_missing .git/index.lock
255 test_expect_success 'rm removes subdirectories recursively' '
256 mkdir -p dir/subdir/subsubdir &&
257 echo content >dir/subdir/subsubdir/file &&
258 git add dir/subdir/subsubdir/file &&
259 git rm -f dir/subdir/subsubdir/file &&
260 test_path_is_missing dir
268 cat >expect.modified <<EOF
272 cat >expect.modified_inside <<EOF
276 cat >expect.modified_untracked <<EOF
280 cat >expect.cached <<EOF
284 cat >expect.both_deleted<<EOF
289 test_expect_success 'rm removes empty submodules from work tree' '
291 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) submod &&
292 git config -f .gitmodules submodule.sub.url ./. &&
293 git config -f .gitmodules submodule.sub.path submod &&
294 git submodule init &&
295 git add .gitmodules &&
296 git commit -m "add submodule" &&
298 test_path_is_missing submod &&
299 git status -s -uno --ignore-submodules=none >actual &&
300 test_cmp expect actual &&
301 test_must_fail git config -f .gitmodules submodule.sub.url &&
302 test_must_fail git config -f .gitmodules submodule.sub.path
305 test_expect_success 'rm removes removed submodule from index and .gitmodules' '
307 git submodule update &&
310 git status -s -uno --ignore-submodules=none >actual &&
311 test_cmp expect actual &&
312 test_must_fail git config -f .gitmodules submodule.sub.url &&
313 test_must_fail git config -f .gitmodules submodule.sub.path
316 test_expect_success 'rm removes work tree of unmodified submodules' '
318 git submodule update &&
320 test_path_is_missing submod &&
321 git status -s -uno --ignore-submodules=none >actual &&
322 test_cmp expect actual &&
323 test_must_fail git config -f .gitmodules submodule.sub.url &&
324 test_must_fail git config -f .gitmodules submodule.sub.path
327 test_expect_success 'rm removes a submodule with a trailing /' '
329 git submodule update &&
331 test_path_is_missing submod &&
332 git status -s -uno --ignore-submodules=none >actual &&
333 test_cmp expect actual
336 test_expect_success 'rm fails when given a file with a trailing /' '
337 test_must_fail git rm empty/
340 test_expect_success 'rm succeeds when given a directory with a trailing /' '
344 test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '
346 git submodule update &&
347 git -C submod checkout HEAD^ &&
348 test_must_fail git rm submod &&
349 test_path_is_dir submod &&
350 test_path_is_file submod/.git &&
351 git status -s -uno --ignore-submodules=none >actual &&
352 test_cmp expect.modified actual &&
354 test_path_is_missing submod &&
355 git status -s -uno --ignore-submodules=none >actual &&
356 test_cmp expect actual &&
357 test_must_fail git config -f .gitmodules submodule.sub.url &&
358 test_must_fail git config -f .gitmodules submodule.sub.path
361 test_expect_success 'rm --cached leaves work tree of populated submodules and .gitmodules alone' '
363 git submodule update &&
364 git rm --cached submod &&
365 test_path_is_dir submod &&
366 test_path_is_file submod/.git &&
367 git status -s -uno >actual &&
368 test_cmp expect.cached actual &&
369 git config -f .gitmodules submodule.sub.url &&
370 git config -f .gitmodules submodule.sub.path
373 test_expect_success 'rm --dry-run does not touch the submodule or .gitmodules' '
375 git submodule update &&
377 test_path_is_file submod/.git &&
378 git diff-index --exit-code HEAD
381 test_expect_success 'rm does not complain when no .gitmodules file is found' '
383 git submodule update &&
384 git rm .gitmodules &&
385 git rm submod >actual 2>actual.err &&
386 test_must_be_empty actual.err &&
387 test_path_is_missing submod &&
388 test_path_is_missing submod/.git &&
389 git status -s -uno >actual &&
390 test_cmp expect.both_deleted actual
393 test_expect_success 'rm will error out on a modified .gitmodules file unless staged' '
395 git submodule update &&
396 git config -f .gitmodules foo.bar true &&
397 test_must_fail git rm submod >actual 2>actual.err &&
398 test_file_not_empty actual.err &&
399 test_path_is_dir submod &&
400 test_path_is_file submod/.git &&
401 git diff-files --quiet -- submod &&
402 git add .gitmodules &&
403 git rm submod >actual 2>actual.err &&
404 test_must_be_empty actual.err &&
405 test_path_is_missing submod &&
406 test_path_is_missing submod/.git &&
407 git status -s -uno >actual &&
408 test_cmp expect actual
411 test_expect_success 'rm issues a warning when section is not found in .gitmodules' '
413 git submodule update &&
414 git config -f .gitmodules --remove-section submodule.sub &&
415 git add .gitmodules &&
416 echo "warning: Could not find section in .gitmodules where path=submod" >expect.err &&
417 git rm submod >actual 2>actual.err &&
418 test_i18ncmp expect.err actual.err &&
419 test_path_is_missing submod &&
420 test_path_is_missing submod/.git &&
421 git status -s -uno >actual &&
422 test_cmp expect actual
425 test_expect_success 'rm of a populated submodule with modifications fails unless forced' '
427 git submodule update &&
428 echo X >submod/empty &&
429 test_must_fail git rm submod &&
430 test_path_is_dir submod &&
431 test_path_is_file submod/.git &&
432 git status -s -uno --ignore-submodules=none >actual &&
433 test_cmp expect.modified_inside actual &&
435 test_path_is_missing submod &&
436 git status -s -uno --ignore-submodules=none >actual &&
437 test_cmp expect actual
440 test_expect_success 'rm of a populated submodule with untracked files fails unless forced' '
442 git submodule update &&
443 echo X >submod/untracked &&
444 test_must_fail git rm submod &&
445 test_path_is_dir submod &&
446 test_path_is_file submod/.git &&
447 git status -s -uno --ignore-submodules=none >actual &&
448 test_cmp expect.modified_untracked actual &&
450 test_path_is_missing submod &&
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 &&
467 git -C submod fetch &&
468 git -C submod checkout branch1 &&
470 git commit -m "submod 1" &&
471 git checkout -b conflict2 master &&
472 git -C submod checkout branch2 &&
474 git commit -m "submod 2"
477 cat >expect.conflict <<EOF
481 test_expect_success 'rm removes work tree of unmodified conflicted submodule' '
482 git checkout conflict1 &&
484 git submodule update &&
485 test_must_fail git merge conflict2 &&
487 test_path_is_missing submod &&
488 git status -s -uno --ignore-submodules=none >actual &&
489 test_cmp expect actual
492 test_expect_success 'rm of a conflicted populated submodule with different HEAD fails unless forced' '
493 git checkout conflict1 &&
495 git submodule update &&
496 git -C submod checkout HEAD^ &&
497 test_must_fail git merge conflict2 &&
498 test_must_fail git rm submod &&
499 test_path_is_dir submod &&
500 test_path_is_file submod/.git &&
501 git status -s -uno --ignore-submodules=none >actual &&
502 test_cmp expect.conflict actual &&
504 test_path_is_missing submod &&
505 git status -s -uno --ignore-submodules=none >actual &&
506 test_cmp expect actual &&
507 test_must_fail git config -f .gitmodules submodule.sub.url &&
508 test_must_fail git config -f .gitmodules submodule.sub.path
511 test_expect_success 'rm of a conflicted populated submodule with modifications fails unless forced' '
512 git checkout conflict1 &&
514 git submodule update &&
515 echo X >submod/empty &&
516 test_must_fail git merge conflict2 &&
517 test_must_fail git rm submod &&
518 test_path_is_dir submod &&
519 test_path_is_file submod/.git &&
520 git status -s -uno --ignore-submodules=none >actual &&
521 test_cmp expect.conflict actual &&
523 test_path_is_missing submod &&
524 git status -s -uno --ignore-submodules=none >actual &&
525 test_cmp expect actual &&
526 test_must_fail git config -f .gitmodules submodule.sub.url &&
527 test_must_fail git config -f .gitmodules submodule.sub.path
530 test_expect_success 'rm of a conflicted populated submodule with untracked files fails unless forced' '
531 git checkout conflict1 &&
533 git submodule update &&
534 echo X >submod/untracked &&
535 test_must_fail git merge conflict2 &&
536 test_must_fail git rm submod &&
537 test_path_is_dir submod &&
538 test_path_is_file submod/.git &&
539 git status -s -uno --ignore-submodules=none >actual &&
540 test_cmp expect.conflict actual &&
542 test_path_is_missing submod &&
543 git status -s -uno --ignore-submodules=none >actual &&
544 test_cmp expect actual
547 test_expect_success 'rm of a conflicted populated submodule with a .git directory fails even when forced' '
548 git checkout conflict1 &&
550 git submodule update &&
554 cp -R ../.git/modules/sub .git &&
555 GIT_WORK_TREE=. git config --unset core.worktree
557 test_must_fail git merge conflict2 &&
558 test_must_fail git rm submod &&
559 test_path_is_dir submod &&
560 test_path_is_dir submod/.git &&
561 git status -s -uno --ignore-submodules=none >actual &&
562 test_cmp expect.conflict actual &&
563 test_must_fail git rm -f submod &&
564 test_path_is_dir submod &&
565 test_path_is_dir submod/.git &&
566 git status -s -uno --ignore-submodules=none >actual &&
567 test_cmp expect.conflict actual &&
572 test_expect_success 'rm of a conflicted unpopulated submodule succeeds' '
573 git checkout conflict1 &&
575 test_must_fail git merge conflict2 &&
577 test_path_is_missing submod &&
578 git status -s -uno --ignore-submodules=none >actual &&
579 test_cmp expect actual
582 test_expect_success 'rm of a populated submodule with a .git directory migrates git dir' '
583 git checkout -f master &&
585 git submodule update &&
589 cp -R ../.git/modules/sub .git &&
590 GIT_WORK_TREE=. git config --unset core.worktree &&
591 rm -r ../.git/modules/sub
593 git rm submod 2>output.err &&
594 test_path_is_missing submod &&
595 test_path_is_missing submod/.git &&
596 git status -s -uno --ignore-submodules=none >actual &&
597 test_file_not_empty actual &&
598 test_i18ngrep Migrating output.err
601 cat >expect.deepmodified <<EOF
605 test_expect_success 'setup subsubmodule' '
607 git submodule update &&
610 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) subsubmod &&
611 git config -f .gitmodules submodule.sub.url ../. &&
612 git config -f .gitmodules submodule.sub.path subsubmod &&
613 git submodule init &&
614 git add .gitmodules &&
615 git commit -m "add subsubmodule" &&
616 git submodule update subsubmod
618 git commit -a -m "added deep submodule"
621 test_expect_success 'rm recursively removes work tree of unmodified submodules' '
623 test_path_is_missing submod &&
624 git status -s -uno --ignore-submodules=none >actual &&
625 test_cmp expect actual
628 test_expect_success 'rm of a populated nested submodule with different nested HEAD fails unless forced' '
630 git submodule update --recursive &&
631 git -C submod/subsubmod checkout HEAD^ &&
632 test_must_fail git rm submod &&
633 test_path_is_dir submod &&
634 test_path_is_file submod/.git &&
635 git status -s -uno --ignore-submodules=none >actual &&
636 test_cmp expect.modified_inside actual &&
638 test_path_is_missing submod &&
639 git status -s -uno --ignore-submodules=none >actual &&
640 test_cmp expect actual
643 test_expect_success 'rm of a populated nested submodule with nested modifications fails unless forced' '
645 git submodule update --recursive &&
646 echo X >submod/subsubmod/empty &&
647 test_must_fail git rm submod &&
648 test_path_is_dir submod &&
649 test_path_is_file submod/.git &&
650 git status -s -uno --ignore-submodules=none >actual &&
651 test_cmp expect.modified_inside actual &&
653 test_path_is_missing submod &&
654 git status -s -uno --ignore-submodules=none >actual &&
655 test_cmp expect actual
658 test_expect_success 'rm of a populated nested submodule with nested untracked files fails unless forced' '
660 git submodule update --recursive &&
661 echo X >submod/subsubmod/untracked &&
662 test_must_fail git rm submod &&
663 test_path_is_dir submod &&
664 test_path_is_file submod/.git &&
665 git status -s -uno --ignore-submodules=none >actual &&
666 test_cmp expect.modified_untracked actual &&
668 test_path_is_missing submod &&
669 git status -s -uno --ignore-submodules=none >actual &&
670 test_cmp expect actual
673 test_expect_success "rm absorbs submodule's nested .git directory" '
675 git submodule update --recursive &&
677 cd submod/subsubmod &&
679 mv ../../.git/modules/sub/modules/sub .git &&
680 GIT_WORK_TREE=. git config --unset core.worktree
682 git rm submod 2>output.err &&
683 test_path_is_missing submod &&
684 test_path_is_missing submod/subsubmod/.git &&
685 git status -s -uno --ignore-submodules=none >actual &&
686 test_file_not_empty actual &&
687 test_i18ngrep Migrating output.err
690 test_expect_success 'checking out a commit after submodule removal needs manual updates' '
691 git commit -m "submodule removal" submod .gitmodules &&
692 git checkout HEAD^ &&
693 git submodule update &&
694 git checkout -q HEAD^ &&
695 git checkout -q master 2>actual &&
696 test_i18ngrep "^warning: unable to rmdir '\''submod'\'':" actual &&
697 git status -s submod >actual &&
698 echo "?? submod/" >expected &&
699 test_cmp expected actual &&
701 git status -s -uno --ignore-submodules=none >actual &&
702 test_must_be_empty actual
705 test_expect_success 'rm of d/f when d has become a non-directory' '
713 test_must_fail git rev-parse --verify :d/f &&
717 test_expect_success SYMLINKS 'rm of d/f when d has become a dangling symlink' '
723 ln -s nonexistent d &&
725 test_must_fail git rev-parse --verify :d/f &&
727 test_path_is_missing d
730 test_expect_success 'rm of file when it has become a directory' '
737 test_must_fail git rm d &&
738 git rev-parse --verify :d &&
739 test_path_is_file d/f
742 test_expect_success SYMLINKS 'rm across a symlinked leading path (no index)' '
748 git commit -m "symlink d to e, e/f exists" &&
749 test_must_fail git rm d/f &&
750 git rev-parse --verify :d &&
751 git rev-parse --verify :e/f &&
753 test_path_is_file e/f
756 test_expect_failure SYMLINKS 'rm across a symlinked leading path (w/ index)' '
761 git commit -m "d/f exists" &&
764 test_must_fail git rm d/f &&
765 git rev-parse --verify :d/f &&
767 test_path_is_file e/f
770 test_expect_success 'setup for testing rm messages' '
773 git add bar.txt foo.txt
776 test_expect_success 'rm files with different staged content' '
777 cat >expect <<-\EOF &&
778 error: the following files have staged content different from both the
782 (use -f to force removal)
784 echo content1 >foo.txt &&
785 echo content1 >bar.txt &&
786 test_must_fail git rm foo.txt bar.txt 2>actual &&
787 test_i18ncmp expect actual
790 test_expect_success 'rm files with different staged content without hints' '
791 cat >expect <<-\EOF &&
792 error: the following files have staged content different from both the
797 echo content2 >foo.txt &&
798 echo content2 >bar.txt &&
799 test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual &&
800 test_i18ncmp expect actual
803 test_expect_success 'rm file with local modification' '
804 cat >expect <<-\EOF &&
805 error: the following file has local modifications:
807 (use --cached to keep the file, or -f to force removal)
809 git commit -m "testing rm 3" &&
810 echo content3 >foo.txt &&
811 test_must_fail git rm foo.txt 2>actual &&
812 test_i18ncmp expect actual
815 test_expect_success 'rm file with local modification without hints' '
816 cat >expect <<-\EOF &&
817 error: the following file has local modifications:
820 echo content4 >bar.txt &&
821 test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual &&
822 test_i18ncmp expect actual
825 test_expect_success 'rm file with changes in the index' '
826 cat >expect <<-\EOF &&
827 error: the following file has changes staged in the index:
829 (use --cached to keep the file, or -f to force removal)
832 echo content5 >foo.txt &&
834 test_must_fail git rm foo.txt 2>actual &&
835 test_i18ncmp expect actual
838 test_expect_success 'rm file with changes in the index without hints' '
839 cat >expect <<-\EOF &&
840 error: the following file has changes staged in the index:
843 test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual &&
844 test_i18ncmp expect actual
847 test_expect_success 'rm files with two different errors' '
848 cat >expect <<-\EOF &&
849 error: the following file has staged content different from both the
852 (use -f to force removal)
853 error: the following file has changes staged in the index:
855 (use --cached to keep the file, or -f to force removal)
857 echo content >foo1.txt &&
859 echo content6 >foo1.txt &&
860 echo content6 >bar1.txt &&
862 test_must_fail git rm bar1.txt foo1.txt 2>actual &&
863 test_i18ncmp expect actual
866 test_expect_success 'rm empty string should fail' '
867 test_must_fail git rm -rf ""