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 git add -- 'tab embedded' 'newline
22 git commit -m 'add files with tabs and newlines'
27 test "$test_tabs" = n && say 'Your filesystem does not allow tabs in filenames.'
29 # Later we will try removing an unremovable path to make sure
30 # git rm barfs, but if the test is run as root that cannot be
33 'Determine rm behavior' \
37 test -f test-file && test_failed_remove=y
42 'Pre-check that foo exists and is in index before git rm foo' \
43 '[ -f foo ] && git ls-files --error-unmatch foo'
46 'Test that git rm foo succeeds' \
50 'Test that git rm --cached foo succeeds if the index matches the file' \
56 'Test that git rm --cached foo succeeds if the index matches the file' \
60 echo "other content" > foo
64 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' '
68 echo "other content" > foo
70 echo "yet another content" > foo
71 test_must_fail git rm --cached foo
75 'Test that git rm --cached -f foo works in case where --cached only did not' \
79 echo "other content" > foo
81 echo "yet another content" > foo
82 git rm --cached -f foo'
85 'Post-check that foo exists but is not in index after git rm foo' \
86 '[ -f foo ] && test_must_fail git ls-files --error-unmatch foo'
89 'Pre-check that bar exists and is in index before "git rm bar"' \
90 '[ -f bar ] && git ls-files --error-unmatch bar'
93 'Test that "git rm bar" succeeds' \
97 'Post-check that bar does not exist and is not in index after "git rm -f bar"' \
98 '! [ -f bar ] && test_must_fail git ls-files --error-unmatch bar'
100 test_expect_success \
101 'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' \
104 test "$test_tabs" = y && test_expect_success \
105 "Test that \"git rm -f\" succeeds with embedded space, tab, or newline characters." \
106 "git rm -f 'space embedded' 'tab embedded' 'newline
109 if test "$test_failed_remove" = y; then
111 test_expect_success \
112 'Test that "git rm -f" fails if its rm fails' \
113 'test_must_fail git rm -f baz'
116 say 'skipping removal failure test (perhaps running as root?)'
119 test_expect_success \
120 'When the rm in "git rm -f" fails, it should not remove the file from the index' \
121 'git ls-files --error-unmatch baz'
123 test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
124 git rm --ignore-unmatch nonexistent
127 test_expect_success '"rm" command printed' '
128 echo frotz > test-file &&
130 git commit -m "add file for rm test" &&
131 git rm test-file > rm-output &&
132 test `grep "^rm " rm-output | wc -l` = 1 &&
133 rm -f test-file rm-output &&
134 git commit -m "remove file from rm test"
137 test_expect_success '"rm" command suppressed with --quiet' '
138 echo frotz > test-file &&
140 git commit -m "add file for rm --quiet test" &&
141 git rm --quiet test-file > rm-output &&
142 test `wc -l < rm-output` = 0 &&
143 rm -f test-file rm-output &&
144 git commit -m "remove file from rm --quiet test"
147 # Now, failure cases.
148 test_expect_success 'Re-add foo and baz' '
150 git ls-files --error-unmatch foo baz
153 test_expect_success 'Modify foo -- rm should refuse' '
155 test_must_fail git rm foo baz &&
158 git ls-files --error-unmatch foo baz
161 test_expect_success 'Modified foo -- rm -f should work' '
165 test_must_fail git ls-files --error-unmatch foo &&
166 test_must_fail git ls-files --error-unmatch bar
169 test_expect_success 'Re-add foo and baz for HEAD tests' '
171 git checkout HEAD -- baz &&
173 git ls-files --error-unmatch foo baz
176 test_expect_success 'foo is different in index from HEAD -- rm should refuse' '
177 test_must_fail git rm foo baz &&
180 git ls-files --error-unmatch foo baz
183 test_expect_success 'but with -f it should work.' '
187 test_must_fail git ls-files --error-unmatch foo
188 test_must_fail git ls-files --error-unmatch baz
191 test_expect_success 'refuse to remove cached empty file with modifications' '
194 echo content >empty &&
195 test_must_fail git rm --cached empty
198 test_expect_success 'remove intent-to-add file without --force' '
199 echo content >intent-to-add &&
200 git add -N intent-to-add
201 git rm --cached intent-to-add
204 test_expect_success 'Recursive test setup' '
206 echo qfwfq >frotz/nitfol &&
208 git commit -m "subdir test"
211 test_expect_success 'Recursive without -r fails' '
212 test_must_fail git rm frotz &&
217 test_expect_success 'Recursive with -r but dirty' '
218 echo qfwfq >>frotz/nitfol
219 test_must_fail git rm -r frotz &&
224 test_expect_success 'Recursive with -r -f' '
225 git rm -f -r frotz &&
226 ! test -f frotz/nitfol &&
230 test_expect_success 'Remove nonexistent file returns nonzero exit status' '
231 test_must_fail git rm nonexistent
234 test_expect_success 'Call "rm" from outside the work tree' '
238 echo something > somefile &&
240 git commit -m "add a file" &&
242 git --git-dir=repo/.git --work-tree=repo rm somefile) &&
243 test_must_fail git ls-files --error-unmatch somefile)
246 test_expect_success 'refresh index before checking if it is up-to-date' '
249 test-chmtime -86400 frotz/nitfol &&
250 git rm frotz/nitfol &&
251 test ! -f frotz/nitfol
255 test_expect_success 'choking "git rm" should not let it die with cruft' '
256 git reset -q --hard &&
257 H=0000000000000000000000000000000000000000 &&
259 while test $i -lt 12000
261 echo "100644 $H 0 some-file-$i"
263 done | git update-index --index-info &&
264 git rm -n "some-file-*" | :;
265 test -f .git/index.lock
267 rm -f .git/index.lock