t / t1000-read-tree-m-3way.shon commit Merge with gitk. (80bd6f3)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Junio C Hamano
   4#
   5
   6test_description='Three way merge with read-tree -m
   7
   8This test tries three-way merge with read-tree -m
   9
  10There is one ancestor (called O for Original) and two branches A
  11and B derived from it.  We want to do a 3-way merge between A and
  12B, using O as the common ancestor.
  13
  14    merge A O B
  15
  16Decisions are made by comparing contents of O, A and B pathname
  17by pathname.  The result is determined by the following guiding
  18principle:
  19
  20 - If only A does something to it and B does not touch it, take
  21   whatever A does.
  22
  23 - If only B does something to it and A does not touch it, take
  24   whatever B does.
  25
  26 - If both A and B does something but in the same way, take
  27   whatever they do.
  28
  29 - If A and B does something but different things, we need a
  30   3-way merge:
  31
  32   - We cannot do anything about the following cases:
  33
  34     * O does not have it.  A and B both must be adding to the
  35       same path independently.
  36
  37     * A deletes it.  B must be modifying.
  38
  39   - Otherwise, A and B are modifying.  Run 3-way merge.
  40
  41First, the case matrix.
  42
  43 - Vertical axis is for A'\''s actions.
  44 - Horizontal axis is for B'\''s actions.
  45
  46.----------------------------------------------------------------.
  47| A        B | No Action  |   Delete   |   Modify   |    Add     |
  48|------------+------------+------------+------------+------------|
  49| No Action  |            |            |            |            |
  50|            | select O   | delete     | select B   | select B   |
  51|            |            |            |            |            |
  52|------------+------------+------------+------------+------------|
  53| Delete     |            |            | ********** |    can     |
  54|            | delete     | delete     | merge      |    not     |
  55|            |            |            |            |  happen    |
  56|------------+------------+------------+------------+------------|
  57| Modify     |            | ********** | ?????????? |    can     |
  58|            | select A   | merge      | select A=B |    not     |
  59|            |            |            | merge      |  happen    |
  60|------------+------------+------------+------------+------------|
  61| Add        |            |    can     |    can     | ?????????? |
  62|            | select A   |    not     |    not     | select A=B |
  63|            |            |  happen    |  happen    | merge      |
  64.----------------------------------------------------------------.
  65
  66In addition:
  67
  68 SS: a special case of MM, where A and B makes the same modification.
  69 LL: a special case of AA, where A and B creates the same file.
  70 TT: a special case of MM, where A and B makes mergeable changes.
  71 DF: a special case, where A makes a directory and B makes a file.
  72
  73'
  74. ./test-lib.sh
  75. ../lib-read-tree-m-3way.sh
  76
  77################################################################
  78# Trivial "majority when 3 stages exist" merge plus #2ALT, #3ALT
  79# and #5ALT trivial merges.
  80
  81cat >expected <<\EOF
  82100644 X 2      AA
  83100644 X 3      AA
  84100644 X 0      AN
  85100644 X 1      DD
  86100644 X 3      DF
  87100644 X 2      DF/DF
  88100644 X 1      DM
  89100644 X 3      DM
  90100644 X 1      DN
  91100644 X 3      DN
  92100644 X 0      LL
  93100644 X 1      MD
  94100644 X 2      MD
  95100644 X 1      MM
  96100644 X 2      MM
  97100644 X 3      MM
  98100644 X 0      MN
  99100644 X 0      NA
 100100644 X 1      ND
 101100644 X 2      ND
 102100644 X 0      NM
 103100644 X 0      NN
 104100644 X 0      SS
 105100644 X 1      TT
 106100644 X 2      TT
 107100644 X 3      TT
 108100644 X 2      Z/AA
 109100644 X 3      Z/AA
 110100644 X 0      Z/AN
 111100644 X 1      Z/DD
 112100644 X 1      Z/DM
 113100644 X 3      Z/DM
 114100644 X 1      Z/DN
 115100644 X 3      Z/DN
 116100644 X 1      Z/MD
 117100644 X 2      Z/MD
 118100644 X 1      Z/MM
 119100644 X 2      Z/MM
 120100644 X 3      Z/MM
 121100644 X 0      Z/MN
 122100644 X 0      Z/NA
 123100644 X 1      Z/ND
 124100644 X 2      Z/ND
 125100644 X 0      Z/NM
 126100644 X 0      Z/NN
 127EOF
 128
 129_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
 130_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
 131
 132check_result () {
 133    git-ls-files --stage | sed -e 's/ '"$_x40"' / X /' >current &&
 134    diff -u expected current
 135}
 136
 137# This is done on an empty work directory, which is the normal
 138# merge person behaviour.
 139test_expect_success \
 140    '3-way merge with git-read-tree -m, empty cache' \
 141    "rm -fr [NDMALTS][NDMALTSF] Z &&
 142     rm .git/index &&
 143     git-read-tree -m $tree_O $tree_A $tree_B &&
 144     check_result"
 145
 146# This starts out with the first head, which is the normal
 147# patch submitter behaviour.
 148test_expect_success \
 149    '3-way merge with git-read-tree -m, match H' \
 150    "rm -fr [NDMALTS][NDMALTSF] Z &&
 151     rm .git/index &&
 152     git-read-tree $tree_A &&
 153     git-checkout-cache -f -u -a &&
 154     git-read-tree -m $tree_O $tree_A $tree_B &&
 155     check_result"
 156
 157: <<\END_OF_CASE_TABLE
 158
 159We have so far tested only empty index and clean-and-matching-A index
 160case which are trivial.  Make sure index requirements are also
 161checked.  The table also lists alternative semantics which is not
 162currently implemented.
 163
 164"git-diff-tree -m O A B"
 165
 166     O       A       B         result      index requirements
 167-------------------------------------------------------------------
 168  1  missing missing missing   -           must not exist.
 169 ------------------------------------------------------------------
 170  2  missing missing exists    no merge    must not exist.
 171                               ------------------------------------
 172    (ALT)                      take B*     must match B, if exists.
 173 ------------------------------------------------------------------
 174  3  missing exists  missing   no merge    must match A and be
 175                                           up-to-date, if exists.
 176                               ------------------------------------
 177    (ALT)                      take A*     must match A, if exists.
 178 ------------------------------------------------------------------
 179  4  missing exists  A!=B      no merge    must match A and be
 180                                           up-to-date, if exists.
 181 ------------------------------------------------------------------
 182  5  missing exists  A==B      no merge    must match A and be
 183                                           up-to-date, if exists.
 184                               ------------------------------------
 185    (ALT)                      take A      must match A, if exists.
 186 ------------------------------------------------------------------
 187  6  exists  missing missing   no merge    must not exist.
 188                               ------------------------------------
 189    (ALT)                      remove      must not exist.
 190 ------------------------------------------------------------------
 191  7  exists  missing O!=B      no merge    must not exist.
 192 ------------------------------------------------------------------
 193  8  exists  missing O==B      no merge    must not exist.
 194                               ------------------------------------
 195    (ALT)                      remove      must not exist.
 196 ------------------------------------------------------------------
 197  9  exists  O!=A    missing   no merge    must match A and be
 198                                           up-to-date, if exists.
 199 ------------------------------------------------------------------
 200 10  exists  O==A    missing   no merge    must match A and be
 201                                           up-to-date, if exists.
 202                               ------------------------------------
 203    (ALT)                      remove      ditto
 204 ------------------------------------------------------------------
 205 11  exists  O!=A    O!=B      no merge    must match A and be
 206                     A!=B                  up-to-date, if exists.
 207 ------------------------------------------------------------------
 208 12  exists  O!=A    O!=B      take A      must match A, if exists.
 209                     A==B
 210 ------------------------------------------------------------------
 211 13  exists  O!=A    O==B      take A      must match A, if exists.
 212 ------------------------------------------------------------------
 213 14  exists  O==A    O!=B      take B      must match A and be
 214                                           be up-to-date, if exists.
 215                               ------------------------------------
 216    (ALT)                      take B      if exists, must either (1)
 217                                           match A and be up-to-date,
 218                                           or (2) match B.
 219 ------------------------------------------------------------------
 220 15  exists  O==A    O==B      take B      must match A if exists.
 221-------------------------------------------------------------------
 222
 223Note: if we want to implement 2ALT and 3ALT we need to be careful.
 224The tree A may contain DF (file) when tree B require DF to be a
 225directory by having DF/DF (file).
 226
 227END_OF_CASE_TABLE
 228
 229test_expect_failure \
 230    '1 - must not have an entry not in A.' \
 231    "rm -f .git/index XX &&
 232     echo XX >XX &&
 233     git-update-cache --add XX &&
 234     git-read-tree -m $tree_O $tree_A $tree_B"
 235
 236test_expect_success \
 237    '2 - must match B in !O && !A && B case.' \
 238    "rm -f .git/index NA &&
 239     cp .orig-B/NA NA &&
 240     git-update-cache --add NA &&
 241     git-read-tree -m $tree_O $tree_A $tree_B"
 242
 243test_expect_success \
 244    '2 - matching B alone is OK in !O && !A && B case.' \
 245    "rm -f .git/index NA &&
 246     cp .orig-B/NA NA &&
 247     git-update-cache --add NA &&
 248     echo extra >>NA &&
 249     git-read-tree -m $tree_O $tree_A $tree_B"
 250
 251test_expect_success \
 252    '3 - must match A in !O && A && !B case.' \
 253    "rm -f .git/index AN &&
 254     cp .orig-A/AN AN &&
 255     git-update-cache --add AN &&
 256     git-read-tree -m $tree_O $tree_A $tree_B &&
 257     check_result"
 258
 259test_expect_success \
 260    '3 - matching A alone is OK in !O && A && !B case.' \
 261    "rm -f .git/index AN &&
 262     cp .orig-A/AN AN &&
 263     git-update-cache --add AN &&
 264     echo extra >>AN &&
 265     git-read-tree -m $tree_O $tree_A $tree_B"
 266
 267test_expect_failure \
 268    '3 (fail) - must match A in !O && A && !B case.' \
 269    "rm -f .git/index AN &&
 270     cp .orig-A/AN AN &&
 271     echo extra >>AN &&
 272     git-update-cache --add AN &&
 273     git-read-tree -m $tree_O $tree_A $tree_B"
 274
 275test_expect_success \
 276    '4 - must match and be up-to-date in !O && A && B && A!=B case.' \
 277    "rm -f .git/index AA &&
 278     cp .orig-A/AA AA &&
 279     git-update-cache --add AA &&
 280     git-read-tree -m $tree_O $tree_A $tree_B &&
 281     check_result"
 282
 283test_expect_failure \
 284    '4 (fail) - must match and be up-to-date in !O && A && B && A!=B case.' \
 285    "rm -f .git/index AA &&
 286     cp .orig-A/AA AA &&
 287     git-update-cache --add AA &&
 288     echo extra >>AA &&
 289     git-read-tree -m $tree_O $tree_A $tree_B"
 290
 291test_expect_failure \
 292    '4 (fail) - must match and be up-to-date in !O && A && B && A!=B case.' \
 293    "rm -f .git/index AA &&
 294     cp .orig-A/AA AA &&
 295     echo extra >>AA &&
 296     git-update-cache --add AA &&
 297     git-read-tree -m $tree_O $tree_A $tree_B"
 298
 299test_expect_success \
 300    '5 - must match in !O && A && B && A==B case.' \
 301    "rm -f .git/index LL &&
 302     cp .orig-A/LL LL &&
 303     git-update-cache --add LL &&
 304     git-read-tree -m $tree_O $tree_A $tree_B &&
 305     check_result"
 306
 307test_expect_success \
 308    '5 - must match in !O && A && B && A==B case.' \
 309    "rm -f .git/index LL &&
 310     cp .orig-A/LL LL &&
 311     git-update-cache --add LL &&
 312     echo extra >>LL &&
 313     git-read-tree -m $tree_O $tree_A $tree_B &&
 314     check_result"
 315
 316test_expect_failure \
 317    '5 (fail) - must match A in !O && A && B && A==B case.' \
 318    "rm -f .git/index LL &&
 319     cp .orig-A/LL LL &&
 320     echo extra >>LL &&
 321     git-update-cache --add LL &&
 322     git-read-tree -m $tree_O $tree_A $tree_B"
 323
 324test_expect_failure \
 325    '6 - must not exist in O && !A && !B case' \
 326    "rm -f .git/index DD &&
 327     echo DD >DD
 328     git-update-cache --add DD &&
 329     git-read-tree -m $tree_O $tree_A $tree_B"
 330
 331test_expect_failure \
 332    '7 - must not exist in O && !A && B && O!=B case' \
 333    "rm -f .git/index DM &&
 334     cp .orig-B/DM DM &&
 335     git-update-cache --add DM &&
 336     git-read-tree -m $tree_O $tree_A $tree_B"
 337
 338test_expect_failure \
 339    '8 - must not exist in O && !A && B && O==B case' \
 340    "rm -f .git/index DN &&
 341     cp .orig-B/DN DN &&
 342     git-update-cache --add DN &&
 343     git-read-tree -m $tree_O $tree_A $tree_B"
 344
 345test_expect_success \
 346    '9 - must match and be up-to-date in O && A && !B && O!=A case' \
 347    "rm -f .git/index MD &&
 348     cp .orig-A/MD MD &&
 349     git-update-cache --add MD &&
 350     git-read-tree -m $tree_O $tree_A $tree_B &&
 351     check_result"
 352
 353test_expect_failure \
 354    '9 (fail) - must match and be up-to-date in O && A && !B && O!=A case' \
 355    "rm -f .git/index MD &&
 356     cp .orig-A/MD MD &&
 357     git-update-cache --add MD &&
 358     echo extra >>MD &&
 359     git-read-tree -m $tree_O $tree_A $tree_B"
 360
 361test_expect_failure \
 362    '9 (fail) - must match and be up-to-date in O && A && !B && O!=A case' \
 363    "rm -f .git/index MD &&
 364     cp .orig-A/MD MD &&
 365     echo extra >>MD &&
 366     git-update-cache --add MD &&
 367     git-read-tree -m $tree_O $tree_A $tree_B"
 368
 369test_expect_success \
 370    '10 - must match and be up-to-date in O && A && !B && O==A case' \
 371    "rm -f .git/index ND &&
 372     cp .orig-A/ND ND &&
 373     git-update-cache --add ND &&
 374     git-read-tree -m $tree_O $tree_A $tree_B &&
 375     check_result"
 376
 377test_expect_failure \
 378    '10 (fail) - must match and be up-to-date in O && A && !B && O==A case' \
 379    "rm -f .git/index ND &&
 380     cp .orig-A/ND ND &&
 381     git-update-cache --add ND &&
 382     echo extra >>ND &&
 383     git-read-tree -m $tree_O $tree_A $tree_B"
 384
 385test_expect_failure \
 386    '10 (fail) - must match and be up-to-date in O && A && !B && O==A case' \
 387    "rm -f .git/index ND &&
 388     cp .orig-A/ND ND &&
 389     echo extra >>ND &&
 390     git-update-cache --add ND &&
 391     git-read-tree -m $tree_O $tree_A $tree_B"
 392
 393test_expect_success \
 394    '11 - must match and be up-to-date in O && A && B && O!=A && O!=B && A!=B case' \
 395    "rm -f .git/index MM &&
 396     cp .orig-A/MM MM &&
 397     git-update-cache --add MM &&
 398     git-read-tree -m $tree_O $tree_A $tree_B &&
 399     check_result"
 400
 401test_expect_failure \
 402    '11 (fail) - must match and be up-to-date in O && A && B && O!=A && O!=B && A!=B case' \
 403    "rm -f .git/index MM &&
 404     cp .orig-A/MM MM &&
 405     git-update-cache --add MM &&
 406     echo extra >>MM &&
 407     git-read-tree -m $tree_O $tree_A $tree_B"
 408
 409test_expect_failure \
 410    '11 (fail) - must match and be up-to-date in O && A && B && O!=A && O!=B && A!=B case' \
 411    "rm -f .git/index MM &&
 412     cp .orig-A/MM MM &&
 413     echo extra >>MM &&
 414     git-update-cache --add MM &&
 415     git-read-tree -m $tree_O $tree_A $tree_B"
 416
 417test_expect_success \
 418    '12 - must match A in O && A && B && O!=A && A==B case' \
 419    "rm -f .git/index SS &&
 420     cp .orig-A/SS SS &&
 421     git-update-cache --add SS &&
 422     git-read-tree -m $tree_O $tree_A $tree_B &&
 423     check_result"
 424
 425test_expect_success \
 426    '12 - must match A in O && A && B && O!=A && A==B case' \
 427    "rm -f .git/index SS &&
 428     cp .orig-A/SS SS &&
 429     git-update-cache --add SS &&
 430     echo extra >>SS &&
 431     git-read-tree -m $tree_O $tree_A $tree_B &&
 432     check_result"
 433
 434test_expect_failure \
 435    '12 (fail) - must match A in O && A && B && O!=A && A==B case' \
 436    "rm -f .git/index SS &&
 437     cp .orig-A/SS SS &&
 438     echo extra >>SS &&
 439     git-update-cache --add SS &&
 440     git-read-tree -m $tree_O $tree_A $tree_B"
 441
 442test_expect_success \
 443    '13 - must match A in O && A && B && O!=A && O==B case' \
 444    "rm -f .git/index MN &&
 445     cp .orig-A/MN MN &&
 446     git-update-cache --add MN &&
 447     git-read-tree -m $tree_O $tree_A $tree_B &&
 448     check_result"
 449
 450test_expect_success \
 451    '13 - must match A in O && A && B && O!=A && O==B case' \
 452    "rm -f .git/index MN &&
 453     cp .orig-A/MN MN &&
 454     git-update-cache --add MN &&
 455     echo extra >>MN &&
 456     git-read-tree -m $tree_O $tree_A $tree_B &&
 457     check_result"
 458
 459test_expect_success \
 460    '14 - must match and be up-to-date in O && A && B && O==A && O!=B case' \
 461    "rm -f .git/index NM &&
 462     cp .orig-A/NM NM &&
 463     git-update-cache --add NM &&
 464     git-read-tree -m $tree_O $tree_A $tree_B &&
 465     check_result"
 466
 467test_expect_success \
 468    '14 - may match B in O && A && B && O==A && O!=B case' \
 469    "rm -f .git/index NM &&
 470     cp .orig-B/NM NM &&
 471     git-update-cache --add NM &&
 472     echo extra >>NM &&
 473     git-read-tree -m $tree_O $tree_A $tree_B &&
 474     check_result"
 475
 476test_expect_failure \
 477    '14 (fail) - must match and be up-to-date in O && A && B && O==A && O!=B case' \
 478    "rm -f .git/index NM &&
 479     cp .orig-A/NM NM &&
 480     git-update-cache --add NM &&
 481     echo extra >>NM &&
 482     git-read-tree -m $tree_O $tree_A $tree_B"
 483
 484test_expect_failure \
 485    '14 (fail) - must match and be up-to-date in O && A && B && O==A && O!=B case' \
 486    "rm -f .git/index NM &&
 487     cp .orig-A/NM NM &&
 488     echo extra >>NM &&
 489     git-update-cache --add NM &&
 490     git-read-tree -m $tree_O $tree_A $tree_B"
 491
 492test_expect_success \
 493    '15 - must match A in O && A && B && O==A && O==B case' \
 494    "rm -f .git/index NN &&
 495     cp .orig-A/NN NN &&
 496     git-update-cache --add NN &&
 497     git-read-tree -m $tree_O $tree_A $tree_B &&
 498     check_result"
 499
 500test_expect_success \
 501    '15 - must match A in O && A && B && O==A && O==B case' \
 502    "rm -f .git/index NN &&
 503     cp .orig-A/NN NN &&
 504     git-update-cache --add NN &&
 505     echo extra >>NN &&
 506     git-read-tree -m $tree_O $tree_A $tree_B &&
 507     check_result"
 508
 509test_expect_failure \
 510    '15 (fail) - must match A in O && A && B && O==A && O==B case' \
 511    "rm -f .git/index NN &&
 512     cp .orig-A/NN NN &&
 513     echo extra >>NN &&
 514     git-update-cache --add NN &&
 515     git-read-tree -m $tree_O $tree_A $tree_B"
 516
 517test_done