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
12 .
"$TEST_DIRECTORY"/lib-merge.sh
24 test_expect_success setup
'
29 echo original > file &&
32 git commit -m sub-root) &&
37 git checkout -b a main &&
42 git commit -m sub-a) &&
47 git checkout -b b main &&
52 git commit -m sub-b) &&
57 git checkout -b c a &&
58 git merge -s ours b &&
60 git checkout -b d b &&
72 # a in the main repository records to sub-a in the submodule and
73 # analogous b and c. d should be automatically found by merging c into
74 # b in the main repository.
75 test_expect_success
'setup for merge search' '
82 echo "file-a" > file-a &&
84 git commit -m "sub-a" &&
86 git commit --allow-empty -m init &&
94 git checkout -b sub-b &&
95 echo "file-b" > file-b &&
97 git commit -m "sub-b") &&
98 git commit -a -m "b" &&
100 git checkout -b c a &&
102 git checkout -b sub-c sub-a &&
103 echo "file-c" > file-c &&
105 git commit -m "sub-c") &&
106 git commit -a -m "c")
109 test_expect_success
'merging should conflict for non fast-forward' '
110 test_when_finished "git -C merge-search reset --hard" &&
112 git checkout -b test-nonforward-a b &&
113 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
115 test_must_fail git merge c >actual &&
116 sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-c)" &&
117 grep "$sub_expect" actual
119 test_must_fail git merge c 2> actual
123 test_expect_success
'finish setup for merge-search' '
125 git checkout -b d a &&
127 git checkout -b sub-d sub-b &&
129 git commit -a -m "d" &&
132 git checkout -b g init &&
134 git checkout -b sub-g sub-c) &&
136 git commit -a -m "g")
139 test_expect_success
'merge with one side as a fast-forward of the other' '
141 git checkout -b test-forward b &&
143 git ls-tree test-forward sub | cut -f1 | cut -f3 -d" " > actual &&
145 git rev-parse sub-d > ../expect) &&
146 test_cmp expect actual)
149 test_expect_success
'merging should conflict for non fast-forward (resolution exists)' '
151 git checkout -b test-nonforward-b b &&
153 git rev-parse --short sub-d > ../expect) &&
154 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
156 test_must_fail git merge c >actual &&
157 sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-c)" &&
158 grep "$sub_expect" actual
160 test_must_fail git merge c 2> actual
162 grep $(cat expect) actual > /dev/null &&
166 test_expect_success
'merging should fail for ambiguous common parent' '
168 git checkout -b test-ambiguous b &&
170 git checkout -b ambiguous sub-b &&
172 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
174 git rev-parse --short sub-d >../expect1 &&
175 git rev-parse --short ambiguous >../expect2
177 git rev-parse sub-d > ../expect1 &&
178 git rev-parse ambiguous > ../expect2
181 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
183 test_must_fail git merge c >actual &&
184 sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-c)" &&
185 grep "$sub_expect" actual
187 test_must_fail git merge c 2> actual
189 grep $(cat expect1) actual > /dev/null &&
190 grep $(cat expect2) actual > /dev/null &&
194 # in a situation like this
198 # sub-a --- sub-b --- sub-d
208 # A merge between e and f should fail because one of the submodule
209 # commits (sub-a) does not descend from the submodule merge-base (sub-b).
211 test_expect_success
'merging should fail for changes that are backwards' '
213 git checkout -b bb a &&
215 git checkout sub-b) &&
216 git commit -a -m "bb" &&
218 git checkout -b e bb &&
220 git checkout sub-a) &&
221 git commit -a -m "e" &&
223 git checkout -b f bb &&
225 git checkout sub-d) &&
226 git commit -a -m "f" &&
228 git checkout -b test-backward e &&
229 test_must_fail git merge f >actual &&
230 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
232 sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-d)" &&
233 grep "$sub_expect" actual
238 # Check that the conflicting submodule is detected when it is
239 # in the common ancestor. status should be 'U00...00"
240 test_expect_success
'git submodule status should display the merge conflict properly with merge base' '
242 cat >.gitmodules <<EOF &&
245 url = $TRASH_DIRECTORY/sub
250 git submodule status > actual &&
251 test_cmp expect actual &&
255 # Check that the conflicting submodule is detected when it is
256 # not in the common ancestor. status should be 'U00...00"
257 test_expect_success
'git submodule status should display the merge conflict properly without merge-base' '
259 git checkout -b test-no-merge-base g &&
260 test_must_fail git merge b &&
261 cat >.gitmodules <<EOF &&
264 url = $TRASH_DIRECTORY/sub
269 git submodule status > actual &&
270 test_cmp expect actual &&
275 test_expect_success
'merging with a modify/modify conflict between merge bases' '
276 git reset --hard HEAD &&
277 git checkout -b test2 c &&
281 # canonical criss-cross history in top and submodule
282 test_expect_success
'setup for recursive merge with submodule' '
283 mkdir merge-recursive &&
284 (cd merge-recursive &&
290 git checkout -b sub-b main &&
292 git checkout -b sub-c main &&
294 git checkout -b sub-bc sub-b &&
296 git checkout -b sub-cb sub-c &&
298 git checkout main) &&
301 git checkout -b top-b main &&
302 (cd sub && git checkout sub-b) &&
305 git checkout -b top-c main &&
306 (cd sub && git checkout sub-c) &&
309 git checkout -b top-bc top-b &&
310 git merge -s ours --no-commit top-c &&
311 (cd sub && git checkout sub-bc) &&
314 git checkout -b top-cb top-c &&
315 git merge -s ours --no-commit top-b &&
316 (cd sub && git checkout sub-cb) &&
321 # merge should leave submodule unmerged in index
322 test_expect_success
'recursive merge with submodule' '
323 (cd merge-recursive &&
324 test_must_fail git merge top-bc &&
325 echo "160000 $(git rev-parse top-cb:sub) 2 sub" > expect2 &&
326 echo "160000 $(git rev-parse top-bc:sub) 3 sub" > expect3 &&
327 git ls-files -u > actual &&
328 grep "$(cat expect2)" actual > /dev/null &&
329 grep "$(cat expect3)" actual > /dev/null)
332 # File/submodule conflict
334 # Commit A: path (submodule)
336 # Expected: path/ is submodule and file contents for B's path are somewhere
338 test_expect_success
'setup file/submodule conflict' '
339 git init file-submodule &&
343 git commit --allow-empty -m O &&
349 echo content >path &&
355 test_commit -C path world &&
356 git submodule add ./path &&
361 test_expect_merge_algorithm failure success
'file/submodule conflict' '
362 test_when_finished "git -C file-submodule reset --hard" &&
367 test_must_fail git merge B^0 &&
369 git ls-files -s >out &&
370 test_line_count = 3 out &&
371 git ls-files -u >out &&
372 test_line_count = 2 out &&
374 # path/ is still a submodule
375 test_path_is_dir path/.git &&
377 # There is a submodule at "path", so B:path cannot be written
378 # there. We expect it to be written somewhere in the same
379 # directory, though, so just grep for its content in all
380 # files, and ignore "grep: path: Is a directory" message
381 echo Checking if contents from B:path showed up anywhere &&
382 grep -q content * 2>/dev/null
386 test_expect_success
'file/submodule conflict; merge --abort works afterward' '
387 test_when_finished "git -C file-submodule reset --hard" &&
392 test_must_fail git merge B^0 >out 2>err &&
394 test_path_is_file .git/MERGE_HEAD &&
399 # Directory/submodule conflict
401 # Commit A: path (submodule), with sole tracked file named 'world'
402 # Commit B1: path/file
403 # Commit B2: path/world
405 # Expected from merge of A & B1:
406 # Contents under path/ from commit B1 are renamed elsewhere; we do not
407 # want to write files from one of our tracked directories into a submodule
409 # Expected from merge of A & B2:
410 # Similar to last merge, but with a slight twist: we don't want paths
411 # under the submodule to be treated as untracked or in the way.
413 test_expect_success
'setup directory/submodule conflict' '
414 git init directory-submodule &&
416 cd directory-submodule &&
418 git commit --allow-empty -m O &&
426 echo contents >path/file &&
432 echo contents >path/world &&
433 git add path/world &&
438 test_commit -C path hello world &&
439 git submodule add ./path &&
444 test_expect_failure
'directory/submodule conflict; keep submodule clean' '
445 test_when_finished "git -C directory-submodule reset --hard" &&
447 cd directory-submodule &&
450 test_must_fail git merge B1^0 &&
452 git ls-files -s >out &&
453 test_line_count = 3 out &&
454 git ls-files -u >out &&
455 test_line_count = 1 out &&
457 # path/ is still a submodule
458 test_path_is_dir path/.git &&
460 echo Checking if contents from B1:path/file showed up &&
461 # Would rather use grep -r, but that is GNU extension...
462 git ls-files -co | xargs grep -q contents 2>/dev/null &&
464 # However, B1:path/file should NOT have shown up at path/file,
465 # because we should not write into the submodule
466 test_path_is_missing path/file
470 test_expect_merge_algorithm failure success
!FAIL_PREREQS
'directory/submodule conflict; should not treat submodule files as untracked or in the way' '
471 test_when_finished "git -C directory-submodule/path reset --hard" &&
472 test_when_finished "git -C directory-submodule reset --hard" &&
474 cd directory-submodule &&
477 test_must_fail git merge B2^0 >out 2>err &&
479 # We do not want files within the submodule to prevent the
480 # merge from starting; we should not be writing to such paths
482 test_i18ngrep ! "refusing to lose untracked file at" err
486 test_expect_failure
'directory/submodule conflict; merge --abort works afterward' '
487 test_when_finished "git -C directory-submodule/path reset --hard" &&
488 test_when_finished "git -C directory-submodule reset --hard" &&
490 cd directory-submodule &&
493 test_must_fail git merge B2^0 &&
494 test_path_is_file .git/MERGE_HEAD &&
496 # merge --abort should succeed, should clear .git/MERGE_HEAD,
497 # and should not leave behind any conflicted files
499 test_path_is_missing .git/MERGE_HEAD &&
500 git ls-files -u >conflicts &&
501 test_must_be_empty conflicts
506 # - Submodule has 2 commits: a and b
507 # - Superproject branch 'a' adds and commits submodule pointing to 'commit a'
508 # - Superproject branch 'b' adds and commits submodule pointing to 'commit b'
509 # If these two branches are now merged, there is no merge base
510 test_expect_success
'setup for null merge base' '
511 mkdir no-merge-base &&
517 echo "file-a" > file-a &&
519 git commit -m "commit a") &&
520 git commit --allow-empty -m init &&
522 git checkout -b a init &&
527 echo "file-b" > file-b &&
529 git commit -m "commit b"))
532 test_expect_success
'merging should fail with no merge base' '
534 git checkout -b b init &&
537 test_must_fail git merge a >actual &&
538 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
540 sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short HEAD^1)" &&
541 grep "$sub_expect" actual