t / t3600-rm.shon commit rm: resolving by removal is not a warning-worthy event (b2b1f61)
   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 test_have_prereq !FUNNYNAMES; then
  18        say 'Your filesystem does not allow tabs in filenames.'
  19fi
  20
  21test_expect_success FUNNYNAMES 'add files with funny names' "
  22     touch -- 'tab      embedded' 'newline
  23embedded' &&
  24     git add -- 'tab    embedded' 'newline
  25embedded' &&
  26     git commit -m 'add files with tabs and newlines'
  27"
  28
  29test_expect_success \
  30    'Pre-check that foo exists and is in index before git rm foo' \
  31    '[ -f foo ] && git ls-files --error-unmatch foo'
  32
  33test_expect_success \
  34    'Test that git rm foo succeeds' \
  35    'git rm --cached foo'
  36
  37test_expect_success \
  38    'Test that git rm --cached foo succeeds if the index matches the file' \
  39    'echo content >foo &&
  40     git add foo &&
  41     git rm --cached foo'
  42
  43test_expect_success \
  44    'Test that git rm --cached foo succeeds if the index matches the file' \
  45    'echo content >foo &&
  46     git add foo &&
  47     git commit -m foo &&
  48     echo "other content" >foo &&
  49     git rm --cached foo'
  50
  51test_expect_success \
  52    'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' '
  53     echo content >foo &&
  54     git add foo &&
  55     git commit -m foo --allow-empty &&
  56     echo "other content" >foo &&
  57     git add foo &&
  58     echo "yet another content" >foo &&
  59     test_must_fail git rm --cached foo
  60'
  61
  62test_expect_success \
  63    'Test that git rm --cached -f foo works in case where --cached only did not' \
  64    'echo content >foo &&
  65     git add foo &&
  66     git commit -m foo --allow-empty &&
  67     echo "other content" >foo &&
  68     git add foo &&
  69     echo "yet another content" >foo &&
  70     git rm --cached -f foo'
  71
  72test_expect_success \
  73    'Post-check that foo exists but is not in index after git rm foo' \
  74    '[ -f foo ] && test_must_fail git ls-files --error-unmatch foo'
  75
  76test_expect_success \
  77    'Pre-check that bar exists and is in index before "git rm bar"' \
  78    '[ -f bar ] && git ls-files --error-unmatch bar'
  79
  80test_expect_success \
  81    'Test that "git rm bar" succeeds' \
  82    'git rm bar'
  83
  84test_expect_success \
  85    'Post-check that bar does not exist and is not in index after "git rm -f bar"' \
  86    '! [ -f bar ] && test_must_fail git ls-files --error-unmatch bar'
  87
  88test_expect_success \
  89    'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' \
  90    'git rm -- -q'
  91
  92test_expect_success FUNNYNAMES \
  93    "Test that \"git rm -f\" succeeds with embedded space, tab, or newline characters." \
  94    "git rm -f 'space embedded' 'tab    embedded' 'newline
  95embedded'"
  96
  97test_expect_success SANITY 'Test that "git rm -f" fails if its rm fails' '
  98        test_when_finished "chmod 775 ." &&
  99        chmod a-w . &&
 100        test_must_fail git rm -f baz
 101'
 102
 103test_expect_success \
 104    '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
 107test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
 108        git rm --ignore-unmatch nonexistent
 109'
 110
 111test_expect_success '"rm" command printed' '
 112        echo frotz >test-file &&
 113        git add test-file &&
 114        git commit -m "add file for rm test" &&
 115        git rm test-file >rm-output &&
 116        test $(grep "^rm " rm-output | wc -l) = 1 &&
 117        rm -f test-file rm-output &&
 118        git commit -m "remove file from rm test"
 119'
 120
 121test_expect_success '"rm" command suppressed with --quiet' '
 122        echo frotz >test-file &&
 123        git add test-file &&
 124        git commit -m "add file for rm --quiet test" &&
 125        git rm --quiet test-file >rm-output &&
 126        test_must_be_empty rm-output &&
 127        rm -f test-file rm-output &&
 128        git commit -m "remove file from rm --quiet test"
 129'
 130
 131# Now, failure cases.
 132test_expect_success 'Re-add foo and baz' '
 133        git add foo baz &&
 134        git ls-files --error-unmatch foo baz
 135'
 136
 137test_expect_success 'Modify foo -- rm should refuse' '
 138        echo >>foo &&
 139        test_must_fail git rm foo baz &&
 140        test -f foo &&
 141        test -f baz &&
 142        git ls-files --error-unmatch foo baz
 143'
 144
 145test_expect_success 'Modified foo -- rm -f should work' '
 146        git rm -f foo baz &&
 147        test ! -f foo &&
 148        test ! -f baz &&
 149        test_must_fail git ls-files --error-unmatch foo &&
 150        test_must_fail git ls-files --error-unmatch bar
 151'
 152
 153test_expect_success 'Re-add foo and baz for HEAD tests' '
 154        echo frotz >foo &&
 155        git checkout HEAD -- baz &&
 156        git add foo baz &&
 157        git ls-files --error-unmatch foo baz
 158'
 159
 160test_expect_success 'foo is different in index from HEAD -- rm should refuse' '
 161        test_must_fail git rm foo baz &&
 162        test -f foo &&
 163        test -f baz &&
 164        git ls-files --error-unmatch foo baz
 165'
 166
 167test_expect_success 'but with -f it should work.' '
 168        git rm -f foo baz &&
 169        test ! -f foo &&
 170        test ! -f baz &&
 171        test_must_fail git ls-files --error-unmatch foo &&
 172        test_must_fail git ls-files --error-unmatch baz
 173'
 174
 175test_expect_success 'refuse to remove cached empty file with modifications' '
 176        >empty &&
 177        git add empty &&
 178        echo content >empty &&
 179        test_must_fail git rm --cached empty
 180'
 181
 182test_expect_success 'remove intent-to-add file without --force' '
 183        echo content >intent-to-add &&
 184        git add -N intent-to-add &&
 185        git rm --cached intent-to-add
 186'
 187
 188test_expect_success 'Recursive test setup' '
 189        mkdir -p frotz &&
 190        echo qfwfq >frotz/nitfol &&
 191        git add frotz &&
 192        git commit -m "subdir test"
 193'
 194
 195test_expect_success 'Recursive without -r fails' '
 196        test_must_fail git rm frotz &&
 197        test -d frotz &&
 198        test -f frotz/nitfol
 199'
 200
 201test_expect_success 'Recursive with -r but dirty' '
 202        echo qfwfq >>frotz/nitfol &&
 203        test_must_fail git rm -r frotz &&
 204        test -d frotz &&
 205        test -f frotz/nitfol
 206'
 207
 208test_expect_success 'Recursive with -r -f' '
 209        git rm -f -r frotz &&
 210        ! test -f frotz/nitfol &&
 211        ! test -d frotz
 212'
 213
 214test_expect_success 'Remove nonexistent file returns nonzero exit status' '
 215        test_must_fail git rm nonexistent
 216'
 217
 218test_expect_success 'Call "rm" from outside the work tree' '
 219        mkdir repo &&
 220        (cd repo &&
 221         git init &&
 222         echo something >somefile &&
 223         git add somefile &&
 224         git commit -m "add a file" &&
 225         (cd .. &&
 226          git --git-dir=repo/.git --work-tree=repo rm somefile) &&
 227        test_must_fail git ls-files --error-unmatch somefile)
 228'
 229
 230test_expect_success 'refresh index before checking if it is up-to-date' '
 231
 232        git reset --hard &&
 233        test-tool chmtime -86400 frotz/nitfol &&
 234        git rm frotz/nitfol &&
 235        test ! -f frotz/nitfol
 236
 237'
 238
 239test_expect_success 'choking "git rm" should not let it die with cruft' '
 240        git reset -q --hard &&
 241        test_when_finished "rm -f .git/index.lock && git reset -q --hard" &&
 242        i=0 &&
 243        while test $i -lt 12000
 244        do
 245            echo "100644 1234567890123456789012345678901234567890 0     some-file-$i"
 246            i=$(( $i + 1 ))
 247        done | git update-index --index-info &&
 248        git rm -n "some-file-*" | : &&
 249        test_path_is_missing .git/index.lock
 250'
 251
 252test_expect_success 'Resolving by removal is not a warning-worthy event' '
 253        git reset -q --hard &&
 254        test_when_finished "rm -f .git/index.lock msg && git reset -q --hard" &&
 255        blob=$(echo blob | git hash-object -w --stdin) &&
 256        for stage in 1 2 3
 257        do
 258                echo "100644 $blob $stage       blob"
 259        done | git update-index --index-info &&
 260        git rm blob >msg 2>&1 &&
 261        test_i18ngrep ! "needs merge" msg &&
 262        test_must_fail git ls-files -s --error-unmatch blob
 263'
 264
 265test_expect_success 'rm removes subdirectories recursively' '
 266        mkdir -p dir/subdir/subsubdir &&
 267        echo content >dir/subdir/subsubdir/file &&
 268        git add dir/subdir/subsubdir/file &&
 269        git rm -f dir/subdir/subsubdir/file &&
 270        ! test -d dir
 271'
 272
 273cat >expect <<EOF
 274M  .gitmodules
 275D  submod
 276EOF
 277
 278cat >expect.modified <<EOF
 279 M submod
 280EOF
 281
 282cat >expect.modified_inside <<EOF
 283 m submod
 284EOF
 285
 286cat >expect.modified_untracked <<EOF
 287 ? submod
 288EOF
 289
 290cat >expect.cached <<EOF
 291D  submod
 292EOF
 293
 294cat >expect.both_deleted<<EOF
 295D  .gitmodules
 296D  submod
 297EOF
 298
 299test_expect_success 'rm removes empty submodules from work tree' '
 300        mkdir submod &&
 301        git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) submod &&
 302        git config -f .gitmodules submodule.sub.url ./. &&
 303        git config -f .gitmodules submodule.sub.path submod &&
 304        git submodule init &&
 305        git add .gitmodules &&
 306        git commit -m "add submodule" &&
 307        git rm submod &&
 308        test ! -e submod &&
 309        git status -s -uno --ignore-submodules=none >actual &&
 310        test_cmp expect actual &&
 311        test_must_fail git config -f .gitmodules submodule.sub.url &&
 312        test_must_fail git config -f .gitmodules submodule.sub.path
 313'
 314
 315test_expect_success 'rm removes removed submodule from index and .gitmodules' '
 316        git reset --hard &&
 317        git submodule update &&
 318        rm -rf submod &&
 319        git rm submod &&
 320        git status -s -uno --ignore-submodules=none >actual &&
 321        test_cmp expect actual &&
 322        test_must_fail git config -f .gitmodules submodule.sub.url &&
 323        test_must_fail git config -f .gitmodules submodule.sub.path
 324'
 325
 326test_expect_success 'rm removes work tree of unmodified submodules' '
 327        git reset --hard &&
 328        git submodule update &&
 329        git rm submod &&
 330        test ! -d submod &&
 331        git status -s -uno --ignore-submodules=none >actual &&
 332        test_cmp expect actual &&
 333        test_must_fail git config -f .gitmodules submodule.sub.url &&
 334        test_must_fail git config -f .gitmodules submodule.sub.path
 335'
 336
 337test_expect_success 'rm removes a submodule with a trailing /' '
 338        git reset --hard &&
 339        git submodule update &&
 340        git rm submod/ &&
 341        test ! -d submod &&
 342        git status -s -uno --ignore-submodules=none >actual &&
 343        test_cmp expect actual
 344'
 345
 346test_expect_success 'rm fails when given a file with a trailing /' '
 347        test_must_fail git rm empty/
 348'
 349
 350test_expect_success 'rm succeeds when given a directory with a trailing /' '
 351        git rm -r frotz/
 352'
 353
 354test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '
 355        git reset --hard &&
 356        git submodule update &&
 357        git -C submod checkout HEAD^ &&
 358        test_must_fail git rm submod &&
 359        test -d submod &&
 360        test -f submod/.git &&
 361        git status -s -uno --ignore-submodules=none >actual &&
 362        test_cmp expect.modified actual &&
 363        git rm -f submod &&
 364        test ! -d submod &&
 365        git status -s -uno --ignore-submodules=none >actual &&
 366        test_cmp expect actual &&
 367        test_must_fail git config -f .gitmodules submodule.sub.url &&
 368        test_must_fail git config -f .gitmodules submodule.sub.path
 369'
 370
 371test_expect_success 'rm --cached leaves work tree of populated submodules and .gitmodules alone' '
 372        git reset --hard &&
 373        git submodule update &&
 374        git rm --cached submod &&
 375        test -d submod &&
 376        test -f submod/.git &&
 377        git status -s -uno >actual &&
 378        test_cmp expect.cached actual &&
 379        git config -f .gitmodules submodule.sub.url &&
 380        git config -f .gitmodules submodule.sub.path
 381'
 382
 383test_expect_success 'rm --dry-run does not touch the submodule or .gitmodules' '
 384        git reset --hard &&
 385        git submodule update &&
 386        git rm -n submod &&
 387        test -f submod/.git &&
 388        git diff-index --exit-code HEAD
 389'
 390
 391test_expect_success 'rm does not complain when no .gitmodules file is found' '
 392        git reset --hard &&
 393        git submodule update &&
 394        git rm .gitmodules &&
 395        git rm submod >actual 2>actual.err &&
 396        test_must_be_empty actual.err &&
 397        ! test -d submod &&
 398        ! test -f submod/.git &&
 399        git status -s -uno >actual &&
 400        test_cmp expect.both_deleted actual
 401'
 402
 403test_expect_success 'rm will error out on a modified .gitmodules file unless staged' '
 404        git reset --hard &&
 405        git submodule update &&
 406        git config -f .gitmodules foo.bar true &&
 407        test_must_fail git rm submod >actual 2>actual.err &&
 408        test -s actual.err &&
 409        test -d submod &&
 410        test -f submod/.git &&
 411        git diff-files --quiet -- submod &&
 412        git add .gitmodules &&
 413        git rm submod >actual 2>actual.err &&
 414        test_must_be_empty 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 issues a warning when section is not found in .gitmodules' '
 422        git reset --hard &&
 423        git submodule update &&
 424        git config -f .gitmodules --remove-section submodule.sub &&
 425        git add .gitmodules &&
 426        echo "warning: Could not find section in .gitmodules where path=submod" >expect.err &&
 427        git rm submod >actual 2>actual.err &&
 428        test_i18ncmp expect.err actual.err &&
 429        ! test -d submod &&
 430        ! test -f submod/.git &&
 431        git status -s -uno >actual &&
 432        test_cmp expect actual
 433'
 434
 435test_expect_success 'rm of a populated submodule with modifications fails unless forced' '
 436        git reset --hard &&
 437        git submodule update &&
 438        echo X >submod/empty &&
 439        test_must_fail git rm submod &&
 440        test -d submod &&
 441        test -f submod/.git &&
 442        git status -s -uno --ignore-submodules=none >actual &&
 443        test_cmp expect.modified_inside actual &&
 444        git rm -f submod &&
 445        test ! -d submod &&
 446        git status -s -uno --ignore-submodules=none >actual &&
 447        test_cmp expect actual
 448'
 449
 450test_expect_success 'rm of a populated submodule with untracked files fails unless forced' '
 451        git reset --hard &&
 452        git submodule update &&
 453        echo X >submod/untracked &&
 454        test_must_fail git rm submod &&
 455        test -d submod &&
 456        test -f submod/.git &&
 457        git status -s -uno --ignore-submodules=none >actual &&
 458        test_cmp expect.modified_untracked actual &&
 459        git rm -f submod &&
 460        test ! -d submod &&
 461        git status -s -uno --ignore-submodules=none >actual &&
 462        test_cmp expect actual
 463'
 464
 465test_expect_success 'setup submodule conflict' '
 466        git reset --hard &&
 467        git submodule update &&
 468        git checkout -b branch1 &&
 469        echo 1 >nitfol &&
 470        git add nitfol &&
 471        git commit -m "added nitfol 1" &&
 472        git checkout -b branch2 master &&
 473        echo 2 >nitfol &&
 474        git add nitfol &&
 475        git commit -m "added nitfol 2" &&
 476        git checkout -b conflict1 master &&
 477        git -C submod fetch &&
 478        git -C submod checkout branch1 &&
 479        git add submod &&
 480        git commit -m "submod 1" &&
 481        git checkout -b conflict2 master &&
 482        git -C submod checkout branch2 &&
 483        git add submod &&
 484        git commit -m "submod 2"
 485'
 486
 487cat >expect.conflict <<EOF
 488UU submod
 489EOF
 490
 491test_expect_success 'rm removes work tree of unmodified conflicted submodule' '
 492        git checkout conflict1 &&
 493        git reset --hard &&
 494        git submodule update &&
 495        test_must_fail git merge conflict2 &&
 496        git rm submod &&
 497        test ! -d submod &&
 498        git status -s -uno --ignore-submodules=none >actual &&
 499        test_cmp expect actual
 500'
 501
 502test_expect_success 'rm of a conflicted populated submodule with different HEAD fails unless forced' '
 503        git checkout conflict1 &&
 504        git reset --hard &&
 505        git submodule update &&
 506        git -C submod checkout HEAD^ &&
 507        test_must_fail git merge conflict2 &&
 508        test_must_fail git rm submod &&
 509        test -d submod &&
 510        test -f submod/.git &&
 511        git status -s -uno --ignore-submodules=none >actual &&
 512        test_cmp expect.conflict actual &&
 513        git rm -f submod &&
 514        test ! -d submod &&
 515        git status -s -uno --ignore-submodules=none >actual &&
 516        test_cmp expect actual &&
 517        test_must_fail git config -f .gitmodules submodule.sub.url &&
 518        test_must_fail git config -f .gitmodules submodule.sub.path
 519'
 520
 521test_expect_success 'rm of a conflicted populated submodule with modifications fails unless forced' '
 522        git checkout conflict1 &&
 523        git reset --hard &&
 524        git submodule update &&
 525        echo X >submod/empty &&
 526        test_must_fail git merge conflict2 &&
 527        test_must_fail git rm submod &&
 528        test -d submod &&
 529        test -f submod/.git &&
 530        git status -s -uno --ignore-submodules=none >actual &&
 531        test_cmp expect.conflict actual &&
 532        git rm -f submod &&
 533        test ! -d submod &&
 534        git status -s -uno --ignore-submodules=none >actual &&
 535        test_cmp expect actual &&
 536        test_must_fail git config -f .gitmodules submodule.sub.url &&
 537        test_must_fail git config -f .gitmodules submodule.sub.path
 538'
 539
 540test_expect_success 'rm of a conflicted populated submodule with untracked files fails unless forced' '
 541        git checkout conflict1 &&
 542        git reset --hard &&
 543        git submodule update &&
 544        echo X >submod/untracked &&
 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 migrates git dir' '
 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                rm -r ../.git/modules/sub
 600        ) &&
 601        git rm submod 2>output.err &&
 602        ! test -d submod &&
 603        ! test -d submod/.git &&
 604        git status -s -uno --ignore-submodules=none >actual &&
 605        test -s actual &&
 606        test_i18ngrep Migrating output.err
 607'
 608
 609cat >expect.deepmodified <<EOF
 610 M submod/subsubmod
 611EOF
 612
 613test_expect_success 'setup subsubmodule' '
 614        git reset --hard &&
 615        git submodule update &&
 616        (cd submod &&
 617                git update-index --add --cacheinfo 160000 $(git rev-parse HEAD) subsubmod &&
 618                git config -f .gitmodules submodule.sub.url ../. &&
 619                git config -f .gitmodules submodule.sub.path subsubmod &&
 620                git submodule init &&
 621                git add .gitmodules &&
 622                git commit -m "add subsubmodule" &&
 623                git submodule update subsubmod
 624        ) &&
 625        git commit -a -m "added deep submodule"
 626'
 627
 628test_expect_success 'rm recursively removes work tree of unmodified submodules' '
 629        git rm submod &&
 630        test ! -d submod &&
 631        git status -s -uno --ignore-submodules=none >actual &&
 632        test_cmp expect actual
 633'
 634
 635test_expect_success 'rm of a populated nested submodule with different nested HEAD fails unless forced' '
 636        git reset --hard &&
 637        git submodule update --recursive &&
 638        git -C submod/subsubmod checkout HEAD^ &&
 639        test_must_fail git rm submod &&
 640        test -d submod &&
 641        test -f submod/.git &&
 642        git status -s -uno --ignore-submodules=none >actual &&
 643        test_cmp expect.modified_inside actual &&
 644        git rm -f submod &&
 645        test ! -d submod &&
 646        git status -s -uno --ignore-submodules=none >actual &&
 647        test_cmp expect actual
 648'
 649
 650test_expect_success 'rm of a populated nested submodule with nested modifications fails unless forced' '
 651        git reset --hard &&
 652        git submodule update --recursive &&
 653        echo X >submod/subsubmod/empty &&
 654        test_must_fail git rm submod &&
 655        test -d submod &&
 656        test -f submod/.git &&
 657        git status -s -uno --ignore-submodules=none >actual &&
 658        test_cmp expect.modified_inside actual &&
 659        git rm -f submod &&
 660        test ! -d submod &&
 661        git status -s -uno --ignore-submodules=none >actual &&
 662        test_cmp expect actual
 663'
 664
 665test_expect_success 'rm of a populated nested submodule with nested untracked files fails unless forced' '
 666        git reset --hard &&
 667        git submodule update --recursive &&
 668        echo X >submod/subsubmod/untracked &&
 669        test_must_fail git rm submod &&
 670        test -d submod &&
 671        test -f submod/.git &&
 672        git status -s -uno --ignore-submodules=none >actual &&
 673        test_cmp expect.modified_untracked actual &&
 674        git rm -f submod &&
 675        test ! -d submod &&
 676        git status -s -uno --ignore-submodules=none >actual &&
 677        test_cmp expect actual
 678'
 679
 680test_expect_success "rm absorbs submodule's nested .git directory" '
 681        git reset --hard &&
 682        git submodule update --recursive &&
 683        (cd submod/subsubmod &&
 684                rm .git &&
 685                mv ../../.git/modules/sub/modules/sub .git &&
 686                GIT_WORK_TREE=. git config --unset core.worktree
 687        ) &&
 688        git rm submod 2>output.err &&
 689        ! test -d submod &&
 690        ! test -d submod/subsubmod/.git &&
 691        git status -s -uno --ignore-submodules=none >actual &&
 692        test -s actual &&
 693        test_i18ngrep Migrating output.err
 694'
 695
 696test_expect_success 'checking out a commit after submodule removal needs manual updates' '
 697        git commit -m "submodule removal" submod .gitmodules &&
 698        git checkout HEAD^ &&
 699        git submodule update &&
 700        git checkout -q HEAD^ &&
 701        git checkout -q master 2>actual &&
 702        test_i18ngrep "^warning: unable to rmdir '\''submod'\'':" actual &&
 703        git status -s submod >actual &&
 704        echo "?? submod/" >expected &&
 705        test_cmp expected actual &&
 706        rm -rf submod &&
 707        git status -s -uno --ignore-submodules=none >actual &&
 708        test_must_be_empty actual
 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_expect_success 'rm empty string should fail' '
 873        test_must_fail git rm -rf ""
 874'
 875
 876test_done