]> git.ipfire.org Git - thirdparty/git.git/blame - t/t3600-rm.sh
Merge branch 'dd/t1509-i18n-fix'
[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" &&
0d913dfa
DL
116 git rm test-file >rm-output.raw &&
117 grep "^rm " rm-output.raw >rm-output &&
118 test_line_count = 1 rm-output &&
119 rm -f test-file rm-output.raw rm-output &&
b48caa20
SG
120 git commit -m "remove file from rm test"
121'
122
123test_expect_success '"rm" command suppressed with --quiet' '
9e189f1a 124 echo frotz >test-file &&
b48caa20
SG
125 git add test-file &&
126 git commit -m "add file for rm --quiet test" &&
9e189f1a
SB
127 git rm --quiet test-file >rm-output &&
128 test_must_be_empty rm-output &&
b48caa20
SG
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 &&
59a06e94
RA
142 test_path_is_file foo &&
143 test_path_is_file baz &&
467e1b53
JH
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 &&
59a06e94
RA
149 test_path_is_missing foo &&
150 test_path_is_missing 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 &&
59a06e94
RA
164 test_path_is_file foo &&
165 test_path_is_file baz &&
467e1b53
JH
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 &&
59a06e94
RA
171 test_path_is_missing foo &&
172 test_path_is_missing baz &&
8fb26872 173 test_must_fail git ls-files --error-unmatch foo &&
d492b31c 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 &&
99094a7a 186 git add -N intent-to-add &&
f55527f8
JK
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 &&
59a06e94
RA
199 test_path_is_dir frotz &&
200 test_path_is_file frotz/nitfol
467e1b53
JH
201'
202
203test_expect_success 'Recursive with -r but dirty' '
99094a7a 204 echo qfwfq >>frotz/nitfol &&
d492b31c 205 test_must_fail git rm -r frotz &&
59a06e94
RA
206 test_path_is_dir frotz &&
207 test_path_is_file frotz/nitfol
467e1b53
JH
208'
209
210test_expect_success 'Recursive with -r -f' '
211 git rm -f -r frotz &&
59a06e94
RA
212 test_path_is_missing frotz/nitfol &&
213 test_path_is_missing frotz
467e1b53
JH
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 &&
b5368c23
RA
222 (
223 cd repo &&
224 git init &&
225 echo something >somefile &&
226 git add somefile &&
227 git commit -m "add a file" &&
228 (
229 cd .. &&
230 git --git-dir=repo/.git --work-tree=repo rm somefile
231 ) &&
232 test_must_fail git ls-files --error-unmatch somefile
233 )
cced48a8
JS
234'
235
236test_expect_success 'refresh index before checking if it is up-to-date' '
cced48a8 237 git reset --hard &&
0e496492 238 test-tool chmtime -86400 frotz/nitfol &&
cced48a8 239 git rm frotz/nitfol &&
59a06e94 240 test_path_is_missing frotz/nitfol
4d264672
OM
241'
242
0693f9dd 243test_expect_success 'choking "git rm" should not let it die with cruft' '
ca6ba942 244 test_oid_init &&
0693f9dd 245 git reset -q --hard &&
056f34bb 246 test_when_finished "rm -f .git/index.lock && git reset -q --hard" &&
0693f9dd 247 i=0 &&
ca6ba942 248 hash=$(test_oid deadbeef) &&
0693f9dd
JH
249 while test $i -lt 12000
250 do
ca6ba942 251 echo "100644 $hash 0 some-file-$i"
b5368c23 252 i=$(( $i + 1 ))
0693f9dd 253 done | git update-index --index-info &&
50cd31c6 254 # git command is intentionally placed upstream of pipe to induce SIGPIPE
056f34bb
SG
255 git rm -n "some-file-*" | : &&
256 test_path_is_missing .git/index.lock
0693f9dd
JH
257'
258
b2b1f615
JH
259test_expect_success 'Resolving by removal is not a warning-worthy event' '
260 git reset -q --hard &&
261 test_when_finished "rm -f .git/index.lock msg && git reset -q --hard" &&
262 blob=$(echo blob | git hash-object -w --stdin) &&
263 for stage in 1 2 3
264 do
265 echo "100644 $blob $stage blob"
266 done | git update-index --index-info &&
267 git rm blob >msg 2>&1 &&
268 test_i18ngrep ! "needs merge" msg &&
269 test_must_fail git ls-files -s --error-unmatch blob
270'
271
3fc0d131
JK
272test_expect_success 'rm removes subdirectories recursively' '
273 mkdir -p dir/subdir/subsubdir &&
274 echo content >dir/subdir/subsubdir/file &&
275 git add dir/subdir/subsubdir/file &&
276 git rm -f dir/subdir/subsubdir/file &&
59a06e94 277 test_path_is_missing dir
3fc0d131
JK
278'
279
293ab15e 280cat >expect <<EOF
95c16418 281M .gitmodules
293ab15e
JL
282D submod
283EOF
284
285cat >expect.modified <<EOF
286 M submod
287EOF
288
dd6962dd
SB
289cat >expect.modified_inside <<EOF
290 m submod
291EOF
292
293cat >expect.modified_untracked <<EOF
294 ? submod
295EOF
296
95c16418
JL
297cat >expect.cached <<EOF
298D submod
299EOF
300
301cat >expect.both_deleted<<EOF
302D .gitmodules
303D submod
304EOF
305
293ab15e
JL
306test_expect_success 'rm removes empty submodules from work tree' '
307 mkdir submod &&
3b737381
DL
308 hash=$(git rev-parse HEAD) &&
309 git update-index --add --cacheinfo 160000 "$hash" submod &&
293ab15e
JL
310 git config -f .gitmodules submodule.sub.url ./. &&
311 git config -f .gitmodules submodule.sub.path submod &&
312 git submodule init &&
313 git add .gitmodules &&
314 git commit -m "add submodule" &&
315 git rm submod &&
59a06e94 316 test_path_is_missing submod &&
9e189f1a 317 git status -s -uno --ignore-submodules=none >actual &&
95c16418
JL
318 test_cmp expect actual &&
319 test_must_fail git config -f .gitmodules submodule.sub.url &&
320 test_must_fail git config -f .gitmodules submodule.sub.path
293ab15e
JL
321'
322
95c16418 323test_expect_success 'rm removes removed submodule from index and .gitmodules' '
293ab15e
JL
324 git reset --hard &&
325 git submodule update &&
326 rm -rf submod &&
327 git rm submod &&
9e189f1a 328 git status -s -uno --ignore-submodules=none >actual &&
95c16418
JL
329 test_cmp expect actual &&
330 test_must_fail git config -f .gitmodules submodule.sub.url &&
331 test_must_fail git config -f .gitmodules submodule.sub.path
293ab15e
JL
332'
333
334test_expect_success 'rm removes work tree of unmodified submodules' '
335 git reset --hard &&
336 git submodule update &&
337 git rm submod &&
59a06e94 338 test_path_is_missing submod &&
9e189f1a 339 git status -s -uno --ignore-submodules=none >actual &&
95c16418
JL
340 test_cmp expect actual &&
341 test_must_fail git config -f .gitmodules submodule.sub.url &&
342 test_must_fail git config -f .gitmodules submodule.sub.path
293ab15e
JL
343'
344
53e4c5dc
JL
345test_expect_success 'rm removes a submodule with a trailing /' '
346 git reset --hard &&
347 git submodule update &&
348 git rm submod/ &&
59a06e94 349 test_path_is_missing submod &&
9e189f1a 350 git status -s -uno --ignore-submodules=none >actual &&
53e4c5dc
JL
351 test_cmp expect actual
352'
353
354test_expect_success 'rm fails when given a file with a trailing /' '
355 test_must_fail git rm empty/
356'
357
358test_expect_success 'rm succeeds when given a directory with a trailing /' '
359 git rm -r frotz/
360'
361
293ab15e
JL
362test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '
363 git reset --hard &&
364 git submodule update &&
9e189f1a 365 git -C submod checkout HEAD^ &&
293ab15e 366 test_must_fail git rm submod &&
59a06e94
RA
367 test_path_is_dir submod &&
368 test_path_is_file submod/.git &&
9e189f1a 369 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
370 test_cmp expect.modified actual &&
371 git rm -f submod &&
59a06e94 372 test_path_is_missing submod &&
9e189f1a 373 git status -s -uno --ignore-submodules=none >actual &&
95c16418
JL
374 test_cmp expect actual &&
375 test_must_fail git config -f .gitmodules submodule.sub.url &&
376 test_must_fail git config -f .gitmodules submodule.sub.path
377'
378
379test_expect_success 'rm --cached leaves work tree of populated submodules and .gitmodules alone' '
380 git reset --hard &&
381 git submodule update &&
382 git rm --cached submod &&
59a06e94
RA
383 test_path_is_dir submod &&
384 test_path_is_file submod/.git &&
95c16418
JL
385 git status -s -uno >actual &&
386 test_cmp expect.cached actual &&
387 git config -f .gitmodules submodule.sub.url &&
388 git config -f .gitmodules submodule.sub.path
389'
390
391test_expect_success 'rm --dry-run does not touch the submodule or .gitmodules' '
392 git reset --hard &&
393 git submodule update &&
394 git rm -n submod &&
59a06e94 395 test_path_is_file submod/.git &&
95c16418
JL
396 git diff-index --exit-code HEAD
397'
398
399test_expect_success 'rm does not complain when no .gitmodules file is found' '
400 git reset --hard &&
401 git submodule update &&
402 git rm .gitmodules &&
403 git rm submod >actual 2>actual.err &&
ec10b018 404 test_must_be_empty actual.err &&
59a06e94
RA
405 test_path_is_missing submod &&
406 test_path_is_missing submod/.git &&
95c16418
JL
407 git status -s -uno >actual &&
408 test_cmp expect.both_deleted actual
409'
410
411test_expect_success 'rm will error out on a modified .gitmodules file unless staged' '
412 git reset --hard &&
413 git submodule update &&
414 git config -f .gitmodules foo.bar true &&
415 test_must_fail git rm submod >actual 2>actual.err &&
59a06e94
RA
416 test_file_not_empty actual.err &&
417 test_path_is_dir submod &&
418 test_path_is_file submod/.git &&
95c16418
JL
419 git diff-files --quiet -- submod &&
420 git add .gitmodules &&
421 git rm submod >actual 2>actual.err &&
ec10b018 422 test_must_be_empty actual.err &&
59a06e94
RA
423 test_path_is_missing submod &&
424 test_path_is_missing submod/.git &&
95c16418
JL
425 git status -s -uno >actual &&
426 test_cmp expect actual
427'
7edee329
DT
428test_expect_success 'rm will not error out on .gitmodules file with zero stat data' '
429 git reset --hard &&
430 git submodule update &&
431 git read-tree HEAD &&
432 git rm submod &&
433 test_path_is_missing submod
434'
95c16418
JL
435
436test_expect_success 'rm issues a warning when section is not found in .gitmodules' '
437 git reset --hard &&
438 git submodule update &&
439 git config -f .gitmodules --remove-section submodule.sub &&
440 git add .gitmodules &&
441 echo "warning: Could not find section in .gitmodules where path=submod" >expect.err &&
442 git rm submod >actual 2>actual.err &&
443 test_i18ncmp expect.err actual.err &&
59a06e94
RA
444 test_path_is_missing submod &&
445 test_path_is_missing submod/.git &&
95c16418 446 git status -s -uno >actual &&
293ab15e
JL
447 test_cmp expect actual
448'
449
450test_expect_success 'rm of a populated submodule with modifications fails unless forced' '
451 git reset --hard &&
452 git submodule update &&
9e189f1a 453 echo X >submod/empty &&
293ab15e 454 test_must_fail git rm submod &&
59a06e94
RA
455 test_path_is_dir submod &&
456 test_path_is_file submod/.git &&
9e189f1a 457 git status -s -uno --ignore-submodules=none >actual &&
dd6962dd 458 test_cmp expect.modified_inside actual &&
293ab15e 459 git rm -f submod &&
59a06e94 460 test_path_is_missing submod &&
9e189f1a 461 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
462 test_cmp expect actual
463'
464
465test_expect_success 'rm of a populated submodule with untracked files fails unless forced' '
466 git reset --hard &&
467 git submodule update &&
9e189f1a 468 echo X >submod/untracked &&
293ab15e 469 test_must_fail git rm submod &&
59a06e94
RA
470 test_path_is_dir submod &&
471 test_path_is_file submod/.git &&
9e189f1a 472 git status -s -uno --ignore-submodules=none >actual &&
dd6962dd 473 test_cmp expect.modified_untracked actual &&
293ab15e 474 git rm -f submod &&
59a06e94 475 test_path_is_missing submod &&
9e189f1a 476 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
477 test_cmp expect actual
478'
479
480test_expect_success 'setup submodule conflict' '
481 git reset --hard &&
482 git submodule update &&
483 git checkout -b branch1 &&
484 echo 1 >nitfol &&
485 git add nitfol &&
486 git commit -m "added nitfol 1" &&
487 git checkout -b branch2 master &&
488 echo 2 >nitfol &&
489 git add nitfol &&
490 git commit -m "added nitfol 2" &&
491 git checkout -b conflict1 master &&
9e189f1a
SB
492 git -C submod fetch &&
493 git -C submod checkout branch1 &&
293ab15e
JL
494 git add submod &&
495 git commit -m "submod 1" &&
496 git checkout -b conflict2 master &&
9e189f1a 497 git -C submod checkout branch2 &&
293ab15e
JL
498 git add submod &&
499 git commit -m "submod 2"
500'
501
502cat >expect.conflict <<EOF
503UU submod
504EOF
505
506test_expect_success 'rm removes work tree of unmodified conflicted submodule' '
507 git checkout conflict1 &&
508 git reset --hard &&
509 git submodule update &&
510 test_must_fail git merge conflict2 &&
511 git rm submod &&
59a06e94 512 test_path_is_missing submod &&
9e189f1a 513 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
514 test_cmp expect actual
515'
516
517test_expect_success 'rm of a conflicted populated submodule with different HEAD fails unless forced' '
518 git checkout conflict1 &&
519 git reset --hard &&
520 git submodule update &&
9e189f1a 521 git -C submod checkout HEAD^ &&
293ab15e
JL
522 test_must_fail git merge conflict2 &&
523 test_must_fail git rm submod &&
59a06e94
RA
524 test_path_is_dir submod &&
525 test_path_is_file submod/.git &&
9e189f1a 526 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
527 test_cmp expect.conflict actual &&
528 git rm -f submod &&
59a06e94 529 test_path_is_missing submod &&
9e189f1a 530 git status -s -uno --ignore-submodules=none >actual &&
95c16418
JL
531 test_cmp expect actual &&
532 test_must_fail git config -f .gitmodules submodule.sub.url &&
533 test_must_fail git config -f .gitmodules submodule.sub.path
293ab15e
JL
534'
535
536test_expect_success 'rm of a conflicted populated submodule with modifications fails unless forced' '
537 git checkout conflict1 &&
538 git reset --hard &&
539 git submodule update &&
9e189f1a 540 echo X >submod/empty &&
293ab15e
JL
541 test_must_fail git merge conflict2 &&
542 test_must_fail git rm submod &&
59a06e94
RA
543 test_path_is_dir submod &&
544 test_path_is_file submod/.git &&
9e189f1a 545 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
546 test_cmp expect.conflict actual &&
547 git rm -f submod &&
59a06e94 548 test_path_is_missing submod &&
9e189f1a 549 git status -s -uno --ignore-submodules=none >actual &&
95c16418
JL
550 test_cmp expect actual &&
551 test_must_fail git config -f .gitmodules submodule.sub.url &&
552 test_must_fail git config -f .gitmodules submodule.sub.path
293ab15e
JL
553'
554
555test_expect_success 'rm of a conflicted populated submodule with untracked files fails unless forced' '
556 git checkout conflict1 &&
557 git reset --hard &&
558 git submodule update &&
9e189f1a 559 echo X >submod/untracked &&
293ab15e
JL
560 test_must_fail git merge conflict2 &&
561 test_must_fail git rm submod &&
59a06e94
RA
562 test_path_is_dir submod &&
563 test_path_is_file submod/.git &&
9e189f1a 564 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
565 test_cmp expect.conflict actual &&
566 git rm -f submod &&
59a06e94 567 test_path_is_missing submod &&
9e189f1a 568 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
569 test_cmp expect actual
570'
571
572test_expect_success 'rm of a conflicted populated submodule with a .git directory fails even when forced' '
573 git checkout conflict1 &&
574 git reset --hard &&
575 git submodule update &&
b5368c23
RA
576 (
577 cd submod &&
293ab15e 578 rm .git &&
2d3ac9ad 579 cp -R ../.git/modules/sub .git &&
293ab15e
JL
580 GIT_WORK_TREE=. git config --unset core.worktree
581 ) &&
582 test_must_fail git merge conflict2 &&
583 test_must_fail git rm submod &&
59a06e94
RA
584 test_path_is_dir submod &&
585 test_path_is_dir submod/.git &&
9e189f1a 586 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
587 test_cmp expect.conflict actual &&
588 test_must_fail git rm -f submod &&
59a06e94
RA
589 test_path_is_dir submod &&
590 test_path_is_dir submod/.git &&
9e189f1a 591 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
592 test_cmp expect.conflict actual &&
593 git merge --abort &&
594 rm -rf submod
595'
596
597test_expect_success 'rm of a conflicted unpopulated submodule succeeds' '
598 git checkout conflict1 &&
599 git reset --hard &&
600 test_must_fail git merge conflict2 &&
601 git rm submod &&
59a06e94 602 test_path_is_missing submod &&
9e189f1a 603 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
604 test_cmp expect actual
605'
606
55856a35 607test_expect_success 'rm of a populated submodule with a .git directory migrates git dir' '
293ab15e
JL
608 git checkout -f master &&
609 git reset --hard &&
610 git submodule update &&
b5368c23
RA
611 (
612 cd submod &&
293ab15e 613 rm .git &&
2d3ac9ad 614 cp -R ../.git/modules/sub .git &&
55856a35
SB
615 GIT_WORK_TREE=. git config --unset core.worktree &&
616 rm -r ../.git/modules/sub
293ab15e 617 ) &&
55856a35 618 git rm submod 2>output.err &&
59a06e94
RA
619 test_path_is_missing submod &&
620 test_path_is_missing submod/.git &&
9e189f1a 621 git status -s -uno --ignore-submodules=none >actual &&
59a06e94 622 test_file_not_empty actual &&
55856a35 623 test_i18ngrep Migrating output.err
293ab15e
JL
624'
625
626cat >expect.deepmodified <<EOF
627 M submod/subsubmod
628EOF
629
630test_expect_success 'setup subsubmodule' '
631 git reset --hard &&
632 git submodule update &&
b5368c23
RA
633 (
634 cd submod &&
3b737381
DL
635 hash=$(git rev-parse HEAD) &&
636 git update-index --add --cacheinfo 160000 "$hash" subsubmod &&
293ab15e
JL
637 git config -f .gitmodules submodule.sub.url ../. &&
638 git config -f .gitmodules submodule.sub.path subsubmod &&
639 git submodule init &&
640 git add .gitmodules &&
641 git commit -m "add subsubmodule" &&
642 git submodule update subsubmod
643 ) &&
644 git commit -a -m "added deep submodule"
645'
646
647test_expect_success 'rm recursively removes work tree of unmodified submodules' '
648 git rm submod &&
59a06e94 649 test_path_is_missing submod &&
9e189f1a 650 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
651 test_cmp expect actual
652'
653
654test_expect_success 'rm of a populated nested submodule with different nested HEAD fails unless forced' '
655 git reset --hard &&
656 git submodule update --recursive &&
9e189f1a 657 git -C submod/subsubmod checkout HEAD^ &&
293ab15e 658 test_must_fail git rm submod &&
59a06e94
RA
659 test_path_is_dir submod &&
660 test_path_is_file submod/.git &&
9e189f1a 661 git status -s -uno --ignore-submodules=none >actual &&
dd6962dd 662 test_cmp expect.modified_inside actual &&
293ab15e 663 git rm -f submod &&
59a06e94 664 test_path_is_missing submod &&
9e189f1a 665 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
666 test_cmp expect actual
667'
668
669test_expect_success 'rm of a populated nested submodule with nested modifications fails unless forced' '
670 git reset --hard &&
671 git submodule update --recursive &&
9e189f1a 672 echo X >submod/subsubmod/empty &&
293ab15e 673 test_must_fail git rm submod &&
59a06e94
RA
674 test_path_is_dir submod &&
675 test_path_is_file submod/.git &&
9e189f1a 676 git status -s -uno --ignore-submodules=none >actual &&
dd6962dd 677 test_cmp expect.modified_inside actual &&
293ab15e 678 git rm -f submod &&
59a06e94 679 test_path_is_missing submod &&
9e189f1a 680 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
681 test_cmp expect actual
682'
683
684test_expect_success 'rm of a populated nested submodule with nested untracked files fails unless forced' '
685 git reset --hard &&
686 git submodule update --recursive &&
9e189f1a 687 echo X >submod/subsubmod/untracked &&
293ab15e 688 test_must_fail git rm submod &&
59a06e94
RA
689 test_path_is_dir submod &&
690 test_path_is_file submod/.git &&
9e189f1a 691 git status -s -uno --ignore-submodules=none >actual &&
40069d6e 692 test_cmp expect.modified_untracked actual &&
293ab15e 693 git rm -f submod &&
59a06e94 694 test_path_is_missing submod &&
9e189f1a 695 git status -s -uno --ignore-submodules=none >actual &&
293ab15e
JL
696 test_cmp expect actual
697'
698
70471ed9 699test_expect_success "rm absorbs submodule's nested .git directory" '
293ab15e
JL
700 git reset --hard &&
701 git submodule update --recursive &&
b5368c23
RA
702 (
703 cd submod/subsubmod &&
293ab15e 704 rm .git &&
55856a35 705 mv ../../.git/modules/sub/modules/sub .git &&
293ab15e
JL
706 GIT_WORK_TREE=. git config --unset core.worktree
707 ) &&
55856a35 708 git rm submod 2>output.err &&
59a06e94
RA
709 test_path_is_missing submod &&
710 test_path_is_missing submod/subsubmod/.git &&
9e189f1a 711 git status -s -uno --ignore-submodules=none >actual &&
59a06e94 712 test_file_not_empty actual &&
55856a35 713 test_i18ngrep Migrating output.err
293ab15e
JL
714'
715
bbad9f93 716test_expect_success 'checking out a commit after submodule removal needs manual updates' '
55856a35 717 git commit -m "submodule removal" submod .gitmodules &&
bbad9f93
JL
718 git checkout HEAD^ &&
719 git submodule update &&
8954bd76 720 git checkout -q HEAD^ &&
bbad9f93 721 git checkout -q master 2>actual &&
0a288d1e 722 test_i18ngrep "^warning: unable to rmdir '\''submod'\'':" actual &&
bbad9f93
JL
723 git status -s submod >actual &&
724 echo "?? submod/" >expected &&
725 test_cmp expected actual &&
726 rm -rf submod &&
9e189f1a 727 git status -s -uno --ignore-submodules=none >actual &&
ec10b018 728 test_must_be_empty actual
bbad9f93
JL
729'
730
9a6728d4
JK
731test_expect_success 'rm of d/f when d has become a non-directory' '
732 rm -rf d &&
733 mkdir d &&
734 >d/f &&
735 git add d &&
736 rm -rf d &&
737 >d &&
738 git rm d/f &&
739 test_must_fail git rev-parse --verify :d/f &&
740 test_path_is_file d
741'
742
743test_expect_success SYMLINKS 'rm of d/f when d has become a dangling symlink' '
744 rm -rf d &&
745 mkdir d &&
746 >d/f &&
747 git add d &&
748 rm -rf d &&
749 ln -s nonexistent d &&
750 git rm d/f &&
751 test_must_fail git rev-parse --verify :d/f &&
752 test -h d &&
753 test_path_is_missing d
754'
755
96ec8ee9
JK
756test_expect_success 'rm of file when it has become a directory' '
757 rm -rf d &&
758 >d &&
759 git add d &&
760 rm -f d &&
761 mkdir d &&
762 >d/f &&
763 test_must_fail git rm d &&
764 git rev-parse --verify :d &&
765 test_path_is_file d/f
766'
767
03415ca8
JK
768test_expect_success SYMLINKS 'rm across a symlinked leading path (no index)' '
769 rm -rf d e &&
770 mkdir e &&
771 echo content >e/f &&
772 ln -s e d &&
773 git add -A e d &&
774 git commit -m "symlink d to e, e/f exists" &&
775 test_must_fail git rm d/f &&
776 git rev-parse --verify :d &&
777 git rev-parse --verify :e/f &&
778 test -h d &&
779 test_path_is_file e/f
780'
781
782test_expect_failure SYMLINKS 'rm across a symlinked leading path (w/ index)' '
783 rm -rf d e &&
784 mkdir d &&
785 echo content >d/f &&
786 git add -A e d &&
787 git commit -m "d/f exists" &&
788 mv d e &&
789 ln -s e d &&
790 test_must_fail git rm d/f &&
791 git rev-parse --verify :d/f &&
792 test -h d &&
793 test_path_is_file e/f
794'
795
914dc028
MLM
796test_expect_success 'setup for testing rm messages' '
797 >bar.txt &&
798 >foo.txt &&
799 git add bar.txt foo.txt
800'
801
802test_expect_success 'rm files with different staged content' '
803 cat >expect <<-\EOF &&
804 error: the following files have staged content different from both the
805 file and the HEAD:
806 bar.txt
807 foo.txt
808 (use -f to force removal)
809 EOF
810 echo content1 >foo.txt &&
811 echo content1 >bar.txt &&
812 test_must_fail git rm foo.txt bar.txt 2>actual &&
813 test_i18ncmp expect actual
814'
815
7e309446
MLM
816test_expect_success 'rm files with different staged content without hints' '
817 cat >expect <<-\EOF &&
818 error: the following files have staged content different from both the
819 file and the HEAD:
820 bar.txt
821 foo.txt
822 EOF
823 echo content2 >foo.txt &&
824 echo content2 >bar.txt &&
825 test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual &&
826 test_i18ncmp expect actual
827'
914dc028
MLM
828
829test_expect_success 'rm file with local modification' '
830 cat >expect <<-\EOF &&
831 error: the following file has local modifications:
832 foo.txt
833 (use --cached to keep the file, or -f to force removal)
834 EOF
835 git commit -m "testing rm 3" &&
836 echo content3 >foo.txt &&
837 test_must_fail git rm foo.txt 2>actual &&
838 test_i18ncmp expect actual
839'
840
7e309446
MLM
841test_expect_success 'rm file with local modification without hints' '
842 cat >expect <<-\EOF &&
843 error: the following file has local modifications:
844 bar.txt
845 EOF
846 echo content4 >bar.txt &&
847 test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual &&
848 test_i18ncmp expect actual
849'
914dc028
MLM
850
851test_expect_success 'rm file with changes in the index' '
852 cat >expect <<-\EOF &&
853 error: the following file has changes staged in the index:
854 foo.txt
855 (use --cached to keep the file, or -f to force removal)
856 EOF
857 git reset --hard &&
858 echo content5 >foo.txt &&
859 git add foo.txt &&
860 test_must_fail git rm foo.txt 2>actual &&
861 test_i18ncmp expect actual
862'
863
7e309446
MLM
864test_expect_success 'rm file with changes in the index without hints' '
865 cat >expect <<-\EOF &&
866 error: the following file has changes staged in the index:
867 foo.txt
868 EOF
869 test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual &&
870 test_i18ncmp expect actual
871'
914dc028
MLM
872
873test_expect_success 'rm files with two different errors' '
874 cat >expect <<-\EOF &&
875 error: the following file has staged content different from both the
876 file and the HEAD:
877 foo1.txt
878 (use -f to force removal)
879 error: the following file has changes staged in the index:
880 bar1.txt
881 (use --cached to keep the file, or -f to force removal)
882 EOF
883 echo content >foo1.txt &&
884 git add foo1.txt &&
885 echo content6 >foo1.txt &&
886 echo content6 >bar1.txt &&
887 git add bar1.txt &&
888 test_must_fail git rm bar1.txt foo1.txt 2>actual &&
889 test_i18ncmp expect actual
890'
891
9e4e8a64
EX
892test_expect_success 'rm empty string should fail' '
893 test_must_fail git rm -rf ""
d426430e
EX
894'
895
d4a1cab5 896test_done