t / t7600-merge.shon commit apply: handle patches with funny filename and colon in timezone (2d502e1)
   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        echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
 153
 154        git reset --hard c0 &&
 155        git merge c1 &&
 156        verify_merge file result.1 &&
 157        verify_head "$c1" &&
 158
 159        git reflog -1 >reflog.actual &&
 160        sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
 161        test_cmp reflog.expected reflog.fuzzy
 162'
 163
 164test_debug 'git log --graph --decorate --oneline --all'
 165
 166test_expect_success 'merge c0 with c1 with --ff-only' '
 167        git reset --hard c0 &&
 168        git merge --ff-only c1 &&
 169        git merge --ff-only HEAD c0 c1 &&
 170        verify_merge file result.1 &&
 171        verify_head "$c1"
 172'
 173
 174test_debug 'git log --graph --decorate --oneline --all'
 175
 176test_expect_success 'merge from unborn branch' '
 177        git checkout -f master &&
 178        test_might_fail git branch -D kid &&
 179
 180        echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
 181
 182        git checkout --orphan kid &&
 183        test_when_finished "git checkout -f master" &&
 184        git rm -fr . &&
 185        test_tick &&
 186        git merge --ff-only c1 &&
 187        verify_merge file result.1 &&
 188        verify_head "$c1" &&
 189
 190        git reflog -1 >reflog.actual &&
 191        sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
 192        test_cmp reflog.expected reflog.fuzzy
 193'
 194
 195test_debug 'git log --graph --decorate --oneline --all'
 196
 197test_expect_success 'merge c1 with c2' '
 198        git reset --hard c1 &&
 199        test_tick &&
 200        git merge c2 &&
 201        verify_merge file result.1-5 msg.1-5 &&
 202        verify_parents $c1 $c2
 203'
 204
 205test_debug 'git log --graph --decorate --oneline --all'
 206
 207test_expect_success 'merge c1 with c2 and c3' '
 208        git reset --hard c1 &&
 209        test_tick &&
 210        git merge c2 c3 &&
 211        verify_merge file result.1-5-9 msg.1-5-9 &&
 212        verify_parents $c1 $c2 $c3
 213'
 214
 215test_debug 'git log --graph --decorate --oneline --all'
 216
 217test_expect_success 'failing merges with --ff-only' '
 218        git reset --hard c1 &&
 219        test_tick &&
 220        test_must_fail git merge --ff-only c2 &&
 221        test_must_fail git merge --ff-only c3 &&
 222        test_must_fail git merge --ff-only c2 c3
 223'
 224
 225test_expect_success 'merge c0 with c1 (no-commit)' '
 226        git reset --hard c0 &&
 227        git merge --no-commit c1 &&
 228        verify_merge file result.1 &&
 229        verify_head $c1
 230'
 231
 232test_debug 'git log --graph --decorate --oneline --all'
 233
 234test_expect_success 'merge c1 with c2 (no-commit)' '
 235        git reset --hard c1 &&
 236        git merge --no-commit c2 &&
 237        verify_merge file result.1-5 &&
 238        verify_head $c1 &&
 239        verify_mergeheads $c2
 240'
 241
 242test_debug 'git log --graph --decorate --oneline --all'
 243
 244test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
 245        git reset --hard c1 &&
 246        git merge --no-commit c2 c3 &&
 247        verify_merge file result.1-5-9 &&
 248        verify_head $c1 &&
 249        verify_mergeheads $c2 $c3
 250'
 251
 252test_debug 'git log --graph --decorate --oneline --all'
 253
 254test_expect_success 'merge c0 with c1 (squash)' '
 255        git reset --hard c0 &&
 256        git merge --squash c1 &&
 257        verify_merge file result.1 &&
 258        verify_head $c0 &&
 259        verify_no_mergehead &&
 260        test_cmp squash.1 .git/SQUASH_MSG
 261'
 262
 263test_debug 'git log --graph --decorate --oneline --all'
 264
 265test_expect_success 'merge c0 with c1 (squash, ff-only)' '
 266        git reset --hard c0 &&
 267        git merge --squash --ff-only c1 &&
 268        verify_merge file result.1 &&
 269        verify_head $c0 &&
 270        verify_no_mergehead &&
 271        test_cmp squash.1 .git/SQUASH_MSG
 272'
 273
 274test_debug 'git log --graph --decorate --oneline --all'
 275
 276test_expect_success 'merge c1 with c2 (squash)' '
 277        git reset --hard c1 &&
 278        git merge --squash c2 &&
 279        verify_merge file result.1-5 &&
 280        verify_head $c1 &&
 281        verify_no_mergehead &&
 282        test_cmp squash.1-5 .git/SQUASH_MSG
 283'
 284
 285test_debug 'git log --graph --decorate --oneline --all'
 286
 287test_expect_success 'unsuccesful merge of c1 with c2 (squash, ff-only)' '
 288        git reset --hard c1 &&
 289        test_must_fail git merge --squash --ff-only c2
 290'
 291
 292test_debug 'git log --graph --decorate --oneline --all'
 293
 294test_expect_success 'merge c1 with c2 and c3 (squash)' '
 295        git reset --hard c1 &&
 296        git merge --squash c2 c3 &&
 297        verify_merge file result.1-5-9 &&
 298        verify_head $c1 &&
 299        verify_no_mergehead &&
 300        test_cmp squash.1-5-9 .git/SQUASH_MSG
 301'
 302
 303test_debug 'git log --graph --decorate --oneline --all'
 304
 305test_expect_success 'merge c1 with c2 (no-commit in config)' '
 306        git reset --hard c1 &&
 307        git config branch.master.mergeoptions "--no-commit" &&
 308        git merge c2 &&
 309        verify_merge file result.1-5 &&
 310        verify_head $c1 &&
 311        verify_mergeheads $c2
 312'
 313
 314test_debug 'git log --graph --decorate --oneline --all'
 315
 316test_expect_success 'merge c1 with c2 (squash in config)' '
 317        git reset --hard c1 &&
 318        git config branch.master.mergeoptions "--squash" &&
 319        git merge c2 &&
 320        verify_merge file result.1-5 &&
 321        verify_head $c1 &&
 322        verify_no_mergehead &&
 323        test_cmp squash.1-5 .git/SQUASH_MSG
 324'
 325
 326test_debug 'git log --graph --decorate --oneline --all'
 327
 328test_expect_success 'override config option -n with --summary' '
 329        git reset --hard c1 &&
 330        git config branch.master.mergeoptions "-n" &&
 331        test_tick &&
 332        git merge --summary c2 >diffstat.txt &&
 333        verify_merge file result.1-5 msg.1-5 &&
 334        verify_parents $c1 $c2 &&
 335        if ! grep "^ file |  *2 +-$" diffstat.txt
 336        then
 337                echo "[OOPS] diffstat was not generated with --summary"
 338                false
 339        fi
 340'
 341
 342test_expect_success 'override config option -n with --stat' '
 343        git reset --hard c1 &&
 344        git config branch.master.mergeoptions "-n" &&
 345        test_tick &&
 346        git merge --stat c2 >diffstat.txt &&
 347        verify_merge file result.1-5 msg.1-5 &&
 348        verify_parents $c1 $c2 &&
 349        if ! grep "^ file |  *2 +-$" diffstat.txt
 350        then
 351                echo "[OOPS] diffstat was not generated with --stat"
 352                false
 353        fi
 354'
 355
 356test_debug 'git log --graph --decorate --oneline --all'
 357
 358test_expect_success 'override config option --stat' '
 359        git reset --hard c1 &&
 360        git config branch.master.mergeoptions "--stat" &&
 361        test_tick &&
 362        git merge -n c2 >diffstat.txt &&
 363        verify_merge file result.1-5 msg.1-5 &&
 364        verify_parents $c1 $c2 &&
 365        if grep "^ file |  *2 +-$" diffstat.txt
 366        then
 367                echo "[OOPS] diffstat was generated"
 368                false
 369        fi
 370'
 371
 372test_debug 'git log --graph --decorate --oneline --all'
 373
 374test_expect_success 'merge c1 with c2 (override --no-commit)' '
 375        git reset --hard c1 &&
 376        git config branch.master.mergeoptions "--no-commit" &&
 377        test_tick &&
 378        git merge --commit c2 &&
 379        verify_merge file result.1-5 msg.1-5 &&
 380        verify_parents $c1 $c2
 381'
 382
 383test_debug 'git log --graph --decorate --oneline --all'
 384
 385test_expect_success 'merge c1 with c2 (override --squash)' '
 386        git reset --hard c1 &&
 387        git config branch.master.mergeoptions "--squash" &&
 388        test_tick &&
 389        git merge --no-squash c2 &&
 390        verify_merge file result.1-5 msg.1-5 &&
 391        verify_parents $c1 $c2
 392'
 393
 394test_debug 'git log --graph --decorate --oneline --all'
 395
 396test_expect_success 'merge c0 with c1 (no-ff)' '
 397        git reset --hard c0 &&
 398        git config branch.master.mergeoptions "" &&
 399        test_tick &&
 400        git merge --no-ff c1 &&
 401        verify_merge file result.1 &&
 402        verify_parents $c0 $c1
 403'
 404
 405test_debug 'git log --graph --decorate --oneline --all'
 406
 407test_expect_success 'combining --squash and --no-ff is refused' '
 408        test_must_fail git merge --squash --no-ff c1 &&
 409        test_must_fail git merge --no-ff --squash c1
 410'
 411
 412test_expect_success 'combining --ff-only and --no-ff is refused' '
 413        test_must_fail git merge --ff-only --no-ff c1 &&
 414        test_must_fail git merge --no-ff --ff-only c1
 415'
 416
 417test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
 418        git reset --hard c0 &&
 419        git config branch.master.mergeoptions "--no-ff" &&
 420        git merge --ff c1 &&
 421        verify_merge file result.1 &&
 422        verify_head $c1
 423'
 424
 425test_expect_success 'merge log message' '
 426        git reset --hard c0 &&
 427        git merge --no-log c2 &&
 428        git show -s --pretty=format:%b HEAD >msg.act &&
 429        test_cmp msg.nolog msg.act &&
 430
 431        git merge --log c3 &&
 432        git show -s --pretty=format:%b HEAD >msg.act &&
 433        test_cmp msg.log msg.act &&
 434
 435        git reset --hard HEAD^ &&
 436        git config merge.log yes &&
 437        git merge c3 &&
 438        git show -s --pretty=format:%b HEAD >msg.act &&
 439        test_cmp msg.log msg.act
 440'
 441
 442test_debug 'git log --graph --decorate --oneline --all'
 443
 444test_expect_success 'merge c1 with c0, c2, c0, and c1' '
 445       git reset --hard c1 &&
 446       git config branch.master.mergeoptions "" &&
 447       test_tick &&
 448       git merge c0 c2 c0 c1 &&
 449       verify_merge file result.1-5 &&
 450       verify_parents $c1 $c2
 451'
 452
 453test_debug 'git log --graph --decorate --oneline --all'
 454
 455test_expect_success 'merge c1 with c0, c2, c0, and c1' '
 456       git reset --hard c1 &&
 457       git config branch.master.mergeoptions "" &&
 458       test_tick &&
 459       git merge c0 c2 c0 c1 &&
 460       verify_merge file result.1-5 &&
 461       verify_parents $c1 $c2
 462'
 463
 464test_debug 'git log --graph --decorate --oneline --all'
 465
 466test_expect_success 'merge c1 with c1 and c2' '
 467       git reset --hard c1 &&
 468       git config branch.master.mergeoptions "" &&
 469       test_tick &&
 470       git merge c1 c2 &&
 471       verify_merge file result.1-5 &&
 472       verify_parents $c1 $c2
 473'
 474
 475test_debug 'git log --graph --decorate --oneline --all'
 476
 477test_expect_success 'merge fast-forward in a dirty tree' '
 478       git reset --hard c0 &&
 479       mv file file1 &&
 480       cat file1 >file &&
 481       rm -f file1 &&
 482       git merge c2
 483'
 484
 485test_debug 'git log --graph --decorate --oneline --all'
 486
 487test_expect_success 'in-index merge' '
 488        git reset --hard c0 &&
 489        git merge --no-ff -s resolve c1 >out &&
 490        grep "Wonderful." out &&
 491        verify_parents $c0 $c1
 492'
 493
 494test_debug 'git log --graph --decorate --oneline --all'
 495
 496test_expect_success 'refresh the index before merging' '
 497        git reset --hard c1 &&
 498        cp file file.n && mv -f file.n file &&
 499        git merge c3
 500'
 501
 502cat >expected.branch <<\EOF
 503Merge branch 'c5-branch' (early part)
 504EOF
 505cat >expected.tag <<\EOF
 506Merge commit 'c5~1'
 507EOF
 508
 509test_expect_success 'merge early part of c2' '
 510        git reset --hard c3 &&
 511        echo c4 >c4.c &&
 512        git add c4.c &&
 513        git commit -m c4 &&
 514        git tag c4 &&
 515        echo c5 >c5.c &&
 516        git add c5.c &&
 517        git commit -m c5 &&
 518        git tag c5 &&
 519        git reset --hard c3 &&
 520        echo c6 >c6.c &&
 521        git add c6.c &&
 522        git commit -m c6 &&
 523        git tag c6 &&
 524        git branch -f c5-branch c5 &&
 525        git merge c5-branch~1 &&
 526        git show -s --pretty=format:%s HEAD >actual.branch &&
 527        git reset --keep HEAD^ &&
 528        git merge c5~1 &&
 529        git show -s --pretty=format:%s HEAD >actual.tag &&
 530        test_cmp expected.branch actual.branch &&
 531        test_cmp expected.tag actual.tag
 532'
 533
 534test_debug 'git log --graph --decorate --oneline --all'
 535
 536test_expect_success 'merge --no-ff --no-commit && commit' '
 537        git reset --hard c0 &&
 538        git merge --no-ff --no-commit c1 &&
 539        EDITOR=: git commit &&
 540        verify_parents $c0 $c1
 541'
 542
 543test_debug 'git log --graph --decorate --oneline --all'
 544
 545test_expect_success 'amending no-ff merge commit' '
 546        EDITOR=: git commit --amend &&
 547        verify_parents $c0 $c1
 548'
 549
 550test_debug 'git log --graph --decorate --oneline --all'
 551
 552test_done