t / t7600-merge.shon commit Merge branch 'lt/core-optim' (dccb3a6)
   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. ./test-lib.sh
  11
  12cat >file <<EOF
  131
  142
  153
  164
  175
  186
  197
  208
  219
  22EOF
  23
  24cat >file.1 <<EOF
  251 X
  262
  273
  284
  295
  306
  317
  328
  339
  34EOF
  35
  36cat >file.5 <<EOF
  371
  382
  393
  404
  415 X
  426
  437
  448
  459
  46EOF
  47
  48cat >file.9 <<EOF
  491
  502
  513
  524
  535
  546
  557
  568
  579 X
  58EOF
  59
  60cat  >result.1 <<EOF
  611 X
  622
  633
  644
  655
  666
  677
  688
  699
  70EOF
  71
  72cat >result.1-5 <<EOF
  731 X
  742
  753
  764
  775 X
  786
  797
  808
  819
  82EOF
  83
  84cat >result.1-5-9 <<EOF
  851 X
  862
  873
  884
  895 X
  906
  917
  928
  939 X
  94EOF
  95
  96create_merge_msgs() {
  97        echo "Merge commit 'c2'" >msg.1-5 &&
  98        echo "Merge commit 'c2'; commit 'c3'" >msg.1-5-9 &&
  99        echo "Squashed commit of the following:" >squash.1 &&
 100        echo >>squash.1 &&
 101        git log --no-merges ^HEAD c1 >>squash.1 &&
 102        echo "Squashed commit of the following:" >squash.1-5 &&
 103        echo >>squash.1-5 &&
 104        git log --no-merges ^HEAD c2 >>squash.1-5 &&
 105        echo "Squashed commit of the following:" >squash.1-5-9 &&
 106        echo >>squash.1-5-9 &&
 107        git log --no-merges ^HEAD c2 c3 >>squash.1-5-9 &&
 108        echo > msg.nolog &&
 109        echo "* commit 'c3':" >msg.log &&
 110        echo "  commit 3" >>msg.log &&
 111        echo >>msg.log
 112}
 113
 114verify_diff() {
 115        if ! test_cmp "$1" "$2"
 116        then
 117                echo "$3"
 118                false
 119        fi
 120}
 121
 122verify_merge() {
 123        verify_diff "$2" "$1" "[OOPS] bad merge result" &&
 124        if test $(git ls-files -u | wc -l) -gt 0
 125        then
 126                echo "[OOPS] unmerged files"
 127                false
 128        fi &&
 129        if ! git diff --exit-code
 130        then
 131                echo "[OOPS] working tree != index"
 132                false
 133        fi &&
 134        if test -n "$3"
 135        then
 136                git show -s --pretty=format:%s HEAD >msg.act &&
 137                verify_diff "$3" msg.act "[OOPS] bad merge message"
 138        fi
 139}
 140
 141verify_head() {
 142        if test "$1" != "$(git rev-parse HEAD)"
 143        then
 144                echo "[OOPS] HEAD != $1"
 145                false
 146        fi
 147}
 148
 149verify_parents() {
 150        i=1
 151        while test $# -gt 0
 152        do
 153                if test "$1" != "$(git rev-parse HEAD^$i)"
 154                then
 155                        echo "[OOPS] HEAD^$i != $1"
 156                        return 1
 157                fi
 158                i=$(expr $i + 1)
 159                shift
 160        done
 161}
 162
 163verify_mergeheads() {
 164        i=1
 165        if ! test -f .git/MERGE_HEAD
 166        then
 167                echo "[OOPS] MERGE_HEAD is missing"
 168                false
 169        fi &&
 170        while test $# -gt 0
 171        do
 172                head=$(head -n $i .git/MERGE_HEAD | sed -ne \$p)
 173                if test "$1" != "$head"
 174                then
 175                        echo "[OOPS] MERGE_HEAD $i != $1"
 176                        return 1
 177                fi
 178                i=$(expr $i + 1)
 179                shift
 180        done
 181}
 182
 183verify_no_mergehead() {
 184        if test -f .git/MERGE_HEAD
 185        then
 186                echo "[OOPS] MERGE_HEAD exists"
 187                false
 188        fi
 189}
 190
 191
 192test_expect_success 'setup' '
 193        git add file &&
 194        test_tick &&
 195        git commit -m "commit 0" &&
 196        git tag c0 &&
 197        c0=$(git rev-parse HEAD) &&
 198        cp file.1 file &&
 199        git add file &&
 200        test_tick &&
 201        git commit -m "commit 1" &&
 202        git tag c1 &&
 203        c1=$(git rev-parse HEAD) &&
 204        git reset --hard "$c0" &&
 205        cp file.5 file &&
 206        git add file &&
 207        test_tick &&
 208        git commit -m "commit 2" &&
 209        git tag c2 &&
 210        c2=$(git rev-parse HEAD) &&
 211        git reset --hard "$c0" &&
 212        cp file.9 file &&
 213        git add file &&
 214        test_tick &&
 215        git commit -m "commit 3" &&
 216        git tag c3 &&
 217        c3=$(git rev-parse HEAD)
 218        git reset --hard "$c0" &&
 219        create_merge_msgs
 220'
 221
 222test_debug 'gitk --all'
 223
 224test_expect_success 'test option parsing' '
 225        if git merge -$ c1
 226        then
 227                echo "[OOPS] -$ accepted"
 228                false
 229        fi &&
 230        if git merge --no-such c1
 231        then
 232                echo "[OOPS] --no-such accepted"
 233                false
 234        fi &&
 235        if git merge -s foobar c1
 236        then
 237                echo "[OOPS] -s foobar accepted"
 238                false
 239        fi &&
 240        if git merge -s=foobar c1
 241        then
 242                echo "[OOPS] -s=foobar accepted"
 243                false
 244        fi &&
 245        if git merge -m
 246        then
 247                echo "[OOPS] missing commit msg accepted"
 248                false
 249        fi &&
 250        if git merge
 251        then
 252                echo "[OOPS] missing commit references accepted"
 253                false
 254        fi
 255'
 256
 257test_expect_success 'merge c0 with c1' '
 258        git reset --hard c0 &&
 259        git merge c1 &&
 260        verify_merge file result.1 &&
 261        verify_head "$c1"
 262'
 263
 264test_debug 'gitk --all'
 265
 266test_expect_success 'merge c1 with c2' '
 267        git reset --hard c1 &&
 268        test_tick &&
 269        git merge c2 &&
 270        verify_merge file result.1-5 msg.1-5 &&
 271        verify_parents $c1 $c2
 272'
 273
 274test_debug 'gitk --all'
 275
 276test_expect_success 'merge c1 with c2 and c3' '
 277        git reset --hard c1 &&
 278        test_tick &&
 279        git merge c2 c3 &&
 280        verify_merge file result.1-5-9 msg.1-5-9 &&
 281        verify_parents $c1 $c2 $c3
 282'
 283
 284test_debug 'gitk --all'
 285
 286test_expect_success 'merge c0 with c1 (no-commit)' '
 287        git reset --hard c0 &&
 288        git merge --no-commit c1 &&
 289        verify_merge file result.1 &&
 290        verify_head $c1
 291'
 292
 293test_debug 'gitk --all'
 294
 295test_expect_success 'merge c1 with c2 (no-commit)' '
 296        git reset --hard c1 &&
 297        git merge --no-commit c2 &&
 298        verify_merge file result.1-5 &&
 299        verify_head $c1 &&
 300        verify_mergeheads $c2
 301'
 302
 303test_debug 'gitk --all'
 304
 305test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
 306        git reset --hard c1 &&
 307        git merge --no-commit c2 c3 &&
 308        verify_merge file result.1-5-9 &&
 309        verify_head $c1 &&
 310        verify_mergeheads $c2 $c3
 311'
 312
 313test_debug 'gitk --all'
 314
 315test_expect_success 'merge c0 with c1 (squash)' '
 316        git reset --hard c0 &&
 317        git merge --squash c1 &&
 318        verify_merge file result.1 &&
 319        verify_head $c0 &&
 320        verify_no_mergehead &&
 321        verify_diff squash.1 .git/SQUASH_MSG "[OOPS] bad squash message"
 322'
 323
 324test_debug 'gitk --all'
 325
 326test_expect_success 'merge c1 with c2 (squash)' '
 327        git reset --hard c1 &&
 328        git merge --squash c2 &&
 329        verify_merge file result.1-5 &&
 330        verify_head $c1 &&
 331        verify_no_mergehead &&
 332        verify_diff squash.1-5 .git/SQUASH_MSG "[OOPS] bad squash message"
 333'
 334
 335test_debug 'gitk --all'
 336
 337test_expect_success 'merge c1 with c2 and c3 (squash)' '
 338        git reset --hard c1 &&
 339        git merge --squash c2 c3 &&
 340        verify_merge file result.1-5-9 &&
 341        verify_head $c1 &&
 342        verify_no_mergehead &&
 343        verify_diff squash.1-5-9 .git/SQUASH_MSG "[OOPS] bad squash message"
 344'
 345
 346test_debug 'gitk --all'
 347
 348test_expect_success 'merge c1 with c2 (no-commit in config)' '
 349        git reset --hard c1 &&
 350        git config branch.master.mergeoptions "--no-commit" &&
 351        git merge c2 &&
 352        verify_merge file result.1-5 &&
 353        verify_head $c1 &&
 354        verify_mergeheads $c2
 355'
 356
 357test_debug 'gitk --all'
 358
 359test_expect_success 'merge c1 with c2 (squash in config)' '
 360        git reset --hard c1 &&
 361        git config branch.master.mergeoptions "--squash" &&
 362        git merge c2 &&
 363        verify_merge file result.1-5 &&
 364        verify_head $c1 &&
 365        verify_no_mergehead &&
 366        verify_diff squash.1-5 .git/SQUASH_MSG "[OOPS] bad squash message"
 367'
 368
 369test_debug 'gitk --all'
 370
 371test_expect_success 'override config option -n with --summary' '
 372        git reset --hard c1 &&
 373        git config branch.master.mergeoptions "-n" &&
 374        test_tick &&
 375        git merge --summary c2 >diffstat.txt &&
 376        verify_merge file result.1-5 msg.1-5 &&
 377        verify_parents $c1 $c2 &&
 378        if ! grep "^ file |  *2 +-$" diffstat.txt
 379        then
 380                echo "[OOPS] diffstat was not generated with --summary"
 381                false
 382        fi
 383'
 384
 385test_expect_success 'override config option -n with --stat' '
 386        git reset --hard c1 &&
 387        git config branch.master.mergeoptions "-n" &&
 388        test_tick &&
 389        git merge --stat c2 >diffstat.txt &&
 390        verify_merge file result.1-5 msg.1-5 &&
 391        verify_parents $c1 $c2 &&
 392        if ! grep "^ file |  *2 +-$" diffstat.txt
 393        then
 394                echo "[OOPS] diffstat was not generated with --stat"
 395                false
 396        fi
 397'
 398
 399test_debug 'gitk --all'
 400
 401test_expect_success 'override config option --stat' '
 402        git reset --hard c1 &&
 403        git config branch.master.mergeoptions "--stat" &&
 404        test_tick &&
 405        git merge -n c2 >diffstat.txt &&
 406        verify_merge file result.1-5 msg.1-5 &&
 407        verify_parents $c1 $c2 &&
 408        if grep "^ file |  *2 +-$" diffstat.txt
 409        then
 410                echo "[OOPS] diffstat was generated"
 411                false
 412        fi
 413'
 414
 415test_debug 'gitk --all'
 416
 417test_expect_success 'merge c1 with c2 (override --no-commit)' '
 418        git reset --hard c1 &&
 419        git config branch.master.mergeoptions "--no-commit" &&
 420        test_tick &&
 421        git merge --commit c2 &&
 422        verify_merge file result.1-5 msg.1-5 &&
 423        verify_parents $c1 $c2
 424'
 425
 426test_debug 'gitk --all'
 427
 428test_expect_success 'merge c1 with c2 (override --squash)' '
 429        git reset --hard c1 &&
 430        git config branch.master.mergeoptions "--squash" &&
 431        test_tick &&
 432        git merge --no-squash c2 &&
 433        verify_merge file result.1-5 msg.1-5 &&
 434        verify_parents $c1 $c2
 435'
 436
 437test_debug 'gitk --all'
 438
 439test_expect_success 'merge c0 with c1 (no-ff)' '
 440        git reset --hard c0 &&
 441        git config branch.master.mergeoptions "" &&
 442        test_tick &&
 443        git merge --no-ff c1 &&
 444        verify_merge file result.1 &&
 445        verify_parents $c0 $c1
 446'
 447
 448test_debug 'gitk --all'
 449
 450test_expect_success 'combining --squash and --no-ff is refused' '
 451        test_must_fail git merge --squash --no-ff c1 &&
 452        test_must_fail git merge --no-ff --squash c1
 453'
 454
 455test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
 456        git reset --hard c0 &&
 457        git config branch.master.mergeoptions "--no-ff" &&
 458        git merge --ff c1 &&
 459        verify_merge file result.1 &&
 460        verify_head $c1
 461'
 462
 463test_expect_success 'merge log message' '
 464        git reset --hard c0 &&
 465        git merge --no-log c2 &&
 466        git show -s --pretty=format:%b HEAD >msg.act &&
 467        verify_diff msg.nolog msg.act "[OOPS] bad merge log message" &&
 468        git merge --log c3 &&
 469        git show -s --pretty=format:%b HEAD >msg.act &&
 470        verify_diff msg.log msg.act "[OOPS] bad merge log message"
 471'
 472
 473test_debug 'gitk --all'
 474
 475test_done