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