c665acdd0a128667a93c57a7942c5bd7a44b2d8b
   1#!/bin/sh
   2#
   3# Copyright (c) 2007 Lars Hjemli
   4#
   5
   6test_description='git merge
   7
   8Testing basic merge operations/option parsing.
   9
  10! [c0] commit 0
  11 ! [c1] commit 1
  12  ! [c2] commit 2
  13   ! [c3] commit 3
  14    ! [c4] c4
  15     ! [c5] c5
  16      ! [c6] c6
  17       * [master] Merge commit 'c1'
  18--------
  19       - [master] Merge commit 'c1'
  20 +     * [c1] commit 1
  21      +  [c6] c6
  22     +   [c5] c5
  23    ++   [c4] c4
  24   ++++  [c3] commit 3
  25  +      [c2] commit 2
  26+++++++* [c0] commit 0
  27'
  28
  29. ./test-lib.sh
  30
  31printf '%s\n' 1 2 3 4 5 6 7 8 9 >file
  32printf '%s\n' '1 X' 2 3 4 5 6 7 8 9 >file.1
  33printf '%s\n' 1 2 3 4 '5 X' 6 7 8 9 >file.5
  34printf '%s\n' 1 2 3 4 5 6 7 8 '9 X' >file.9
  35printf '%s\n' '1 X' 2 3 4 5 6 7 8 9 >result.1
  36printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
  37printf '%s\n' '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
  38
  39create_merge_msgs () {
  40        echo "Merge commit 'c2'" >msg.1-5 &&
  41        echo "Merge commit 'c2'; commit 'c3'" >msg.1-5-9 &&
  42        {
  43                echo "Squashed commit of the following:" &&
  44                echo &&
  45                git log --no-merges ^HEAD c1
  46        } >squash.1 &&
  47        {
  48                echo "Squashed commit of the following:" &&
  49                echo &&
  50                git log --no-merges ^HEAD c2
  51        } >squash.1-5 &&
  52        {
  53                echo "Squashed commit of the following:" &&
  54                echo &&
  55                git log --no-merges ^HEAD c2 c3
  56        } >squash.1-5-9 &&
  57        echo >msg.nolog &&
  58        {
  59                echo "* commit 'c3':" &&
  60                echo "  commit 3" &&
  61                echo
  62        } >msg.log
  63}
  64
  65verify_merge () {
  66        test_cmp "$2" "$1" &&
  67        git update-index --refresh &&
  68        git diff --exit-code &&
  69        if test -n "$3"
  70        then
  71                git show -s --pretty=format:%s HEAD >msg.act &&
  72                test_cmp "$3" msg.act
  73        fi
  74}
  75
  76verify_head () {
  77        echo "$1" >head.expected &&
  78        git rev-parse HEAD >head.actual &&
  79        test_cmp head.expected head.actual
  80}
  81
  82verify_parents () {
  83        printf '%s\n' "$@" >parents.expected &&
  84        >parents.actual &&
  85        i=1 &&
  86        while test $i -le $#
  87        do
  88                git rev-parse HEAD^$i >>parents.actual &&
  89                i=$(expr $i + 1) ||
  90                return 1
  91        done &&
  92        test_cmp parents.expected parents.actual
  93}
  94
  95verify_mergeheads () {
  96        printf '%s\n' "$@" >mergehead.expected &&
  97        test_cmp mergehead.expected .git/MERGE_HEAD
  98}
  99
 100verify_no_mergehead () {
 101        ! test -e .git/MERGE_HEAD
 102}
 103
 104test_expect_success 'setup' '
 105        git add file &&
 106        test_tick &&
 107        git commit -m "commit 0" &&
 108        git tag c0 &&
 109        c0=$(git rev-parse HEAD) &&
 110        cp file.1 file &&
 111        git add file &&
 112        test_tick &&
 113        git commit -m "commit 1" &&
 114        git tag c1 &&
 115        c1=$(git rev-parse HEAD) &&
 116        git reset --hard "$c0" &&
 117        cp file.5 file &&
 118        git add file &&
 119        test_tick &&
 120        git commit -m "commit 2" &&
 121        git tag c2 &&
 122        c2=$(git rev-parse HEAD) &&
 123        git reset --hard "$c0" &&
 124        cp file.9 file &&
 125        git add file &&
 126        test_tick &&
 127        git commit -m "commit 3" &&
 128        git tag c3 &&
 129        c3=$(git rev-parse HEAD)
 130        git reset --hard "$c0" &&
 131        create_merge_msgs
 132'
 133
 134test_debug 'git log --graph --decorate --oneline --all'
 135
 136test_expect_success 'test option parsing' '
 137        test_must_fail git merge -$ c1 &&
 138        test_must_fail git merge --no-such c1 &&
 139        test_must_fail git merge -s foobar c1 &&
 140        test_must_fail git merge -s=foobar c1 &&
 141        test_must_fail git merge -m &&
 142        test_must_fail git merge
 143'
 144
 145test_expect_success 'merge -h with invalid index' '
 146        mkdir broken &&
 147        (
 148                cd broken &&
 149                git init &&
 150                >.git/index &&
 151                test_expect_code 129 git merge -h 2>usage
 152        ) &&
 153        grep "[Uu]sage: git merge" broken/usage
 154'
 155
 156test_expect_success 'reject non-strategy with a git-merge-foo name' '
 157        test_must_fail git merge -s index c1
 158'
 159
 160test_expect_success 'merge c0 with c1' '
 161        echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
 162
 163        git reset --hard c0 &&
 164        git merge c1 &&
 165        verify_merge file result.1 &&
 166        verify_head "$c1" &&
 167
 168        git reflog -1 >reflog.actual &&
 169        sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
 170        test_cmp reflog.expected reflog.fuzzy
 171'
 172
 173test_debug 'git log --graph --decorate --oneline --all'
 174
 175test_expect_success 'merge c0 with c1 with --ff-only' '
 176        git reset --hard c0 &&
 177        git merge --ff-only c1 &&
 178        git merge --ff-only HEAD c0 c1 &&
 179        verify_merge file result.1 &&
 180        verify_head "$c1"
 181'
 182
 183test_debug 'git log --graph --decorate --oneline --all'
 184
 185test_expect_success 'merge from unborn branch' '
 186        git checkout -f master &&
 187        test_might_fail git branch -D kid &&
 188
 189        echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
 190
 191        git checkout --orphan kid &&
 192        test_when_finished "git checkout -f master" &&
 193        git rm -fr . &&
 194        test_tick &&
 195        git merge --ff-only c1 &&
 196        verify_merge file result.1 &&
 197        verify_head "$c1" &&
 198
 199        git reflog -1 >reflog.actual &&
 200        sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
 201        test_cmp reflog.expected reflog.fuzzy
 202'
 203
 204test_debug 'git log --graph --decorate --oneline --all'
 205
 206test_expect_success 'merge c1 with c2' '
 207        git reset --hard c1 &&
 208        test_tick &&
 209        git merge c2 &&
 210        verify_merge file result.1-5 msg.1-5 &&
 211        verify_parents $c1 $c2
 212'
 213
 214test_debug 'git log --graph --decorate --oneline --all'
 215
 216test_expect_success 'merge c1 with c2 and c3' '
 217        git reset --hard c1 &&
 218        test_tick &&
 219        git merge c2 c3 &&
 220        verify_merge file result.1-5-9 msg.1-5-9 &&
 221        verify_parents $c1 $c2 $c3
 222'
 223
 224test_debug 'git log --graph --decorate --oneline --all'
 225
 226test_expect_success 'failing merges with --ff-only' '
 227        git reset --hard c1 &&
 228        test_tick &&
 229        test_must_fail git merge --ff-only c2 &&
 230        test_must_fail git merge --ff-only c3 &&
 231        test_must_fail git merge --ff-only c2 c3
 232'
 233
 234test_expect_success 'merge c0 with c1 (no-commit)' '
 235        git reset --hard c0 &&
 236        git merge --no-commit c1 &&
 237        verify_merge file result.1 &&
 238        verify_head $c1
 239'
 240
 241test_debug 'git log --graph --decorate --oneline --all'
 242
 243test_expect_success 'merge c1 with c2 (no-commit)' '
 244        git reset --hard c1 &&
 245        git merge --no-commit c2 &&
 246        verify_merge file result.1-5 &&
 247        verify_head $c1 &&
 248        verify_mergeheads $c2
 249'
 250
 251test_debug 'git log --graph --decorate --oneline --all'
 252
 253test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
 254        git reset --hard c1 &&
 255        git merge --no-commit c2 c3 &&
 256        verify_merge file result.1-5-9 &&
 257        verify_head $c1 &&
 258        verify_mergeheads $c2 $c3
 259'
 260
 261test_debug 'git log --graph --decorate --oneline --all'
 262
 263test_expect_success 'merge c0 with c1 (squash)' '
 264        git reset --hard c0 &&
 265        git merge --squash c1 &&
 266        verify_merge file result.1 &&
 267        verify_head $c0 &&
 268        verify_no_mergehead &&
 269        test_cmp squash.1 .git/SQUASH_MSG
 270'
 271
 272test_debug 'git log --graph --decorate --oneline --all'
 273
 274test_expect_success 'merge c0 with c1 (squash, ff-only)' '
 275        git reset --hard c0 &&
 276        git merge --squash --ff-only c1 &&
 277        verify_merge file result.1 &&
 278        verify_head $c0 &&
 279        verify_no_mergehead &&
 280        test_cmp squash.1 .git/SQUASH_MSG
 281'
 282
 283test_debug 'git log --graph --decorate --oneline --all'
 284
 285test_expect_success 'merge c1 with c2 (squash)' '
 286        git reset --hard c1 &&
 287        git merge --squash c2 &&
 288        verify_merge file result.1-5 &&
 289        verify_head $c1 &&
 290        verify_no_mergehead &&
 291        test_cmp squash.1-5 .git/SQUASH_MSG
 292'
 293
 294test_debug 'git log --graph --decorate --oneline --all'
 295
 296test_expect_success 'unsuccesful merge of c1 with c2 (squash, ff-only)' '
 297        git reset --hard c1 &&
 298        test_must_fail git merge --squash --ff-only c2
 299'
 300
 301test_debug 'git log --graph --decorate --oneline --all'
 302
 303test_expect_success 'merge c1 with c2 and c3 (squash)' '
 304        git reset --hard c1 &&
 305        git merge --squash c2 c3 &&
 306        verify_merge file result.1-5-9 &&
 307        verify_head $c1 &&
 308        verify_no_mergehead &&
 309        test_cmp squash.1-5-9 .git/SQUASH_MSG
 310'
 311
 312test_debug 'git log --graph --decorate --oneline --all'
 313
 314test_expect_success 'merge c1 with c2 (no-commit in config)' '
 315        git reset --hard c1 &&
 316        git config branch.master.mergeoptions "--no-commit" &&
 317        git merge c2 &&
 318        verify_merge file result.1-5 &&
 319        verify_head $c1 &&
 320        verify_mergeheads $c2
 321'
 322
 323test_debug 'git log --graph --decorate --oneline --all'
 324
 325test_expect_success 'merge c1 with c2 (squash in config)' '
 326        git reset --hard c1 &&
 327        git config branch.master.mergeoptions "--squash" &&
 328        git merge c2 &&
 329        verify_merge file result.1-5 &&
 330        verify_head $c1 &&
 331        verify_no_mergehead &&
 332        test_cmp squash.1-5 .git/SQUASH_MSG
 333'
 334
 335test_debug 'git log --graph --decorate --oneline --all'
 336
 337test_expect_success 'override config option -n with --summary' '
 338        git reset --hard c1 &&
 339        git config branch.master.mergeoptions "-n" &&
 340        test_tick &&
 341        git merge --summary c2 >diffstat.txt &&
 342        verify_merge file result.1-5 msg.1-5 &&
 343        verify_parents $c1 $c2 &&
 344        if ! grep "^ file |  *2 +-$" diffstat.txt
 345        then
 346                echo "[OOPS] diffstat was not generated with --summary"
 347                false
 348        fi
 349'
 350
 351test_expect_success 'override config option -n with --stat' '
 352        git reset --hard c1 &&
 353        git config branch.master.mergeoptions "-n" &&
 354        test_tick &&
 355        git merge --stat c2 >diffstat.txt &&
 356        verify_merge file result.1-5 msg.1-5 &&
 357        verify_parents $c1 $c2 &&
 358        if ! grep "^ file |  *2 +-$" diffstat.txt
 359        then
 360                echo "[OOPS] diffstat was not generated with --stat"
 361                false
 362        fi
 363'
 364
 365test_debug 'git log --graph --decorate --oneline --all'
 366
 367test_expect_success 'override config option --stat' '
 368        git reset --hard c1 &&
 369        git config branch.master.mergeoptions "--stat" &&
 370        test_tick &&
 371        git merge -n c2 >diffstat.txt &&
 372        verify_merge file result.1-5 msg.1-5 &&
 373        verify_parents $c1 $c2 &&
 374        if grep "^ file |  *2 +-$" diffstat.txt
 375        then
 376                echo "[OOPS] diffstat was generated"
 377                false
 378        fi
 379'
 380
 381test_debug 'git log --graph --decorate --oneline --all'
 382
 383test_expect_success 'merge c1 with c2 (override --no-commit)' '
 384        git reset --hard c1 &&
 385        git config branch.master.mergeoptions "--no-commit" &&
 386        test_tick &&
 387        git merge --commit c2 &&
 388        verify_merge file result.1-5 msg.1-5 &&
 389        verify_parents $c1 $c2
 390'
 391
 392test_debug 'git log --graph --decorate --oneline --all'
 393
 394test_expect_success 'merge c1 with c2 (override --squash)' '
 395        git reset --hard c1 &&
 396        git config branch.master.mergeoptions "--squash" &&
 397        test_tick &&
 398        git merge --no-squash c2 &&
 399        verify_merge file result.1-5 msg.1-5 &&
 400        verify_parents $c1 $c2
 401'
 402
 403test_debug 'git log --graph --decorate --oneline --all'
 404
 405test_expect_success 'merge c0 with c1 (no-ff)' '
 406        git reset --hard c0 &&
 407        git config branch.master.mergeoptions "" &&
 408        test_tick &&
 409        git merge --no-ff c1 &&
 410        verify_merge file result.1 &&
 411        verify_parents $c0 $c1
 412'
 413
 414test_debug 'git log --graph --decorate --oneline --all'
 415
 416test_expect_success 'combining --squash and --no-ff is refused' '
 417        test_must_fail git merge --squash --no-ff c1 &&
 418        test_must_fail git merge --no-ff --squash c1
 419'
 420
 421test_expect_success 'combining --ff-only and --no-ff is refused' '
 422        test_must_fail git merge --ff-only --no-ff c1 &&
 423        test_must_fail git merge --no-ff --ff-only c1
 424'
 425
 426test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
 427        git reset --hard c0 &&
 428        git config branch.master.mergeoptions "--no-ff" &&
 429        git merge --ff c1 &&
 430        verify_merge file result.1 &&
 431        verify_head $c1
 432'
 433
 434test_expect_success 'merge log message' '
 435        git reset --hard c0 &&
 436        git merge --no-log c2 &&
 437        git show -s --pretty=format:%b HEAD >msg.act &&
 438        test_cmp msg.nolog msg.act &&
 439
 440        git merge --log c3 &&
 441        git show -s --pretty=format:%b HEAD >msg.act &&
 442        test_cmp msg.log msg.act &&
 443
 444        git reset --hard HEAD^ &&
 445        git config merge.log yes &&
 446        git merge c3 &&
 447        git show -s --pretty=format:%b HEAD >msg.act &&
 448        test_cmp msg.log msg.act
 449'
 450
 451test_debug 'git log --graph --decorate --oneline --all'
 452
 453test_expect_success 'merge c1 with c0, c2, c0, and c1' '
 454       git reset --hard c1 &&
 455       git config branch.master.mergeoptions "" &&
 456       test_tick &&
 457       git merge c0 c2 c0 c1 &&
 458       verify_merge file result.1-5 &&
 459       verify_parents $c1 $c2
 460'
 461
 462test_debug 'git log --graph --decorate --oneline --all'
 463
 464test_expect_success 'merge c1 with c0, c2, c0, and c1' '
 465       git reset --hard c1 &&
 466       git config branch.master.mergeoptions "" &&
 467       test_tick &&
 468       git merge c0 c2 c0 c1 &&
 469       verify_merge file result.1-5 &&
 470       verify_parents $c1 $c2
 471'
 472
 473test_debug 'git log --graph --decorate --oneline --all'
 474
 475test_expect_success 'merge c1 with c1 and c2' '
 476       git reset --hard c1 &&
 477       git config branch.master.mergeoptions "" &&
 478       test_tick &&
 479       git merge c1 c2 &&
 480       verify_merge file result.1-5 &&
 481       verify_parents $c1 $c2
 482'
 483
 484test_debug 'git log --graph --decorate --oneline --all'
 485
 486test_expect_success 'merge fast-forward in a dirty tree' '
 487       git reset --hard c0 &&
 488       mv file file1 &&
 489       cat file1 >file &&
 490       rm -f file1 &&
 491       git merge c2
 492'
 493
 494test_debug 'git log --graph --decorate --oneline --all'
 495
 496test_expect_success C_LOCALE_OUTPUT 'in-index merge' '
 497        git reset --hard c0 &&
 498        git merge --no-ff -s resolve c1 >out &&
 499        grep "Wonderful." out &&
 500        verify_parents $c0 $c1
 501'
 502
 503test_debug 'git log --graph --decorate --oneline --all'
 504
 505test_expect_success 'refresh the index before merging' '
 506        git reset --hard c1 &&
 507        cp file file.n && mv -f file.n file &&
 508        git merge c3
 509'
 510
 511cat >expected.branch <<\EOF
 512Merge branch 'c5-branch' (early part)
 513EOF
 514cat >expected.tag <<\EOF
 515Merge commit 'c5~1'
 516EOF
 517
 518test_expect_success 'merge early part of c2' '
 519        git reset --hard c3 &&
 520        echo c4 >c4.c &&
 521        git add c4.c &&
 522        git commit -m c4 &&
 523        git tag c4 &&
 524        echo c5 >c5.c &&
 525        git add c5.c &&
 526        git commit -m c5 &&
 527        git tag c5 &&
 528        git reset --hard c3 &&
 529        echo c6 >c6.c &&
 530        git add c6.c &&
 531        git commit -m c6 &&
 532        git tag c6 &&
 533        git branch -f c5-branch c5 &&
 534        git merge c5-branch~1 &&
 535        git show -s --pretty=format:%s HEAD >actual.branch &&
 536        git reset --keep HEAD^ &&
 537        git merge c5~1 &&
 538        git show -s --pretty=format:%s HEAD >actual.tag &&
 539        test_cmp expected.branch actual.branch &&
 540        test_cmp expected.tag actual.tag
 541'
 542
 543test_debug 'git log --graph --decorate --oneline --all'
 544
 545test_expect_success 'merge --no-ff --no-commit && commit' '
 546        git reset --hard c0 &&
 547        git merge --no-ff --no-commit c1 &&
 548        EDITOR=: git commit &&
 549        verify_parents $c0 $c1
 550'
 551
 552test_debug 'git log --graph --decorate --oneline --all'
 553
 554test_expect_success 'amending no-ff merge commit' '
 555        EDITOR=: git commit --amend &&
 556        verify_parents $c0 $c1
 557'
 558
 559test_debug 'git log --graph --decorate --oneline --all'
 560
 561test_done