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