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" &&
108 git checkout -b d a &&
110 git checkout -b sub-d sub-b &&
112 git commit -a -m "d" &&
115 git checkout -b g init &&
117 git checkout -b sub-g sub-c) &&
119 git commit -a -m "g")
122 test_expect_success
'merge with one side as a fast-forward of the other' '
124 git checkout -b test-forward b &&
126 git ls-tree test-forward sub | cut -f1 | cut -f3 -d" " > actual &&
128 git rev-parse sub-d > ../expect) &&
129 test_cmp expect actual)
132 test_expect_success
'merging should conflict for non fast-forward' '
134 git checkout -b test-nonforward b &&
136 git rev-parse sub-d > ../expect) &&
137 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
139 test_must_fail git merge c >actual
141 test_must_fail git merge c 2> actual
143 grep $(cat expect) actual > /dev/null &&
147 test_expect_success
'merging should fail for ambiguous common parent' '
149 git checkout -b test-ambiguous b &&
151 git checkout -b ambiguous sub-b &&
153 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
155 git rev-parse --short sub-d >../expect1 &&
156 git rev-parse --short ambiguous >../expect2
158 git rev-parse sub-d > ../expect1 &&
159 git rev-parse ambiguous > ../expect2
162 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
164 test_must_fail git merge c >actual
166 test_must_fail git merge c 2> actual
168 grep $(cat expect1) actual > /dev/null &&
169 grep $(cat expect2) actual > /dev/null &&
173 # in a situation like this
177 # sub-a --- sub-b --- sub-d
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).
190 test_expect_success
'merging should fail for changes that are backwards' '
192 git checkout -b bb a &&
194 git checkout sub-b) &&
195 git commit -a -m "bb" &&
197 git checkout -b e bb &&
199 git checkout sub-a) &&
200 git commit -a -m "e" &&
202 git checkout -b f bb &&
204 git checkout sub-d) &&
205 git commit -a -m "f" &&
207 git checkout -b test-backward e &&
208 test_must_fail git merge f)
212 # Check that the conflicting submodule is detected when it is
213 # in the common ancestor. status should be 'U00...00"
214 test_expect_success
'git submodule status should display the merge conflict properly with merge base' '
216 cat >.gitmodules <<EOF &&
219 url = $TRASH_DIRECTORY/sub
224 git submodule status > actual &&
225 test_cmp expect actual &&
229 # Check that the conflicting submodule is detected when it is
230 # not in the common ancestor. status should be 'U00...00"
231 test_expect_success
'git submodule status should display the merge conflict properly without merge-base' '
233 git checkout -b test-no-merge-base g &&
234 test_must_fail git merge b &&
235 cat >.gitmodules <<EOF &&
238 url = $TRASH_DIRECTORY/sub
243 git submodule status > actual &&
244 test_cmp expect actual &&
249 test_expect_success
'merging with a modify/modify conflict between merge bases' '
250 git reset --hard HEAD &&
251 git checkout -b test2 c &&
255 # canonical criss-cross history in top and submodule
256 test_expect_success
'setup for recursive merge with submodule' '
257 mkdir merge-recursive &&
258 (cd merge-recursive &&
264 git checkout -b sub-b main &&
266 git checkout -b sub-c main &&
268 git checkout -b sub-bc sub-b &&
270 git checkout -b sub-cb sub-c &&
272 git checkout main) &&
275 git checkout -b top-b main &&
276 (cd sub && git checkout sub-b) &&
279 git checkout -b top-c main &&
280 (cd sub && git checkout sub-c) &&
283 git checkout -b top-bc top-b &&
284 git merge -s ours --no-commit top-c &&
285 (cd sub && git checkout sub-bc) &&
288 git checkout -b top-cb top-c &&
289 git merge -s ours --no-commit top-b &&
290 (cd sub && git checkout sub-cb) &&
295 # merge should leave submodule unmerged in index
296 test_expect_success
'recursive merge with submodule' '
297 (cd merge-recursive &&
298 test_must_fail git merge top-bc &&
299 echo "160000 $(git rev-parse top-cb:sub) 2 sub" > expect2 &&
300 echo "160000 $(git rev-parse top-bc:sub) 3 sub" > expect3 &&
301 git ls-files -u > actual &&
302 grep "$(cat expect2)" actual > /dev/null &&
303 grep "$(cat expect3)" actual > /dev/null)
306 # File/submodule conflict
308 # Commit A: path (submodule)
310 # Expected: path/ is submodule and file contents for B's path are somewhere
312 test_expect_success
'setup file/submodule conflict' '
313 test_create_repo file-submodule &&
317 git commit --allow-empty -m O &&
323 echo content >path &&
328 test_create_repo path &&
329 test_commit -C path world &&
330 git submodule add ./path &&
335 test_expect_merge_algorithm failure success
'file/submodule conflict' '
336 test_when_finished "git -C file-submodule reset --hard" &&
341 test_must_fail git merge B^0 &&
343 git ls-files -s >out &&
344 test_line_count = 3 out &&
345 git ls-files -u >out &&
346 test_line_count = 2 out &&
348 # path/ is still a submodule
349 test_path_is_dir path/.git &&
351 # There is a submodule at "path", so B:path cannot be written
352 # there. We expect it to be written somewhere in the same
353 # directory, though, so just grep for its content in all
354 # files, and ignore "grep: path: Is a directory" message
355 echo Checking if contents from B:path showed up anywhere &&
356 grep -q content * 2>/dev/null
360 test_expect_success
'file/submodule conflict; merge --abort works afterward' '
361 test_when_finished "git -C file-submodule reset --hard" &&
366 test_must_fail git merge B^0 >out 2>err &&
368 test_path_is_file .git/MERGE_HEAD &&
373 # Directory/submodule conflict
375 # Commit A: path (submodule), with sole tracked file named 'world'
376 # Commit B1: path/file
377 # Commit B2: path/world
379 # Expected from merge of A & B1:
380 # Contents under path/ from commit B1 are renamed elsewhere; we do not
381 # want to write files from one of our tracked directories into a submodule
383 # Expected from merge of A & B2:
384 # Similar to last merge, but with a slight twist: we don't want paths
385 # under the submodule to be treated as untracked or in the way.
387 test_expect_success
'setup directory/submodule conflict' '
388 test_create_repo directory-submodule &&
390 cd directory-submodule &&
392 git commit --allow-empty -m O &&
400 echo contents >path/file &&
406 echo contents >path/world &&
407 git add path/world &&
411 test_create_repo path &&
412 test_commit -C path hello world &&
413 git submodule add ./path &&
418 test_expect_failure
'directory/submodule conflict; keep submodule clean' '
419 test_when_finished "git -C directory-submodule reset --hard" &&
421 cd directory-submodule &&
424 test_must_fail git merge B1^0 &&
426 git ls-files -s >out &&
427 test_line_count = 3 out &&
428 git ls-files -u >out &&
429 test_line_count = 1 out &&
431 # path/ is still a submodule
432 test_path_is_dir path/.git &&
434 echo Checking if contents from B1:path/file showed up &&
435 # Would rather use grep -r, but that is GNU extension...
436 git ls-files -co | xargs grep -q contents 2>/dev/null &&
438 # However, B1:path/file should NOT have shown up at path/file,
439 # because we should not write into the submodule
440 test_path_is_missing path/file
444 test_expect_merge_algorithm failure success
!FAIL_PREREQS
'directory/submodule conflict; should not treat submodule files as untracked or in the way' '
445 test_when_finished "git -C directory-submodule/path reset --hard" &&
446 test_when_finished "git -C directory-submodule reset --hard" &&
448 cd directory-submodule &&
451 test_must_fail git merge B2^0 >out 2>err &&
453 # We do not want files within the submodule to prevent the
454 # merge from starting; we should not be writing to such paths
456 test_i18ngrep ! "refusing to lose untracked file at" err
460 test_expect_failure
'directory/submodule conflict; merge --abort works afterward' '
461 test_when_finished "git -C directory-submodule/path reset --hard" &&
462 test_when_finished "git -C directory-submodule reset --hard" &&
464 cd directory-submodule &&
467 test_must_fail git merge B2^0 &&
468 test_path_is_file .git/MERGE_HEAD &&
470 # merge --abort should succeed, should clear .git/MERGE_HEAD,
471 # and should not leave behind any conflicted files
473 test_path_is_missing .git/MERGE_HEAD &&
474 git ls-files -u >conflicts &&
475 test_must_be_empty conflicts