t / t3600-rm.shon commit Merge branch 'js/rebase-config-bitfix' (8feb8e2)
   1#!/bin/sh
   2#
   3# Copyright (c) 2006 Carl D. Worth
   4#
   5
   6test_description='Test of the various options to git rm.'
   7
   8. ./test-lib.sh
   9
  10# Setup some files to be removed, some with funny characters
  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'
  16
  17if test_have_prereq !FUNNYNAMES
  18then
  19        say 'Your filesystem does not allow tabs in filenames.'
  20fi
  21
  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' '
  29        test_path_is_file foo &&
  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' '
  72        test_path_is_file foo &&
  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"' '
  77        test_path_is_file bar &&
  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"' '
  86        test_path_is_missing bar &&
  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'
  97
  98test_expect_success SANITY 'Test that "git rm -f" fails if its rm fails' '
  99        test_when_finished "chmod 775 ." &&
 100        chmod a-w . &&
 101        test_must_fail git rm -f baz
 102'
 103
 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'
 107
 108test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
 109        git rm --ignore-unmatch nonexistent
 110'
 111
 112test_expect_success '"rm" command printed' '
 113        echo frotz >test-file &&
 114        git add test-file &&
 115        git commit -m "add file for rm test" &&
 116        git rm test-file >rm-output &&
 117        test $(grep "^rm " rm-output | wc -l) = 1 &&
 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' '
 123        echo frotz >test-file &&
 124        git add test-file &&
 125        git commit -m "add file for rm --quiet test" &&
 126        git rm --quiet test-file >rm-output &&
 127        test_must_be_empty rm-output &&
 128        rm -f test-file rm-output &&
 129        git commit -m "remove file from rm --quiet test"
 130'
 131
 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 &&
 140        test_must_fail git rm foo baz &&
 141        test_path_is_file foo &&
 142        test_path_is_file baz &&
 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 &&
 148        test_path_is_missing foo &&
 149        test_path_is_missing baz &&
 150        test_must_fail git ls-files --error-unmatch foo &&
 151        test_must_fail git ls-files --error-unmatch bar
 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' '
 162        test_must_fail git rm foo baz &&
 163        test_path_is_file foo &&
 164        test_path_is_file baz &&
 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 &&
 170        test_path_is_missing foo &&
 171        test_path_is_missing baz &&
 172        test_must_fail git ls-files --error-unmatch foo &&
 173        test_must_fail git ls-files --error-unmatch baz
 174'
 175
 176test_expect_success 'refuse to remove cached empty file with modifications' '
 177        >empty &&
 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 &&
 185        git add -N intent-to-add &&
 186        git rm --cached intent-to-add
 187'
 188
 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' '
 197        test_must_fail git rm frotz &&
 198        test_path_is_dir frotz &&
 199        test_path_is_file frotz/nitfol
 200'
 201
 202test_expect_success 'Recursive with -r but dirty' '
 203        echo qfwfq >>frotz/nitfol &&
 204        test_must_fail git rm -r frotz &&
 205        test_path_is_dir frotz &&
 206        test_path_is_file frotz/nitfol
 207'
 208
 209test_expect_success 'Recursive with -r -f' '
 210        git rm -f -r frotz &&
 211        test_path_is_missing frotz/nitfol &&
 212        test_path_is_missing frotz
 213'
 214
 215test_expect_success 'Remove nonexistent file returns nonzero exit status' '
 216        test_must_fail git rm nonexistent
 217'
 218
 219test_expect_success 'Call "rm" from outside the work tree' '
 220        mkdir repo &&
 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        )
 233'
 234
 235test_expect_success 'refresh index before checking if it is up-to-date' '
 236        git reset --hard &&
 237        test-tool chmtime -86400 frotz/nitfol &&
 238        git rm frotz/nitfol &&
 239        test_path_is_missing frotz/nitfol
 240'
 241
 242test_expect_success 'choking "git rm" should not let it die with cruft' '
 243        git reset -q --hard &&
 244        test_when_finished "rm -f .git/index.lock && git reset -q --hard" &&
 245        i=0 &&
 246        while test $i -lt 12000
 247        do
 248                echo "100644 1234567890123456789012345678901234567890 0 some-file-$i"
 249                i=$(( $i + 1 ))
 250        done | git update-index --index-info &&
 251        git rm -n "some-file-*" | : &&
 252        test_path_is_missing .git/index.lock
 253'
 254
 255test_expect_success 'rm removes subdirectories recursively' '
 256        mkdir -p dir/subdir/subsubdir &&
 257        echo content >dir/subdir/subsubdir/file &&
 258        git add dir/subdir/subsubdir/file &&
 259        git rm -f dir/subdir/subsubdir/file &&
 260        test_path_is_missing dir
 261'
 262
 263cat >expect <<EOF
 264M  .gitmodules
 265D  submod
 266EOF
 267
 268cat >expect.modified <<EOF
 269 M submod
 270EOF
 271
 272cat >expect.modified_inside <<EOF
 273 m submod
 274EOF
 275
 276cat >expect.modified_untracked <<EOF
 277 ? submod
 278EOF
 279
 280cat >expect.cached <<EOF
 281D  submod
 282EOF
 283
 284cat >expect.both_deleted<<EOF
 285D  .gitmodules
 286D  submod
 287EOF
 288
 289test_expect_success 'rm removes empty submodules from work tree' '
 290        mkdir submod &&
 291        git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) submod &&
 292        git config -f .gitmodules submodule.sub.url ./. &&
 293        git config -f .gitmodules submodule.sub.path submod &&
 294        git submodule init &&
 295        git add .gitmodules &&
 296        git commit -m "add submodule" &&
 297        git rm submod &&
 298        test_path_is_missing submod &&
 299        git status -s -uno --ignore-submodules=none >actual &&
 300        test_cmp expect actual &&
 301        test_must_fail git config -f .gitmodules submodule.sub.url &&
 302        test_must_fail git config -f .gitmodules submodule.sub.path
 303'
 304
 305test_expect_success 'rm removes removed submodule from index and .gitmodules' '
 306        git reset --hard &&
 307        git submodule update &&
 308        rm -rf submod &&
 309        git rm submod &&
 310        git status -s -uno --ignore-submodules=none >actual &&
 311        test_cmp expect actual &&
 312        test_must_fail git config -f .gitmodules submodule.sub.url &&
 313        test_must_fail git config -f .gitmodules submodule.sub.path
 314'
 315
 316test_expect_success 'rm removes work tree of unmodified submodules' '
 317        git reset --hard &&
 318        git submodule update &&
 319        git rm submod &&
 320        test_path_is_missing submod &&
 321        git status -s -uno --ignore-submodules=none >actual &&
 322        test_cmp expect actual &&
 323        test_must_fail git config -f .gitmodules submodule.sub.url &&
 324        test_must_fail git config -f .gitmodules submodule.sub.path
 325'
 326
 327test_expect_success 'rm removes a submodule with a trailing /' '
 328        git reset --hard &&
 329        git submodule update &&
 330        git rm submod/ &&
 331        test_path_is_missing submod &&
 332        git status -s -uno --ignore-submodules=none >actual &&
 333        test_cmp expect actual
 334'
 335
 336test_expect_success 'rm fails when given a file with a trailing /' '
 337        test_must_fail git rm empty/
 338'
 339
 340test_expect_success 'rm succeeds when given a directory with a trailing /' '
 341        git rm -r frotz/
 342'
 343
 344test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '
 345        git reset --hard &&
 346        git submodule update &&
 347        git -C submod checkout HEAD^ &&
 348        test_must_fail git rm submod &&
 349        test_path_is_dir submod &&
 350        test_path_is_file submod/.git &&
 351        git status -s -uno --ignore-submodules=none >actual &&
 352        test_cmp expect.modified actual &&
 353        git rm -f submod &&
 354        test_path_is_missing submod &&
 355        git status -s -uno --ignore-submodules=none >actual &&
 356        test_cmp expect actual &&
 357        test_must_fail git config -f .gitmodules submodule.sub.url &&
 358        test_must_fail git config -f .gitmodules submodule.sub.path
 359'
 360
 361test_expect_success 'rm --cached leaves work tree of populated submodules and .gitmodules alone' '
 362        git reset --hard &&
 363        git submodule update &&
 364        git rm --cached submod &&
 365        test_path_is_dir submod &&
 366        test_path_is_file submod/.git &&
 367        git status -s -uno >actual &&
 368        test_cmp expect.cached actual &&
 369        git config -f .gitmodules submodule.sub.url &&
 370        git config -f .gitmodules submodule.sub.path
 371'
 372
 373test_expect_success 'rm --dry-run does not touch the submodule or .gitmodules' '
 374        git reset --hard &&
 375        git submodule update &&
 376        git rm -n submod &&
 377        test_path_is_file submod/.git &&
 378        git diff-index --exit-code HEAD
 379'
 380
 381test_expect_success 'rm does not complain when no .gitmodules file is found' '
 382        git reset --hard &&
 383        git submodule update &&
 384        git rm .gitmodules &&
 385        git rm submod >actual 2>actual.err &&
 386        test_must_be_empty actual.err &&
 387        test_path_is_missing submod &&
 388        test_path_is_missing submod/.git &&
 389        git status -s -uno >actual &&
 390        test_cmp expect.both_deleted actual
 391'
 392
 393test_expect_success 'rm will error out on a modified .gitmodules file unless staged' '
 394        git reset --hard &&
 395        git submodule update &&
 396        git config -f .gitmodules foo.bar true &&
 397        test_must_fail git rm submod >actual 2>actual.err &&
 398        test_file_not_empty actual.err &&
 399        test_path_is_dir submod &&
 400        test_path_is_file submod/.git &&
 401        git diff-files --quiet -- submod &&
 402        git add .gitmodules &&
 403        git rm submod >actual 2>actual.err &&
 404        test_must_be_empty actual.err &&
 405        test_path_is_missing submod &&
 406        test_path_is_missing submod/.git &&
 407        git status -s -uno >actual &&
 408        test_cmp expect actual
 409'
 410
 411test_expect_success 'rm issues a warning when section is not found in .gitmodules' '
 412        git reset --hard &&
 413        git submodule update &&
 414        git config -f .gitmodules --remove-section submodule.sub &&
 415        git add .gitmodules &&
 416        echo "warning: Could not find section in .gitmodules where path=submod" >expect.err &&
 417        git rm submod >actual 2>actual.err &&
 418        test_i18ncmp expect.err actual.err &&
 419        test_path_is_missing submod &&
 420        test_path_is_missing submod/.git &&
 421        git status -s -uno >actual &&
 422        test_cmp expect actual
 423'
 424
 425test_expect_success 'rm of a populated submodule with modifications fails unless forced' '
 426        git reset --hard &&
 427        git submodule update &&
 428        echo X >submod/empty &&
 429        test_must_fail git rm submod &&
 430        test_path_is_dir submod &&
 431        test_path_is_file submod/.git &&
 432        git status -s -uno --ignore-submodules=none >actual &&
 433        test_cmp expect.modified_inside actual &&
 434        git rm -f submod &&
 435        test_path_is_missing submod &&
 436        git status -s -uno --ignore-submodules=none >actual &&
 437        test_cmp expect actual
 438'
 439
 440test_expect_success 'rm of a populated submodule with untracked files fails unless forced' '
 441        git reset --hard &&
 442        git submodule update &&
 443        echo X >submod/untracked &&
 444        test_must_fail git rm submod &&
 445        test_path_is_dir submod &&
 446        test_path_is_file submod/.git &&
 447        git status -s -uno --ignore-submodules=none >actual &&
 448        test_cmp expect.modified_untracked actual &&
 449        git rm -f submod &&
 450        test_path_is_missing submod &&
 451        git status -s -uno --ignore-submodules=none >actual &&
 452        test_cmp expect actual
 453'
 454
 455test_expect_success 'setup submodule conflict' '
 456        git reset --hard &&
 457        git submodule update &&
 458        git checkout -b branch1 &&
 459        echo 1 >nitfol &&
 460        git add nitfol &&
 461        git commit -m "added nitfol 1" &&
 462        git checkout -b branch2 master &&
 463        echo 2 >nitfol &&
 464        git add nitfol &&
 465        git commit -m "added nitfol 2" &&
 466        git checkout -b conflict1 master &&
 467        git -C submod fetch &&
 468        git -C submod checkout branch1 &&
 469        git add submod &&
 470        git commit -m "submod 1" &&
 471        git checkout -b conflict2 master &&
 472        git -C submod checkout branch2 &&
 473        git add submod &&
 474        git commit -m "submod 2"
 475'
 476
 477cat >expect.conflict <<EOF
 478UU submod
 479EOF
 480
 481test_expect_success 'rm removes work tree of unmodified conflicted submodule' '
 482        git checkout conflict1 &&
 483        git reset --hard &&
 484        git submodule update &&
 485        test_must_fail git merge conflict2 &&
 486        git rm submod &&
 487        test_path_is_missing submod &&
 488        git status -s -uno --ignore-submodules=none >actual &&
 489        test_cmp expect actual
 490'
 491
 492test_expect_success 'rm of a conflicted populated submodule with different HEAD fails unless forced' '
 493        git checkout conflict1 &&
 494        git reset --hard &&
 495        git submodule update &&
 496        git -C submod checkout HEAD^ &&
 497        test_must_fail git merge conflict2 &&
 498        test_must_fail git rm submod &&
 499        test_path_is_dir submod &&
 500        test_path_is_file submod/.git &&
 501        git status -s -uno --ignore-submodules=none >actual &&
 502        test_cmp expect.conflict actual &&
 503        git rm -f submod &&
 504        test_path_is_missing submod &&
 505        git status -s -uno --ignore-submodules=none >actual &&
 506        test_cmp expect actual &&
 507        test_must_fail git config -f .gitmodules submodule.sub.url &&
 508        test_must_fail git config -f .gitmodules submodule.sub.path
 509'
 510
 511test_expect_success 'rm of a conflicted populated submodule with modifications fails unless forced' '
 512        git checkout conflict1 &&
 513        git reset --hard &&
 514        git submodule update &&
 515        echo X >submod/empty &&
 516        test_must_fail git merge conflict2 &&
 517        test_must_fail git rm submod &&
 518        test_path_is_dir submod &&
 519        test_path_is_file submod/.git &&
 520        git status -s -uno --ignore-submodules=none >actual &&
 521        test_cmp expect.conflict actual &&
 522        git rm -f submod &&
 523        test_path_is_missing submod &&
 524        git status -s -uno --ignore-submodules=none >actual &&
 525        test_cmp expect actual &&
 526        test_must_fail git config -f .gitmodules submodule.sub.url &&
 527        test_must_fail git config -f .gitmodules submodule.sub.path
 528'
 529
 530test_expect_success 'rm of a conflicted populated submodule with untracked files fails unless forced' '
 531        git checkout conflict1 &&
 532        git reset --hard &&
 533        git submodule update &&
 534        echo X >submod/untracked &&
 535        test_must_fail git merge conflict2 &&
 536        test_must_fail git rm submod &&
 537        test_path_is_dir submod &&
 538        test_path_is_file submod/.git &&
 539        git status -s -uno --ignore-submodules=none >actual &&
 540        test_cmp expect.conflict actual &&
 541        git rm -f submod &&
 542        test_path_is_missing submod &&
 543        git status -s -uno --ignore-submodules=none >actual &&
 544        test_cmp expect actual
 545'
 546
 547test_expect_success 'rm of a conflicted populated submodule with a .git directory fails even when forced' '
 548        git checkout conflict1 &&
 549        git reset --hard &&
 550        git submodule update &&
 551        (
 552                cd submod &&
 553                rm .git &&
 554                cp -R ../.git/modules/sub .git &&
 555                GIT_WORK_TREE=. git config --unset core.worktree
 556        ) &&
 557        test_must_fail git merge conflict2 &&
 558        test_must_fail git rm submod &&
 559        test_path_is_dir submod &&
 560        test_path_is_dir submod/.git &&
 561        git status -s -uno --ignore-submodules=none >actual &&
 562        test_cmp expect.conflict actual &&
 563        test_must_fail git rm -f submod &&
 564        test_path_is_dir submod &&
 565        test_path_is_dir submod/.git &&
 566        git status -s -uno --ignore-submodules=none >actual &&
 567        test_cmp expect.conflict actual &&
 568        git merge --abort &&
 569        rm -rf submod
 570'
 571
 572test_expect_success 'rm of a conflicted unpopulated submodule succeeds' '
 573        git checkout conflict1 &&
 574        git reset --hard &&
 575        test_must_fail git merge conflict2 &&
 576        git rm submod &&
 577        test_path_is_missing submod &&
 578        git status -s -uno --ignore-submodules=none >actual &&
 579        test_cmp expect actual
 580'
 581
 582test_expect_success 'rm of a populated submodule with a .git directory migrates git dir' '
 583        git checkout -f master &&
 584        git reset --hard &&
 585        git submodule update &&
 586        (
 587                cd submod &&
 588                rm .git &&
 589                cp -R ../.git/modules/sub .git &&
 590                GIT_WORK_TREE=. git config --unset core.worktree &&
 591                rm -r ../.git/modules/sub
 592        ) &&
 593        git rm submod 2>output.err &&
 594        test_path_is_missing submod &&
 595        test_path_is_missing submod/.git &&
 596        git status -s -uno --ignore-submodules=none >actual &&
 597        test_file_not_empty actual &&
 598        test_i18ngrep Migrating output.err
 599'
 600
 601cat >expect.deepmodified <<EOF
 602 M submod/subsubmod
 603EOF
 604
 605test_expect_success 'setup subsubmodule' '
 606        git reset --hard &&
 607        git submodule update &&
 608        (
 609                cd submod &&
 610                git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) subsubmod &&
 611                git config -f .gitmodules submodule.sub.url ../. &&
 612                git config -f .gitmodules submodule.sub.path subsubmod &&
 613                git submodule init &&
 614                git add .gitmodules &&
 615                git commit -m "add subsubmodule" &&
 616                git submodule update subsubmod
 617        ) &&
 618        git commit -a -m "added deep submodule"
 619'
 620
 621test_expect_success 'rm recursively removes work tree of unmodified submodules' '
 622        git rm submod &&
 623        test_path_is_missing submod &&
 624        git status -s -uno --ignore-submodules=none >actual &&
 625        test_cmp expect actual
 626'
 627
 628test_expect_success 'rm of a populated nested submodule with different nested HEAD fails unless forced' '
 629        git reset --hard &&
 630        git submodule update --recursive &&
 631        git -C submod/subsubmod checkout HEAD^ &&
 632        test_must_fail git rm submod &&
 633        test_path_is_dir submod &&
 634        test_path_is_file submod/.git &&
 635        git status -s -uno --ignore-submodules=none >actual &&
 636        test_cmp expect.modified_inside actual &&
 637        git rm -f submod &&
 638        test_path_is_missing submod &&
 639        git status -s -uno --ignore-submodules=none >actual &&
 640        test_cmp expect actual
 641'
 642
 643test_expect_success 'rm of a populated nested submodule with nested modifications fails unless forced' '
 644        git reset --hard &&
 645        git submodule update --recursive &&
 646        echo X >submod/subsubmod/empty &&
 647        test_must_fail git rm submod &&
 648        test_path_is_dir submod &&
 649        test_path_is_file submod/.git &&
 650        git status -s -uno --ignore-submodules=none >actual &&
 651        test_cmp expect.modified_inside actual &&
 652        git rm -f submod &&
 653        test_path_is_missing submod &&
 654        git status -s -uno --ignore-submodules=none >actual &&
 655        test_cmp expect actual
 656'
 657
 658test_expect_success 'rm of a populated nested submodule with nested untracked files fails unless forced' '
 659        git reset --hard &&
 660        git submodule update --recursive &&
 661        echo X >submod/subsubmod/untracked &&
 662        test_must_fail git rm submod &&
 663        test_path_is_dir submod &&
 664        test_path_is_file submod/.git &&
 665        git status -s -uno --ignore-submodules=none >actual &&
 666        test_cmp expect.modified_untracked actual &&
 667        git rm -f submod &&
 668        test_path_is_missing submod &&
 669        git status -s -uno --ignore-submodules=none >actual &&
 670        test_cmp expect actual
 671'
 672
 673test_expect_success "rm absorbs submodule's nested .git directory" '
 674        git reset --hard &&
 675        git submodule update --recursive &&
 676        (
 677                cd submod/subsubmod &&
 678                rm .git &&
 679                mv ../../.git/modules/sub/modules/sub .git &&
 680                GIT_WORK_TREE=. git config --unset core.worktree
 681        ) &&
 682        git rm submod 2>output.err &&
 683        test_path_is_missing submod &&
 684        test_path_is_missing submod/subsubmod/.git &&
 685        git status -s -uno --ignore-submodules=none >actual &&
 686        test_file_not_empty actual &&
 687        test_i18ngrep Migrating output.err
 688'
 689
 690test_expect_success 'checking out a commit after submodule removal needs manual updates' '
 691        git commit -m "submodule removal" submod .gitmodules &&
 692        git checkout HEAD^ &&
 693        git submodule update &&
 694        git checkout -q HEAD^ &&
 695        git checkout -q master 2>actual &&
 696        test_i18ngrep "^warning: unable to rmdir '\''submod'\'':" actual &&
 697        git status -s submod >actual &&
 698        echo "?? submod/" >expected &&
 699        test_cmp expected actual &&
 700        rm -rf submod &&
 701        git status -s -uno --ignore-submodules=none >actual &&
 702        test_must_be_empty actual
 703'
 704
 705test_expect_success 'rm of d/f when d has become a non-directory' '
 706        rm -rf d &&
 707        mkdir d &&
 708        >d/f &&
 709        git add d &&
 710        rm -rf d &&
 711        >d &&
 712        git rm d/f &&
 713        test_must_fail git rev-parse --verify :d/f &&
 714        test_path_is_file d
 715'
 716
 717test_expect_success SYMLINKS 'rm of d/f when d has become a dangling symlink' '
 718        rm -rf d &&
 719        mkdir d &&
 720        >d/f &&
 721        git add d &&
 722        rm -rf d &&
 723        ln -s nonexistent d &&
 724        git rm d/f &&
 725        test_must_fail git rev-parse --verify :d/f &&
 726        test -h d &&
 727        test_path_is_missing d
 728'
 729
 730test_expect_success 'rm of file when it has become a directory' '
 731        rm -rf d &&
 732        >d &&
 733        git add d &&
 734        rm -f d &&
 735        mkdir d &&
 736        >d/f &&
 737        test_must_fail git rm d &&
 738        git rev-parse --verify :d &&
 739        test_path_is_file d/f
 740'
 741
 742test_expect_success SYMLINKS 'rm across a symlinked leading path (no index)' '
 743        rm -rf d e &&
 744        mkdir e &&
 745        echo content >e/f &&
 746        ln -s e d &&
 747        git add -A e d &&
 748        git commit -m "symlink d to e, e/f exists" &&
 749        test_must_fail git rm d/f &&
 750        git rev-parse --verify :d &&
 751        git rev-parse --verify :e/f &&
 752        test -h d &&
 753        test_path_is_file e/f
 754'
 755
 756test_expect_failure SYMLINKS 'rm across a symlinked leading path (w/ index)' '
 757        rm -rf d e &&
 758        mkdir d &&
 759        echo content >d/f &&
 760        git add -A e d &&
 761        git commit -m "d/f exists" &&
 762        mv d e &&
 763        ln -s e d &&
 764        test_must_fail git rm d/f &&
 765        git rev-parse --verify :d/f &&
 766        test -h d &&
 767        test_path_is_file e/f
 768'
 769
 770test_expect_success 'setup for testing rm messages' '
 771        >bar.txt &&
 772        >foo.txt &&
 773        git add bar.txt foo.txt
 774'
 775
 776test_expect_success 'rm files with different staged content' '
 777        cat >expect <<-\EOF &&
 778        error: the following files have staged content different from both the
 779        file and the HEAD:
 780            bar.txt
 781            foo.txt
 782        (use -f to force removal)
 783        EOF
 784        echo content1 >foo.txt &&
 785        echo content1 >bar.txt &&
 786        test_must_fail git rm foo.txt bar.txt 2>actual &&
 787        test_i18ncmp expect actual
 788'
 789
 790test_expect_success 'rm files with different staged content without hints' '
 791        cat >expect <<-\EOF &&
 792        error: the following files have staged content different from both the
 793        file and the HEAD:
 794            bar.txt
 795            foo.txt
 796        EOF
 797        echo content2 >foo.txt &&
 798        echo content2 >bar.txt &&
 799        test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual &&
 800        test_i18ncmp expect actual
 801'
 802
 803test_expect_success 'rm file with local modification' '
 804        cat >expect <<-\EOF &&
 805        error: the following file has local modifications:
 806            foo.txt
 807        (use --cached to keep the file, or -f to force removal)
 808        EOF
 809        git commit -m "testing rm 3" &&
 810        echo content3 >foo.txt &&
 811        test_must_fail git rm foo.txt 2>actual &&
 812        test_i18ncmp expect actual
 813'
 814
 815test_expect_success 'rm file with local modification without hints' '
 816        cat >expect <<-\EOF &&
 817        error: the following file has local modifications:
 818            bar.txt
 819        EOF
 820        echo content4 >bar.txt &&
 821        test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual &&
 822        test_i18ncmp expect actual
 823'
 824
 825test_expect_success 'rm file with changes in the index' '
 826        cat >expect <<-\EOF &&
 827        error: the following file has changes staged in the index:
 828            foo.txt
 829        (use --cached to keep the file, or -f to force removal)
 830        EOF
 831        git reset --hard &&
 832        echo content5 >foo.txt &&
 833        git add foo.txt &&
 834        test_must_fail git rm foo.txt 2>actual &&
 835        test_i18ncmp expect actual
 836'
 837
 838test_expect_success 'rm file with changes in the index without hints' '
 839        cat >expect <<-\EOF &&
 840        error: the following file has changes staged in the index:
 841            foo.txt
 842        EOF
 843        test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual &&
 844        test_i18ncmp expect actual
 845'
 846
 847test_expect_success 'rm files with two different errors' '
 848        cat >expect <<-\EOF &&
 849        error: the following file has staged content different from both the
 850        file and the HEAD:
 851            foo1.txt
 852        (use -f to force removal)
 853        error: the following file has changes staged in the index:
 854            bar1.txt
 855        (use --cached to keep the file, or -f to force removal)
 856        EOF
 857        echo content >foo1.txt &&
 858        git add foo1.txt &&
 859        echo content6 >foo1.txt &&
 860        echo content6 >bar1.txt &&
 861        git add bar1.txt &&
 862        test_must_fail git rm bar1.txt foo1.txt 2>actual &&
 863        test_i18ncmp expect actual
 864'
 865
 866test_expect_success 'rm empty string should fail' '
 867        test_must_fail git rm -rf ""
 868'
 869
 870test_done