]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3600-rm.sh
Merge branch 'nd/maint-relative'
[thirdparty/git.git] / t / t3600-rm.sh
CommitLineData
d4a1cab5
CW
1#!/bin/sh
2#
3# Copyright (c) 2006 Carl D. Worth
4#
5
5be60078 6test_description='Test of the various options to git rm.'
d4a1cab5
CW
7
8. ./test-lib.sh
9
3844cdc8 10# Setup some files to be removed, some with funny characters
22669a04
SP
11test_expect_success \
12 'Initialize test directory' \
13 "touch -- foo bar baz 'space embedded' -q &&
5be60078 14 git add -- foo bar baz 'space embedded' -q &&
56e78bfb
JS
15 git commit -m 'add normal files'"
16
17if touch -- 'tab embedded' 'newline
18embedded' 2>/dev/null
19then
20 test_set_prereq FUNNYNAMES
21else
22 say 'Your filesystem does not allow tabs in filenames.'
23fi
24
25test_expect_success FUNNYNAMES 'add files with funny names' "
5be60078 26 git add -- 'tab embedded' 'newline
22669a04 27embedded' &&
3604e7c5 28 git commit -m 'add files with tabs and newlines'
56e78bfb 29"
5b46a428 30
d4a1cab5 31test_expect_success \
5be60078
JH
32 'Pre-check that foo exists and is in index before git rm foo' \
33 '[ -f foo ] && git ls-files --error-unmatch foo'
d4a1cab5
CW
34
35test_expect_success \
5be60078
JH
36 'Test that git rm foo succeeds' \
37 'git rm --cached foo'
d4a1cab5 38
bdecd9d4
MM
39test_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'
44
45test_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'
52
41ac414e
JH
53test_expect_success \
54 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' '
55 echo content > foo
bdecd9d4
MM
56 git add foo
57 git commit -m foo
58 echo "other content" > foo
59 git add foo
60 echo "yet another content" > foo
d492b31c 61 test_must_fail git rm --cached foo
41ac414e 62'
bdecd9d4
MM
63
64test_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'
73
3844cdc8 74test_expect_success \
5be60078 75 'Post-check that foo exists but is not in index after git rm foo' \
d492b31c 76 '[ -f foo ] && test_must_fail git ls-files --error-unmatch foo'
3844cdc8
CW
77
78test_expect_success \
5be60078
JH
79 'Pre-check that bar exists and is in index before "git rm bar"' \
80 '[ -f bar ] && git ls-files --error-unmatch bar'
d4a1cab5
CW
81
82test_expect_success \
5be60078
JH
83 'Test that "git rm bar" succeeds' \
84 'git rm bar'
d4a1cab5 85
3844cdc8 86test_expect_success \
5be60078 87 'Post-check that bar does not exist and is not in index after "git rm -f bar"' \
d492b31c 88 '! [ -f bar ] && test_must_fail git ls-files --error-unmatch bar'
d4a1cab5
CW
89
90test_expect_success \
5be60078
JH
91 'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' \
92 'git rm -- -q'
d4a1cab5 93
56e78bfb 94test_expect_success FUNNYNAMES \
5be60078
JH
95 "Test that \"git rm -f\" succeeds with embedded space, tab, or newline characters." \
96 "git rm -f 'space embedded' 'tab embedded' 'newline
3844cdc8
CW
97embedded'"
98
56e78bfb
JS
99test_expect_success RO_DIR '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 .
103'
3844cdc8
CW
104
105test_expect_success \
5be60078
JH
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'
3844cdc8 108
bb1faf0d
SG
109test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
110 git rm --ignore-unmatch nonexistent
111'
112
b48caa20
SG
113test_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 &&
0feb4d1c 118 test `grep "^rm " rm-output | wc -l` = 1 &&
b48caa20
SG
119 rm -f test-file rm-output &&
120 git commit -m "remove file from rm test"
121'
122
123test_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"
131'
132
467e1b53
JH
133# Now, failure cases.
134test_expect_success 'Re-add foo and baz' '
135 git add foo baz &&
136 git ls-files --error-unmatch foo baz
137'
138
139test_expect_success 'Modify foo -- rm should refuse' '
140 echo >>foo &&
d492b31c 141 test_must_fail git rm foo baz &&
467e1b53
JH
142 test -f foo &&
143 test -f baz &&
144 git ls-files --error-unmatch foo baz
145'
146
147test_expect_success 'Modified foo -- rm -f should work' '
148 git rm -f foo baz &&
149 test ! -f foo &&
150 test ! -f baz &&
d492b31c
SB
151 test_must_fail git ls-files --error-unmatch foo &&
152 test_must_fail git ls-files --error-unmatch bar
467e1b53
JH
153'
154
155test_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
160'
161
162test_expect_success 'foo is different in index from HEAD -- rm should refuse' '
d492b31c 163 test_must_fail git rm foo baz &&
467e1b53
JH
164 test -f foo &&
165 test -f baz &&
166 git ls-files --error-unmatch foo baz
167'
168
169test_expect_success 'but with -f it should work.' '
170 git rm -f foo baz &&
171 test ! -f foo &&
172 test ! -f baz &&
d492b31c
SB
173 test_must_fail git ls-files --error-unmatch foo
174 test_must_fail git ls-files --error-unmatch baz
467e1b53
JH
175'
176
388b2acd
JH
177test_expect_success 'refuse to remove cached empty file with modifications' '
178 >empty &&
f55527f8
JK
179 git add empty &&
180 echo content >empty &&
181 test_must_fail git rm --cached empty
182'
183
184test_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
188'
189
467e1b53
JH
190test_expect_success 'Recursive test setup' '
191 mkdir -p frotz &&
192 echo qfwfq >frotz/nitfol &&
193 git add frotz &&
194 git commit -m "subdir test"
195'
196
197test_expect_success 'Recursive without -r fails' '
d492b31c 198 test_must_fail git rm frotz &&
467e1b53
JH
199 test -d frotz &&
200 test -f frotz/nitfol
201'
202
203test_expect_success 'Recursive with -r but dirty' '
204 echo qfwfq >>frotz/nitfol
d492b31c 205 test_must_fail git rm -r frotz &&
467e1b53
JH
206 test -d frotz &&
207 test -f frotz/nitfol
208'
209
210test_expect_success 'Recursive with -r -f' '
211 git rm -f -r frotz &&
212 ! test -f frotz/nitfol &&
213 ! test -d frotz
214'
215
41ac414e 216test_expect_success 'Remove nonexistent file returns nonzero exit status' '
d492b31c 217 test_must_fail git rm nonexistent
b48caa20
SG
218'
219
4d264672
OM
220test_expect_success 'Call "rm" from outside the work tree' '
221 mkdir repo &&
cced48a8
JS
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)
230'
231
232test_expect_success 'refresh index before checking if it is up-to-date' '
233
234 git reset --hard &&
235 test-chmtime -86400 frotz/nitfol &&
236 git rm frotz/nitfol &&
237 test ! -f frotz/nitfol
238
4d264672
OM
239'
240
0693f9dd
JH
241test_expect_success 'choking "git rm" should not let it die with cruft' '
242 git reset -q --hard &&
243 H=0000000000000000000000000000000000000000 &&
244 i=0 &&
245 while test $i -lt 12000
246 do
247 echo "100644 $H 0 some-file-$i"
248 i=$(( $i + 1 ))
249 done | git update-index --index-info &&
250 git rm -n "some-file-*" | :;
251 test -f .git/index.lock
252 status=$?
253 rm -f .git/index.lock
254 git reset -q --hard
255 test "$status" != 0
256'
257
3fc0d131
JK
258test_expect_success 'rm removes subdirectories recursively' '
259 mkdir -p dir/subdir/subsubdir &&
260 echo content >dir/subdir/subsubdir/file &&
261 git add dir/subdir/subsubdir/file &&
262 git rm -f dir/subdir/subsubdir/file &&
263 ! test -d dir
264'
265
d4a1cab5 266test_done