reftable/stack: expose option to disable auto-compaction
[git.git] / t / t6437-submodule-merge.sh
blob7a3f1cb27c12b468cb8a97e80c75a86070c7c4a8
1 #!/bin/sh
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
12 . ./test-lib.sh
13 . "$TEST_DIRECTORY"/lib-merge.sh
16 # history
18 # a --- c
19 # / \ /
20 # root X
21 # \ / \
22 # b --- d
25 test_expect_success setup '
27 mkdir sub &&
28 (cd sub &&
29 git init &&
30 echo original > file &&
31 git add file &&
32 test_tick &&
33 git commit -m sub-root) &&
34 git add sub &&
35 test_tick &&
36 git commit -m root &&
38 git checkout -b a main &&
39 (cd sub &&
40 echo A > file &&
41 git add file &&
42 test_tick &&
43 git commit -m sub-a) &&
44 git add sub &&
45 test_tick &&
46 git commit -m a &&
48 git checkout -b b main &&
49 (cd sub &&
50 echo B > file &&
51 git add file &&
52 test_tick &&
53 git commit -m sub-b) &&
54 git add sub &&
55 test_tick &&
56 git commit -m b &&
58 git checkout -b c a &&
59 git merge -s ours b &&
61 git checkout -b d b &&
62 git merge -s ours a
65 # History setup
67 # b
68 # / \
69 # init -- a d
70 # \ \ /
71 # g c
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' '
77 mkdir merge-search &&
78 (cd merge-search &&
79 git init &&
80 mkdir sub &&
81 (cd sub &&
82 git init &&
83 echo "file-a" > file-a &&
84 git add file-a &&
85 git commit -m "sub-a" &&
86 git branch sub-a) &&
87 git commit --allow-empty -m init &&
88 git branch init &&
89 git add sub &&
90 git commit -m "a" &&
91 git branch a &&
93 git checkout -b b &&
94 (cd sub &&
95 git checkout -b sub-b &&
96 echo "file-b" > file-b &&
97 git add file-b &&
98 git commit -m "sub-b") &&
99 git commit -a -m "b" &&
101 git checkout -b c a &&
102 (cd sub &&
103 git checkout -b sub-c sub-a &&
104 echo "file-c" > file-c &&
105 git add 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" &&
112 (cd merge-search &&
113 git checkout -b test-nonforward-a b &&
114 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
115 then
116 test_must_fail git merge c 2>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
119 else
120 test_must_fail git merge c 2> actual
124 test_expect_success 'finish setup for merge-search' '
125 (cd merge-search &&
126 git checkout -b d a &&
127 (cd sub &&
128 git checkout -b sub-d sub-b &&
129 git merge sub-c) &&
130 git commit -a -m "d" &&
131 git branch test b &&
133 git checkout -b g init &&
134 (cd sub &&
135 git checkout -b sub-g sub-c) &&
136 git add sub &&
137 git commit -a -m "g")
140 test_expect_success 'merge with one side as a fast-forward of the other' '
141 (cd merge-search &&
142 git checkout -b test-forward b &&
143 git merge d &&
144 git ls-tree test-forward sub | cut -f1 | cut -f3 -d" " > actual &&
145 (cd sub &&
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)' '
151 (cd merge-search &&
152 git checkout -b test-nonforward-b b &&
153 (cd sub &&
154 git rev-parse --short sub-d > ../expect) &&
155 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
156 then
157 test_must_fail git merge c >actual 2>sub-actual &&
158 sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-c)" &&
159 grep "$sub_expect" sub-actual
160 else
161 test_must_fail git merge c 2> actual
162 fi &&
163 grep $(cat expect) actual > /dev/null &&
164 git reset --hard)
167 test_expect_success 'merging should fail for ambiguous common parent' '
168 (cd merge-search &&
169 git checkout -b test-ambiguous b &&
170 (cd sub &&
171 git checkout -b ambiguous sub-b &&
172 git merge sub-c &&
173 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
174 then
175 git rev-parse --short sub-d >../expect1 &&
176 git rev-parse --short ambiguous >../expect2
177 else
178 git rev-parse sub-d > ../expect1 &&
179 git rev-parse ambiguous > ../expect2
181 ) &&
182 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
183 then
184 test_must_fail git merge c >actual 2>sub-actual &&
185 sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-c)" &&
186 grep "$sub_expect" sub-actual
187 else
188 test_must_fail git merge c 2> actual
189 fi &&
190 grep $(cat expect1) actual > /dev/null &&
191 grep $(cat expect2) actual > /dev/null &&
192 git reset --hard)
195 # in a situation like this
197 # submodule tree:
199 # sub-a --- sub-b --- sub-d
201 # main tree:
203 # e (sub-a)
205 # bb (sub-b)
207 # f (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' '
213 (cd merge-search &&
214 git checkout -b bb a &&
215 (cd sub &&
216 git checkout sub-b) &&
217 git commit -a -m "bb" &&
219 git checkout -b e bb &&
220 (cd sub &&
221 git checkout sub-a) &&
222 git commit -a -m "e" &&
224 git checkout -b f bb &&
225 (cd sub &&
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 2>actual &&
231 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
232 then
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' '
242 (cd merge-search &&
243 cat >.gitmodules <<EOF &&
244 [submodule "sub"]
245 path = sub
246 url = $TRASH_DIRECTORY/sub
248 cat >expect <<EOF &&
249 U$ZERO_OID sub
251 git submodule status > actual &&
252 test_cmp expect actual &&
253 git reset --hard)
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' '
259 (cd merge-search &&
260 git checkout -b test-no-merge-base g &&
261 test_must_fail git merge b &&
262 cat >.gitmodules <<EOF &&
263 [submodule "sub"]
264 path = sub
265 url = $TRASH_DIRECTORY/sub
267 cat >expect <<EOF &&
268 U$ZERO_OID sub
270 git submodule status > actual &&
271 test_cmp expect actual &&
272 git reset --hard)
276 test_expect_success 'merging with a modify/modify conflict between merge bases' '
277 git reset --hard HEAD &&
278 git checkout -b test2 c &&
279 git merge d
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 &&
286 git init &&
287 mkdir sub &&
288 (cd sub &&
289 git init &&
290 test_commit a &&
291 git checkout -b sub-b main &&
292 test_commit b &&
293 git checkout -b sub-c main &&
294 test_commit c &&
295 git checkout -b sub-bc sub-b &&
296 git merge sub-c &&
297 git checkout -b sub-cb sub-c &&
298 git merge sub-b &&
299 git checkout main) &&
300 git add sub &&
301 git commit -m a &&
302 git checkout -b top-b main &&
303 (cd sub && git checkout sub-b) &&
304 git add sub &&
305 git commit -m b &&
306 git checkout -b top-c main &&
307 (cd sub && git checkout sub-c) &&
308 git add sub &&
309 git commit -m 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) &&
313 git add sub &&
314 git commit -m 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) &&
318 git add sub &&
319 git commit -m 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
334 # Commit O: <empty>
335 # Commit A: path (submodule)
336 # Commit B: path
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 &&
342 cd file-submodule &&
344 git commit --allow-empty -m O &&
346 git branch A &&
347 git branch B &&
349 git checkout B &&
350 echo content >path &&
351 git add path &&
352 git commit -m B &&
354 git checkout A &&
355 git init path &&
356 test_commit -C path world &&
357 git submodule add ./path &&
358 git commit -m A
362 test_expect_merge_algorithm failure success 'file/submodule conflict' '
363 test_when_finished "git -C file-submodule reset --hard" &&
365 cd file-submodule &&
367 git checkout A^0 &&
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" &&
390 cd file-submodule &&
392 git checkout A^0 &&
393 test_must_fail git merge B^0 >out 2>err &&
395 test_path_is_file .git/MERGE_HEAD &&
396 git merge --abort
400 # Directory/submodule conflict
401 # Commit O: <empty>
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 &&
421 git branch A &&
422 git branch B1 &&
423 git branch B2 &&
425 git checkout B1 &&
426 mkdir path &&
427 echo contents >path/file &&
428 git add path/file &&
429 git commit -m B1 &&
431 git checkout B2 &&
432 mkdir path &&
433 echo contents >path/world &&
434 git add path/world &&
435 git commit -m B2 &&
437 git checkout A &&
438 git init path &&
439 test_commit -C path hello world &&
440 git submodule add ./path &&
441 git commit -m A
445 test_expect_failure 'directory/submodule conflict; keep submodule clean' '
446 test_when_finished "git -C directory-submodule reset --hard" &&
448 cd directory-submodule &&
450 git checkout A^0 &&
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 &&
477 git checkout A^0 &&
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
482 # anyway.
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 &&
493 git checkout A^0 &&
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
499 git merge --abort &&
500 test_path_is_missing .git/MERGE_HEAD &&
501 git ls-files -u >conflicts &&
502 test_must_be_empty conflicts
506 # Setup:
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 &&
513 (cd no-merge-base &&
514 git init &&
515 mkdir sub &&
516 (cd sub &&
517 git init &&
518 echo "file-a" > file-a &&
519 git add file-a &&
520 git commit -m "commit a") &&
521 git commit --allow-empty -m init &&
522 git branch init &&
523 git checkout -b a init &&
524 git add sub &&
525 git commit -m "a" &&
526 git switch main &&
527 (cd sub &&
528 echo "file-b" > file-b &&
529 git add file-b &&
530 git commit -m "commit b"))
533 test_expect_success 'merging should fail with no merge base' '
534 (cd no-merge-base &&
535 git checkout -b b init &&
536 git add sub &&
537 git commit -m "b" &&
538 test_must_fail git merge a 2>actual &&
539 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
540 then
541 sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short HEAD^1)" &&
542 grep "$sub_expect" actual
546 test_done