Merge branch 'jc/diff-I-status-fix'
[git] / t / t6437-submodule-merge.sh
1 #!/bin/sh
2
3 test_description='merging with submodules'
4
5 . ./test-lib.sh
6
7 #
8 # history
9 #
10 #        a --- c
11 #      /   \ /
12 # root      X
13 #      \   / \
14 #        b --- d
15 #
16
17 test_expect_success setup '
18
19         mkdir sub &&
20         (cd sub &&
21          git init &&
22          echo original > file &&
23          git add file &&
24          test_tick &&
25          git commit -m sub-root) &&
26         git add sub &&
27         test_tick &&
28         git commit -m root &&
29
30         git checkout -b a master &&
31         (cd sub &&
32          echo A > file &&
33          git add file &&
34          test_tick &&
35          git commit -m sub-a) &&
36         git add sub &&
37         test_tick &&
38         git commit -m a &&
39
40         git checkout -b b master &&
41         (cd sub &&
42          echo B > file &&
43          git add file &&
44          test_tick &&
45          git commit -m sub-b) &&
46         git add sub &&
47         test_tick &&
48         git commit -m b &&
49
50         git checkout -b c a &&
51         git merge -s ours b &&
52
53         git checkout -b d b &&
54         git merge -s ours a
55 '
56
57 # History setup
58 #
59 #             b
60 #           /   \
61 #  init -- a     d
62 #    \      \   /
63 #     g       c
64 #
65 # a in the main repository records to sub-a in the submodule and
66 # analogous b and c. d should be automatically found by merging c into
67 # b in the main repository.
68 test_expect_success 'setup for merge search' '
69         mkdir merge-search &&
70         (cd merge-search &&
71         git init &&
72         mkdir sub &&
73         (cd sub &&
74          git init &&
75          echo "file-a" > file-a &&
76          git add file-a &&
77          git commit -m "sub-a" &&
78          git branch sub-a) &&
79         git commit --allow-empty -m init &&
80         git branch init &&
81         git add sub &&
82         git commit -m "a" &&
83         git branch a &&
84
85         git checkout -b b &&
86         (cd sub &&
87          git checkout -b sub-b &&
88          echo "file-b" > file-b &&
89          git add file-b &&
90          git commit -m "sub-b") &&
91         git commit -a -m "b" &&
92
93         git checkout -b c a &&
94         (cd sub &&
95          git checkout -b sub-c sub-a &&
96          echo "file-c" > file-c &&
97          git add file-c &&
98          git commit -m "sub-c") &&
99         git commit -a -m "c" &&
100
101         git checkout -b d a &&
102         (cd sub &&
103          git checkout -b sub-d sub-b &&
104          git merge sub-c) &&
105         git commit -a -m "d" &&
106         git branch test b &&
107
108         git checkout -b g init &&
109         (cd sub &&
110          git checkout -b sub-g sub-c) &&
111         git add sub &&
112         git commit -a -m "g")
113 '
114
115 test_expect_success 'merge with one side as a fast-forward of the other' '
116         (cd merge-search &&
117          git checkout -b test-forward b &&
118          git merge d &&
119          git ls-tree test-forward sub | cut -f1 | cut -f3 -d" " > actual &&
120          (cd sub &&
121           git rev-parse sub-d > ../expect) &&
122          test_cmp expect actual)
123 '
124
125 test_expect_success 'merging should conflict for non fast-forward' '
126         (cd merge-search &&
127          git checkout -b test-nonforward b &&
128          (cd sub &&
129           git rev-parse sub-d > ../expect) &&
130           if test "$GIT_TEST_MERGE_ALGORITHM" = ort
131           then
132                 test_must_fail git merge c >actual
133           else
134                 test_must_fail git merge c 2> actual
135           fi &&
136          grep $(cat expect) actual > /dev/null &&
137          git reset --hard)
138 '
139
140 test_expect_success 'merging should fail for ambiguous common parent' '
141         (cd merge-search &&
142         git checkout -b test-ambiguous b &&
143         (cd sub &&
144          git checkout -b ambiguous sub-b &&
145          git merge sub-c &&
146          if test "$GIT_TEST_MERGE_ALGORITHM" = ort
147          then
148                 git rev-parse --short sub-d >../expect1 &&
149                 git rev-parse --short ambiguous >../expect2
150          else
151                 git rev-parse sub-d > ../expect1 &&
152                 git rev-parse ambiguous > ../expect2
153          fi
154          ) &&
155          if test "$GIT_TEST_MERGE_ALGORITHM" = ort
156          then
157                 test_must_fail git merge c >actual
158          else
159                 test_must_fail git merge c 2> actual
160          fi &&
161         grep $(cat expect1) actual > /dev/null &&
162         grep $(cat expect2) actual > /dev/null &&
163         git reset --hard)
164 '
165
166 # in a situation like this
167 #
168 # submodule tree:
169 #
170 #    sub-a --- sub-b --- sub-d
171 #
172 # main tree:
173 #
174 #    e (sub-a)
175 #   /
176 #  bb (sub-b)
177 #   \
178 #    f (sub-d)
179 #
180 # A merge between e and f should fail because one of the submodule
181 # commits (sub-a) does not descend from the submodule merge-base (sub-b).
182 #
183 test_expect_success 'merging should fail for changes that are backwards' '
184         (cd merge-search &&
185         git checkout -b bb a &&
186         (cd sub &&
187          git checkout sub-b) &&
188         git commit -a -m "bb" &&
189
190         git checkout -b e bb &&
191         (cd sub &&
192          git checkout sub-a) &&
193         git commit -a -m "e" &&
194
195         git checkout -b f bb &&
196         (cd sub &&
197          git checkout sub-d) &&
198         git commit -a -m "f" &&
199
200         git checkout -b test-backward e &&
201         test_must_fail git merge f)
202 '
203
204
205 # Check that the conflicting submodule is detected when it is
206 # in the common ancestor. status should be 'U00...00"
207 test_expect_success 'git submodule status should display the merge conflict properly with merge base' '
208        (cd merge-search &&
209        cat >.gitmodules <<EOF &&
210 [submodule "sub"]
211        path = sub
212        url = $TRASH_DIRECTORY/sub
213 EOF
214        cat >expect <<EOF &&
215 U$ZERO_OID sub
216 EOF
217        git submodule status > actual &&
218        test_cmp expect actual &&
219         git reset --hard)
220 '
221
222 # Check that the conflicting submodule is detected when it is
223 # not in the common ancestor. status should be 'U00...00"
224 test_expect_success 'git submodule status should display the merge conflict properly without merge-base' '
225        (cd merge-search &&
226         git checkout -b test-no-merge-base g &&
227         test_must_fail git merge b &&
228        cat >.gitmodules <<EOF &&
229 [submodule "sub"]
230        path = sub
231        url = $TRASH_DIRECTORY/sub
232 EOF
233        cat >expect <<EOF &&
234 U$ZERO_OID sub
235 EOF
236        git submodule status > actual &&
237        test_cmp expect actual &&
238        git reset --hard)
239 '
240
241
242 test_expect_success 'merging with a modify/modify conflict between merge bases' '
243         git reset --hard HEAD &&
244         git checkout -b test2 c &&
245         git merge d
246 '
247
248 # canonical criss-cross history in top and submodule
249 test_expect_success 'setup for recursive merge with submodule' '
250         mkdir merge-recursive &&
251         (cd merge-recursive &&
252          git init &&
253          mkdir sub &&
254          (cd sub &&
255           git init &&
256           test_commit a &&
257           git checkout -b sub-b master &&
258           test_commit b &&
259           git checkout -b sub-c master &&
260           test_commit c &&
261           git checkout -b sub-bc sub-b &&
262           git merge sub-c &&
263           git checkout -b sub-cb sub-c &&
264           git merge sub-b &&
265           git checkout master) &&
266          git add sub &&
267          git commit -m a &&
268          git checkout -b top-b master &&
269          (cd sub && git checkout sub-b) &&
270          git add sub &&
271          git commit -m b &&
272          git checkout -b top-c master &&
273          (cd sub && git checkout sub-c) &&
274          git add sub &&
275          git commit -m c &&
276          git checkout -b top-bc top-b &&
277          git merge -s ours --no-commit top-c &&
278          (cd sub && git checkout sub-bc) &&
279          git add sub &&
280          git commit -m bc &&
281          git checkout -b top-cb top-c &&
282          git merge -s ours --no-commit top-b &&
283          (cd sub && git checkout sub-cb) &&
284          git add sub &&
285          git commit -m cb)
286 '
287
288 # merge should leave submodule unmerged in index
289 test_expect_success 'recursive merge with submodule' '
290         (cd merge-recursive &&
291          test_must_fail git merge top-bc &&
292          echo "160000 $(git rev-parse top-cb:sub) 2     sub" > expect2 &&
293          echo "160000 $(git rev-parse top-bc:sub) 3     sub" > expect3 &&
294          git ls-files -u > actual &&
295          grep "$(cat expect2)" actual > /dev/null &&
296          grep "$(cat expect3)" actual > /dev/null)
297 '
298
299 # File/submodule conflict
300 #   Commit O: <empty>
301 #   Commit A: path (submodule)
302 #   Commit B: path
303 #   Expected: path/ is submodule and file contents for B's path are somewhere
304
305 test_expect_success 'setup file/submodule conflict' '
306         test_create_repo file-submodule &&
307         (
308                 cd file-submodule &&
309
310                 git commit --allow-empty -m O &&
311
312                 git branch A &&
313                 git branch B &&
314
315                 git checkout B &&
316                 echo content >path &&
317                 git add path &&
318                 git commit -m B &&
319
320                 git checkout A &&
321                 test_create_repo path &&
322                 test_commit -C path world &&
323                 git submodule add ./path &&
324                 git commit -m A
325         )
326 '
327
328 test_expect_failure 'file/submodule conflict' '
329         test_when_finished "git -C file-submodule reset --hard" &&
330         (
331                 cd file-submodule &&
332
333                 git checkout A^0 &&
334                 test_must_fail git merge B^0 &&
335
336                 git ls-files -s >out &&
337                 test_line_count = 3 out &&
338                 git ls-files -u >out &&
339                 test_line_count = 2 out &&
340
341                 # path/ is still a submodule
342                 test_path_is_dir path/.git &&
343
344                 # There is a submodule at "path", so B:path cannot be written
345                 # there.  We expect it to be written somewhere in the same
346                 # directory, though, so just grep for its content in all
347                 # files, and ignore "grep: path: Is a directory" message
348                 echo Checking if contents from B:path showed up anywhere &&
349                 grep -q content * 2>/dev/null
350         )
351 '
352
353 test_expect_success 'file/submodule conflict; merge --abort works afterward' '
354         test_when_finished "git -C file-submodule reset --hard" &&
355         (
356                 cd file-submodule &&
357
358                 git checkout A^0 &&
359                 test_must_fail git merge B^0 >out 2>err &&
360
361                 test_path_is_file .git/MERGE_HEAD &&
362                 git merge --abort
363         )
364 '
365
366 # Directory/submodule conflict
367 #   Commit O: <empty>
368 #   Commit A: path (submodule), with sole tracked file named 'world'
369 #   Commit B1: path/file
370 #   Commit B2: path/world
371 #
372 #   Expected from merge of A & B1:
373 #     Contents under path/ from commit B1 are renamed elsewhere; we do not
374 #     want to write files from one of our tracked directories into a submodule
375 #
376 #   Expected from merge of A & B2:
377 #     Similar to last merge, but with a slight twist: we don't want paths
378 #     under the submodule to be treated as untracked or in the way.
379
380 test_expect_success 'setup directory/submodule conflict' '
381         test_create_repo directory-submodule &&
382         (
383                 cd directory-submodule &&
384
385                 git commit --allow-empty -m O &&
386
387                 git branch A &&
388                 git branch B1 &&
389                 git branch B2 &&
390
391                 git checkout B1 &&
392                 mkdir path &&
393                 echo contents >path/file &&
394                 git add path/file &&
395                 git commit -m B1 &&
396
397                 git checkout B2 &&
398                 mkdir path &&
399                 echo contents >path/world &&
400                 git add path/world &&
401                 git commit -m B2 &&
402
403                 git checkout A &&
404                 test_create_repo path &&
405                 test_commit -C path hello world &&
406                 git submodule add ./path &&
407                 git commit -m A
408         )
409 '
410
411 test_expect_failure 'directory/submodule conflict; keep submodule clean' '
412         test_when_finished "git -C directory-submodule reset --hard" &&
413         (
414                 cd directory-submodule &&
415
416                 git checkout A^0 &&
417                 test_must_fail git merge B1^0 &&
418
419                 git ls-files -s >out &&
420                 test_line_count = 3 out &&
421                 git ls-files -u >out &&
422                 test_line_count = 1 out &&
423
424                 # path/ is still a submodule
425                 test_path_is_dir path/.git &&
426
427                 echo Checking if contents from B1:path/file showed up &&
428                 # Would rather use grep -r, but that is GNU extension...
429                 git ls-files -co | xargs grep -q contents 2>/dev/null &&
430
431                 # However, B1:path/file should NOT have shown up at path/file,
432                 # because we should not write into the submodule
433                 test_path_is_missing path/file
434         )
435 '
436
437 test_expect_failure !FAIL_PREREQS 'directory/submodule conflict; should not treat submodule files as untracked or in the way' '
438         test_when_finished "git -C directory-submodule/path reset --hard" &&
439         test_when_finished "git -C directory-submodule reset --hard" &&
440         (
441                 cd directory-submodule &&
442
443                 git checkout A^0 &&
444                 test_must_fail git merge B2^0 >out 2>err &&
445
446                 # We do not want files within the submodule to prevent the
447                 # merge from starting; we should not be writing to such paths
448                 # anyway.
449                 test_i18ngrep ! "refusing to lose untracked file at" err
450         )
451 '
452
453 test_expect_failure 'directory/submodule conflict; merge --abort works afterward' '
454         test_when_finished "git -C directory-submodule/path reset --hard" &&
455         test_when_finished "git -C directory-submodule reset --hard" &&
456         (
457                 cd directory-submodule &&
458
459                 git checkout A^0 &&
460                 test_must_fail git merge B2^0 &&
461                 test_path_is_file .git/MERGE_HEAD &&
462
463                 # merge --abort should succeed, should clear .git/MERGE_HEAD,
464                 # and should not leave behind any conflicted files
465                 git merge --abort &&
466                 test_path_is_missing .git/MERGE_HEAD &&
467                 git ls-files -u >conflicts &&
468                 test_must_be_empty conflicts
469         )
470 '
471
472 test_done