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 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' '
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' \
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 `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' '
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 &&
244 while test $i -lt 12000
246 echo "100644 $_z40 0 some-file-$i"
248 done | git update-index --index-info &&
249 git rm -n "some-file-*" | :;
250 test -f .git/index.lock
252 rm -f .git/index.lock
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 &&
269 cat >expect.modified <<EOF
273 test_expect_success 'rm removes empty submodules from work tree' '
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" &&
283 git status -s -uno --ignore-submodules=none > actual &&
284 test_cmp expect actual
287 test_expect_success 'rm removes removed submodule from index' '
289 git submodule update &&
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' '
298 git submodule update &&
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 /' '
307 git submodule update &&
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 /' '
322 test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '
324 git submodule update &&
328 test_must_fail git rm submod &&
330 test -f submod/.git &&
331 git status -s -uno --ignore-submodules=none > actual &&
332 test_cmp expect.modified actual &&
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' '
341 git submodule update &&
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
356 test_expect_success 'rm of a populated submodule with untracked files fails unless forced' '
358 git submodule update &&
362 test_must_fail git rm submod &&
364 test -f submod/.git &&
365 git status -s -uno --ignore-submodules=none > actual &&
366 test_cmp expect.modified actual &&
369 git status -s -uno --ignore-submodules=none > actual &&
370 test_cmp expect actual
373 test_expect_success 'setup submodule conflict' '
375 git submodule update &&
376 git checkout -b branch1 &&
379 git commit -m "added nitfol 1" &&
380 git checkout -b branch2 master &&
383 git commit -m "added nitfol 2" &&
384 git checkout -b conflict1 master &&
390 git commit -m "submod 1" &&
391 git checkout -b conflict2 master &&
396 git commit -m "submod 2"
399 cat >expect.conflict <<EOF
403 test_expect_success 'rm removes work tree of unmodified conflicted submodule' '
404 git checkout conflict1 &&
406 git submodule update &&
407 test_must_fail git merge conflict2 &&
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 &&
417 git submodule update &&
421 test_must_fail git merge conflict2 &&
422 test_must_fail git rm submod &&
424 test -f submod/.git &&
425 git status -s -uno --ignore-submodules=none > actual &&
426 test_cmp expect.conflict actual &&
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 &&
436 git submodule update &&
440 test_must_fail git merge conflict2 &&
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.conflict actual &&
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 &&
455 git submodule update &&
459 test_must_fail git merge conflict2 &&
460 test_must_fail git rm submod &&
462 test -f submod/.git &&
463 git status -s -uno --ignore-submodules=none > actual &&
464 test_cmp expect.conflict actual &&
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 &&
474 git submodule update &&
477 cp -a ../.git/modules/sub .git &&
478 GIT_WORK_TREE=. git config --unset core.worktree
480 test_must_fail git merge conflict2 &&
481 test_must_fail git rm 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 &&
488 test -d submod/.git &&
489 git status -s -uno --ignore-submodules=none > actual &&
490 test_cmp expect.conflict actual &&
495 test_expect_success 'rm of a conflicted unpopulated submodule succeeds' '
496 git checkout conflict1 &&
498 test_must_fail git merge conflict2 &&
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 &&
508 git submodule update &&
511 cp -a ../.git/modules/sub .git &&
512 GIT_WORK_TREE=. git config --unset core.worktree
514 test_must_fail git rm submod &&
516 test -d submod/.git &&
517 git status -s -uno --ignore-submodules=none > actual &&
519 test_must_fail git rm -f submod &&
521 test -d submod/.git &&
522 git status -s -uno --ignore-submodules=none > actual &&
527 cat >expect.deepmodified <<EOF
531 test_expect_success 'setup subsubmodule' '
533 git submodule update &&
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
543 git commit -a -m "added deep submodule"
546 test_expect_success 'rm recursively removes work tree of unmodified submodules' '
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' '
555 git submodule update --recursive &&
556 (cd submod/subsubmod &&
559 test_must_fail git rm submod &&
561 test -f submod/.git &&
562 git status -s -uno --ignore-submodules=none > actual &&
563 test_cmp expect.modified actual &&
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' '
572 git submodule update --recursive &&
573 (cd submod/subsubmod &&
576 test_must_fail git rm submod &&
578 test -f submod/.git &&
579 git status -s -uno --ignore-submodules=none > actual &&
580 test_cmp expect.modified actual &&
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' '
589 git submodule update --recursive &&
590 (cd submod/subsubmod &&
593 test_must_fail git rm submod &&
595 test -f submod/.git &&
596 git status -s -uno --ignore-submodules=none > actual &&
597 test_cmp expect.modified actual &&
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' '
606 git submodule update --recursive &&
607 (cd submod/subsubmod &&
609 cp -a ../../.git/modules/sub/modules/sub .git &&
610 GIT_WORK_TREE=. git config --unset core.worktree
612 test_must_fail git rm submod &&
614 test -d submod/subsubmod/.git &&
615 git status -s -uno --ignore-submodules=none > actual &&
617 test_must_fail git rm -f submod &&
619 test -d submod/subsubmod/.git &&
620 git status -s -uno --ignore-submodules=none > actual &&