]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3600-rm.sh
Merge branch 'bw/format-patch-o-create-leading-dirs'
[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
b5368c23
RA
11test_expect_success 'Initialize test directory' '
12 touch -- foo bar baz "space embedded" -q &&
13 git add -- foo bar baz "space embedded" -q &&
14 git commit -m "add normal files"
15'
56e78bfb 16
b5368c23
RA
17if test_have_prereq !FUNNYNAMES
18then
56e78bfb
JS
19 say 'Your filesystem does not allow tabs in filenames.'
20fi
21
b5368c23
RA
22test_expect_success FUNNYNAMES 'add files with funny names' '
23 touch -- "tab embedded" "newline${LF}embedded" &&
24 git add -- "tab embedded" "newline${LF}embedded" &&
25 git commit -m "add files with tabs and newlines"
26'
27
28test_expect_success 'Pre-check that foo exists and is in index before git rm foo' '
59a06e94 29 test_path_is_file foo &&
b5368c23
RA
30 git ls-files --error-unmatch foo
31'
32
33test_expect_success 'Test that git rm foo succeeds' '
34 git rm --cached foo
35'
36
37test_expect_success 'Test that git rm --cached foo succeeds if the index matches the file' '
38 echo content >foo &&
39 git add foo &&
40 git rm --cached foo
41'
42
43test_expect_success 'Test that git rm --cached foo succeeds if the index matches the file' '
44 echo content >foo &&
45 git add foo &&
46 git commit -m foo &&
47 echo "other content" >foo &&
48 git rm --cached foo
49'
50
51test_expect_success 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' '
52 echo content >foo &&
53 git add foo &&
54 git commit -m foo --allow-empty &&
55 echo "other content" >foo &&
56 git add foo &&
57 echo "yet another content" >foo &&
58 test_must_fail git rm --cached foo
59'
60
61test_expect_success 'Test that git rm --cached -f foo works in case where --cached only did not' '
62 echo content >foo &&
63 git add foo &&
64 git commit -m foo --allow-empty &&
65 echo "other content" >foo &&
66 git add foo &&
67 echo "yet another content" >foo &&
68 git rm --cached -f foo
69'
70
71test_expect_success 'Post-check that foo exists but is not in index after git rm foo' '
59a06e94 72 test_path_is_file foo &&
b5368c23
RA
73 test_must_fail git ls-files --error-unmatch foo
74'
75
76test_expect_success 'Pre-check that bar exists and is in index before "git rm bar"' '
59a06e94 77 test_path_is_file bar &&
b5368c23
RA
78 git ls-files --error-unmatch bar
79'
80
81test_expect_success 'Test that "git rm bar" succeeds' '
82 git rm bar
83'
84
85test_expect_success 'Post-check that bar does not exist and is not in index after "git rm -f bar"' '
59a06e94 86 test_path_is_missing bar &&
b5368c23
RA
87 test_must_fail git ls-files --error-unmatch bar
88'
89
90test_expect_success 'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' '
91 git rm -- -q
92'
93
94test_expect_success FUNNYNAMES 'Test that "git rm -f" succeeds with embedded space, tab, or newline characters.' '
95 git rm -f "space embedded" "tab embedded" "newline${LF}embedded"
96'
3844cdc8 97
fbbfc8a9 98test_expect_success SANITY 'Test that "git rm -f" fails if its rm fails' '
23739aa2 99 test_when_finished "chmod 775 ." &&
56e78bfb 100 chmod a-w . &&
23739aa2 101 test_must_fail git rm -f baz
56e78bfb 102'
3844cdc8 103
b5368c23
RA
104test_expect_success 'When the rm in "git rm -f" fails, it should not remove the file from the index' '
105 git ls-files --error-unmatch baz
106'
3844cdc8 107
bb1faf0d
SG
108test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
109 git rm --ignore-unmatch nonexistent
110'
111
b48caa20 112test_expect_success '"rm" command printed' '
9e189f1a 113 echo frotz >test-file &&
b48caa20
SG
114 git add test-file &&
115 git commit -m "add file for rm test" &&
9e189f1a 116 git rm test-file >rm-output &&
e3ab3bc2 117 test $(grep "^rm " rm-output | wc -l) = 1 &&
b48caa20
SG
118 rm -f test-file rm-output &&
119 git commit -m "remove file from rm test"
120'
121
122test_expect_success '"rm" command suppressed with --quiet' '
9e189f1a 123 echo frotz >test-file &&
b48caa20
SG
124 git add test-file &&
125 git commit -m "add file for rm --quiet test" &&
9e189f1a
SB
126 git rm --quiet test-file >rm-output &&
127 test_must_be_empty rm-output &&
b48caa20
SG
128 rm -f test-file rm-output &&
129 git commit -m "remove file from rm --quiet test"
130'
131
467e1b53
JH
132# Now, failure cases.
133test_expect_success 'Re-add foo and baz' '
134 git add foo baz &&
135 git ls-files --error-unmatch foo baz
136'
137
138test_expect_success 'Modify foo -- rm should refuse' '
139 echo >>foo &&
d492b31c 140 test_must_fail git rm foo baz &&
59a06e94
RA
141 test_path_is_file foo &&
142 test_path_is_file baz &&
467e1b53
JH
143 git ls-files --error-unmatch foo baz
144'
145
146test_expect_success 'Modified foo -- rm -f should work' '
147 git rm -f foo baz &&
59a06e94
RA
148 test_path_is_missing foo &&
149 test_path_is_missing baz &&
d492b31c
SB
150 test_must_fail git ls-files --error-unmatch foo &&
151 test_must_fail git ls-files --error-unmatch bar
467e1b53
JH
152'
153
154test_expect_success 'Re-add foo and baz for HEAD tests' '
155 echo frotz >foo &&
156 git checkout HEAD -- baz &&
157 git add foo baz &&
158 git ls-files --error-unmatch foo baz
159'
160
161test_expect_success 'foo is different in index from HEAD -- rm should refuse' '
d492b31c 162 test_must_fail git rm foo baz &&
59a06e94
RA
163 test_path_is_file foo &&
164 test_path_is_file baz &&
467e1b53
JH
165 git ls-files --error-unmatch foo baz
166'
167
168test_expect_success 'but with -f it should work.' '
169 git rm -f foo baz &&
59a06e94
RA
170 test_path_is_missing foo &&
171 test_path_is_missing baz &&
8fb26872 172 test_must_fail git ls-files --error-unmatch foo &&
d492b31c 173 test_must_fail git ls-files --error-unmatch baz
467e1b53
JH
174'
175
388b2acd
JH
176test_expect_success 'refuse to remove cached empty file with modifications' '
177 >empty &&
f55527f8
JK
178 git add empty &&
179 echo content >empty &&
180 test_must_fail git rm --cached empty
181'
182
183test_expect_success 'remove intent-to-add file without --force' '
184 echo content >intent-to-add &&
99094a7a 185 git add -N intent-to-add &&
f55527f8
JK
186 git rm --cached intent-to-add
187'
188
467e1b53
JH
189test_expect_success 'Recursive test setup' '
190 mkdir -p frotz &&
191 echo qfwfq >frotz/nitfol &&
192 git add frotz &&
193 git commit -m "subdir test"
194'
195
196test_expect_success 'Recursive without -r fails' '
d492b31c 197 test_must_fail git rm frotz &&
59a06e94
RA
198 test_path_is_dir frotz &&
199 test_path_is_file frotz/nitfol
467e1b53
JH
200'
201
202test_expect_success 'Recursive with -r but dirty' '
99094a7a 203 echo qfwfq >>frotz/nitfol &&
d492b31c 204 test_must_fail git rm -r frotz &&
59a06e94
RA
205 test_path_is_dir frotz &&
206 test_path_is_file frotz/nitfol
467e1b53
JH
207'
208
209test_expect_success 'Recursive with -r -f' '
210 git rm -f -r frotz &&
59a06e94
RA
211 test_path_is_missing frotz/nitfol &&
212 test_path_is_missing frotz
467e1b53
JH
213'
214
41ac414e 215test_expect_success 'Remove nonexistent file returns nonzero exit status' '
d492b31c 216 test_must_fail git rm nonexistent
b48caa20
SG
217'
218
4d264672
OM
219test_expect_success 'Call "rm" from outside the work tree' '
220 mkdir repo &&
b5368c23
RA
221 (
222 cd repo &&
223 git init &&
224 echo something >somefile &&
225 git add somefile &&
226 git commit -m "add a file" &&
227 (
228 cd .. &&
229 git --git-dir=repo/.git --work-tree=repo rm somefile
230 ) &&
231 test_must_fail git ls-files --error-unmatch somefile
232 )
cced48a8
JS
233'
234
235test_expect_success 'refresh index before checking if it is up-to-date' '
cced48a8 236 git reset --hard &&
0e496492 237 test-tool chmtime -86400 frotz/nitfol &&
cced48a8 238 git rm frotz/nitfol &&
59a06e94 239 test_path_is_missing frotz/nitfol
4d264672
OM
240'
241
0693f9dd 242test_expect_success 'choking "git rm" should not let it die with cruft' '
ca6ba942 243 test_oid_init &&
0693f9dd 244 git reset -q --hard &&
056f34bb 245 test_when_finished "rm -f .git/index.lock && git reset -q --hard" &&
0693f9dd 246 i=0 &&
ca6ba942 247 hash=$(test_oid deadbeef) &&
0693f9dd
JH
248 while test $i -lt 12000
249 do
ca6ba942 250 echo "100644 $hash 0 some-file-$i"
b5368c23 251 i=$(( $i + 1 ))
0693f9dd 252 done | git update-index --index-info &&
056f34bb
SG
253 git rm -n "some-file-*" | : &&
254 test_path_is_missing .git/index.lock
0693f9dd
JH
255'
256
b2b1f615
JH
257test_expect_success 'Resolving by removal is not a warning-worthy event' '
258 git reset -q --hard &&
259 test_when_finished "rm -f .git/index.lock msg && git reset -q --hard" &&
260 blob=$(echo blob | git hash-object -w --stdin) &&
261 for stage in 1 2 3
262 do
263 echo "100644 $blob $stage blob"
264 done | git update-index --index-info &&
265 git rm blob >msg 2>&1 &&
266 test_i18ngrep ! "needs merge" msg &&
267 test_must_fail git ls-files -s --error-unmatch blob
268'
269
3fc0d131
JK
270test_expect_success 'rm removes subdirectories recursively' '
271 mkdir -p dir/subdir/subsubdir &&
272 echo content >dir/subdir/subsubdir/file &&
273 git add dir/subdir/subsubdir/file &&
274 git rm -f dir/subdir/subsubdir/file &&
59a06e94 275 test_path_is_missing dir
3fc0d131
JK
276'
277
293ab15e 278cat >expect <<EOF
95c16418 279M .gitmodules
293ab15e
JL
280D submod
281EOF
282
283cat >expect.modified <<EOF
284 M submod
285EOF
286
dd6962dd
SB
287cat >expect.modified_inside <<EOF
288 m submod
289EOF
290
291cat >expect.modified_untracked <<EOF
292 ? submod
293EOF
294
95c16418
JL
295cat >expect.cached <<EOF
296D submod
297EOF
298
299cat >expect.both_deleted<<EOF
300D .gitmodules
301D submod
302EOF
303
293ab15e
JL
304test_expect_success 'rm removes empty submodules from work tree' '
305 mkdir submod &&
306 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) submod &&
307 git config -f .gitmodules submodule.sub.url ./. &&
308 git config -f .gitmodules submodule.sub.path submod &&
309 git submodule init &&
310 git add .gitmodules &&
311 git commit -m "add submodule" &&
312 git rm submod &&
59a06e94 313 test_path_is_missing submod &&
9e189f1a 314 git status -s -uno --ignore-submodules=none >actual &&
95c16418
JL
315 test_cmp expect actual &&
316 test_must_fail git config -f .gitmodules submodule.sub.url &&
317 test_must_fail git config -f .gitmodules submodule.sub.path
293ab15e
JL
318'
319
95c16418 320test_expect_success 'rm removes removed submodule from index and .gitmodules' '
293ab15e
JL
321 git reset --hard &&
322 git submodule update &&
323 rm -rf submod &&
324 git rm submod &&
9e189f1a 325 git status -s -uno --ignore-submodules=none >actual &&
95c16418
JL
326 test_cmp expect actual &&
327 test_must_fail git config -f .gitmodules submodule.sub.url &&
328 test_must_fail git config -f .gitmodules submodule.sub.path
293ab15e
JL
329'
330
331test_expect_success 'rm removes work tree of unmodified submodules' '
332 git reset --hard &&
333 git submodule update &&
334 git rm submod &&
59a06e94 335 test_path_is_missing submod &&
9e189f1a 336 git status -s -uno --ignore-submodules=none >actual &&
95c16418
JL
337 test_cmp expect actual &&
338 test_must_fail git config -f .gitmodules submodule.sub.url &&
339 test_must_fail git config -f .gitmodules submodule.sub.path
293ab15e
JL
340'
341
53e4c5dc
JL
342test_expect_success 'rm removes a submodule with a trailing /' '
343 git reset --hard &&
344 git submodule update &&
345 git rm submod/ &&
59a06e94 346 test_path_is_missing submod &&
9e189f1a 347 git status -s -uno --ignore-submodules=none >actual &&
53e4c5dc
JL
348 test_cmp expect actual
349'
350
351test_expect_success 'rm fails when given a file with a trailing /' '
352 test_must_fail git rm empty/
353'
354
355test_expect_success 'rm succeeds when given a directory with a trailing /' '
356 git rm -r frotz/
357'
358
293ab15e
JL
359test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '
360 git reset --hard &&
361 git submodule update &&
9e189f1a 362 git -C submod checkout HEAD^ &&
293ab15e 363 test_must_fail git rm submod &&
59a06e94
RA
364 test_path_is_dir submod &&
365 test_path_is_file submod/.git &&
9e189f1a 366 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
367 test_cmp expect.modified actual &&
368 git rm -f submod &&
59a06e94 369 test_path_is_missing submod &&
9e189f1a 370 git status -s -uno --ignore-submodules=none >actual &&
95c16418
JL
371 test_cmp expect actual &&
372 test_must_fail git config -f .gitmodules submodule.sub.url &&
373 test_must_fail git config -f .gitmodules submodule.sub.path
374'
375
376test_expect_success 'rm --cached leaves work tree of populated submodules and .gitmodules alone' '
377 git reset --hard &&
378 git submodule update &&
379 git rm --cached submod &&
59a06e94
RA
380 test_path_is_dir submod &&
381 test_path_is_file submod/.git &&
95c16418
JL
382 git status -s -uno >actual &&
383 test_cmp expect.cached actual &&
384 git config -f .gitmodules submodule.sub.url &&
385 git config -f .gitmodules submodule.sub.path
386'
387
388test_expect_success 'rm --dry-run does not touch the submodule or .gitmodules' '
389 git reset --hard &&
390 git submodule update &&
391 git rm -n submod &&
59a06e94 392 test_path_is_file submod/.git &&
95c16418
JL
393 git diff-index --exit-code HEAD
394'
395
396test_expect_success 'rm does not complain when no .gitmodules file is found' '
397 git reset --hard &&
398 git submodule update &&
399 git rm .gitmodules &&
400 git rm submod >actual 2>actual.err &&
ec10b018 401 test_must_be_empty actual.err &&
59a06e94
RA
402 test_path_is_missing submod &&
403 test_path_is_missing submod/.git &&
95c16418
JL
404 git status -s -uno >actual &&
405 test_cmp expect.both_deleted actual
406'
407
408test_expect_success 'rm will error out on a modified .gitmodules file unless staged' '
409 git reset --hard &&
410 git submodule update &&
411 git config -f .gitmodules foo.bar true &&
412 test_must_fail git rm submod >actual 2>actual.err &&
59a06e94
RA
413 test_file_not_empty actual.err &&
414 test_path_is_dir submod &&
415 test_path_is_file submod/.git &&
95c16418
JL
416 git diff-files --quiet -- submod &&
417 git add .gitmodules &&
418 git rm submod >actual 2>actual.err &&
ec10b018 419 test_must_be_empty actual.err &&
59a06e94
RA
420 test_path_is_missing submod &&
421 test_path_is_missing submod/.git &&
95c16418
JL
422 git status -s -uno >actual &&
423 test_cmp expect actual
424'
425
426test_expect_success 'rm issues a warning when section is not found in .gitmodules' '
427 git reset --hard &&
428 git submodule update &&
429 git config -f .gitmodules --remove-section submodule.sub &&
430 git add .gitmodules &&
431 echo "warning: Could not find section in .gitmodules where path=submod" >expect.err &&
432 git rm submod >actual 2>actual.err &&
433 test_i18ncmp expect.err actual.err &&
59a06e94
RA
434 test_path_is_missing submod &&
435 test_path_is_missing submod/.git &&
95c16418 436 git status -s -uno >actual &&
293ab15e
JL
437 test_cmp expect actual
438'
439
440test_expect_success 'rm of a populated submodule with modifications fails unless forced' '
441 git reset --hard &&
442 git submodule update &&
9e189f1a 443 echo X >submod/empty &&
293ab15e 444 test_must_fail git rm submod &&
59a06e94
RA
445 test_path_is_dir submod &&
446 test_path_is_file submod/.git &&
9e189f1a 447 git status -s -uno --ignore-submodules=none >actual &&
dd6962dd 448 test_cmp expect.modified_inside actual &&
293ab15e 449 git rm -f submod &&
59a06e94 450 test_path_is_missing submod &&
9e189f1a 451 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
452 test_cmp expect actual
453'
454
455test_expect_success 'rm of a populated submodule with untracked files fails unless forced' '
456 git reset --hard &&
457 git submodule update &&
9e189f1a 458 echo X >submod/untracked &&
293ab15e 459 test_must_fail git rm submod &&
59a06e94
RA
460 test_path_is_dir submod &&
461 test_path_is_file submod/.git &&
9e189f1a 462 git status -s -uno --ignore-submodules=none >actual &&
dd6962dd 463 test_cmp expect.modified_untracked actual &&
293ab15e 464 git rm -f submod &&
59a06e94 465 test_path_is_missing submod &&
9e189f1a 466 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
467 test_cmp expect actual
468'
469
470test_expect_success 'setup submodule conflict' '
471 git reset --hard &&
472 git submodule update &&
473 git checkout -b branch1 &&
474 echo 1 >nitfol &&
475 git add nitfol &&
476 git commit -m "added nitfol 1" &&
477 git checkout -b branch2 master &&
478 echo 2 >nitfol &&
479 git add nitfol &&
480 git commit -m "added nitfol 2" &&
481 git checkout -b conflict1 master &&
9e189f1a
SB
482 git -C submod fetch &&
483 git -C submod checkout branch1 &&
293ab15e
JL
484 git add submod &&
485 git commit -m "submod 1" &&
486 git checkout -b conflict2 master &&
9e189f1a 487 git -C submod checkout branch2 &&
293ab15e
JL
488 git add submod &&
489 git commit -m "submod 2"
490'
491
492cat >expect.conflict <<EOF
493UU submod
494EOF
495
496test_expect_success 'rm removes work tree of unmodified conflicted submodule' '
497 git checkout conflict1 &&
498 git reset --hard &&
499 git submodule update &&
500 test_must_fail git merge conflict2 &&
501 git rm submod &&
59a06e94 502 test_path_is_missing submod &&
9e189f1a 503 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
504 test_cmp expect actual
505'
506
507test_expect_success 'rm of a conflicted populated submodule with different HEAD fails unless forced' '
508 git checkout conflict1 &&
509 git reset --hard &&
510 git submodule update &&
9e189f1a 511 git -C submod checkout HEAD^ &&
293ab15e
JL
512 test_must_fail git merge conflict2 &&
513 test_must_fail git rm submod &&
59a06e94
RA
514 test_path_is_dir submod &&
515 test_path_is_file submod/.git &&
9e189f1a 516 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
517 test_cmp expect.conflict actual &&
518 git rm -f submod &&
59a06e94 519 test_path_is_missing submod &&
9e189f1a 520 git status -s -uno --ignore-submodules=none >actual &&
95c16418
JL
521 test_cmp expect actual &&
522 test_must_fail git config -f .gitmodules submodule.sub.url &&
523 test_must_fail git config -f .gitmodules submodule.sub.path
293ab15e
JL
524'
525
526test_expect_success 'rm of a conflicted populated submodule with modifications fails unless forced' '
527 git checkout conflict1 &&
528 git reset --hard &&
529 git submodule update &&
9e189f1a 530 echo X >submod/empty &&
293ab15e
JL
531 test_must_fail git merge conflict2 &&
532 test_must_fail git rm submod &&
59a06e94
RA
533 test_path_is_dir submod &&
534 test_path_is_file submod/.git &&
9e189f1a 535 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
536 test_cmp expect.conflict actual &&
537 git rm -f submod &&
59a06e94 538 test_path_is_missing submod &&
9e189f1a 539 git status -s -uno --ignore-submodules=none >actual &&
95c16418
JL
540 test_cmp expect actual &&
541 test_must_fail git config -f .gitmodules submodule.sub.url &&
542 test_must_fail git config -f .gitmodules submodule.sub.path
293ab15e
JL
543'
544
545test_expect_success 'rm of a conflicted populated submodule with untracked files fails unless forced' '
546 git checkout conflict1 &&
547 git reset --hard &&
548 git submodule update &&
9e189f1a 549 echo X >submod/untracked &&
293ab15e
JL
550 test_must_fail git merge conflict2 &&
551 test_must_fail git rm submod &&
59a06e94
RA
552 test_path_is_dir submod &&
553 test_path_is_file submod/.git &&
9e189f1a 554 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
555 test_cmp expect.conflict actual &&
556 git rm -f submod &&
59a06e94 557 test_path_is_missing submod &&
9e189f1a 558 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
559 test_cmp expect actual
560'
561
562test_expect_success 'rm of a conflicted populated submodule with a .git directory fails even when forced' '
563 git checkout conflict1 &&
564 git reset --hard &&
565 git submodule update &&
b5368c23
RA
566 (
567 cd submod &&
293ab15e 568 rm .git &&
2d3ac9ad 569 cp -R ../.git/modules/sub .git &&
293ab15e
JL
570 GIT_WORK_TREE=. git config --unset core.worktree
571 ) &&
572 test_must_fail git merge conflict2 &&
573 test_must_fail git rm submod &&
59a06e94
RA
574 test_path_is_dir submod &&
575 test_path_is_dir submod/.git &&
9e189f1a 576 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
577 test_cmp expect.conflict actual &&
578 test_must_fail git rm -f submod &&
59a06e94
RA
579 test_path_is_dir submod &&
580 test_path_is_dir submod/.git &&
9e189f1a 581 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
582 test_cmp expect.conflict actual &&
583 git merge --abort &&
584 rm -rf submod
585'
586
587test_expect_success 'rm of a conflicted unpopulated submodule succeeds' '
588 git checkout conflict1 &&
589 git reset --hard &&
590 test_must_fail git merge conflict2 &&
591 git rm submod &&
59a06e94 592 test_path_is_missing submod &&
9e189f1a 593 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
594 test_cmp expect actual
595'
596
55856a35 597test_expect_success 'rm of a populated submodule with a .git directory migrates git dir' '
293ab15e
JL
598 git checkout -f master &&
599 git reset --hard &&
600 git submodule update &&
b5368c23
RA
601 (
602 cd submod &&
293ab15e 603 rm .git &&
2d3ac9ad 604 cp -R ../.git/modules/sub .git &&
55856a35
SB
605 GIT_WORK_TREE=. git config --unset core.worktree &&
606 rm -r ../.git/modules/sub
293ab15e 607 ) &&
55856a35 608 git rm submod 2>output.err &&
59a06e94
RA
609 test_path_is_missing submod &&
610 test_path_is_missing submod/.git &&
9e189f1a 611 git status -s -uno --ignore-submodules=none >actual &&
59a06e94 612 test_file_not_empty actual &&
55856a35 613 test_i18ngrep Migrating output.err
293ab15e
JL
614'
615
616cat >expect.deepmodified <<EOF
617 M submod/subsubmod
618EOF
619
620test_expect_success 'setup subsubmodule' '
621 git reset --hard &&
622 git submodule update &&
b5368c23
RA
623 (
624 cd submod &&
293ab15e
JL
625 git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) subsubmod &&
626 git config -f .gitmodules submodule.sub.url ../. &&
627 git config -f .gitmodules submodule.sub.path subsubmod &&
628 git submodule init &&
629 git add .gitmodules &&
630 git commit -m "add subsubmodule" &&
631 git submodule update subsubmod
632 ) &&
633 git commit -a -m "added deep submodule"
634'
635
636test_expect_success 'rm recursively removes work tree of unmodified submodules' '
637 git rm submod &&
59a06e94 638 test_path_is_missing submod &&
9e189f1a 639 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
640 test_cmp expect actual
641'
642
643test_expect_success 'rm of a populated nested submodule with different nested HEAD fails unless forced' '
644 git reset --hard &&
645 git submodule update --recursive &&
9e189f1a 646 git -C submod/subsubmod checkout HEAD^ &&
293ab15e 647 test_must_fail git rm submod &&
59a06e94
RA
648 test_path_is_dir submod &&
649 test_path_is_file submod/.git &&
9e189f1a 650 git status -s -uno --ignore-submodules=none >actual &&
dd6962dd 651 test_cmp expect.modified_inside actual &&
293ab15e 652 git rm -f submod &&
59a06e94 653 test_path_is_missing submod &&
9e189f1a 654 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
655 test_cmp expect actual
656'
657
658test_expect_success 'rm of a populated nested submodule with nested modifications fails unless forced' '
659 git reset --hard &&
660 git submodule update --recursive &&
9e189f1a 661 echo X >submod/subsubmod/empty &&
293ab15e 662 test_must_fail git rm submod &&
59a06e94
RA
663 test_path_is_dir submod &&
664 test_path_is_file submod/.git &&
9e189f1a 665 git status -s -uno --ignore-submodules=none >actual &&
dd6962dd 666 test_cmp expect.modified_inside actual &&
293ab15e 667 git rm -f submod &&
59a06e94 668 test_path_is_missing submod &&
9e189f1a 669 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
670 test_cmp expect actual
671'
672
673test_expect_success 'rm of a populated nested submodule with nested untracked files fails unless forced' '
674 git reset --hard &&
675 git submodule update --recursive &&
9e189f1a 676 echo X >submod/subsubmod/untracked &&
293ab15e 677 test_must_fail git rm submod &&
59a06e94
RA
678 test_path_is_dir submod &&
679 test_path_is_file submod/.git &&
9e189f1a 680 git status -s -uno --ignore-submodules=none >actual &&
40069d6e 681 test_cmp expect.modified_untracked actual &&
293ab15e 682 git rm -f submod &&
59a06e94 683 test_path_is_missing submod &&
9e189f1a 684 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
685 test_cmp expect actual
686'
687
70471ed9 688test_expect_success "rm absorbs submodule's nested .git directory" '
293ab15e
JL
689 git reset --hard &&
690 git submodule update --recursive &&
b5368c23
RA
691 (
692 cd submod/subsubmod &&
293ab15e 693 rm .git &&
55856a35 694 mv ../../.git/modules/sub/modules/sub .git &&
293ab15e
JL
695 GIT_WORK_TREE=. git config --unset core.worktree
696 ) &&
55856a35 697 git rm submod 2>output.err &&
59a06e94
RA
698 test_path_is_missing submod &&
699 test_path_is_missing submod/subsubmod/.git &&
9e189f1a 700 git status -s -uno --ignore-submodules=none >actual &&
59a06e94 701 test_file_not_empty actual &&
55856a35 702 test_i18ngrep Migrating output.err
293ab15e
JL
703'
704
bbad9f93 705test_expect_success 'checking out a commit after submodule removal needs manual updates' '
55856a35 706 git commit -m "submodule removal" submod .gitmodules &&
bbad9f93
JL
707 git checkout HEAD^ &&
708 git submodule update &&
8954bd76 709 git checkout -q HEAD^ &&
bbad9f93 710 git checkout -q master 2>actual &&
0a288d1e 711 test_i18ngrep "^warning: unable to rmdir '\''submod'\'':" actual &&
bbad9f93
JL
712 git status -s submod >actual &&
713 echo "?? submod/" >expected &&
714 test_cmp expected actual &&
715 rm -rf submod &&
9e189f1a 716 git status -s -uno --ignore-submodules=none >actual &&
ec10b018 717 test_must_be_empty actual
bbad9f93
JL
718'
719
9a6728d4
JK
720test_expect_success 'rm of d/f when d has become a non-directory' '
721 rm -rf d &&
722 mkdir d &&
723 >d/f &&
724 git add d &&
725 rm -rf d &&
726 >d &&
727 git rm d/f &&
728 test_must_fail git rev-parse --verify :d/f &&
729 test_path_is_file d
730'
731
732test_expect_success SYMLINKS 'rm of d/f when d has become a dangling symlink' '
733 rm -rf d &&
734 mkdir d &&
735 >d/f &&
736 git add d &&
737 rm -rf d &&
738 ln -s nonexistent d &&
739 git rm d/f &&
740 test_must_fail git rev-parse --verify :d/f &&
741 test -h d &&
742 test_path_is_missing d
743'
744
96ec8ee9
JK
745test_expect_success 'rm of file when it has become a directory' '
746 rm -rf d &&
747 >d &&
748 git add d &&
749 rm -f d &&
750 mkdir d &&
751 >d/f &&
752 test_must_fail git rm d &&
753 git rev-parse --verify :d &&
754 test_path_is_file d/f
755'
756
03415ca8
JK
757test_expect_success SYMLINKS 'rm across a symlinked leading path (no index)' '
758 rm -rf d e &&
759 mkdir e &&
760 echo content >e/f &&
761 ln -s e d &&
762 git add -A e d &&
763 git commit -m "symlink d to e, e/f exists" &&
764 test_must_fail git rm d/f &&
765 git rev-parse --verify :d &&
766 git rev-parse --verify :e/f &&
767 test -h d &&
768 test_path_is_file e/f
769'
770
771test_expect_failure SYMLINKS 'rm across a symlinked leading path (w/ index)' '
772 rm -rf d e &&
773 mkdir d &&
774 echo content >d/f &&
775 git add -A e d &&
776 git commit -m "d/f exists" &&
777 mv d e &&
778 ln -s e d &&
779 test_must_fail git rm d/f &&
780 git rev-parse --verify :d/f &&
781 test -h d &&
782 test_path_is_file e/f
783'
784
914dc028
MLM
785test_expect_success 'setup for testing rm messages' '
786 >bar.txt &&
787 >foo.txt &&
788 git add bar.txt foo.txt
789'
790
791test_expect_success 'rm files with different staged content' '
792 cat >expect <<-\EOF &&
793 error: the following files have staged content different from both the
794 file and the HEAD:
795 bar.txt
796 foo.txt
797 (use -f to force removal)
798 EOF
799 echo content1 >foo.txt &&
800 echo content1 >bar.txt &&
801 test_must_fail git rm foo.txt bar.txt 2>actual &&
802 test_i18ncmp expect actual
803'
804
7e309446
MLM
805test_expect_success 'rm files with different staged content without hints' '
806 cat >expect <<-\EOF &&
807 error: the following files have staged content different from both the
808 file and the HEAD:
809 bar.txt
810 foo.txt
811 EOF
812 echo content2 >foo.txt &&
813 echo content2 >bar.txt &&
814 test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual &&
815 test_i18ncmp expect actual
816'
914dc028
MLM
817
818test_expect_success 'rm file with local modification' '
819 cat >expect <<-\EOF &&
820 error: the following file has local modifications:
821 foo.txt
822 (use --cached to keep the file, or -f to force removal)
823 EOF
824 git commit -m "testing rm 3" &&
825 echo content3 >foo.txt &&
826 test_must_fail git rm foo.txt 2>actual &&
827 test_i18ncmp expect actual
828'
829
7e309446
MLM
830test_expect_success 'rm file with local modification without hints' '
831 cat >expect <<-\EOF &&
832 error: the following file has local modifications:
833 bar.txt
834 EOF
835 echo content4 >bar.txt &&
836 test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual &&
837 test_i18ncmp expect actual
838'
914dc028
MLM
839
840test_expect_success 'rm file with changes in the index' '
841 cat >expect <<-\EOF &&
842 error: the following file has changes staged in the index:
843 foo.txt
844 (use --cached to keep the file, or -f to force removal)
845 EOF
846 git reset --hard &&
847 echo content5 >foo.txt &&
848 git add foo.txt &&
849 test_must_fail git rm foo.txt 2>actual &&
850 test_i18ncmp expect actual
851'
852
7e309446
MLM
853test_expect_success 'rm file with changes in the index without hints' '
854 cat >expect <<-\EOF &&
855 error: the following file has changes staged in the index:
856 foo.txt
857 EOF
858 test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual &&
859 test_i18ncmp expect actual
860'
914dc028
MLM
861
862test_expect_success 'rm files with two different errors' '
863 cat >expect <<-\EOF &&
864 error: the following file has staged content different from both the
865 file and the HEAD:
866 foo1.txt
867 (use -f to force removal)
868 error: the following file has changes staged in the index:
869 bar1.txt
870 (use --cached to keep the file, or -f to force removal)
871 EOF
872 echo content >foo1.txt &&
873 git add foo1.txt &&
874 echo content6 >foo1.txt &&
875 echo content6 >bar1.txt &&
876 git add bar1.txt &&
877 test_must_fail git rm bar1.txt foo1.txt 2>actual &&
878 test_i18ncmp expect actual
879'
880
9e4e8a64
EX
881test_expect_success 'rm empty string should fail' '
882 test_must_fail git rm -rf ""
d426430e
EX
883'
884
d4a1cab5 885test_done