3 test_description
='merging with submodules'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
20 test_expect_success setup
'
25 echo original > file &&
28 git commit -m sub-root) &&
33 git checkout -b a main &&
38 git commit -m sub-a) &&
43 git checkout -b b main &&
48 git commit -m sub-b) &&
53 git checkout -b c a &&
54 git merge -s ours b &&
56 git checkout -b d b &&
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' '
78 echo "file-a" > file-a &&
80 git commit -m "sub-a" &&
82 git commit --allow-empty -m init &&
90 git checkout -b sub-b &&
91 echo "file-b" > file-b &&
93 git commit -m "sub-b") &&
94 git commit -a -m "b" &&
96 git checkout -b c a &&
98 git checkout -b sub-c sub-a &&
99 echo "file-c" > file-c &&
101 git commit -m "sub-c") &&
102 git commit -a -m "c" &&
104 git checkout -b d a &&
106 git checkout -b sub-d sub-b &&
108 git commit -a -m "d" &&
111 git checkout -b g init &&
113 git checkout -b sub-g sub-c) &&
115 git commit -a -m "g")
118 test_expect_success
'merge with one side as a fast-forward of the other' '
120 git checkout -b test-forward b &&
122 git ls-tree test-forward sub | cut -f1 | cut -f3 -d" " > actual &&
124 git rev-parse sub-d > ../expect) &&
125 test_cmp expect actual)
128 test_expect_success
'merging should conflict for non fast-forward' '
130 git checkout -b test-nonforward b &&
132 git rev-parse sub-d > ../expect) &&
133 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
135 test_must_fail git merge c >actual
137 test_must_fail git merge c 2> actual
139 grep $(cat expect) actual > /dev/null &&
143 test_expect_success
'merging should fail for ambiguous common parent' '
145 git checkout -b test-ambiguous b &&
147 git checkout -b ambiguous sub-b &&
149 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
151 git rev-parse --short sub-d >../expect1 &&
152 git rev-parse --short ambiguous >../expect2
154 git rev-parse sub-d > ../expect1 &&
155 git rev-parse ambiguous > ../expect2
158 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
160 test_must_fail git merge c >actual
162 test_must_fail git merge c 2> actual
164 grep $(cat expect1) actual > /dev/null &&
165 grep $(cat expect2) actual > /dev/null &&
169 # in a situation like this
173 # sub-a --- sub-b --- sub-d
183 # A merge between e and f should fail because one of the submodule
184 # commits (sub-a) does not descend from the submodule merge-base (sub-b).
186 test_expect_success
'merging should fail for changes that are backwards' '
188 git checkout -b bb a &&
190 git checkout sub-b) &&
191 git commit -a -m "bb" &&
193 git checkout -b e bb &&
195 git checkout sub-a) &&
196 git commit -a -m "e" &&
198 git checkout -b f bb &&
200 git checkout sub-d) &&
201 git commit -a -m "f" &&
203 git checkout -b test-backward e &&
204 test_must_fail git merge f)
208 # Check that the conflicting submodule is detected when it is
209 # in the common ancestor. status should be 'U00...00"
210 test_expect_success
'git submodule status should display the merge conflict properly with merge base' '
212 cat >.gitmodules <<EOF &&
215 url = $TRASH_DIRECTORY/sub
220 git submodule status > actual &&
221 test_cmp expect actual &&
225 # Check that the conflicting submodule is detected when it is
226 # not in the common ancestor. status should be 'U00...00"
227 test_expect_success
'git submodule status should display the merge conflict properly without merge-base' '
229 git checkout -b test-no-merge-base g &&
230 test_must_fail git merge b &&
231 cat >.gitmodules <<EOF &&
234 url = $TRASH_DIRECTORY/sub
239 git submodule status > actual &&
240 test_cmp expect actual &&
245 test_expect_success
'merging with a modify/modify conflict between merge bases' '
246 git reset --hard HEAD &&
247 git checkout -b test2 c &&
251 # canonical criss-cross history in top and submodule
252 test_expect_success
'setup for recursive merge with submodule' '
253 mkdir merge-recursive &&
254 (cd merge-recursive &&
260 git checkout -b sub-b main &&
262 git checkout -b sub-c main &&
264 git checkout -b sub-bc sub-b &&
266 git checkout -b sub-cb sub-c &&
268 git checkout main) &&
271 git checkout -b top-b main &&
272 (cd sub && git checkout sub-b) &&
275 git checkout -b top-c main &&
276 (cd sub && git checkout sub-c) &&
279 git checkout -b top-bc top-b &&
280 git merge -s ours --no-commit top-c &&
281 (cd sub && git checkout sub-bc) &&
284 git checkout -b top-cb top-c &&
285 git merge -s ours --no-commit top-b &&
286 (cd sub && git checkout sub-cb) &&
291 # merge should leave submodule unmerged in index
292 test_expect_success
'recursive merge with submodule' '
293 (cd merge-recursive &&
294 test_must_fail git merge top-bc &&
295 echo "160000 $(git rev-parse top-cb:sub) 2 sub" > expect2 &&
296 echo "160000 $(git rev-parse top-bc:sub) 3 sub" > expect3 &&
297 git ls-files -u > actual &&
298 grep "$(cat expect2)" actual > /dev/null &&
299 grep "$(cat expect3)" actual > /dev/null)
302 # File/submodule conflict
304 # Commit A: path (submodule)
306 # Expected: path/ is submodule and file contents for B's path are somewhere
308 test_expect_success
'setup file/submodule conflict' '
309 test_create_repo file-submodule &&
313 git commit --allow-empty -m O &&
319 echo content >path &&
324 test_create_repo path &&
325 test_commit -C path world &&
326 git submodule add ./path &&
331 test_expect_failure
'file/submodule conflict' '
332 test_when_finished "git -C file-submodule reset --hard" &&
337 test_must_fail git merge B^0 &&
339 git ls-files -s >out &&
340 test_line_count = 3 out &&
341 git ls-files -u >out &&
342 test_line_count = 2 out &&
344 # path/ is still a submodule
345 test_path_is_dir path/.git &&
347 # There is a submodule at "path", so B:path cannot be written
348 # there. We expect it to be written somewhere in the same
349 # directory, though, so just grep for its content in all
350 # files, and ignore "grep: path: Is a directory" message
351 echo Checking if contents from B:path showed up anywhere &&
352 grep -q content * 2>/dev/null
356 test_expect_success
'file/submodule conflict; merge --abort works afterward' '
357 test_when_finished "git -C file-submodule reset --hard" &&
362 test_must_fail git merge B^0 >out 2>err &&
364 test_path_is_file .git/MERGE_HEAD &&
369 # Directory/submodule conflict
371 # Commit A: path (submodule), with sole tracked file named 'world'
372 # Commit B1: path/file
373 # Commit B2: path/world
375 # Expected from merge of A & B1:
376 # Contents under path/ from commit B1 are renamed elsewhere; we do not
377 # want to write files from one of our tracked directories into a submodule
379 # Expected from merge of A & B2:
380 # Similar to last merge, but with a slight twist: we don't want paths
381 # under the submodule to be treated as untracked or in the way.
383 test_expect_success
'setup directory/submodule conflict' '
384 test_create_repo directory-submodule &&
386 cd directory-submodule &&
388 git commit --allow-empty -m O &&
396 echo contents >path/file &&
402 echo contents >path/world &&
403 git add path/world &&
407 test_create_repo path &&
408 test_commit -C path hello world &&
409 git submodule add ./path &&
414 test_expect_failure
'directory/submodule conflict; keep submodule clean' '
415 test_when_finished "git -C directory-submodule reset --hard" &&
417 cd directory-submodule &&
420 test_must_fail git merge B1^0 &&
422 git ls-files -s >out &&
423 test_line_count = 3 out &&
424 git ls-files -u >out &&
425 test_line_count = 1 out &&
427 # path/ is still a submodule
428 test_path_is_dir path/.git &&
430 echo Checking if contents from B1:path/file showed up &&
431 # Would rather use grep -r, but that is GNU extension...
432 git ls-files -co | xargs grep -q contents 2>/dev/null &&
434 # However, B1:path/file should NOT have shown up at path/file,
435 # because we should not write into the submodule
436 test_path_is_missing path/file
440 test_expect_failure
!FAIL_PREREQS
'directory/submodule conflict; should not treat submodule files as untracked or in the way' '
441 test_when_finished "git -C directory-submodule/path reset --hard" &&
442 test_when_finished "git -C directory-submodule reset --hard" &&
444 cd directory-submodule &&
447 test_must_fail git merge B2^0 >out 2>err &&
449 # We do not want files within the submodule to prevent the
450 # merge from starting; we should not be writing to such paths
452 test_i18ngrep ! "refusing to lose untracked file at" err
456 test_expect_failure
'directory/submodule conflict; merge --abort works afterward' '
457 test_when_finished "git -C directory-submodule/path reset --hard" &&
458 test_when_finished "git -C directory-submodule reset --hard" &&
460 cd directory-submodule &&
463 test_must_fail git merge B2^0 &&
464 test_path_is_file .git/MERGE_HEAD &&
466 # merge --abort should succeed, should clear .git/MERGE_HEAD,
467 # and should not leave behind any conflicted files
469 test_path_is_missing .git/MERGE_HEAD &&
470 git ls-files -u >conflicts &&
471 test_must_be_empty conflicts