28b5f847a55142bd0b3a12fc67c7c37440d64a78
   1#!/bin/sh
   2#
   3# Copyright (c) 2008 Charles Bailey
   4#
   5
   6test_description='git mergetool
   7
   8Testing basic merge tool invocation'
   9
  10. ./test-lib.sh
  11
  12# All the mergetool test work by checking out a temporary branch based
  13# off 'branch1' and then merging in master and checking the results of
  14# running mergetool
  15
  16test_expect_success 'setup' '
  17        test_config rerere.enabled true &&
  18        echo master >file1 &&
  19        echo master spaced >"spaced name" &&
  20        echo master file11 >file11 &&
  21        echo master file12 >file12 &&
  22        echo master file13 >file13 &&
  23        echo master file14 >file14 &&
  24        mkdir subdir &&
  25        echo master sub >subdir/file3 &&
  26        test_create_repo submod &&
  27        (
  28                cd submod &&
  29                : >foo &&
  30                git add foo &&
  31                git commit -m "Add foo"
  32        ) &&
  33        git submodule add git://example.com/submod submod &&
  34        git add file1 "spaced name" file1[1-4] subdir/file3 .gitmodules submod &&
  35        git commit -m "add initial versions" &&
  36
  37        git checkout -b branch1 master &&
  38        git submodule update -N &&
  39        echo branch1 change >file1 &&
  40        echo branch1 newfile >file2 &&
  41        echo branch1 spaced >"spaced name" &&
  42        echo branch1 both added >both &&
  43        echo branch1 change file11 >file11 &&
  44        echo branch1 change file13 >file13 &&
  45        echo branch1 sub >subdir/file3 &&
  46        (
  47                cd submod &&
  48                echo branch1 submodule >bar &&
  49                git add bar &&
  50                git commit -m "Add bar on branch1" &&
  51                git checkout -b submod-branch1
  52        ) &&
  53        git add file1 "spaced name" file11 file13 file2 subdir/file3 submod &&
  54        git add both &&
  55        git rm file12 &&
  56        git commit -m "branch1 changes" &&
  57
  58        git checkout -b delete-base branch1 &&
  59        mkdir -p a/a &&
  60        (echo one; echo two; echo 3; echo 4) >a/a/file.txt &&
  61        git add a/a/file.txt &&
  62        git commit -m"base file" &&
  63        git checkout -b move-to-b delete-base &&
  64        mkdir -p b/b &&
  65        git mv a/a/file.txt b/b/file.txt &&
  66        (echo one; echo two; echo 4) >b/b/file.txt &&
  67        git commit -a -m"move to b" &&
  68        git checkout -b move-to-c delete-base &&
  69        mkdir -p c/c &&
  70        git mv a/a/file.txt c/c/file.txt &&
  71        (echo one; echo two; echo 3) >c/c/file.txt &&
  72        git commit -a -m"move to c" &&
  73
  74        git checkout -b stash1 master &&
  75        echo stash1 change file11 >file11 &&
  76        git add file11 &&
  77        git commit -m "stash1 changes" &&
  78
  79        git checkout -b stash2 master &&
  80        echo stash2 change file11 >file11 &&
  81        git add file11 &&
  82        git commit -m "stash2 changes" &&
  83
  84        git checkout master &&
  85        git submodule update -N &&
  86        echo master updated >file1 &&
  87        echo master new >file2 &&
  88        echo master updated spaced >"spaced name" &&
  89        echo master both added >both &&
  90        echo master updated file12 >file12 &&
  91        echo master updated file14 >file14 &&
  92        echo master new sub >subdir/file3 &&
  93        (
  94                cd submod &&
  95                echo master submodule >bar &&
  96                git add bar &&
  97                git commit -m "Add bar on master" &&
  98                git checkout -b submod-master
  99        ) &&
 100        git add file1 "spaced name" file12 file14 file2 subdir/file3 submod &&
 101        git add both &&
 102        git rm file11 &&
 103        git commit -m "master updates" &&
 104
 105        git clean -fdx &&
 106        git checkout -b order-file-start master &&
 107        echo start >a &&
 108        echo start >b &&
 109        git add a b &&
 110        git commit -m start &&
 111        git checkout -b order-file-side1 order-file-start &&
 112        echo side1 >a &&
 113        echo side1 >b &&
 114        git add a b &&
 115        git commit -m side1 &&
 116        git checkout -b order-file-side2 order-file-start &&
 117        echo side2 >a &&
 118        echo side2 >b &&
 119        git add a b &&
 120        git commit -m side2 &&
 121
 122        git config merge.tool mytool &&
 123        git config mergetool.mytool.cmd "cat \"\$REMOTE\" >\"\$MERGED\"" &&
 124        git config mergetool.mytool.trustExitCode true &&
 125        git config mergetool.mybase.cmd "cat \"\$BASE\" >\"\$MERGED\"" &&
 126        git config mergetool.mybase.trustExitCode true
 127'
 128
 129test_expect_success 'custom mergetool' '
 130        test_when_finished "git reset --hard" &&
 131        git checkout -b test$test_count branch1 &&
 132        git submodule update -N &&
 133        test_must_fail git merge master >/dev/null 2>&1 &&
 134        ( yes "" | git mergetool both >/dev/null 2>&1 ) &&
 135        ( yes "" | git mergetool file1 file1 ) &&
 136        ( yes "" | git mergetool file2 "spaced name" >/dev/null 2>&1 ) &&
 137        ( yes "" | git mergetool subdir/file3 >/dev/null 2>&1 ) &&
 138        ( yes "d" | git mergetool file11 >/dev/null 2>&1 ) &&
 139        ( yes "d" | git mergetool file12 >/dev/null 2>&1 ) &&
 140        ( yes "l" | git mergetool submod >/dev/null 2>&1 ) &&
 141        test "$(cat file1)" = "master updated" &&
 142        test "$(cat file2)" = "master new" &&
 143        test "$(cat subdir/file3)" = "master new sub" &&
 144        test "$(cat submod/bar)" = "branch1 submodule" &&
 145        git commit -m "branch1 resolved with mergetool"
 146'
 147
 148test_expect_success 'mergetool crlf' '
 149        test_when_finished "git reset --hard" &&
 150        # This test_config line must go after the above reset line so that
 151        # core.autocrlf is unconfigured before reset runs.  (The
 152        # test_config command uses test_when_finished internally and
 153        # test_when_finished is LIFO.)
 154        test_config core.autocrlf true &&
 155        git checkout -b test$test_count branch1 &&
 156        test_must_fail git merge master >/dev/null 2>&1 &&
 157        ( yes "" | git mergetool file1 >/dev/null 2>&1 ) &&
 158        ( yes "" | git mergetool file2 >/dev/null 2>&1 ) &&
 159        ( yes "" | git mergetool "spaced name" >/dev/null 2>&1 ) &&
 160        ( yes "" | git mergetool both >/dev/null 2>&1 ) &&
 161        ( yes "" | git mergetool subdir/file3 >/dev/null 2>&1 ) &&
 162        ( yes "d" | git mergetool file11 >/dev/null 2>&1 ) &&
 163        ( yes "d" | git mergetool file12 >/dev/null 2>&1 ) &&
 164        ( yes "r" | git mergetool submod >/dev/null 2>&1 ) &&
 165        test "$(printf x | cat file1 -)" = "$(printf "master updated\r\nx")" &&
 166        test "$(printf x | cat file2 -)" = "$(printf "master new\r\nx")" &&
 167        test "$(printf x | cat subdir/file3 -)" = "$(printf "master new sub\r\nx")" &&
 168        git submodule update -N &&
 169        test "$(cat submod/bar)" = "master submodule" &&
 170        git commit -m "branch1 resolved with mergetool - autocrlf"
 171'
 172
 173test_expect_success 'mergetool in subdir' '
 174        test_when_finished "git reset --hard" &&
 175        git checkout -b test$test_count branch1 &&
 176        git submodule update -N &&
 177        (
 178                cd subdir &&
 179                test_must_fail git merge master >/dev/null 2>&1 &&
 180                ( yes "" | git mergetool file3 >/dev/null 2>&1 ) &&
 181                test "$(cat file3)" = "master new sub"
 182        )
 183'
 184
 185test_expect_success 'mergetool on file in parent dir' '
 186        test_when_finished "git reset --hard" &&
 187        git submodule update -N &&
 188        (
 189                cd subdir &&
 190                test_must_fail git merge master >/dev/null 2>&1 &&
 191                ( yes "" | git mergetool file3 >/dev/null 2>&1 ) &&
 192                ( yes "" | git mergetool ../file1 >/dev/null 2>&1 ) &&
 193                ( yes "" | git mergetool ../file2 ../spaced\ name >/dev/null 2>&1 ) &&
 194                ( yes "" | git mergetool ../both >/dev/null 2>&1 ) &&
 195                ( yes "d" | git mergetool ../file11 >/dev/null 2>&1 ) &&
 196                ( yes "d" | git mergetool ../file12 >/dev/null 2>&1 ) &&
 197                ( yes "l" | git mergetool ../submod >/dev/null 2>&1 ) &&
 198                test "$(cat ../file1)" = "master updated" &&
 199                test "$(cat ../file2)" = "master new" &&
 200                test "$(cat ../submod/bar)" = "branch1 submodule" &&
 201                git commit -m "branch1 resolved with mergetool - subdir"
 202        )
 203'
 204
 205test_expect_success 'mergetool skips autoresolved' '
 206        test_when_finished "git reset --hard" &&
 207        git checkout -b test$test_count branch1 &&
 208        git submodule update -N &&
 209        test_must_fail git merge master &&
 210        test -n "$(git ls-files -u)" &&
 211        ( yes "d" | git mergetool file11 >/dev/null 2>&1 ) &&
 212        ( yes "d" | git mergetool file12 >/dev/null 2>&1 ) &&
 213        ( yes "l" | git mergetool submod >/dev/null 2>&1 ) &&
 214        output="$(git mergetool --no-prompt)" &&
 215        test "$output" = "No files need merging"
 216'
 217
 218test_expect_success 'mergetool merges all from subdir' '
 219        test_when_finished "git reset --hard" &&
 220        test_config rerere.enabled false &&
 221        (
 222                cd subdir &&
 223                test_must_fail git merge master &&
 224                ( yes "r" | git mergetool ../submod ) &&
 225                ( yes "d" "d" | git mergetool --no-prompt ) &&
 226                test "$(cat ../file1)" = "master updated" &&
 227                test "$(cat ../file2)" = "master new" &&
 228                test "$(cat file3)" = "master new sub" &&
 229                ( cd .. && git submodule update -N ) &&
 230                test "$(cat ../submod/bar)" = "master submodule" &&
 231                git commit -m "branch2 resolved by mergetool from subdir"
 232        )
 233'
 234
 235test_expect_success 'mergetool skips resolved paths when rerere is active' '
 236        test_when_finished "git reset --hard" &&
 237        test_config rerere.enabled true &&
 238        rm -rf .git/rr-cache &&
 239        git checkout -b test$test_count branch1 &&
 240        git submodule update -N &&
 241        test_must_fail git merge master >/dev/null 2>&1 &&
 242        ( yes "l" | git mergetool --no-prompt submod >/dev/null 2>&1 ) &&
 243        ( yes "d" "d" | git mergetool --no-prompt >/dev/null 2>&1 ) &&
 244        git submodule update -N &&
 245        output="$(yes "n" | git mergetool --no-prompt)" &&
 246        test "$output" = "No files need merging"
 247'
 248
 249test_expect_success 'conflicted stash sets up rerere'  '
 250        test_when_finished "git reset --hard" &&
 251        test_config rerere.enabled true &&
 252        git checkout stash1 &&
 253        echo "Conflicting stash content" >file11 &&
 254        git stash &&
 255
 256        git checkout --detach stash2 &&
 257        test_must_fail git stash apply &&
 258
 259        test -n "$(git ls-files -u)" &&
 260        conflicts="$(git rerere remaining)" &&
 261        test "$conflicts" = "file11" &&
 262        output="$(git mergetool --no-prompt)" &&
 263        test "$output" != "No files need merging" &&
 264
 265        git commit -am "save the stash resolution" &&
 266
 267        git reset --hard stash2 &&
 268        test_must_fail git stash apply &&
 269
 270        test -n "$(git ls-files -u)" &&
 271        conflicts="$(git rerere remaining)" &&
 272        test -z "$conflicts" &&
 273        output="$(git mergetool --no-prompt)" &&
 274        test "$output" = "No files need merging"
 275'
 276
 277test_expect_success 'mergetool takes partial path' '
 278        test_when_finished "git reset --hard" &&
 279        test_config rerere.enabled false &&
 280        git checkout -b test$test_count branch1 &&
 281        git submodule update -N &&
 282        test_must_fail git merge master &&
 283
 284        ( yes "" | git mergetool subdir ) &&
 285
 286        test "$(cat subdir/file3)" = "master new sub"
 287'
 288
 289test_expect_success 'mergetool delete/delete conflict' '
 290        test_when_finished "git reset --hard HEAD" &&
 291        git checkout move-to-c &&
 292        test_must_fail git merge move-to-b &&
 293        echo d | git mergetool a/a/file.txt &&
 294        ! test -f a/a/file.txt &&
 295        git reset --hard HEAD &&
 296        test_must_fail git merge move-to-b &&
 297        echo m | git mergetool a/a/file.txt &&
 298        test -f b/b/file.txt &&
 299        git reset --hard HEAD &&
 300        test_must_fail git merge move-to-b &&
 301        ! echo a | git mergetool a/a/file.txt &&
 302        ! test -f a/a/file.txt
 303'
 304
 305test_expect_success 'mergetool produces no errors when keepBackup is used' '
 306        test_when_finished "git reset --hard HEAD" &&
 307        test_config mergetool.keepBackup true &&
 308        test_must_fail git merge move-to-b &&
 309        : >expect &&
 310        echo d | git mergetool a/a/file.txt 2>actual &&
 311        test_cmp expect actual &&
 312        ! test -d a
 313'
 314
 315test_expect_success 'mergetool honors tempfile config for deleted files' '
 316        test_when_finished "git reset --hard HEAD" &&
 317        test_config mergetool.keepTemporaries false &&
 318        test_must_fail git merge move-to-b &&
 319        echo d | git mergetool a/a/file.txt &&
 320        ! test -d a
 321'
 322
 323test_expect_success 'mergetool keeps tempfiles when aborting delete/delete' '
 324        test_when_finished "git reset --hard HEAD" &&
 325        test_when_finished "git clean -fdx" &&
 326        test_config mergetool.keepTemporaries true &&
 327        test_must_fail git merge move-to-b &&
 328        ! (echo a; echo n) | git mergetool a/a/file.txt &&
 329        test -d a/a &&
 330        cat >expect <<-\EOF &&
 331        file_BASE_.txt
 332        file_LOCAL_.txt
 333        file_REMOTE_.txt
 334        EOF
 335        ls -1 a/a | sed -e "s/[0-9]*//g" >actual &&
 336        test_cmp expect actual
 337'
 338
 339test_expect_success 'deleted vs modified submodule' '
 340        test_when_finished "git reset --hard HEAD" &&
 341        git checkout -b test$test_count branch1 &&
 342        git submodule update -N &&
 343        mv submod submod-movedaside &&
 344        git rm --cached submod &&
 345        git commit -m "Submodule deleted from branch" &&
 346        git checkout -b test$test_count.a test$test_count &&
 347        test_must_fail git merge master &&
 348        test -n "$(git ls-files -u)" &&
 349        ( yes "" | git mergetool file1 file2 spaced\ name subdir/file3 >/dev/null 2>&1 ) &&
 350        ( yes "" | git mergetool both >/dev/null 2>&1 ) &&
 351        ( yes "d" | git mergetool file11 file12 >/dev/null 2>&1 ) &&
 352        ( yes "r" | git mergetool submod ) &&
 353        rmdir submod && mv submod-movedaside submod &&
 354        test "$(cat submod/bar)" = "branch1 submodule" &&
 355        git submodule update -N &&
 356        test "$(cat submod/bar)" = "master submodule" &&
 357        output="$(git mergetool --no-prompt)" &&
 358        test "$output" = "No files need merging" &&
 359        git commit -m "Merge resolved by keeping module" &&
 360
 361        mv submod submod-movedaside &&
 362        git checkout -b test$test_count.b test$test_count &&
 363        git submodule update -N &&
 364        test_must_fail git merge master &&
 365        test -n "$(git ls-files -u)" &&
 366        ( yes "" | git mergetool file1 file2 spaced\ name subdir/file3 >/dev/null 2>&1 ) &&
 367        ( yes "" | git mergetool both >/dev/null 2>&1 ) &&
 368        ( yes "d" | git mergetool file11 file12 >/dev/null 2>&1 ) &&
 369        ( yes "l" | git mergetool submod ) &&
 370        test ! -e submod &&
 371        output="$(git mergetool --no-prompt)" &&
 372        test "$output" = "No files need merging" &&
 373        git commit -m "Merge resolved by deleting module" &&
 374
 375        mv submod-movedaside submod &&
 376        git checkout -b test$test_count.c master &&
 377        git submodule update -N &&
 378        test_must_fail git merge test$test_count &&
 379        test -n "$(git ls-files -u)" &&
 380        ( yes "" | git mergetool file1 file2 spaced\ name subdir/file3 >/dev/null 2>&1 ) &&
 381        ( yes "" | git mergetool both >/dev/null 2>&1 ) &&
 382        ( yes "d" | git mergetool file11 file12 >/dev/null 2>&1 ) &&
 383        ( yes "r" | git mergetool submod ) &&
 384        test ! -e submod &&
 385        test -d submod.orig &&
 386        git submodule update -N &&
 387        output="$(git mergetool --no-prompt)" &&
 388        test "$output" = "No files need merging" &&
 389        git commit -m "Merge resolved by deleting module" &&
 390        mv submod.orig submod &&
 391
 392        git checkout -b test$test_count.d master &&
 393        git submodule update -N &&
 394        test_must_fail git merge test$test_count &&
 395        test -n "$(git ls-files -u)" &&
 396        ( yes "" | git mergetool file1 file2 spaced\ name subdir/file3 >/dev/null 2>&1 ) &&
 397        ( yes "" | git mergetool both >/dev/null 2>&1 ) &&
 398        ( yes "d" | git mergetool file11 file12 >/dev/null 2>&1 ) &&
 399        ( yes "l" | git mergetool submod ) &&
 400        test "$(cat submod/bar)" = "master submodule" &&
 401        git submodule update -N &&
 402        test "$(cat submod/bar)" = "master submodule" &&
 403        output="$(git mergetool --no-prompt)" &&
 404        test "$output" = "No files need merging" &&
 405        git commit -m "Merge resolved by keeping module"
 406'
 407
 408test_expect_success 'file vs modified submodule' '
 409        test_when_finished "git reset --hard" &&
 410        git checkout -b test$test_count branch1 &&
 411        git submodule update -N &&
 412        mv submod submod-movedaside &&
 413        git rm --cached submod &&
 414        echo not a submodule >submod &&
 415        git add submod &&
 416        git commit -m "Submodule path becomes file" &&
 417        git checkout -b test$test_count.a branch1 &&
 418        test_must_fail git merge master &&
 419        test -n "$(git ls-files -u)" &&
 420        ( yes "" | git mergetool file1 file2 spaced\ name subdir/file3 >/dev/null 2>&1 ) &&
 421        ( yes "" | git mergetool both >/dev/null 2>&1 ) &&
 422        ( yes "d" | git mergetool file11 file12 >/dev/null 2>&1 ) &&
 423        ( yes "r" | git mergetool submod ) &&
 424        rmdir submod && mv submod-movedaside submod &&
 425        test "$(cat submod/bar)" = "branch1 submodule" &&
 426        git submodule update -N &&
 427        test "$(cat submod/bar)" = "master submodule" &&
 428        output="$(git mergetool --no-prompt)" &&
 429        test "$output" = "No files need merging" &&
 430        git commit -m "Merge resolved by keeping module" &&
 431
 432        mv submod submod-movedaside &&
 433        git checkout -b test$test_count.b test$test_count &&
 434        test_must_fail git merge master &&
 435        test -n "$(git ls-files -u)" &&
 436        ( yes "" | git mergetool file1 file2 spaced\ name subdir/file3 >/dev/null 2>&1 ) &&
 437        ( yes "" | git mergetool both >/dev/null 2>&1 ) &&
 438        ( yes "d" | git mergetool file11 file12 >/dev/null 2>&1 ) &&
 439        ( yes "l" | git mergetool submod ) &&
 440        git submodule update -N &&
 441        test "$(cat submod)" = "not a submodule" &&
 442        output="$(git mergetool --no-prompt)" &&
 443        test "$output" = "No files need merging" &&
 444        git commit -m "Merge resolved by keeping file" &&
 445
 446        git checkout -b test$test_count.c master &&
 447        rmdir submod && mv submod-movedaside submod &&
 448        test ! -e submod.orig &&
 449        git submodule update -N &&
 450        test_must_fail git merge test$test_count &&
 451        test -n "$(git ls-files -u)" &&
 452        ( yes "" | git mergetool file1 file2 spaced\ name subdir/file3 >/dev/null 2>&1 ) &&
 453        ( yes "" | git mergetool both >/dev/null 2>&1 ) &&
 454        ( yes "d" | git mergetool file11 file12 >/dev/null 2>&1 ) &&
 455        ( yes "r" | git mergetool submod ) &&
 456        test -d submod.orig &&
 457        git submodule update -N &&
 458        test "$(cat submod)" = "not a submodule" &&
 459        output="$(git mergetool --no-prompt)" &&
 460        test "$output" = "No files need merging" &&
 461        git commit -m "Merge resolved by keeping file" &&
 462
 463        git checkout -b test$test_count.d master &&
 464        rmdir submod && mv submod.orig submod &&
 465        git submodule update -N &&
 466        test_must_fail git merge test$test_count &&
 467        test -n "$(git ls-files -u)" &&
 468        ( yes "" | git mergetool file1 file2 spaced\ name subdir/file3 >/dev/null 2>&1 ) &&
 469        ( yes "" | git mergetool both>/dev/null 2>&1 ) &&
 470        ( yes "d" | git mergetool file11 file12 >/dev/null 2>&1 ) &&
 471        ( yes "l" | git mergetool submod ) &&
 472        test "$(cat submod/bar)" = "master submodule" &&
 473        git submodule update -N &&
 474        test "$(cat submod/bar)" = "master submodule" &&
 475        output="$(git mergetool --no-prompt)" &&
 476        test "$output" = "No files need merging" &&
 477        git commit -m "Merge resolved by keeping module"
 478'
 479
 480test_expect_success 'submodule in subdirectory' '
 481        test_when_finished "git reset --hard" &&
 482        git checkout -b test$test_count branch1 &&
 483        git submodule update -N &&
 484        (
 485                cd subdir &&
 486                test_create_repo subdir_module &&
 487                (
 488                cd subdir_module &&
 489                : >file15 &&
 490                git add file15 &&
 491                git commit -m "add initial versions"
 492                )
 493        ) &&
 494        test_when_finished "rm -rf subdir/subdir_module" &&
 495        git submodule add git://example.com/subsubmodule subdir/subdir_module &&
 496        git add subdir/subdir_module &&
 497        git commit -m "add submodule in subdirectory" &&
 498
 499        git checkout -b test$test_count.a test$test_count &&
 500        git submodule update -N &&
 501        (
 502        cd subdir/subdir_module &&
 503                git checkout -b super10.a &&
 504                echo test$test_count.a >file15 &&
 505                git add file15 &&
 506                git commit -m "on branch 10.a"
 507        ) &&
 508        git add subdir/subdir_module &&
 509        git commit -m "change submodule in subdirectory on test$test_count.a" &&
 510
 511        git checkout -b test$test_count.b test$test_count &&
 512        git submodule update -N &&
 513        (
 514                cd subdir/subdir_module &&
 515                git checkout -b super10.b &&
 516                echo test$test_count.b >file15 &&
 517                git add file15 &&
 518                git commit -m "on branch 10.b"
 519        ) &&
 520        git add subdir/subdir_module &&
 521        git commit -m "change submodule in subdirectory on test$test_count.b" &&
 522
 523        test_must_fail git merge test$test_count.a >/dev/null 2>&1 &&
 524        (
 525                cd subdir &&
 526                ( yes "l" | git mergetool subdir_module )
 527        ) &&
 528        test "$(cat subdir/subdir_module/file15)" = "test$test_count.b" &&
 529        git submodule update -N &&
 530        test "$(cat subdir/subdir_module/file15)" = "test$test_count.b" &&
 531        git reset --hard &&
 532        git submodule update -N &&
 533
 534        test_must_fail git merge test$test_count.a >/dev/null 2>&1 &&
 535        ( yes "r" | git mergetool subdir/subdir_module ) &&
 536        test "$(cat subdir/subdir_module/file15)" = "test$test_count.b" &&
 537        git submodule update -N &&
 538        test "$(cat subdir/subdir_module/file15)" = "test$test_count.a" &&
 539        git commit -m "branch1 resolved with mergetool"
 540'
 541
 542test_expect_success 'directory vs modified submodule' '
 543        test_when_finished "git reset --hard" &&
 544        git checkout -b test$test_count branch1 &&
 545        mv submod submod-movedaside &&
 546        git rm --cached submod &&
 547        mkdir submod &&
 548        echo not a submodule >submod/file16 &&
 549        git add submod/file16 &&
 550        git commit -m "Submodule path becomes directory" &&
 551
 552        test_must_fail git merge master &&
 553        test -n "$(git ls-files -u)" &&
 554        ( yes "l" | git mergetool submod ) &&
 555        test "$(cat submod/file16)" = "not a submodule" &&
 556        rm -rf submod.orig &&
 557
 558        git reset --hard >/dev/null 2>&1 &&
 559        test_must_fail git merge master &&
 560        test -n "$(git ls-files -u)" &&
 561        test ! -e submod.orig &&
 562        ( yes "r" | git mergetool submod ) &&
 563        test -d submod.orig &&
 564        test "$(cat submod.orig/file16)" = "not a submodule" &&
 565        rm -r submod.orig &&
 566        mv submod-movedaside/.git submod &&
 567        ( cd submod && git clean -f && git reset --hard ) &&
 568        git submodule update -N &&
 569        test "$(cat submod/bar)" = "master submodule" &&
 570        git reset --hard >/dev/null 2>&1 && rm -rf submod-movedaside &&
 571
 572        git checkout -b test$test_count.c master &&
 573        git submodule update -N &&
 574        test_must_fail git merge test$test_count &&
 575        test -n "$(git ls-files -u)" &&
 576        ( yes "l" | git mergetool submod ) &&
 577        git submodule update -N &&
 578        test "$(cat submod/bar)" = "master submodule" &&
 579
 580        git reset --hard >/dev/null 2>&1 &&
 581        git submodule update -N &&
 582        test_must_fail git merge test$test_count &&
 583        test -n "$(git ls-files -u)" &&
 584        test ! -e submod.orig &&
 585        ( yes "r" | git mergetool submod ) &&
 586        test "$(cat submod/file16)" = "not a submodule" &&
 587
 588        git reset --hard master >/dev/null 2>&1 &&
 589        ( cd submod && git clean -f && git reset --hard ) &&
 590        git submodule update -N
 591'
 592
 593test_expect_success 'file with no base' '
 594        test_when_finished "git reset --hard master >/dev/null 2>&1" &&
 595        git checkout -b test$test_count branch1 &&
 596        test_must_fail git merge master &&
 597        git mergetool --no-prompt --tool mybase -- both &&
 598        >expected &&
 599        test_cmp both expected
 600'
 601
 602test_expect_success 'custom commands override built-ins' '
 603        test_when_finished "git reset --hard master >/dev/null 2>&1" &&
 604        git checkout -b test$test_count branch1 &&
 605        test_config mergetool.defaults.cmd "cat \"\$REMOTE\" >\"\$MERGED\"" &&
 606        test_config mergetool.defaults.trustExitCode true &&
 607        test_must_fail git merge master &&
 608        git mergetool --no-prompt --tool defaults -- both &&
 609        echo master both added >expected &&
 610        test_cmp both expected
 611'
 612
 613test_expect_success 'filenames seen by tools start with ./' '
 614        test_when_finished "git reset --hard master >/dev/null 2>&1" &&
 615        git checkout -b test$test_count branch1 &&
 616        test_config mergetool.writeToTemp false &&
 617        test_config mergetool.myecho.cmd "echo \"\$LOCAL\"" &&
 618        test_config mergetool.myecho.trustExitCode true &&
 619        test_must_fail git merge master &&
 620        git mergetool --no-prompt --tool myecho -- both >actual &&
 621        grep ^\./both_LOCAL_ actual >/dev/null
 622'
 623
 624test_lazy_prereq MKTEMP '
 625        tempdir=$(mktemp -d -t foo.XXXXXX) &&
 626        test -d "$tempdir" &&
 627        rmdir "$tempdir"
 628'
 629
 630test_expect_success MKTEMP 'temporary filenames are used with mergetool.writeToTemp' '
 631        test_when_finished "git reset --hard master >/dev/null 2>&1" &&
 632        git checkout -b test$test_count branch1 &&
 633        test_config mergetool.writeToTemp true &&
 634        test_config mergetool.myecho.cmd "echo \"\$LOCAL\"" &&
 635        test_config mergetool.myecho.trustExitCode true &&
 636        test_must_fail git merge master &&
 637        git mergetool --no-prompt --tool myecho -- both >actual &&
 638        test_must_fail grep ^\./both_LOCAL_ actual >/dev/null &&
 639        grep /both_LOCAL_ actual >/dev/null
 640'
 641
 642test_expect_success 'diff.orderFile configuration is honored' '
 643        test_when_finished "git reset --hard >/dev/null" &&
 644        git checkout order-file-side2 &&
 645        test_config diff.orderFile order-file &&
 646        test_config mergetool.myecho.cmd "echo \"\$LOCAL\"" &&
 647        test_config mergetool.myecho.trustExitCode true &&
 648        echo b >order-file &&
 649        echo a >>order-file &&
 650        test_must_fail git merge order-file-side1 &&
 651        cat >expect <<-\EOF &&
 652                Merging:
 653                b
 654                a
 655        EOF
 656        git mergetool --no-prompt --tool myecho >output &&
 657        git grep --no-index -h -A2 Merging: output >actual &&
 658        test_cmp expect actual
 659'
 660test_expect_success 'mergetool -Oorder-file is honored' '
 661        test_when_finished "git reset --hard >/dev/null 2>&1" &&
 662        test_config diff.orderFile order-file &&
 663        test_config mergetool.myecho.cmd "echo \"\$LOCAL\"" &&
 664        test_config mergetool.myecho.trustExitCode true &&
 665        echo b >order-file &&
 666        echo a >>order-file &&
 667        test_must_fail git merge order-file-side1 &&
 668        cat >expect <<-\EOF &&
 669                Merging:
 670                a
 671                b
 672        EOF
 673        git mergetool -O/dev/null --no-prompt --tool myecho >output &&
 674        git grep --no-index -h -A2 Merging: output >actual &&
 675        test_cmp expect actual &&
 676        git reset --hard >/dev/null 2>&1 &&
 677
 678        git config --unset diff.orderFile &&
 679        test_must_fail git merge order-file-side1 &&
 680        cat >expect <<-\EOF &&
 681                Merging:
 682                b
 683                a
 684        EOF
 685        git mergetool -Oorder-file --no-prompt --tool myecho >output &&
 686        git grep --no-index -h -A2 Merging: output >actual &&
 687        test_cmp expect actual
 688'
 689
 690test_done