line-range: teach -L/RE/ to search relative to anchor point
[alt-git.git] / t / t3600-rm.sh
blob5c87b55645e249929585c2ed0e7bcf98a1b62da3
1 #!/bin/sh
3 # Copyright (c) 2006 Carl D. Worth
6 test_description='Test of the various options to git rm.'
8 . ./test-lib.sh
10 # Setup some files to be removed, some with funny characters
11 test_expect_success \
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
18 embedded' 2>/dev/null
19 then
20 test_set_prereq FUNNYNAMES
21 else
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
27 embedded' &&
28 git commit -m 'add files with tabs and newlines'
31 test_expect_success \
32 'Pre-check that foo exists and is in index before git rm foo' \
33 '[ -f foo ] && git ls-files --error-unmatch foo'
35 test_expect_success \
36 'Test that git rm foo succeeds' \
37 'git rm --cached foo'
39 test_expect_success \
40 'Test that git rm --cached foo succeeds if the index matches the file' \
41 'echo content > foo
42 git add foo
43 git rm --cached foo'
45 test_expect_success \
46 'Test that git rm --cached foo succeeds if the index matches the file' \
47 'echo content > foo
48 git add foo
49 git commit -m foo
50 echo "other content" > foo
51 git rm --cached foo'
53 test_expect_success \
54 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' '
55 echo content > foo
56 git add foo
57 git commit -m foo
58 echo "other content" > foo
59 git add foo
60 echo "yet another content" > foo
61 test_must_fail git rm --cached foo
64 test_expect_success \
65 'Test that git rm --cached -f foo works in case where --cached only did not' \
66 'echo content > foo
67 git add foo
68 git commit -m foo
69 echo "other content" > foo
70 git add foo
71 echo "yet another content" > foo
72 git rm --cached -f foo'
74 test_expect_success \
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'
78 test_expect_success \
79 'Pre-check that bar exists and is in index before "git rm bar"' \
80 '[ -f bar ] && git ls-files --error-unmatch bar'
82 test_expect_success \
83 'Test that "git rm bar" succeeds' \
84 'git rm bar'
86 test_expect_success \
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'
90 test_expect_success \
91 'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' \
92 'git rm -- -q'
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
97 embedded'"
99 test_expect_success SANITY 'Test that "git rm -f" fails if its rm fails' '
100 chmod a-w . &&
101 test_must_fail git rm -f baz &&
102 chmod 775 .
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 &&
115 git add 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 &&
125 git add 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' '
135 git add foo baz &&
136 git ls-files --error-unmatch foo baz
139 test_expect_success 'Modify foo -- rm should refuse' '
140 echo >>foo &&
141 test_must_fail git rm foo baz &&
142 test -f foo &&
143 test -f baz &&
144 git ls-files --error-unmatch foo baz
147 test_expect_success 'Modified foo -- rm -f should work' '
148 git rm -f foo baz &&
149 test ! -f foo &&
150 test ! -f baz &&
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' '
156 echo frotz >foo &&
157 git checkout HEAD -- baz &&
158 git add foo 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 &&
164 test -f foo &&
165 test -f baz &&
166 git ls-files --error-unmatch foo baz
169 test_expect_success 'but with -f it should work.' '
170 git rm -f foo baz &&
171 test ! -f foo &&
172 test ! -f baz &&
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' '
178 >empty &&
179 git add empty &&
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' '
191 mkdir -p frotz &&
192 echo qfwfq >frotz/nitfol &&
193 git add frotz &&
194 git commit -m "subdir test"
197 test_expect_success 'Recursive without -r fails' '
198 test_must_fail git rm frotz &&
199 test -d frotz &&
200 test -f frotz/nitfol
203 test_expect_success 'Recursive with -r but dirty' '
204 echo qfwfq >>frotz/nitfol
205 test_must_fail git rm -r frotz &&
206 test -d frotz &&
207 test -f frotz/nitfol
210 test_expect_success 'Recursive with -r -f' '
211 git rm -f -r frotz &&
212 ! test -f frotz/nitfol &&
213 ! test -d frotz
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' '
221 mkdir repo &&
222 (cd repo &&
223 git init &&
224 echo something > somefile &&
225 git add somefile &&
226 git commit -m "add a file" &&
227 (cd .. &&
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' '
234 git reset --hard &&
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 i=0 &&
244 while test $i -lt 12000
246 echo "100644 $_z40 0 some-file-$i"
247 i=$(( $i + 1 ))
248 done | git update-index --index-info &&
249 git rm -n "some-file-*" | :;
250 test -f .git/index.lock
251 status=$?
252 rm -f .git/index.lock
253 git reset -q --hard
254 test "$status" != 0
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 &&
262 ! test -d dir
265 cat >expect <<EOF
266 D submod
269 cat >expect.modified <<EOF
270 M submod
273 test_expect_success 'rm removes empty submodules from work tree' '
274 mkdir submod &&
275 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) submod &&
276 git config -f .gitmodules submodule.sub.url ./. &&
277 git config -f .gitmodules submodule.sub.path submod &&
278 git submodule init &&
279 git add .gitmodules &&
280 git commit -m "add submodule" &&
281 git rm submod &&
282 test ! -e submod &&
283 git status -s -uno --ignore-submodules=none > actual &&
284 test_cmp expect actual
287 test_expect_success 'rm removes removed submodule from index' '
288 git reset --hard &&
289 git submodule update &&
290 rm -rf submod &&
291 git rm submod &&
292 git status -s -uno --ignore-submodules=none > actual &&
293 test_cmp expect actual
296 test_expect_success 'rm removes work tree of unmodified submodules' '
297 git reset --hard &&
298 git submodule update &&
299 git rm submod &&
300 test ! -d submod &&
301 git status -s -uno --ignore-submodules=none > actual &&
302 test_cmp expect actual
305 test_expect_success 'rm removes a submodule with a trailing /' '
306 git reset --hard &&
307 git submodule update &&
308 git rm submod/ &&
309 test ! -d submod &&
310 git status -s -uno --ignore-submodules=none > actual &&
311 test_cmp expect actual
314 test_expect_success 'rm fails when given a file with a trailing /' '
315 test_must_fail git rm empty/
318 test_expect_success 'rm succeeds when given a directory with a trailing /' '
319 git rm -r frotz/
322 test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '
323 git reset --hard &&
324 git submodule update &&
325 (cd submod &&
326 git checkout HEAD^
327 ) &&
328 test_must_fail git rm submod &&
329 test -d submod &&
330 test -f submod/.git &&
331 git status -s -uno --ignore-submodules=none > actual &&
332 test_cmp expect.modified actual &&
333 git rm -f submod &&
334 test ! -d submod &&
335 git status -s -uno --ignore-submodules=none > actual &&
336 test_cmp expect actual
339 test_expect_success 'rm of a populated submodule with modifications fails unless forced' '
340 git reset --hard &&
341 git submodule update &&
342 (cd submod &&
343 echo X >empty
344 ) &&
345 test_must_fail git rm submod &&
346 test -d submod &&
347 test -f submod/.git &&
348 git status -s -uno --ignore-submodules=none > actual &&
349 test_cmp expect.modified actual &&
350 git rm -f submod &&
351 test ! -d submod &&
352 git status -s -uno --ignore-submodules=none > actual &&
353 test_cmp expect actual
356 test_expect_success 'rm of a populated submodule with untracked files fails unless forced' '
357 git reset --hard &&
358 git submodule update &&
359 (cd submod &&
360 echo X >untracked
361 ) &&
362 test_must_fail git rm submod &&
363 test -d submod &&
364 test -f submod/.git &&
365 git status -s -uno --ignore-submodules=none > actual &&
366 test_cmp expect.modified actual &&
367 git rm -f submod &&
368 test ! -d submod &&
369 git status -s -uno --ignore-submodules=none > actual &&
370 test_cmp expect actual
373 test_expect_success 'setup submodule conflict' '
374 git reset --hard &&
375 git submodule update &&
376 git checkout -b branch1 &&
377 echo 1 >nitfol &&
378 git add nitfol &&
379 git commit -m "added nitfol 1" &&
380 git checkout -b branch2 master &&
381 echo 2 >nitfol &&
382 git add nitfol &&
383 git commit -m "added nitfol 2" &&
384 git checkout -b conflict1 master &&
385 (cd submod &&
386 git fetch &&
387 git checkout branch1
388 ) &&
389 git add submod &&
390 git commit -m "submod 1" &&
391 git checkout -b conflict2 master &&
392 (cd submod &&
393 git checkout branch2
394 ) &&
395 git add submod &&
396 git commit -m "submod 2"
399 cat >expect.conflict <<EOF
400 UU submod
403 test_expect_success 'rm removes work tree of unmodified conflicted submodule' '
404 git checkout conflict1 &&
405 git reset --hard &&
406 git submodule update &&
407 test_must_fail git merge conflict2 &&
408 git rm submod &&
409 test ! -d submod &&
410 git status -s -uno --ignore-submodules=none > actual &&
411 test_cmp expect actual
414 test_expect_success 'rm of a conflicted populated submodule with different HEAD fails unless forced' '
415 git checkout conflict1 &&
416 git reset --hard &&
417 git submodule update &&
418 (cd submod &&
419 git checkout HEAD^
420 ) &&
421 test_must_fail git merge conflict2 &&
422 test_must_fail git rm submod &&
423 test -d submod &&
424 test -f submod/.git &&
425 git status -s -uno --ignore-submodules=none > actual &&
426 test_cmp expect.conflict actual &&
427 git rm -f submod &&
428 test ! -d submod &&
429 git status -s -uno --ignore-submodules=none > actual &&
430 test_cmp expect actual
433 test_expect_success 'rm of a conflicted populated submodule with modifications fails unless forced' '
434 git checkout conflict1 &&
435 git reset --hard &&
436 git submodule update &&
437 (cd submod &&
438 echo X >empty
439 ) &&
440 test_must_fail git merge conflict2 &&
441 test_must_fail git rm submod &&
442 test -d submod &&
443 test -f submod/.git &&
444 git status -s -uno --ignore-submodules=none > actual &&
445 test_cmp expect.conflict actual &&
446 git rm -f submod &&
447 test ! -d submod &&
448 git status -s -uno --ignore-submodules=none > actual &&
449 test_cmp expect actual
452 test_expect_success 'rm of a conflicted populated submodule with untracked files fails unless forced' '
453 git checkout conflict1 &&
454 git reset --hard &&
455 git submodule update &&
456 (cd submod &&
457 echo X >untracked
458 ) &&
459 test_must_fail git merge conflict2 &&
460 test_must_fail git rm submod &&
461 test -d submod &&
462 test -f submod/.git &&
463 git status -s -uno --ignore-submodules=none > actual &&
464 test_cmp expect.conflict actual &&
465 git rm -f submod &&
466 test ! -d submod &&
467 git status -s -uno --ignore-submodules=none > actual &&
468 test_cmp expect actual
471 test_expect_success 'rm of a conflicted populated submodule with a .git directory fails even when forced' '
472 git checkout conflict1 &&
473 git reset --hard &&
474 git submodule update &&
475 (cd submod &&
476 rm .git &&
477 cp -R ../.git/modules/sub .git &&
478 GIT_WORK_TREE=. git config --unset core.worktree
479 ) &&
480 test_must_fail git merge conflict2 &&
481 test_must_fail git rm submod &&
482 test -d submod &&
483 test -d submod/.git &&
484 git status -s -uno --ignore-submodules=none > actual &&
485 test_cmp expect.conflict actual &&
486 test_must_fail git rm -f submod &&
487 test -d submod &&
488 test -d submod/.git &&
489 git status -s -uno --ignore-submodules=none > actual &&
490 test_cmp expect.conflict actual &&
491 git merge --abort &&
492 rm -rf submod
495 test_expect_success 'rm of a conflicted unpopulated submodule succeeds' '
496 git checkout conflict1 &&
497 git reset --hard &&
498 test_must_fail git merge conflict2 &&
499 git rm submod &&
500 test ! -d submod &&
501 git status -s -uno --ignore-submodules=none > actual &&
502 test_cmp expect actual
505 test_expect_success 'rm of a populated submodule with a .git directory fails even when forced' '
506 git checkout -f master &&
507 git reset --hard &&
508 git submodule update &&
509 (cd submod &&
510 rm .git &&
511 cp -R ../.git/modules/sub .git &&
512 GIT_WORK_TREE=. git config --unset core.worktree
513 ) &&
514 test_must_fail git rm submod &&
515 test -d submod &&
516 test -d submod/.git &&
517 git status -s -uno --ignore-submodules=none > actual &&
518 ! test -s actual &&
519 test_must_fail git rm -f submod &&
520 test -d submod &&
521 test -d submod/.git &&
522 git status -s -uno --ignore-submodules=none > actual &&
523 ! test -s actual &&
524 rm -rf submod
527 cat >expect.deepmodified <<EOF
528 M submod/subsubmod
531 test_expect_success 'setup subsubmodule' '
532 git reset --hard &&
533 git submodule update &&
534 (cd submod &&
535 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) subsubmod &&
536 git config -f .gitmodules submodule.sub.url ../. &&
537 git config -f .gitmodules submodule.sub.path subsubmod &&
538 git submodule init &&
539 git add .gitmodules &&
540 git commit -m "add subsubmodule" &&
541 git submodule update subsubmod
542 ) &&
543 git commit -a -m "added deep submodule"
546 test_expect_success 'rm recursively removes work tree of unmodified submodules' '
547 git rm submod &&
548 test ! -d submod &&
549 git status -s -uno --ignore-submodules=none > actual &&
550 test_cmp expect actual
553 test_expect_success 'rm of a populated nested submodule with different nested HEAD fails unless forced' '
554 git reset --hard &&
555 git submodule update --recursive &&
556 (cd submod/subsubmod &&
557 git checkout HEAD^
558 ) &&
559 test_must_fail git rm submod &&
560 test -d submod &&
561 test -f submod/.git &&
562 git status -s -uno --ignore-submodules=none > actual &&
563 test_cmp expect.modified actual &&
564 git rm -f submod &&
565 test ! -d submod &&
566 git status -s -uno --ignore-submodules=none > actual &&
567 test_cmp expect actual
570 test_expect_success 'rm of a populated nested submodule with nested modifications fails unless forced' '
571 git reset --hard &&
572 git submodule update --recursive &&
573 (cd submod/subsubmod &&
574 echo X >empty
575 ) &&
576 test_must_fail git rm submod &&
577 test -d submod &&
578 test -f submod/.git &&
579 git status -s -uno --ignore-submodules=none > actual &&
580 test_cmp expect.modified actual &&
581 git rm -f submod &&
582 test ! -d submod &&
583 git status -s -uno --ignore-submodules=none > actual &&
584 test_cmp expect actual
587 test_expect_success 'rm of a populated nested submodule with nested untracked files fails unless forced' '
588 git reset --hard &&
589 git submodule update --recursive &&
590 (cd submod/subsubmod &&
591 echo X >untracked
592 ) &&
593 test_must_fail git rm submod &&
594 test -d submod &&
595 test -f submod/.git &&
596 git status -s -uno --ignore-submodules=none > actual &&
597 test_cmp expect.modified actual &&
598 git rm -f submod &&
599 test ! -d submod &&
600 git status -s -uno --ignore-submodules=none > actual &&
601 test_cmp expect actual
604 test_expect_success 'rm of a populated nested submodule with a nested .git directory fails even when forced' '
605 git reset --hard &&
606 git submodule update --recursive &&
607 (cd submod/subsubmod &&
608 rm .git &&
609 cp -R ../../.git/modules/sub/modules/sub .git &&
610 GIT_WORK_TREE=. git config --unset core.worktree
611 ) &&
612 test_must_fail git rm submod &&
613 test -d submod &&
614 test -d submod/subsubmod/.git &&
615 git status -s -uno --ignore-submodules=none > actual &&
616 ! test -s actual &&
617 test_must_fail git rm -f submod &&
618 test -d submod &&
619 test -d submod/subsubmod/.git &&
620 git status -s -uno --ignore-submodules=none > actual &&
621 ! test -s actual &&
622 rm -rf submod
625 test_expect_success 'rm of d/f when d has become a non-directory' '
626 rm -rf d &&
627 mkdir d &&
628 >d/f &&
629 git add d &&
630 rm -rf d &&
631 >d &&
632 git rm d/f &&
633 test_must_fail git rev-parse --verify :d/f &&
634 test_path_is_file d
637 test_expect_success SYMLINKS 'rm of d/f when d has become a dangling symlink' '
638 rm -rf d &&
639 mkdir d &&
640 >d/f &&
641 git add d &&
642 rm -rf d &&
643 ln -s nonexistent d &&
644 git rm d/f &&
645 test_must_fail git rev-parse --verify :d/f &&
646 test -h d &&
647 test_path_is_missing d
650 test_expect_success 'rm of file when it has become a directory' '
651 rm -rf d &&
652 >d &&
653 git add d &&
654 rm -f d &&
655 mkdir d &&
656 >d/f &&
657 test_must_fail git rm d &&
658 git rev-parse --verify :d &&
659 test_path_is_file d/f
662 test_expect_success SYMLINKS 'rm across a symlinked leading path (no index)' '
663 rm -rf d e &&
664 mkdir e &&
665 echo content >e/f &&
666 ln -s e d &&
667 git add -A e d &&
668 git commit -m "symlink d to e, e/f exists" &&
669 test_must_fail git rm d/f &&
670 git rev-parse --verify :d &&
671 git rev-parse --verify :e/f &&
672 test -h d &&
673 test_path_is_file e/f
676 test_expect_failure SYMLINKS 'rm across a symlinked leading path (w/ index)' '
677 rm -rf d e &&
678 mkdir d &&
679 echo content >d/f &&
680 git add -A e d &&
681 git commit -m "d/f exists" &&
682 mv d e &&
683 ln -s e d &&
684 test_must_fail git rm d/f &&
685 git rev-parse --verify :d/f &&
686 test -h d &&
687 test_path_is_file e/f
690 test_expect_success 'setup for testing rm messages' '
691 >bar.txt &&
692 >foo.txt &&
693 git add bar.txt foo.txt
696 test_expect_success 'rm files with different staged content' '
697 cat >expect <<-\EOF &&
698 error: the following files have staged content different from both the
699 file and the HEAD:
700 bar.txt
701 foo.txt
702 (use -f to force removal)
704 echo content1 >foo.txt &&
705 echo content1 >bar.txt &&
706 test_must_fail git rm foo.txt bar.txt 2>actual &&
707 test_i18ncmp expect actual
710 test_expect_success 'rm files with different staged content without hints' '
711 cat >expect <<-\EOF &&
712 error: the following files have staged content different from both the
713 file and the HEAD:
714 bar.txt
715 foo.txt
717 echo content2 >foo.txt &&
718 echo content2 >bar.txt &&
719 test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual &&
720 test_i18ncmp expect actual
723 test_expect_success 'rm file with local modification' '
724 cat >expect <<-\EOF &&
725 error: the following file has local modifications:
726 foo.txt
727 (use --cached to keep the file, or -f to force removal)
729 git commit -m "testing rm 3" &&
730 echo content3 >foo.txt &&
731 test_must_fail git rm foo.txt 2>actual &&
732 test_i18ncmp expect actual
735 test_expect_success 'rm file with local modification without hints' '
736 cat >expect <<-\EOF &&
737 error: the following file has local modifications:
738 bar.txt
740 echo content4 >bar.txt &&
741 test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual &&
742 test_i18ncmp expect actual
745 test_expect_success 'rm file with changes in the index' '
746 cat >expect <<-\EOF &&
747 error: the following file has changes staged in the index:
748 foo.txt
749 (use --cached to keep the file, or -f to force removal)
751 git reset --hard &&
752 echo content5 >foo.txt &&
753 git add foo.txt &&
754 test_must_fail git rm foo.txt 2>actual &&
755 test_i18ncmp expect actual
758 test_expect_success 'rm file with changes in the index without hints' '
759 cat >expect <<-\EOF &&
760 error: the following file has changes staged in the index:
761 foo.txt
763 test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual &&
764 test_i18ncmp expect actual
767 test_expect_success 'rm files with two different errors' '
768 cat >expect <<-\EOF &&
769 error: the following file has staged content different from both the
770 file and the HEAD:
771 foo1.txt
772 (use -f to force removal)
773 error: the following file has changes staged in the index:
774 bar1.txt
775 (use --cached to keep the file, or -f to force removal)
777 echo content >foo1.txt &&
778 git add foo1.txt &&
779 echo content6 >foo1.txt &&
780 echo content6 >bar1.txt &&
781 git add bar1.txt &&
782 test_must_fail git rm bar1.txt foo1.txt 2>actual &&
783 test_i18ncmp expect actual
786 test_done