git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / t / t6437-submodule-merge.sh
| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='merging with submodules' |
| 4 | |
| 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
| 10 | # |
| 11 | # history |
| 12 | # |
| 13 | # a --- c |
| 14 | # / \ / |
| 15 | # root X |
| 16 | # \ / \ |
| 17 | # b --- d |
| 18 | # |
| 19 | |
| 20 | test_expect_success setup ' |
| 21 | |
| 22 | mkdir sub && |
| 23 | (cd sub && |
| 24 | git init && |
| 25 | echo original > file && |
| 26 | git add file && |
| 27 | test_tick && |
| 28 | git commit -m sub-root) && |
| 29 | git add sub && |
| 30 | test_tick && |
| 31 | git commit -m root && |
| 32 | |
| 33 | git checkout -b a main && |
| 34 | (cd sub && |
| 35 | echo A > file && |
| 36 | git add file && |
| 37 | test_tick && |
| 38 | git commit -m sub-a) && |
| 39 | git add sub && |
| 40 | test_tick && |
| 41 | git commit -m a && |
| 42 | |
| 43 | git checkout -b b main && |
| 44 | (cd sub && |
| 45 | echo B > file && |
| 46 | git add file && |
| 47 | test_tick && |
| 48 | git commit -m sub-b) && |
| 49 | git add sub && |
| 50 | test_tick && |
| 51 | git commit -m b && |
| 52 | |
| 53 | git checkout -b c a && |
| 54 | git merge -s ours b && |
| 55 | |
| 56 | git checkout -b d b && |
| 57 | git merge -s ours a |
| 58 | ' |
| 59 | |
| 60 | # History setup |
| 61 | # |
| 62 | # b |
| 63 | # / \ |
| 64 | # init -- a d |
| 65 | # \ \ / |
| 66 | # g c |
| 67 | # |
| 68 | # a in the main repository records to sub-a in the submodule and |
| 69 | # analogous b and c. d should be automatically found by merging c into |
| 70 | # b in the main repository. |
| 71 | test_expect_success 'setup for merge search' ' |
| 72 | mkdir merge-search && |
| 73 | (cd merge-search && |
| 74 | git init && |
| 75 | mkdir sub && |
| 76 | (cd sub && |
| 77 | git init && |
| 78 | echo "file-a" > file-a && |
| 79 | git add file-a && |
| 80 | git commit -m "sub-a" && |
| 81 | git branch sub-a) && |
| 82 | git commit --allow-empty -m init && |
| 83 | git branch init && |
| 84 | git add sub && |
| 85 | git commit -m "a" && |
| 86 | git branch a && |
| 87 | |
| 88 | git checkout -b b && |
| 89 | (cd sub && |
| 90 | git checkout -b sub-b && |
| 91 | echo "file-b" > file-b && |
| 92 | git add file-b && |
| 93 | git commit -m "sub-b") && |
| 94 | git commit -a -m "b" && |
| 95 | |
| 96 | git checkout -b c a && |
| 97 | (cd sub && |
| 98 | git checkout -b sub-c sub-a && |
| 99 | echo "file-c" > file-c && |
| 100 | git add file-c && |
| 101 | git commit -m "sub-c") && |
| 102 | git commit -a -m "c") |
| 103 | ' |
| 104 | |
| 105 | test_expect_success 'merging should conflict for non fast-forward' ' |
| 106 | test_when_finished "git -C merge-search reset --hard" && |
| 107 | (cd merge-search && |
| 108 | git checkout -b test-nonforward-a b && |
| 109 | test_must_fail git merge c 2>actual && |
| 110 | sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-c)" && |
| 111 | test_grep "$sub_expect" actual |
| 112 | ) |
| 113 | ' |
| 114 | |
| 115 | test_expect_success 'finish setup for merge-search' ' |
| 116 | (cd merge-search && |
| 117 | git checkout -b d a && |
| 118 | (cd sub && |
| 119 | git checkout -b sub-d sub-b && |
| 120 | git merge sub-c) && |
| 121 | git commit -a -m "d" && |
| 122 | git branch test b && |
| 123 | |
| 124 | git checkout -b g init && |
| 125 | (cd sub && |
| 126 | git checkout -b sub-g sub-c) && |
| 127 | git add sub && |
| 128 | git commit -a -m "g") |
| 129 | ' |
| 130 | |
| 131 | test_expect_success 'merge with one side as a fast-forward of the other' ' |
| 132 | (cd merge-search && |
| 133 | git checkout -b test-forward b && |
| 134 | git merge d && |
| 135 | git ls-tree test-forward sub | cut -f1 | cut -f3 -d" " > actual && |
| 136 | (cd sub && |
| 137 | git rev-parse sub-d > ../expect) && |
| 138 | test_cmp expect actual) |
| 139 | ' |
| 140 | |
| 141 | test_expect_success 'merging should conflict for non fast-forward (resolution exists)' ' |
| 142 | (cd merge-search && |
| 143 | git checkout -b test-nonforward-b b && |
| 144 | (cd sub && |
| 145 | git rev-parse --short sub-d > ../expect) && |
| 146 | test_must_fail git merge c >actual 2>sub-actual && |
| 147 | sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-c)" && |
| 148 | test_grep "$sub_expect" sub-actual && |
| 149 | grep $(cat expect) actual > /dev/null && |
| 150 | git reset --hard) |
| 151 | ' |
| 152 | |
| 153 | test_expect_success 'merging should fail for ambiguous common parent' ' |
| 154 | (cd merge-search && |
| 155 | git checkout -b test-ambiguous b && |
| 156 | (cd sub && |
| 157 | git checkout -b ambiguous sub-b && |
| 158 | git merge sub-c && |
| 159 | git rev-parse --short sub-d >../expect1 && |
| 160 | git rev-parse --short ambiguous >../expect2 |
| 161 | ) && |
| 162 | test_must_fail git merge c >actual 2>sub-actual && |
| 163 | sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-c)" && |
| 164 | test_grep "$sub_expect" sub-actual && |
| 165 | grep $(cat expect1) actual > /dev/null && |
| 166 | grep $(cat expect2) actual > /dev/null && |
| 167 | git reset --hard) |
| 168 | ' |
| 169 | |
| 170 | # in a situation like this |
| 171 | # |
| 172 | # submodule tree: |
| 173 | # |
| 174 | # sub-a --- sub-b --- sub-d |
| 175 | # |
| 176 | # main tree: |
| 177 | # |
| 178 | # e (sub-a) |
| 179 | # / |
| 180 | # bb (sub-b) |
| 181 | # \ |
| 182 | # f (sub-d) |
| 183 | # |
| 184 | # A merge between e and f should fail because one of the submodule |
| 185 | # commits (sub-a) does not descend from the submodule merge-base (sub-b). |
| 186 | # |
| 187 | test_expect_success 'merging should fail for changes that are backwards' ' |
| 188 | (cd merge-search && |
| 189 | git checkout -b bb a && |
| 190 | (cd sub && |
| 191 | git checkout sub-b) && |
| 192 | git commit -a -m "bb" && |
| 193 | |
| 194 | git checkout -b e bb && |
| 195 | (cd sub && |
| 196 | git checkout sub-a) && |
| 197 | git commit -a -m "e" && |
| 198 | |
| 199 | git checkout -b f bb && |
| 200 | (cd sub && |
| 201 | git checkout sub-d) && |
| 202 | git commit -a -m "f" && |
| 203 | |
| 204 | git checkout -b test-backward e && |
| 205 | test_must_fail git merge f 2>actual && |
| 206 | sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-d)" && |
| 207 | test_grep "$sub_expect" actual |
| 208 | ) |
| 209 | ' |
| 210 | |
| 211 | |
| 212 | # Check that the conflicting submodule is detected when it is |
| 213 | # in the common ancestor. status should be 'U00...00" |
| 214 | test_expect_success 'git submodule status should display the merge conflict properly with merge base' ' |
| 215 | (cd merge-search && |
| 216 | cat >.gitmodules <<EOF && |
| 217 | [submodule "sub"] |
| 218 | path = sub |
| 219 | url = $TRASH_DIRECTORY/sub |
| 220 | EOF |
| 221 | cat >expect <<EOF && |
| 222 | U$ZERO_OID sub |
| 223 | EOF |
| 224 | git submodule status > actual && |
| 225 | test_cmp expect actual && |
| 226 | git reset --hard) |
| 227 | ' |
| 228 | |
| 229 | # Check that the conflicting submodule is detected when it is |
| 230 | # not in the common ancestor. status should be 'U00...00" |
| 231 | test_expect_success 'git submodule status should display the merge conflict properly without merge-base' ' |
| 232 | (cd merge-search && |
| 233 | git checkout -b test-no-merge-base g && |
| 234 | test_must_fail git merge b && |
| 235 | cat >.gitmodules <<EOF && |
| 236 | [submodule "sub"] |
| 237 | path = sub |
| 238 | url = $TRASH_DIRECTORY/sub |
| 239 | EOF |
| 240 | cat >expect <<EOF && |
| 241 | U$ZERO_OID sub |
| 242 | EOF |
| 243 | git submodule status > actual && |
| 244 | test_cmp expect actual && |
| 245 | git reset --hard) |
| 246 | ' |
| 247 | |
| 248 | |
| 249 | test_expect_success 'merging with a modify/modify conflict between merge bases' ' |
| 250 | git reset --hard HEAD && |
| 251 | git checkout -b test2 c && |
| 252 | git merge d |
| 253 | ' |
| 254 | |
| 255 | # canonical criss-cross history in top and submodule |
| 256 | test_expect_success 'setup for recursive merge with submodule' ' |
| 257 | mkdir merge-recursive && |
| 258 | (cd merge-recursive && |
| 259 | git init && |
| 260 | mkdir sub && |
| 261 | (cd sub && |
| 262 | git init && |
| 263 | test_commit a && |
| 264 | git checkout -b sub-b main && |
| 265 | test_commit b && |
| 266 | git checkout -b sub-c main && |
| 267 | test_commit c && |
| 268 | git checkout -b sub-bc sub-b && |
| 269 | git merge sub-c && |
| 270 | git checkout -b sub-cb sub-c && |
| 271 | git merge sub-b && |
| 272 | git checkout main) && |
| 273 | git add sub && |
| 274 | git commit -m a && |
| 275 | git checkout -b top-b main && |
| 276 | (cd sub && git checkout sub-b) && |
| 277 | git add sub && |
| 278 | git commit -m b && |
| 279 | git checkout -b top-c main && |
| 280 | (cd sub && git checkout sub-c) && |
| 281 | git add sub && |
| 282 | git commit -m c && |
| 283 | git checkout -b top-bc top-b && |
| 284 | git merge -s ours --no-commit top-c && |
| 285 | (cd sub && git checkout sub-bc) && |
| 286 | git add sub && |
| 287 | git commit -m bc && |
| 288 | git checkout -b top-cb top-c && |
| 289 | git merge -s ours --no-commit top-b && |
| 290 | (cd sub && git checkout sub-cb) && |
| 291 | git add sub && |
| 292 | git commit -m cb) |
| 293 | ' |
| 294 | |
| 295 | # merge should leave submodule unmerged in index |
| 296 | test_expect_success 'recursive merge with submodule' ' |
| 297 | (cd merge-recursive && |
| 298 | test_must_fail git merge top-bc && |
| 299 | echo "160000 $(git rev-parse top-cb:sub) 2 sub" > expect2 && |
| 300 | echo "160000 $(git rev-parse top-bc:sub) 3 sub" > expect3 && |
| 301 | git ls-files -u > actual && |
| 302 | grep "$(cat expect2)" actual > /dev/null && |
| 303 | grep "$(cat expect3)" actual > /dev/null) |
| 304 | ' |
| 305 | |
| 306 | # File/submodule conflict |
| 307 | # Commit O: <empty> |
| 308 | # Commit A: path (submodule) |
| 309 | # Commit B: path |
| 310 | # Expected: path/ is submodule and file contents for B's path are somewhere |
| 311 | |
| 312 | test_expect_success 'setup file/submodule conflict' ' |
| 313 | git init file-submodule && |
| 314 | ( |
| 315 | cd file-submodule && |
| 316 | |
| 317 | git commit --allow-empty -m O && |
| 318 | |
| 319 | git branch A && |
| 320 | git branch B && |
| 321 | |
| 322 | git checkout B && |
| 323 | echo content >path && |
| 324 | git add path && |
| 325 | git commit -m B && |
| 326 | |
| 327 | git checkout A && |
| 328 | git init path && |
| 329 | test_commit -C path world && |
| 330 | git submodule add ./path && |
| 331 | git commit -m A |
| 332 | ) |
| 333 | ' |
| 334 | |
| 335 | test_expect_success 'file/submodule conflict' ' |
| 336 | test_when_finished "git -C file-submodule reset --hard" && |
| 337 | ( |
| 338 | cd file-submodule && |
| 339 | |
| 340 | git checkout A^0 && |
| 341 | test_must_fail git merge B^0 && |
| 342 | |
| 343 | git ls-files -s >out && |
| 344 | test_line_count = 3 out && |
| 345 | git ls-files -u >out && |
| 346 | test_line_count = 2 out && |
| 347 | |
| 348 | # path/ is still a submodule |
| 349 | test_path_is_dir path/.git && |
| 350 | |
| 351 | # There is a submodule at "path", so B:path cannot be written |
| 352 | # there. We expect it to be written somewhere in the same |
| 353 | # directory, though, so just grep for its content in all |
| 354 | # files, and ignore "grep: path: Is a directory" message |
| 355 | echo Checking if contents from B:path showed up anywhere && |
| 356 | grep -q content * 2>/dev/null |
| 357 | ) |
| 358 | ' |
| 359 | |
| 360 | test_expect_success 'file/submodule conflict; merge --abort works afterward' ' |
| 361 | test_when_finished "git -C file-submodule reset --hard" && |
| 362 | ( |
| 363 | cd file-submodule && |
| 364 | |
| 365 | git checkout A^0 && |
| 366 | test_must_fail git merge B^0 >out 2>err && |
| 367 | |
| 368 | test_path_is_file .git/MERGE_HEAD && |
| 369 | git merge --abort |
| 370 | ) |
| 371 | ' |
| 372 | |
| 373 | # Directory/submodule conflict |
| 374 | # Commit O: <empty> |
| 375 | # Commit A: path (submodule), with sole tracked file named 'world' |
| 376 | # Commit B1: path/file |
| 377 | # Commit B2: path/world |
| 378 | # |
| 379 | # Expected from merge of A & B1: |
| 380 | # Contents under path/ from commit B1 are renamed elsewhere; we do not |
| 381 | # want to write files from one of our tracked directories into a submodule |
| 382 | # |
| 383 | # Expected from merge of A & B2: |
| 384 | # Similar to last merge, but with a slight twist: we don't want paths |
| 385 | # under the submodule to be treated as untracked or in the way. |
| 386 | |
| 387 | test_expect_success 'setup directory/submodule conflict' ' |
| 388 | git init directory-submodule && |
| 389 | ( |
| 390 | cd directory-submodule && |
| 391 | |
| 392 | git commit --allow-empty -m O && |
| 393 | |
| 394 | git branch A && |
| 395 | git branch B1 && |
| 396 | git branch B2 && |
| 397 | |
| 398 | git checkout B1 && |
| 399 | mkdir path && |
| 400 | echo contents >path/file && |
| 401 | git add path/file && |
| 402 | git commit -m B1 && |
| 403 | |
| 404 | git checkout B2 && |
| 405 | mkdir path && |
| 406 | echo contents >path/world && |
| 407 | git add path/world && |
| 408 | git commit -m B2 && |
| 409 | |
| 410 | git checkout A && |
| 411 | git init path && |
| 412 | test_commit -C path hello world && |
| 413 | git submodule add ./path && |
| 414 | git commit -m A |
| 415 | ) |
| 416 | ' |
| 417 | |
| 418 | test_expect_failure 'directory/submodule conflict; keep submodule clean' ' |
| 419 | test_when_finished "git -C directory-submodule reset --hard" && |
| 420 | ( |
| 421 | cd directory-submodule && |
| 422 | |
| 423 | git checkout A^0 && |
| 424 | test_must_fail git merge B1^0 && |
| 425 | |
| 426 | git ls-files -s >out && |
| 427 | test_line_count = 3 out && |
| 428 | git ls-files -u >out && |
| 429 | test_line_count = 1 out && |
| 430 | |
| 431 | # path/ is still a submodule |
| 432 | test_path_is_dir path/.git && |
| 433 | |
| 434 | echo Checking if contents from B1:path/file showed up && |
| 435 | # Would rather use grep -r, but that is GNU extension... |
| 436 | git ls-files -co | xargs grep -q contents 2>/dev/null && |
| 437 | |
| 438 | # However, B1:path/file should NOT have shown up at path/file, |
| 439 | # because we should not write into the submodule |
| 440 | test_path_is_missing path/file |
| 441 | ) |
| 442 | ' |
| 443 | |
| 444 | test_expect_success !FAIL_PREREQS 'directory/submodule conflict; should not treat submodule files as untracked or in the way' ' |
| 445 | test_when_finished "git -C directory-submodule/path reset --hard" && |
| 446 | test_when_finished "git -C directory-submodule reset --hard" && |
| 447 | ( |
| 448 | cd directory-submodule && |
| 449 | |
| 450 | git checkout A^0 && |
| 451 | test_must_fail git merge B2^0 >out 2>err && |
| 452 | |
| 453 | # We do not want files within the submodule to prevent the |
| 454 | # merge from starting; we should not be writing to such paths |
| 455 | # anyway. |
| 456 | test_grep ! "refusing to lose untracked file at" err |
| 457 | ) |
| 458 | ' |
| 459 | |
| 460 | test_expect_failure 'directory/submodule conflict; merge --abort works afterward' ' |
| 461 | test_when_finished "git -C directory-submodule/path reset --hard" && |
| 462 | test_when_finished "git -C directory-submodule reset --hard" && |
| 463 | ( |
| 464 | cd directory-submodule && |
| 465 | |
| 466 | git checkout A^0 && |
| 467 | test_must_fail git merge B2^0 && |
| 468 | test_path_is_file .git/MERGE_HEAD && |
| 469 | |
| 470 | # merge --abort should succeed, should clear .git/MERGE_HEAD, |
| 471 | # and should not leave behind any conflicted files |
| 472 | git merge --abort && |
| 473 | test_path_is_missing .git/MERGE_HEAD && |
| 474 | git ls-files -u >conflicts && |
| 475 | test_must_be_empty conflicts |
| 476 | ) |
| 477 | ' |
| 478 | |
| 479 | # Setup: |
| 480 | # - Submodule has 2 commits: a and b |
| 481 | # - Superproject branch 'a' adds and commits submodule pointing to 'commit a' |
| 482 | # - Superproject branch 'b' adds and commits submodule pointing to 'commit b' |
| 483 | # If these two branches are now merged, there is no merge base |
| 484 | test_expect_success 'setup for null merge base' ' |
| 485 | mkdir no-merge-base && |
| 486 | (cd no-merge-base && |
| 487 | git init && |
| 488 | mkdir sub && |
| 489 | (cd sub && |
| 490 | git init && |
| 491 | echo "file-a" > file-a && |
| 492 | git add file-a && |
| 493 | git commit -m "commit a") && |
| 494 | git commit --allow-empty -m init && |
| 495 | git branch init && |
| 496 | git checkout -b a init && |
| 497 | git add sub && |
| 498 | git commit -m "a" && |
| 499 | git switch main && |
| 500 | (cd sub && |
| 501 | echo "file-b" > file-b && |
| 502 | git add file-b && |
| 503 | git commit -m "commit b")) |
| 504 | ' |
| 505 | |
| 506 | test_expect_success 'merging should fail with no merge base' ' |
| 507 | (cd no-merge-base && |
| 508 | git checkout -b b init && |
| 509 | git add sub && |
| 510 | git commit -m "b" && |
| 511 | test_must_fail git merge a 2>actual && |
| 512 | sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short HEAD^1)" && |
| 513 | test_grep "$sub_expect" actual |
| 514 | ) |
| 515 | ' |
| 516 | |
| 517 | test_expect_success 'setup for commit-graphs in superproject and submodule' ' |
| 518 | git init commit-graph && |
| 519 | (cd commit-graph && |
| 520 | git init sub && |
| 521 | for i in 1 2 3 4 5 6 7 8 9 10 |
| 522 | do |
| 523 | test_commit -C sub "sub-$i" || return 1 |
| 524 | done && |
| 525 | |
| 526 | git -C sub checkout --detach sub-1 && |
| 527 | git add sub && |
| 528 | git commit -m base && |
| 529 | |
| 530 | # Write the commit-graph of the superproject while it only |
| 531 | # contains a single commit. Any commit-graph position of the |
| 532 | # submodule is thus out of bounds for the superproject. |
| 533 | git commit-graph write --reachable && |
| 534 | |
| 535 | git checkout -b side && |
| 536 | git -C sub checkout --detach sub-5 && |
| 537 | git add sub && |
| 538 | git commit -m side && |
| 539 | |
| 540 | git checkout main && |
| 541 | git -C sub checkout --detach sub-10 && |
| 542 | git add sub && |
| 543 | git commit -m main && |
| 544 | |
| 545 | git -C sub commit-graph write --reachable) |
| 546 | ' |
| 547 | |
| 548 | test_expect_success 'merge does not mix up superproject and submodule commit-graphs' ' |
| 549 | (cd commit-graph && |
| 550 | git merge side && |
| 551 | git rev-parse HEAD:sub >actual && |
| 552 | git -C sub rev-parse sub-10^{commit} >expect && |
| 553 | test_cmp expect actual) |
| 554 | ' |
| 555 | |
| 556 | test_expect_success 'merge with many packed submodules reports conflicts' ' |
| 557 | test_config_global protocol.file.allow always && |
| 558 | |
| 559 | # Create 16 submodules with two divergent branches each. |
| 560 | submodules="A B C D E F G H I J K L M N O P" && |
| 561 | for name in $submodules |
| 562 | do |
| 563 | git init source-$name && |
| 564 | test_commit -C source-$name $name-main && |
| 565 | git -C source-$name switch --create branch-a main && |
| 566 | git -C source-$name commit --allow-empty --message $name-branch-a && |
| 567 | git -C source-$name switch --create branch-b main && |
| 568 | git -C source-$name commit --allow-empty --message $name-branch-b || return 1 |
| 569 | done && |
| 570 | |
| 571 | # Create the superproject and add all submodules. |
| 572 | git init many-packed && |
| 573 | for name in $submodules |
| 574 | do |
| 575 | git -C many-packed submodule add --branch main "file://$PWD/source-$name" $name || return 1 |
| 576 | done && |
| 577 | git -C many-packed commit --message main && |
| 578 | |
| 579 | # Create two divergent commits in the superproject that update all |
| 580 | # submodules to the divergent branches. |
| 581 | for branch in branch-a branch-b |
| 582 | do |
| 583 | git -C many-packed switch -c $branch main && |
| 584 | for name in $submodules |
| 585 | do |
| 586 | git -C many-packed/$name switch $branch || return 1 |
| 587 | done && |
| 588 | git -C many-packed add $submodules && |
| 589 | git -C many-packed commit --message $branch || return 1 |
| 590 | done && |
| 591 | |
| 592 | # Clone the superproject to ensure that everything is well-packed and |
| 593 | # then merge the two branches, creating conflicts for every submodule. |
| 594 | git clone many-packed many-packed-clone && |
| 595 | git -C many-packed-clone submodule update --init && |
| 596 | git -C many-packed-clone switch branch-a && |
| 597 | test_expect_code 1 git -C many-packed-clone -c advice.submoduleMergeConflict=false merge branch-b >out 2>err && |
| 598 | grep "^CONFLICT (submodule)" out >conflicts && |
| 599 | test_line_count = 16 conflicts && |
| 600 | test_must_be_empty err |
| 601 | ' |
| 602 | |
| 603 | test_done |