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