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