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'
31 # Determine rm behavior
32 # Later we will try removing an unremovable path to make sure
33 # git rm barfs, but if the test is run as root that cannot be
37 rm -f test-file 2>/dev/null
40 test_set_prereq RO_DIR
42 skip_all='skipping removal failure test (perhaps running as root?)'
48 'Pre-check that foo exists and is in index before git rm foo' \
49 '[ -f foo ] && git ls-files --error-unmatch foo'
52 'Test that git rm foo succeeds' \
56 'Test that git rm --cached foo succeeds if the index matches the file' \
62 'Test that git rm --cached foo succeeds if the index matches the file' \
66 echo "other content" > foo
70 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' '
74 echo "other content" > foo
76 echo "yet another content" > foo
77 test_must_fail git rm --cached foo
81 'Test that git rm --cached -f foo works in case where --cached only did not' \
85 echo "other content" > foo
87 echo "yet another content" > foo
88 git rm --cached -f foo'
91 'Post-check that foo exists but is not in index after git rm foo' \
92 '[ -f foo ] && test_must_fail git ls-files --error-unmatch foo'
95 'Pre-check that bar exists and is in index before "git rm bar"' \
96 '[ -f bar ] && git ls-files --error-unmatch bar'
99 'Test that "git rm bar" succeeds' \
102 test_expect_success \
103 'Post-check that bar does not exist and is not in index after "git rm -f bar"' \
104 '! [ -f bar ] && test_must_fail git ls-files --error-unmatch bar'
106 test_expect_success \
107 'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' \
110 test_expect_success FUNNYNAMES \
111 "Test that \"git rm -f\" succeeds with embedded space, tab, or newline characters." \
112 "git rm -f 'space embedded' 'tab embedded' 'newline
115 test_expect_success RO_DIR 'Test that "git rm -f" fails if its rm fails' '
117 test_must_fail git rm -f baz &&
121 test_expect_success \
122 'When the rm in "git rm -f" fails, it should not remove the file from the index' \
123 'git ls-files --error-unmatch baz'
125 test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
126 git rm --ignore-unmatch nonexistent
129 test_expect_success '"rm" command printed' '
130 echo frotz > test-file &&
132 git commit -m "add file for rm test" &&
133 git rm test-file > rm-output &&
134 test `grep "^rm " rm-output | wc -l` = 1 &&
135 rm -f test-file rm-output &&
136 git commit -m "remove file from rm test"
139 test_expect_success '"rm" command suppressed with --quiet' '
140 echo frotz > test-file &&
142 git commit -m "add file for rm --quiet test" &&
143 git rm --quiet test-file > rm-output &&
144 test `wc -l < rm-output` = 0 &&
145 rm -f test-file rm-output &&
146 git commit -m "remove file from rm --quiet test"
149 # Now, failure cases.
150 test_expect_success 'Re-add foo and baz' '
152 git ls-files --error-unmatch foo baz
155 test_expect_success 'Modify foo -- rm should refuse' '
157 test_must_fail git rm foo baz &&
160 git ls-files --error-unmatch foo baz
163 test_expect_success 'Modified foo -- rm -f should work' '
167 test_must_fail git ls-files --error-unmatch foo &&
168 test_must_fail git ls-files --error-unmatch bar
171 test_expect_success 'Re-add foo and baz for HEAD tests' '
173 git checkout HEAD -- baz &&
175 git ls-files --error-unmatch foo baz
178 test_expect_success 'foo is different in index from HEAD -- rm should refuse' '
179 test_must_fail git rm foo baz &&
182 git ls-files --error-unmatch foo baz
185 test_expect_success 'but with -f it should work.' '
189 test_must_fail git ls-files --error-unmatch foo
190 test_must_fail git ls-files --error-unmatch baz
193 test_expect_success 'refuse to remove cached empty file with modifications' '
196 echo content >empty &&
197 test_must_fail git rm --cached empty
200 test_expect_success 'remove intent-to-add file without --force' '
201 echo content >intent-to-add &&
202 git add -N intent-to-add
203 git rm --cached intent-to-add
206 test_expect_success 'Recursive test setup' '
208 echo qfwfq >frotz/nitfol &&
210 git commit -m "subdir test"
213 test_expect_success 'Recursive without -r fails' '
214 test_must_fail git rm frotz &&
219 test_expect_success 'Recursive with -r but dirty' '
220 echo qfwfq >>frotz/nitfol
221 test_must_fail git rm -r frotz &&
226 test_expect_success 'Recursive with -r -f' '
227 git rm -f -r frotz &&
228 ! test -f frotz/nitfol &&
232 test_expect_success 'Remove nonexistent file returns nonzero exit status' '
233 test_must_fail git rm nonexistent
236 test_expect_success 'Call "rm" from outside the work tree' '
240 echo something > somefile &&
242 git commit -m "add a file" &&
244 git --git-dir=repo/.git --work-tree=repo rm somefile) &&
245 test_must_fail git ls-files --error-unmatch somefile)
248 test_expect_success 'refresh index before checking if it is up-to-date' '
251 test-chmtime -86400 frotz/nitfol &&
252 git rm frotz/nitfol &&
253 test ! -f frotz/nitfol
257 test_expect_success 'choking "git rm" should not let it die with cruft' '
258 git reset -q --hard &&
259 H=0000000000000000000000000000000000000000 &&
261 while test $i -lt 12000
263 echo "100644 $H 0 some-file-$i"
265 done | git update-index --index-info &&
266 git rm -n "some-file-*" | :;
267 test -f .git/index.lock
269 rm -f .git/index.lock
274 test_expect_success 'rm removes subdirectories recursively' '
275 mkdir -p dir/subdir/subsubdir &&
276 echo content >dir/subdir/subsubdir/file &&
277 git add dir/subdir/subsubdir/file &&
278 git rm -f dir/subdir/subsubdir/file &&