Merge branch 'tb/commit-graph-genv2-upgrade-fix' into maint
[git/debian.git] / t / t6437-submodule-merge.sh
blob178413c22f0dc1dd03152ec1d84843e070d1d843
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-lib.sh
12 . "$TEST_DIRECTORY"/lib-merge.sh
15 # history
17 # a --- c
18 # / \ /
19 # root X
20 # \ / \
21 # b --- d
24 test_expect_success setup '
26 mkdir sub &&
27 (cd sub &&
28 git init &&
29 echo original > file &&
30 git add file &&
31 test_tick &&
32 git commit -m sub-root) &&
33 git add sub &&
34 test_tick &&
35 git commit -m root &&
37 git checkout -b a main &&
38 (cd sub &&
39 echo A > file &&
40 git add file &&
41 test_tick &&
42 git commit -m sub-a) &&
43 git add sub &&
44 test_tick &&
45 git commit -m a &&
47 git checkout -b b main &&
48 (cd sub &&
49 echo B > file &&
50 git add file &&
51 test_tick &&
52 git commit -m sub-b) &&
53 git add sub &&
54 test_tick &&
55 git commit -m b &&
57 git checkout -b c a &&
58 git merge -s ours b &&
60 git checkout -b d b &&
61 git merge -s ours a
64 # History setup
66 # b
67 # / \
68 # init -- a d
69 # \ \ /
70 # g c
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' '
76 mkdir merge-search &&
77 (cd merge-search &&
78 git init &&
79 mkdir sub &&
80 (cd sub &&
81 git init &&
82 echo "file-a" > file-a &&
83 git add file-a &&
84 git commit -m "sub-a" &&
85 git branch sub-a) &&
86 git commit --allow-empty -m init &&
87 git branch init &&
88 git add sub &&
89 git commit -m "a" &&
90 git branch a &&
92 git checkout -b b &&
93 (cd sub &&
94 git checkout -b sub-b &&
95 echo "file-b" > file-b &&
96 git add file-b &&
97 git commit -m "sub-b") &&
98 git commit -a -m "b" &&
100 git checkout -b c a &&
101 (cd sub &&
102 git checkout -b sub-c sub-a &&
103 echo "file-c" > file-c &&
104 git add file-c &&
105 git commit -m "sub-c") &&
106 git commit -a -m "c" &&
108 git checkout -b d a &&
109 (cd sub &&
110 git checkout -b sub-d sub-b &&
111 git merge sub-c) &&
112 git commit -a -m "d" &&
113 git branch test b &&
115 git checkout -b g init &&
116 (cd sub &&
117 git checkout -b sub-g sub-c) &&
118 git add sub &&
119 git commit -a -m "g")
122 test_expect_success 'merge with one side as a fast-forward of the other' '
123 (cd merge-search &&
124 git checkout -b test-forward b &&
125 git merge d &&
126 git ls-tree test-forward sub | cut -f1 | cut -f3 -d" " > actual &&
127 (cd sub &&
128 git rev-parse sub-d > ../expect) &&
129 test_cmp expect actual)
132 test_expect_success 'merging should conflict for non fast-forward' '
133 (cd merge-search &&
134 git checkout -b test-nonforward b &&
135 (cd sub &&
136 git rev-parse sub-d > ../expect) &&
137 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
138 then
139 test_must_fail git merge c >actual
140 else
141 test_must_fail git merge c 2> actual
142 fi &&
143 grep $(cat expect) actual > /dev/null &&
144 git reset --hard)
147 test_expect_success 'merging should fail for ambiguous common parent' '
148 (cd merge-search &&
149 git checkout -b test-ambiguous b &&
150 (cd sub &&
151 git checkout -b ambiguous sub-b &&
152 git merge sub-c &&
153 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
154 then
155 git rev-parse --short sub-d >../expect1 &&
156 git rev-parse --short ambiguous >../expect2
157 else
158 git rev-parse sub-d > ../expect1 &&
159 git rev-parse ambiguous > ../expect2
161 ) &&
162 if test "$GIT_TEST_MERGE_ALGORITHM" = ort
163 then
164 test_must_fail git merge c >actual
165 else
166 test_must_fail git merge c 2> actual
167 fi &&
168 grep $(cat expect1) actual > /dev/null &&
169 grep $(cat expect2) actual > /dev/null &&
170 git reset --hard)
173 # in a situation like this
175 # submodule tree:
177 # sub-a --- sub-b --- sub-d
179 # main tree:
181 # e (sub-a)
183 # bb (sub-b)
185 # f (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' '
191 (cd merge-search &&
192 git checkout -b bb a &&
193 (cd sub &&
194 git checkout sub-b) &&
195 git commit -a -m "bb" &&
197 git checkout -b e bb &&
198 (cd sub &&
199 git checkout sub-a) &&
200 git commit -a -m "e" &&
202 git checkout -b f bb &&
203 (cd sub &&
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' '
215 (cd merge-search &&
216 cat >.gitmodules <<EOF &&
217 [submodule "sub"]
218 path = sub
219 url = $TRASH_DIRECTORY/sub
221 cat >expect <<EOF &&
222 U$ZERO_OID sub
224 git submodule status > actual &&
225 test_cmp expect actual &&
226 git reset --hard)
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' '
232 (cd merge-search &&
233 git checkout -b test-no-merge-base g &&
234 test_must_fail git merge b &&
235 cat >.gitmodules <<EOF &&
236 [submodule "sub"]
237 path = sub
238 url = $TRASH_DIRECTORY/sub
240 cat >expect <<EOF &&
241 U$ZERO_OID sub
243 git submodule status > actual &&
244 test_cmp expect actual &&
245 git reset --hard)
249 test_expect_success 'merging with a modify/modify conflict between merge bases' '
250 git reset --hard HEAD &&
251 git checkout -b test2 c &&
252 git merge d
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 &&
259 git init &&
260 mkdir sub &&
261 (cd sub &&
262 git init &&
263 test_commit a &&
264 git checkout -b sub-b main &&
265 test_commit b &&
266 git checkout -b sub-c main &&
267 test_commit c &&
268 git checkout -b sub-bc sub-b &&
269 git merge sub-c &&
270 git checkout -b sub-cb sub-c &&
271 git merge sub-b &&
272 git checkout main) &&
273 git add sub &&
274 git commit -m a &&
275 git checkout -b top-b main &&
276 (cd sub && git checkout sub-b) &&
277 git add sub &&
278 git commit -m b &&
279 git checkout -b top-c main &&
280 (cd sub && git checkout sub-c) &&
281 git add sub &&
282 git commit -m 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) &&
286 git add sub &&
287 git commit -m 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) &&
291 git add sub &&
292 git commit -m 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
307 # Commit O: <empty>
308 # Commit A: path (submodule)
309 # Commit B: path
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 &&
315 cd file-submodule &&
317 git commit --allow-empty -m O &&
319 git branch A &&
320 git branch B &&
322 git checkout B &&
323 echo content >path &&
324 git add path &&
325 git commit -m B &&
327 git checkout A &&
328 test_create_repo path &&
329 test_commit -C path world &&
330 git submodule add ./path &&
331 git commit -m A
335 test_expect_merge_algorithm failure success 'file/submodule conflict' '
336 test_when_finished "git -C file-submodule reset --hard" &&
338 cd file-submodule &&
340 git checkout A^0 &&
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" &&
363 cd file-submodule &&
365 git checkout A^0 &&
366 test_must_fail git merge B^0 >out 2>err &&
368 test_path_is_file .git/MERGE_HEAD &&
369 git merge --abort
373 # Directory/submodule conflict
374 # Commit O: <empty>
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 &&
394 git branch A &&
395 git branch B1 &&
396 git branch B2 &&
398 git checkout B1 &&
399 mkdir path &&
400 echo contents >path/file &&
401 git add path/file &&
402 git commit -m B1 &&
404 git checkout B2 &&
405 mkdir path &&
406 echo contents >path/world &&
407 git add path/world &&
408 git commit -m B2 &&
410 git checkout A &&
411 test_create_repo path &&
412 test_commit -C path hello world &&
413 git submodule add ./path &&
414 git commit -m A
418 test_expect_failure 'directory/submodule conflict; keep submodule clean' '
419 test_when_finished "git -C directory-submodule reset --hard" &&
421 cd directory-submodule &&
423 git checkout A^0 &&
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 &&
450 git checkout A^0 &&
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
455 # anyway.
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 &&
466 git checkout A^0 &&
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
472 git merge --abort &&
473 test_path_is_missing .git/MERGE_HEAD &&
474 git ls-files -u >conflicts &&
475 test_must_be_empty conflicts
479 test_done