3 test_description
='merging with submodules'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9 .
"$TEST_DIRECTORY"/lib-merge.sh
21 test_expect_success setup
'
26 echo original > file &&
29 git commit -m sub-root) &&
34 git checkout -b a main &&
39 git commit -m sub-a) &&
44 git checkout -b b main &&
49 git commit -m sub-b) &&
54 git checkout -b c a &&
55 git merge -s ours b &&
57 git checkout -b d b &&
69 # a in the main repository records to sub-a in the submodule and
70 # analogous b and c. d should be automatically found by merging c into
71 # b in the main repository.
72 test_expect_success
'setup for merge search' '
79 echo "file-a" > file-a &&
81 git commit -m "sub-a" &&
83 git commit --allow-empty -m init &&
91 git checkout -b sub-b &&
92 echo "file-b" > file-b &&
94 git commit -m "sub-b") &&
95 git commit -a -m "b" &&
97 git checkout -b c a &&
99 git checkout -b sub-c sub-a &&
100 echo "file-c" > file-c &&
102 git commit -m "sub-c") &&
103 git commit -a -m "c" &&
105 git checkout -b d a &&
107 git checkout -b sub-d sub-b &&
109 git commit -a -m "d" &&
112 git checkout -b g init &&
114 git checkout -b sub-g sub-c) &&
116 git commit -a -m "g")
119 test_expect_success
'merge with one side as a fast-forward of the other' '
121 git checkout -b test-forward b &&
123 git ls-tree test-forward sub | cut -f1 | cut -f3 -d" " > actual &&
125 git rev-parse sub-d > ../expect) &&
126 test_cmp expect actual)
129 test_expect_success
'merging should conflict for non fast-forward' '
131 git checkout -b test-nonforward b &&
133 git rev-parse sub-d > ../expect) &&
134 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
136 test_must_fail git merge c >actual
138 test_must_fail git merge c 2> actual
140 grep $(cat expect) actual > /dev/null &&
144 test_expect_success
'merging should fail for ambiguous common parent' '
146 git checkout -b test-ambiguous b &&
148 git checkout -b ambiguous sub-b &&
150 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
152 git rev-parse --short sub-d >../expect1 &&
153 git rev-parse --short ambiguous >../expect2
155 git rev-parse sub-d > ../expect1 &&
156 git rev-parse ambiguous > ../expect2
159 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
161 test_must_fail git merge c >actual
163 test_must_fail git merge c 2> actual
165 grep $(cat expect1) actual > /dev/null &&
166 grep $(cat expect2) actual > /dev/null &&
170 # in a situation like this
174 # sub-a --- sub-b --- sub-d
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).
187 test_expect_success
'merging should fail for changes that are backwards' '
189 git checkout -b bb a &&
191 git checkout sub-b) &&
192 git commit -a -m "bb" &&
194 git checkout -b e bb &&
196 git checkout sub-a) &&
197 git commit -a -m "e" &&
199 git checkout -b f bb &&
201 git checkout sub-d) &&
202 git commit -a -m "f" &&
204 git checkout -b test-backward e &&
205 test_must_fail git merge f)
209 # Check that the conflicting submodule is detected when it is
210 # in the common ancestor. status should be 'U00...00"
211 test_expect_success
'git submodule status should display the merge conflict properly with merge base' '
213 cat >.gitmodules <<EOF &&
216 url = $TRASH_DIRECTORY/sub
221 git submodule status > actual &&
222 test_cmp expect actual &&
226 # Check that the conflicting submodule is detected when it is
227 # not in the common ancestor. status should be 'U00...00"
228 test_expect_success
'git submodule status should display the merge conflict properly without merge-base' '
230 git checkout -b test-no-merge-base g &&
231 test_must_fail git merge b &&
232 cat >.gitmodules <<EOF &&
235 url = $TRASH_DIRECTORY/sub
240 git submodule status > actual &&
241 test_cmp expect actual &&
246 test_expect_success
'merging with a modify/modify conflict between merge bases' '
247 git reset --hard HEAD &&
248 git checkout -b test2 c &&
252 # canonical criss-cross history in top and submodule
253 test_expect_success
'setup for recursive merge with submodule' '
254 mkdir merge-recursive &&
255 (cd merge-recursive &&
261 git checkout -b sub-b main &&
263 git checkout -b sub-c main &&
265 git checkout -b sub-bc sub-b &&
267 git checkout -b sub-cb sub-c &&
269 git checkout main) &&
272 git checkout -b top-b main &&
273 (cd sub && git checkout sub-b) &&
276 git checkout -b top-c main &&
277 (cd sub && git checkout sub-c) &&
280 git checkout -b top-bc top-b &&
281 git merge -s ours --no-commit top-c &&
282 (cd sub && git checkout sub-bc) &&
285 git checkout -b top-cb top-c &&
286 git merge -s ours --no-commit top-b &&
287 (cd sub && git checkout sub-cb) &&
292 # merge should leave submodule unmerged in index
293 test_expect_success
'recursive merge with submodule' '
294 (cd merge-recursive &&
295 test_must_fail git merge top-bc &&
296 echo "160000 $(git rev-parse top-cb:sub) 2 sub" > expect2 &&
297 echo "160000 $(git rev-parse top-bc:sub) 3 sub" > expect3 &&
298 git ls-files -u > actual &&
299 grep "$(cat expect2)" actual > /dev/null &&
300 grep "$(cat expect3)" actual > /dev/null)
303 # File/submodule conflict
305 # Commit A: path (submodule)
307 # Expected: path/ is submodule and file contents for B's path are somewhere
309 test_expect_success
'setup file/submodule conflict' '
310 test_create_repo file-submodule &&
314 git commit --allow-empty -m O &&
320 echo content >path &&
325 test_create_repo path &&
326 test_commit -C path world &&
327 git submodule add ./path &&
332 test_expect_merge_algorithm failure success
'file/submodule conflict' '
333 test_when_finished "git -C file-submodule reset --hard" &&
338 test_must_fail git merge B^0 &&
340 git ls-files -s >out &&
341 test_line_count = 3 out &&
342 git ls-files -u >out &&
343 test_line_count = 2 out &&
345 # path/ is still a submodule
346 test_path_is_dir path/.git &&
348 # There is a submodule at "path", so B:path cannot be written
349 # there. We expect it to be written somewhere in the same
350 # directory, though, so just grep for its content in all
351 # files, and ignore "grep: path: Is a directory" message
352 echo Checking if contents from B:path showed up anywhere &&
353 grep -q content * 2>/dev/null
357 test_expect_success
'file/submodule conflict; merge --abort works afterward' '
358 test_when_finished "git -C file-submodule reset --hard" &&
363 test_must_fail git merge B^0 >out 2>err &&
365 test_path_is_file .git/MERGE_HEAD &&
370 # Directory/submodule conflict
372 # Commit A: path (submodule), with sole tracked file named 'world'
373 # Commit B1: path/file
374 # Commit B2: path/world
376 # Expected from merge of A & B1:
377 # Contents under path/ from commit B1 are renamed elsewhere; we do not
378 # want to write files from one of our tracked directories into a submodule
380 # Expected from merge of A & B2:
381 # Similar to last merge, but with a slight twist: we don't want paths
382 # under the submodule to be treated as untracked or in the way.
384 test_expect_success
'setup directory/submodule conflict' '
385 test_create_repo directory-submodule &&
387 cd directory-submodule &&
389 git commit --allow-empty -m O &&
397 echo contents >path/file &&
403 echo contents >path/world &&
404 git add path/world &&
408 test_create_repo path &&
409 test_commit -C path hello world &&
410 git submodule add ./path &&
415 test_expect_failure
'directory/submodule conflict; keep submodule clean' '
416 test_when_finished "git -C directory-submodule reset --hard" &&
418 cd directory-submodule &&
421 test_must_fail git merge B1^0 &&
423 git ls-files -s >out &&
424 test_line_count = 3 out &&
425 git ls-files -u >out &&
426 test_line_count = 1 out &&
428 # path/ is still a submodule
429 test_path_is_dir path/.git &&
431 echo Checking if contents from B1:path/file showed up &&
432 # Would rather use grep -r, but that is GNU extension...
433 git ls-files -co | xargs grep -q contents 2>/dev/null &&
435 # However, B1:path/file should NOT have shown up at path/file,
436 # because we should not write into the submodule
437 test_path_is_missing path/file
441 test_expect_merge_algorithm failure success
!FAIL_PREREQS
'directory/submodule conflict; should not treat submodule files as untracked or in the way' '
442 test_when_finished "git -C directory-submodule/path reset --hard" &&
443 test_when_finished "git -C directory-submodule reset --hard" &&
445 cd directory-submodule &&
448 test_must_fail git merge B2^0 >out 2>err &&
450 # We do not want files within the submodule to prevent the
451 # merge from starting; we should not be writing to such paths
453 test_i18ngrep ! "refusing to lose untracked file at" err
457 test_expect_failure
'directory/submodule conflict; merge --abort works afterward' '
458 test_when_finished "git -C directory-submodule/path reset --hard" &&
459 test_when_finished "git -C directory-submodule reset --hard" &&
461 cd directory-submodule &&
464 test_must_fail git merge B2^0 &&
465 test_path_is_file .git/MERGE_HEAD &&
467 # merge --abort should succeed, should clear .git/MERGE_HEAD,
468 # and should not leave behind any conflicted files
470 test_path_is_missing .git/MERGE_HEAD &&
471 git ls-files -u >conflicts &&
472 test_must_be_empty conflicts