3 test_description
='merging with submodules'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
=1
9 export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
11 TEST_PASSES_SANITIZE_LEAK
=true
13 .
"$TEST_DIRECTORY"/lib-merge.sh
25 test_expect_success setup
'
30 echo original > file &&
33 git commit -m sub-root) &&
38 git checkout -b a main &&
43 git commit -m sub-a) &&
48 git checkout -b b main &&
53 git commit -m sub-b) &&
58 git checkout -b c a &&
59 git merge -s ours b &&
61 git checkout -b d b &&
73 # a in the main repository records to sub-a in the submodule and
74 # analogous b and c. d should be automatically found by merging c into
75 # b in the main repository.
76 test_expect_success
'setup for merge search' '
83 echo "file-a" > file-a &&
85 git commit -m "sub-a" &&
87 git commit --allow-empty -m init &&
95 git checkout -b sub-b &&
96 echo "file-b" > file-b &&
98 git commit -m "sub-b") &&
99 git commit -a -m "b" &&
101 git checkout -b c a &&
103 git checkout -b sub-c sub-a &&
104 echo "file-c" > file-c &&
106 git commit -m "sub-c") &&
107 git commit -a -m "c")
110 test_expect_success
'merging should conflict for non fast-forward' '
111 test_when_finished "git -C merge-search reset --hard" &&
113 git checkout -b test-nonforward-a b &&
114 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
116 test_must_fail git merge c >actual &&
117 sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-c)" &&
118 grep "$sub_expect" actual
120 test_must_fail git merge c 2> actual
124 test_expect_success
'finish setup for merge-search' '
126 git checkout -b d a &&
128 git checkout -b sub-d sub-b &&
130 git commit -a -m "d" &&
133 git checkout -b g init &&
135 git checkout -b sub-g sub-c) &&
137 git commit -a -m "g")
140 test_expect_success
'merge with one side as a fast-forward of the other' '
142 git checkout -b test-forward b &&
144 git ls-tree test-forward sub | cut -f1 | cut -f3 -d" " > actual &&
146 git rev-parse sub-d > ../expect) &&
147 test_cmp expect actual)
150 test_expect_success
'merging should conflict for non fast-forward (resolution exists)' '
152 git checkout -b test-nonforward-b b &&
154 git rev-parse --short sub-d > ../expect) &&
155 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
157 test_must_fail git merge c >actual &&
158 sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-c)" &&
159 grep "$sub_expect" actual
161 test_must_fail git merge c 2> actual
163 grep $(cat expect) actual > /dev/null &&
167 test_expect_success
'merging should fail for ambiguous common parent' '
169 git checkout -b test-ambiguous b &&
171 git checkout -b ambiguous sub-b &&
173 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
175 git rev-parse --short sub-d >../expect1 &&
176 git rev-parse --short ambiguous >../expect2
178 git rev-parse sub-d > ../expect1 &&
179 git rev-parse ambiguous > ../expect2
182 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
184 test_must_fail git merge c >actual &&
185 sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-c)" &&
186 grep "$sub_expect" actual
188 test_must_fail git merge c 2> actual
190 grep $(cat expect1) actual > /dev/null &&
191 grep $(cat expect2) actual > /dev/null &&
195 # in a situation like this
199 # sub-a --- sub-b --- sub-d
209 # A merge between e and f should fail because one of the submodule
210 # commits (sub-a) does not descend from the submodule merge-base (sub-b).
212 test_expect_success
'merging should fail for changes that are backwards' '
214 git checkout -b bb a &&
216 git checkout sub-b) &&
217 git commit -a -m "bb" &&
219 git checkout -b e bb &&
221 git checkout sub-a) &&
222 git commit -a -m "e" &&
224 git checkout -b f bb &&
226 git checkout sub-d) &&
227 git commit -a -m "f" &&
229 git checkout -b test-backward e &&
230 test_must_fail git merge f >actual &&
231 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
233 sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-d)" &&
234 grep "$sub_expect" actual
239 # Check that the conflicting submodule is detected when it is
240 # in the common ancestor. status should be 'U00...00"
241 test_expect_success
'git submodule status should display the merge conflict properly with merge base' '
243 cat >.gitmodules <<EOF &&
246 url = $TRASH_DIRECTORY/sub
251 git submodule status > actual &&
252 test_cmp expect actual &&
256 # Check that the conflicting submodule is detected when it is
257 # not in the common ancestor. status should be 'U00...00"
258 test_expect_success
'git submodule status should display the merge conflict properly without merge-base' '
260 git checkout -b test-no-merge-base g &&
261 test_must_fail git merge b &&
262 cat >.gitmodules <<EOF &&
265 url = $TRASH_DIRECTORY/sub
270 git submodule status > actual &&
271 test_cmp expect actual &&
276 test_expect_success
'merging with a modify/modify conflict between merge bases' '
277 git reset --hard HEAD &&
278 git checkout -b test2 c &&
282 # canonical criss-cross history in top and submodule
283 test_expect_success
'setup for recursive merge with submodule' '
284 mkdir merge-recursive &&
285 (cd merge-recursive &&
291 git checkout -b sub-b main &&
293 git checkout -b sub-c main &&
295 git checkout -b sub-bc sub-b &&
297 git checkout -b sub-cb sub-c &&
299 git checkout main) &&
302 git checkout -b top-b main &&
303 (cd sub && git checkout sub-b) &&
306 git checkout -b top-c main &&
307 (cd sub && git checkout sub-c) &&
310 git checkout -b top-bc top-b &&
311 git merge -s ours --no-commit top-c &&
312 (cd sub && git checkout sub-bc) &&
315 git checkout -b top-cb top-c &&
316 git merge -s ours --no-commit top-b &&
317 (cd sub && git checkout sub-cb) &&
322 # merge should leave submodule unmerged in index
323 test_expect_success
'recursive merge with submodule' '
324 (cd merge-recursive &&
325 test_must_fail git merge top-bc &&
326 echo "160000 $(git rev-parse top-cb:sub) 2 sub" > expect2 &&
327 echo "160000 $(git rev-parse top-bc:sub) 3 sub" > expect3 &&
328 git ls-files -u > actual &&
329 grep "$(cat expect2)" actual > /dev/null &&
330 grep "$(cat expect3)" actual > /dev/null)
333 # File/submodule conflict
335 # Commit A: path (submodule)
337 # Expected: path/ is submodule and file contents for B's path are somewhere
339 test_expect_success
'setup file/submodule conflict' '
340 git init file-submodule &&
344 git commit --allow-empty -m O &&
350 echo content >path &&
356 test_commit -C path world &&
357 git submodule add ./path &&
362 test_expect_merge_algorithm failure success
'file/submodule conflict' '
363 test_when_finished "git -C file-submodule reset --hard" &&
368 test_must_fail git merge B^0 &&
370 git ls-files -s >out &&
371 test_line_count = 3 out &&
372 git ls-files -u >out &&
373 test_line_count = 2 out &&
375 # path/ is still a submodule
376 test_path_is_dir path/.git &&
378 # There is a submodule at "path", so B:path cannot be written
379 # there. We expect it to be written somewhere in the same
380 # directory, though, so just grep for its content in all
381 # files, and ignore "grep: path: Is a directory" message
382 echo Checking if contents from B:path showed up anywhere &&
383 grep -q content * 2>/dev/null
387 test_expect_success
'file/submodule conflict; merge --abort works afterward' '
388 test_when_finished "git -C file-submodule reset --hard" &&
393 test_must_fail git merge B^0 >out 2>err &&
395 test_path_is_file .git/MERGE_HEAD &&
400 # Directory/submodule conflict
402 # Commit A: path (submodule), with sole tracked file named 'world'
403 # Commit B1: path/file
404 # Commit B2: path/world
406 # Expected from merge of A & B1:
407 # Contents under path/ from commit B1 are renamed elsewhere; we do not
408 # want to write files from one of our tracked directories into a submodule
410 # Expected from merge of A & B2:
411 # Similar to last merge, but with a slight twist: we don't want paths
412 # under the submodule to be treated as untracked or in the way.
414 test_expect_success
'setup directory/submodule conflict' '
415 git init directory-submodule &&
417 cd directory-submodule &&
419 git commit --allow-empty -m O &&
427 echo contents >path/file &&
433 echo contents >path/world &&
434 git add path/world &&
439 test_commit -C path hello world &&
440 git submodule add ./path &&
445 test_expect_failure
'directory/submodule conflict; keep submodule clean' '
446 test_when_finished "git -C directory-submodule reset --hard" &&
448 cd directory-submodule &&
451 test_must_fail git merge B1^0 &&
453 git ls-files -s >out &&
454 test_line_count = 3 out &&
455 git ls-files -u >out &&
456 test_line_count = 1 out &&
458 # path/ is still a submodule
459 test_path_is_dir path/.git &&
461 echo Checking if contents from B1:path/file showed up &&
462 # Would rather use grep -r, but that is GNU extension...
463 git ls-files -co | xargs grep -q contents 2>/dev/null &&
465 # However, B1:path/file should NOT have shown up at path/file,
466 # because we should not write into the submodule
467 test_path_is_missing path/file
471 test_expect_merge_algorithm failure success
!FAIL_PREREQS
'directory/submodule conflict; should not treat submodule files as untracked or in the way' '
472 test_when_finished "git -C directory-submodule/path reset --hard" &&
473 test_when_finished "git -C directory-submodule reset --hard" &&
475 cd directory-submodule &&
478 test_must_fail git merge B2^0 >out 2>err &&
480 # We do not want files within the submodule to prevent the
481 # merge from starting; we should not be writing to such paths
483 test_grep ! "refusing to lose untracked file at" err
487 test_expect_failure
'directory/submodule conflict; merge --abort works afterward' '
488 test_when_finished "git -C directory-submodule/path reset --hard" &&
489 test_when_finished "git -C directory-submodule reset --hard" &&
491 cd directory-submodule &&
494 test_must_fail git merge B2^0 &&
495 test_path_is_file .git/MERGE_HEAD &&
497 # merge --abort should succeed, should clear .git/MERGE_HEAD,
498 # and should not leave behind any conflicted files
500 test_path_is_missing .git/MERGE_HEAD &&
501 git ls-files -u >conflicts &&
502 test_must_be_empty conflicts
507 # - Submodule has 2 commits: a and b
508 # - Superproject branch 'a' adds and commits submodule pointing to 'commit a'
509 # - Superproject branch 'b' adds and commits submodule pointing to 'commit b'
510 # If these two branches are now merged, there is no merge base
511 test_expect_success
'setup for null merge base' '
512 mkdir no-merge-base &&
518 echo "file-a" > file-a &&
520 git commit -m "commit a") &&
521 git commit --allow-empty -m init &&
523 git checkout -b a init &&
528 echo "file-b" > file-b &&
530 git commit -m "commit b"))
533 test_expect_success
'merging should fail with no merge base' '
535 git checkout -b b init &&
538 test_must_fail git merge a >actual &&
539 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
541 sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short HEAD^1)" &&
542 grep "$sub_expect" actual