| 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2005 Junio C Hamano |
| 4 | # |
| 5 | |
| 6 | test_description='Three way merge with read-tree -m |
| 7 | |
| 8 | This test tries three-way merge with read-tree -m |
| 9 | |
| 10 | There is one ancestor (called O for Original) and two branches A |
| 11 | and B derived from it. We want to do a 3-way merge between A and |
| 12 | B, using O as the common ancestor. |
| 13 | |
| 14 | merge A O B |
| 15 | |
| 16 | Decisions are made by comparing contents of O, A and B pathname |
| 17 | by pathname. The result is determined by the following guiding |
| 18 | principle: |
| 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 | |
| 41 | First, 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 | |
| 66 | In 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 | |
| 75 | . ./test-lib.sh |
| 76 | . "$TEST_DIRECTORY"/lib-read-tree.sh |
| 77 | . "$TEST_DIRECTORY"/lib-read-tree-m-3way.sh |
| 78 | |
| 79 | ################################################################ |
| 80 | # Trivial "majority when 3 stages exist" merge plus #2ALT, #3ALT |
| 81 | # and #5ALT trivial merges. |
| 82 | |
| 83 | cat >expected <<\EOF |
| 84 | 100644 X 2 AA |
| 85 | 100644 X 3 AA |
| 86 | 100644 X 0 AN |
| 87 | 100644 X 1 DD |
| 88 | 100644 X 3 DF |
| 89 | 100644 X 2 DF/DF |
| 90 | 100644 X 1 DM |
| 91 | 100644 X 3 DM |
| 92 | 100644 X 1 DN |
| 93 | 100644 X 3 DN |
| 94 | 100644 X 0 LL |
| 95 | 100644 X 1 MD |
| 96 | 100644 X 2 MD |
| 97 | 100644 X 1 MM |
| 98 | 100644 X 2 MM |
| 99 | 100644 X 3 MM |
| 100 | 100644 X 0 MN |
| 101 | 100644 X 0 NA |
| 102 | 100644 X 1 ND |
| 103 | 100644 X 2 ND |
| 104 | 100644 X 0 NM |
| 105 | 100644 X 0 NN |
| 106 | 100644 X 0 SS |
| 107 | 100644 X 1 TT |
| 108 | 100644 X 2 TT |
| 109 | 100644 X 3 TT |
| 110 | 100644 X 2 Z/AA |
| 111 | 100644 X 3 Z/AA |
| 112 | 100644 X 0 Z/AN |
| 113 | 100644 X 1 Z/DD |
| 114 | 100644 X 1 Z/DM |
| 115 | 100644 X 3 Z/DM |
| 116 | 100644 X 1 Z/DN |
| 117 | 100644 X 3 Z/DN |
| 118 | 100644 X 1 Z/MD |
| 119 | 100644 X 2 Z/MD |
| 120 | 100644 X 1 Z/MM |
| 121 | 100644 X 2 Z/MM |
| 122 | 100644 X 3 Z/MM |
| 123 | 100644 X 0 Z/MN |
| 124 | 100644 X 0 Z/NA |
| 125 | 100644 X 1 Z/ND |
| 126 | 100644 X 2 Z/ND |
| 127 | 100644 X 0 Z/NM |
| 128 | 100644 X 0 Z/NN |
| 129 | EOF |
| 130 | |
| 131 | check_result () { |
| 132 | git ls-files --stage | sed -e 's/ '"$OID_REGEX"' / X /' >current && |
| 133 | test_cmp expected current |
| 134 | } |
| 135 | |
| 136 | # This is done on an empty work directory, which is the normal |
| 137 | # merge person behaviour. |
| 138 | test_expect_success '3-way merge with git read-tree -m, empty cache' ' |
| 139 | rm -fr [NDMALTS][NDMALTSF] Z && |
| 140 | rm .git/index && |
| 141 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 142 | check_result |
| 143 | ' |
| 144 | |
| 145 | # This starts out with the first head, which is the normal |
| 146 | # patch submitter behaviour. |
| 147 | test_expect_success '3-way merge with git read-tree -m, match H' ' |
| 148 | rm -fr [NDMALTS][NDMALTSF] Z && |
| 149 | rm .git/index && |
| 150 | read_tree_must_succeed $tree_A && |
| 151 | git checkout-index -f -u -a && |
| 152 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 153 | check_result |
| 154 | ' |
| 155 | |
| 156 | : <<\END_OF_CASE_TABLE |
| 157 | |
| 158 | We have so far tested only empty index and clean-and-matching-A index |
| 159 | case which are trivial. Make sure index requirements are also |
| 160 | checked. |
| 161 | |
| 162 | "git read-tree -m O A B" |
| 163 | |
| 164 | O A B result index requirements |
| 165 | ------------------------------------------------------------------- |
| 166 | 1 missing missing missing - must not exist. |
| 167 | ------------------------------------------------------------------ |
| 168 | 2 missing missing exists take B* must match B, if exists. |
| 169 | ------------------------------------------------------------------ |
| 170 | 3 missing exists missing take A* must match A, if exists. |
| 171 | ------------------------------------------------------------------ |
| 172 | 4 missing exists A!=B no merge must match A and be |
| 173 | up-to-date, if exists. |
| 174 | ------------------------------------------------------------------ |
| 175 | 5 missing exists A==B take A must match A, if exists. |
| 176 | ------------------------------------------------------------------ |
| 177 | 6 exists missing missing remove must not exist. |
| 178 | ------------------------------------------------------------------ |
| 179 | 7 exists missing O!=B no merge must not exist. |
| 180 | ------------------------------------------------------------------ |
| 181 | 8 exists missing O==B remove must not exist. |
| 182 | ------------------------------------------------------------------ |
| 183 | 9 exists O!=A missing no merge must match A and be |
| 184 | up-to-date, if exists. |
| 185 | ------------------------------------------------------------------ |
| 186 | 10 exists O==A missing no merge must match A |
| 187 | ------------------------------------------------------------------ |
| 188 | 11 exists O!=A O!=B no merge must match A and be |
| 189 | A!=B up-to-date, if exists. |
| 190 | ------------------------------------------------------------------ |
| 191 | 12 exists O!=A O!=B take A must match A, if exists. |
| 192 | A==B |
| 193 | ------------------------------------------------------------------ |
| 194 | 13 exists O!=A O==B take A must match A, if exists. |
| 195 | ------------------------------------------------------------------ |
| 196 | 14 exists O==A O!=B take B if exists, must either (1) |
| 197 | match A and be up-to-date, |
| 198 | or (2) match B. |
| 199 | ------------------------------------------------------------------ |
| 200 | 15 exists O==A O==B take B must match A if exists. |
| 201 | ------------------------------------------------------------------ |
| 202 | 16 exists O==A O==B barf must match A if exists. |
| 203 | *multi* in one in another |
| 204 | ------------------------------------------------------------------- |
| 205 | |
| 206 | Note: we need to be careful in case 2 and 3. The tree A may contain |
| 207 | DF (file) when tree B require DF to be a directory by having DF/DF |
| 208 | (file). |
| 209 | |
| 210 | END_OF_CASE_TABLE |
| 211 | |
| 212 | test_expect_success '1 - must not have an entry not in A.' ' |
| 213 | rm -f .git/index XX && |
| 214 | echo XX >XX && |
| 215 | git update-index --add XX && |
| 216 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 217 | ' |
| 218 | |
| 219 | test_expect_success '2 - must match B in !O && !A && B case.' ' |
| 220 | rm -f .git/index NA && |
| 221 | cp .orig-B/NA NA && |
| 222 | git update-index --add NA && |
| 223 | read_tree_must_succeed -m $tree_O $tree_A $tree_B |
| 224 | ' |
| 225 | |
| 226 | test_expect_success '2 - matching B alone is OK in !O && !A && B case.' ' |
| 227 | rm -f .git/index NA && |
| 228 | cp .orig-B/NA NA && |
| 229 | git update-index --add NA && |
| 230 | echo extra >>NA && |
| 231 | read_tree_must_succeed -m $tree_O $tree_A $tree_B |
| 232 | ' |
| 233 | |
| 234 | test_expect_success '3 - must match A in !O && A && !B case.' ' |
| 235 | rm -f .git/index AN && |
| 236 | cp .orig-A/AN AN && |
| 237 | git update-index --add AN && |
| 238 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 239 | check_result |
| 240 | ' |
| 241 | |
| 242 | test_expect_success '3 - matching A alone is OK in !O && A && !B case.' ' |
| 243 | rm -f .git/index AN && |
| 244 | cp .orig-A/AN AN && |
| 245 | git update-index --add AN && |
| 246 | echo extra >>AN && |
| 247 | read_tree_must_succeed -m $tree_O $tree_A $tree_B |
| 248 | ' |
| 249 | |
| 250 | test_expect_success '3 (fail) - must match A in !O && A && !B case.' ' |
| 251 | rm -f .git/index AN && |
| 252 | cp .orig-A/AN AN && |
| 253 | echo extra >>AN && |
| 254 | git update-index --add AN && |
| 255 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 256 | ' |
| 257 | |
| 258 | test_expect_success '4 - must match and be up-to-date in !O && A && B && A!=B case.' ' |
| 259 | rm -f .git/index AA && |
| 260 | cp .orig-A/AA AA && |
| 261 | git update-index --add AA && |
| 262 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 263 | check_result |
| 264 | ' |
| 265 | |
| 266 | test_expect_success '4 (fail) - must match and be up-to-date in !O && A && B && A!=B case.' ' |
| 267 | rm -f .git/index AA && |
| 268 | cp .orig-A/AA AA && |
| 269 | git update-index --add AA && |
| 270 | echo extra >>AA && |
| 271 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 272 | ' |
| 273 | |
| 274 | test_expect_success '4 (fail) - must match and be up-to-date in !O && A && B && A!=B case.' ' |
| 275 | rm -f .git/index AA && |
| 276 | cp .orig-A/AA AA && |
| 277 | echo extra >>AA && |
| 278 | git update-index --add AA && |
| 279 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 280 | ' |
| 281 | |
| 282 | test_expect_success '5 - must match in !O && A && B && A==B case.' ' |
| 283 | rm -f .git/index LL && |
| 284 | cp .orig-A/LL LL && |
| 285 | git update-index --add LL && |
| 286 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 287 | check_result |
| 288 | ' |
| 289 | |
| 290 | test_expect_success '5 - must match in !O && A && B && A==B case.' ' |
| 291 | rm -f .git/index LL && |
| 292 | cp .orig-A/LL LL && |
| 293 | git update-index --add LL && |
| 294 | echo extra >>LL && |
| 295 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 296 | check_result |
| 297 | ' |
| 298 | |
| 299 | test_expect_success '5 (fail) - must match A in !O && A && B && A==B case.' ' |
| 300 | rm -f .git/index LL && |
| 301 | cp .orig-A/LL LL && |
| 302 | echo extra >>LL && |
| 303 | git update-index --add LL && |
| 304 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 305 | ' |
| 306 | |
| 307 | test_expect_success '6 - must not exist in O && !A && !B case' ' |
| 308 | rm -f .git/index DD && |
| 309 | echo DD >DD && |
| 310 | git update-index --add DD && |
| 311 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 312 | ' |
| 313 | |
| 314 | test_expect_success '7 - must not exist in O && !A && B && O!=B case' ' |
| 315 | rm -f .git/index DM && |
| 316 | cp .orig-B/DM DM && |
| 317 | git update-index --add DM && |
| 318 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 319 | ' |
| 320 | |
| 321 | test_expect_success '8 - must not exist in O && !A && B && O==B case' ' |
| 322 | rm -f .git/index DN && |
| 323 | cp .orig-B/DN DN && |
| 324 | git update-index --add DN && |
| 325 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 326 | ' |
| 327 | |
| 328 | test_expect_success '9 - must match and be up-to-date in O && A && !B && O!=A case' ' |
| 329 | rm -f .git/index MD && |
| 330 | cp .orig-A/MD MD && |
| 331 | git update-index --add MD && |
| 332 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 333 | check_result |
| 334 | ' |
| 335 | |
| 336 | test_expect_success '9 (fail) - must match and be up-to-date in O && A && !B && O!=A case' ' |
| 337 | rm -f .git/index MD && |
| 338 | cp .orig-A/MD MD && |
| 339 | git update-index --add MD && |
| 340 | echo extra >>MD && |
| 341 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 342 | ' |
| 343 | |
| 344 | test_expect_success '9 (fail) - must match and be up-to-date in O && A && !B && O!=A case' ' |
| 345 | rm -f .git/index MD && |
| 346 | cp .orig-A/MD MD && |
| 347 | echo extra >>MD && |
| 348 | git update-index --add MD && |
| 349 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 350 | ' |
| 351 | |
| 352 | test_expect_success '10 - must match and be up-to-date in O && A && !B && O==A case' ' |
| 353 | rm -f .git/index ND && |
| 354 | cp .orig-A/ND ND && |
| 355 | git update-index --add ND && |
| 356 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 357 | check_result |
| 358 | ' |
| 359 | |
| 360 | test_expect_success '10 (fail) - must match and be up-to-date in O && A && !B && O==A case' ' |
| 361 | rm -f .git/index ND && |
| 362 | cp .orig-A/ND ND && |
| 363 | git update-index --add ND && |
| 364 | echo extra >>ND && |
| 365 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 366 | ' |
| 367 | |
| 368 | test_expect_success '10 (fail) - must match and be up-to-date in O && A && !B && O==A case' ' |
| 369 | rm -f .git/index ND && |
| 370 | cp .orig-A/ND ND && |
| 371 | echo extra >>ND && |
| 372 | git update-index --add ND && |
| 373 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 374 | ' |
| 375 | |
| 376 | test_expect_success '11 - must match and be up-to-date in O && A && B && O!=A && O!=B && A!=B case' ' |
| 377 | rm -f .git/index MM && |
| 378 | cp .orig-A/MM MM && |
| 379 | git update-index --add MM && |
| 380 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 381 | check_result |
| 382 | ' |
| 383 | |
| 384 | test_expect_success '11 (fail) - must match and be up-to-date in O && A && B && O!=A && O!=B && A!=B case' ' |
| 385 | rm -f .git/index MM && |
| 386 | cp .orig-A/MM MM && |
| 387 | git update-index --add MM && |
| 388 | echo extra >>MM && |
| 389 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 390 | ' |
| 391 | |
| 392 | test_expect_success '11 (fail) - must match and be up-to-date in O && A && B && O!=A && O!=B && A!=B case' ' |
| 393 | rm -f .git/index MM && |
| 394 | cp .orig-A/MM MM && |
| 395 | echo extra >>MM && |
| 396 | git update-index --add MM && |
| 397 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 398 | ' |
| 399 | |
| 400 | test_expect_success '12 - must match A in O && A && B && O!=A && A==B case' ' |
| 401 | rm -f .git/index SS && |
| 402 | cp .orig-A/SS SS && |
| 403 | git update-index --add SS && |
| 404 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 405 | check_result |
| 406 | ' |
| 407 | |
| 408 | test_expect_success '12 - must match A in O && A && B && O!=A && A==B case' ' |
| 409 | rm -f .git/index SS && |
| 410 | cp .orig-A/SS SS && |
| 411 | git update-index --add SS && |
| 412 | echo extra >>SS && |
| 413 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 414 | check_result |
| 415 | ' |
| 416 | |
| 417 | test_expect_success '12 (fail) - must match A in O && A && B && O!=A && A==B case' ' |
| 418 | rm -f .git/index SS && |
| 419 | cp .orig-A/SS SS && |
| 420 | echo extra >>SS && |
| 421 | git update-index --add SS && |
| 422 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 423 | ' |
| 424 | |
| 425 | test_expect_success '13 - must match A in O && A && B && O!=A && O==B case' ' |
| 426 | rm -f .git/index MN && |
| 427 | cp .orig-A/MN MN && |
| 428 | git update-index --add MN && |
| 429 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 430 | check_result |
| 431 | ' |
| 432 | |
| 433 | test_expect_success '13 - must match A in O && A && B && O!=A && O==B case' ' |
| 434 | rm -f .git/index MN && |
| 435 | cp .orig-A/MN MN && |
| 436 | git update-index --add MN && |
| 437 | echo extra >>MN && |
| 438 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 439 | check_result |
| 440 | ' |
| 441 | |
| 442 | test_expect_success '14 - must match and be up-to-date in O && A && B && O==A && O!=B case' ' |
| 443 | rm -f .git/index NM && |
| 444 | cp .orig-A/NM NM && |
| 445 | git update-index --add NM && |
| 446 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 447 | check_result |
| 448 | ' |
| 449 | |
| 450 | test_expect_success '14 - may match B in O && A && B && O==A && O!=B case' ' |
| 451 | rm -f .git/index NM && |
| 452 | cp .orig-B/NM NM && |
| 453 | git update-index --add NM && |
| 454 | echo extra >>NM && |
| 455 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 456 | check_result |
| 457 | ' |
| 458 | |
| 459 | test_expect_success '14 (fail) - must match and be up-to-date in O && A && B && O==A && O!=B case' ' |
| 460 | rm -f .git/index NM && |
| 461 | cp .orig-A/NM NM && |
| 462 | git update-index --add NM && |
| 463 | echo extra >>NM && |
| 464 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 465 | ' |
| 466 | |
| 467 | test_expect_success '14 (fail) - must match and be up-to-date in O && A && B && O==A && O!=B case' ' |
| 468 | rm -f .git/index NM && |
| 469 | cp .orig-A/NM NM && |
| 470 | echo extra >>NM && |
| 471 | git update-index --add NM && |
| 472 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 473 | ' |
| 474 | |
| 475 | test_expect_success '15 - must match A in O && A && B && O==A && O==B case' ' |
| 476 | rm -f .git/index NN && |
| 477 | cp .orig-A/NN NN && |
| 478 | git update-index --add NN && |
| 479 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 480 | check_result |
| 481 | ' |
| 482 | |
| 483 | test_expect_success '15 - must match A in O && A && B && O==A && O==B case' ' |
| 484 | rm -f .git/index NN && |
| 485 | cp .orig-A/NN NN && |
| 486 | git update-index --add NN && |
| 487 | echo extra >>NN && |
| 488 | read_tree_must_succeed -m $tree_O $tree_A $tree_B && |
| 489 | check_result |
| 490 | ' |
| 491 | |
| 492 | test_expect_success '15 (fail) - must match A in O && A && B && O==A && O==B case' ' |
| 493 | rm -f .git/index NN && |
| 494 | cp .orig-A/NN NN && |
| 495 | echo extra >>NN && |
| 496 | git update-index --add NN && |
| 497 | read_tree_must_fail -m $tree_O $tree_A $tree_B |
| 498 | ' |
| 499 | |
| 500 | test_expect_success '16 - A matches in one and B matches in another.' ' |
| 501 | rm -f .git/index F16 && |
| 502 | echo F16 >F16 && |
| 503 | git update-index --add F16 && |
| 504 | tree0=$(git write-tree) && |
| 505 | echo E16 >F16 && |
| 506 | git update-index F16 && |
| 507 | tree1=$(git write-tree) && |
| 508 | read_tree_must_succeed -m $tree0 $tree1 $tree1 $tree0 && |
| 509 | git ls-files --stage |
| 510 | ' |
| 511 | |
| 512 | test_done |