about summary refs log blame commit diff
path: root/t/t7405-submodule-merge.sh
blob: aa33978ed2868e1585b4acdf87cdba2907597f45 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455






































































































































































































































































































































































































































































                                                                                                                               
#!/bin/sh

test_description='merging with submodules'

. ./test-lib.sh

#
# history
#
#        a --- c
#      /   \ /
# root      X
#      \   / \
#        b --- d
#

test_expect_success setup '

	mkdir sub &&
	(cd sub &&
	 git init &&
	 echo original > file &&
	 git add file &&
	 test_tick &&
	 git commit -m sub-root) &&
	git add sub &&
	test_tick &&
	git commit -m root &&

	git checkout -b a master &&
	(cd sub &&
	 echo A > file &&
	 git add file &&
	 test_tick &&
	 git commit -m sub-a) &&
	git add sub &&
	test_tick &&
	git commit -m a &&

	git checkout -b b master &&
	(cd sub &&
	 echo B > file &&
	 git add file &&
	 test_tick &&
	 git commit -m sub-b) &&
	git add sub &&
	test_tick &&
	git commit -m b &&

	git checkout -b c a &&
	git merge -s ours b &&

	git checkout -b d b &&
	git merge -s ours a
'

# History setup
#
#             b
#           /   \
#  init -- a     d
#    \      \   /
#     g       c
#
# a in the main repository records to sub-a in the submodule and
# analogous b and c. d should be automatically found by merging c into
# b in the main repository.
test_expect_success 'setup for merge search' '
	mkdir merge-search &&
	(cd merge-search &&
	git init &&
	mkdir sub &&
	(cd sub &&
	 git init &&
	 echo "file-a" > file-a &&
	 git add file-a &&
	 git commit -m "sub-a" &&
	 git branch sub-a) &&
	git commit --allow-empty -m init &&
	git branch init &&
	git add sub &&
	git commit -m "a" &&
	git branch a &&

	git checkout -b b &&
	(cd sub &&
	 git checkout -b sub-b &&
	 echo "file-b" > file-b &&
	 git add file-b &&
	 git commit -m "sub-b") &&
	git commit -a -m "b" &&

	git checkout -b c a &&
	(cd sub &&
	 git checkout -b sub-c sub-a &&
	 echo "file-c" > file-c &&
	 git add file-c &&
	 git commit -m "sub-c") &&
	git commit -a -m "c" &&

	git checkout -b d a &&
	(cd sub &&
	 git checkout -b sub-d sub-b &&
	 git merge sub-c) &&
	git commit -a -m "d" &&
	git branch test b &&

	git checkout -b g init &&
	(cd sub &&
	 git checkout -b sub-g sub-c) &&
	git add sub &&
	git commit -a -m "g")
'

test_expect_success 'merge with one side as a fast-forward of the other' '
	(cd merge-search &&
	 git checkout -b test-forward b &&
	 git merge d &&
	 git ls-tree test-forward sub | cut -f1 | cut -f3 -d" " > actual &&
	 (cd sub &&
	  git rev-parse sub-d > ../expect) &&
	 test_cmp expect actual)
'

test_expect_success 'merging should conflict for non fast-forward' '
	(cd merge-search &&
	 git checkout -b test-nonforward b &&
	 (cd sub &&
	  git rev-parse sub-d > ../expect) &&
	 test_must_fail git merge c 2> actual  &&
	 grep $(cat expect) actual > /dev/null &&
	 git reset --hard)
'

test_expect_success 'merging should fail for ambiguous common parent' '
	(cd merge-search &&
	git checkout -b test-ambiguous b &&
	(cd sub &&
	 git checkout -b ambiguous sub-b &&
	 git merge sub-c &&
	 git rev-parse sub-d > ../expect1 &&
	 git rev-parse ambiguous > ../expect2) &&
	test_must_fail git merge c 2> actual &&
	grep $(cat expect1) actual > /dev/null &&
	grep $(cat expect2) actual > /dev/null &&
	git reset --hard)
'

# in a situation like this
#
# submodule tree:
#
#    sub-a --- sub-b --- sub-d
#
# main tree:
#
#    e (sub-a)
#   /
#  bb (sub-b)
#   \
#    f (sub-d)
#
# A merge between e and f should fail because one of the submodule
# commits (sub-a) does not descend from the submodule merge-base (sub-b).
#
test_expect_success 'merging should fail for changes that are backwards' '
	(cd merge-search &&
	git checkout -b bb a &&
	(cd sub &&
	 git checkout sub-b) &&
	git commit -a -m "bb" &&

	git checkout -b e bb &&
	(cd sub &&
	 git checkout sub-a) &&
	git commit -a -m "e" &&

	git checkout -b f bb &&
	(cd sub &&
	 git checkout sub-d) &&
	git commit -a -m "f" &&

	git checkout -b test-backward e &&
	test_must_fail git merge f)
'


# Check that the conflicting submodule is detected when it is
# in the common ancestor. status should be 'U00...00"
test_expect_success 'git submodule status should display the merge conflict properly with merge base' '
       (cd merge-search &&
       cat >.gitmodules <<EOF &&
[submodule "sub"]
       path = sub
       url = $TRASH_DIRECTORY/sub
EOF
       cat >expect <<EOF &&
U0000000000000000000000000000000000000000 sub
EOF
       git submodule status > actual &&
       test_cmp expect actual &&
	git reset --hard)
'

# Check that the conflicting submodule is detected when it is
# not in the common ancestor. status should be 'U00...00"
test_expect_success 'git submodule status should display the merge conflict properly without merge-base' '
       (cd merge-search &&
	git checkout -b test-no-merge-base g &&
	test_must_fail git merge b &&
       cat >.gitmodules <<EOF &&
[submodule "sub"]
       path = sub
       url = $TRASH_DIRECTORY/sub
EOF
       cat >expect <<EOF &&
U0000000000000000000000000000000000000000 sub
EOF
       git submodule status > actual &&
       test_cmp expect actual &&
       git reset --hard)
'


test_expect_success 'merging with a modify/modify conflict between merge bases' '
	git reset --hard HEAD &&
	git checkout -b test2 c &&
	git merge d
'

# canonical criss-cross history in top and submodule
test_expect_success 'setup for recursive merge with submodule' '
	mkdir merge-recursive &&
	(cd merge-recursive &&
	 git init &&
	 mkdir sub &&
	 (cd sub &&
	  git init &&
	  test_commit a &&
	  git checkout -b sub-b master &&
	  test_commit b &&
	  git checkout -b sub-c master &&
	  test_commit c &&
	  git checkout -b sub-bc sub-b &&
	  git merge sub-c &&
	  git checkout -b sub-cb sub-c &&
	  git merge sub-b &&
	  git checkout master) &&
	 git add sub &&
	 git commit -m a &&
	 git checkout -b top-b master &&
	 (cd sub && git checkout sub-b) &&
	 git add sub &&
	 git commit -m b &&
	 git checkout -b top-c master &&
	 (cd sub && git checkout sub-c) &&
	 git add sub &&
	 git commit -m c &&
	 git checkout -b top-bc top-b &&
	 git merge -s ours --no-commit top-c &&
	 (cd sub && git checkout sub-bc) &&
	 git add sub &&
	 git commit -m bc &&
	 git checkout -b top-cb top-c &&
	 git merge -s ours --no-commit top-b &&
	 (cd sub && git checkout sub-cb) &&
	 git add sub &&
	 git commit -m cb)
'

# merge should leave submodule unmerged in index
test_expect_success 'recursive merge with submodule' '
	(cd merge-recursive &&
	 test_must_fail git merge top-bc &&
	 echo "160000 $(git rev-parse top-cb:sub) 2	sub" > expect2 &&
	 echo "160000 $(git rev-parse top-bc:sub) 3	sub" > expect3 &&
	 git ls-files -u > actual &&
	 grep "$(cat expect2)" actual > /dev/null &&
	 grep "$(cat expect3)" actual > /dev/null)
'

# File/submodule conflict
#   Commit O: <empty>
#   Commit A: path (submodule)
#   Commit B: path
#   Expected: path/ is submodule and file contents for B's path are somewhere

test_expect_success 'setup file/submodule conflict' '
	test_create_repo file-submodule &&
	(
		cd file-submodule &&

		git commit --allow-empty -m O &&

		git branch A &&
		git branch B &&

		git checkout B &&
		echo content >path &&
		git add path &&
		git commit -m B &&

		git checkout A &&
		test_create_repo path &&
		test_commit -C path world &&
		git submodule add ./path &&
		git commit -m A
	)
'

test_expect_failure 'file/submodule conflict' '
	test_when_finished "git -C file-submodule reset --hard" &&
	(
		cd file-submodule &&

		git checkout A^0 &&
		test_must_fail git merge B^0 &&

		git ls-files -s >out &&
		test_line_count = 3 out &&
		git ls-files -u >out &&
		test_line_count = 2 out &&

		# path/ is still a submodule
		test_path_is_dir path/.git &&

		# There is a submodule at "path", so B:path cannot be written
		# there.  We expect it to be written somewhere in the same
		# directory, though, so just grep for its content in all
		# files, and ignore "grep: path: Is a directory" message
		echo Checking if contents from B:path showed up anywhere &&
		grep -q content * 2>/dev/null
	)
'

test_expect_success 'file/submodule conflict; merge --abort works afterward' '
	test_when_finished "git -C file-submodule reset --hard" &&
	(
		cd file-submodule &&

		git checkout A^0 &&
		test_must_fail git merge B^0 >out 2>err &&

		test_path_is_file .git/MERGE_HEAD &&
		git merge --abort
	)
'

# Directory/submodule conflict
#   Commit O: <empty>
#   Commit A: path (submodule), with sole tracked file named 'world'
#   Commit B1: path/file
#   Commit B2: path/world
#
#   Expected from merge of A & B1:
#     Contents under path/ from commit B1 are renamed elsewhere; we do not
#     want to write files from one of our tracked directories into a submodule
#
#   Expected from merge of A & B2:
#     Similar to last merge, but with a slight twist: we don't want paths
#     under the submodule to be treated as untracked or in the way.

test_expect_success 'setup directory/submodule conflict' '
	test_create_repo directory-submodule &&
	(
		cd directory-submodule &&

		git commit --allow-empty -m O &&

		git branch A &&
		git branch B1 &&
		git branch B2 &&

		git checkout B1 &&
		mkdir path &&
		echo contents >path/file &&
		git add path/file &&
		git commit -m B1 &&

		git checkout B2 &&
		mkdir path &&
		echo contents >path/world &&
		git add path/world &&
		git commit -m B2 &&

		git checkout A &&
		test_create_repo path &&
		test_commit -C path hello world &&
		git submodule add ./path &&
		git commit -m A
	)
'

test_expect_failure 'directory/submodule conflict; keep submodule clean' '
	test_when_finished "git -C directory-submodule reset --hard" &&
	(
		cd directory-submodule &&

		git checkout A^0 &&
		test_must_fail git merge B1^0 &&

		git ls-files -s >out &&
		test_line_count = 3 out &&
		git ls-files -u >out &&
		test_line_count = 1 out &&

		# path/ is still a submodule
		test_path_is_dir path/.git &&

		echo Checking if contents from B1:path/file showed up &&
		# Would rather use grep -r, but that is GNU extension...
		git ls-files -co | xargs grep -q contents 2>/dev/null &&

		# However, B1:path/file should NOT have shown up at path/file,
		# because we should not write into the submodule
		test_path_is_missing path/file
	)
'

test_expect_failure !FAIL_PREREQS 'directory/submodule conflict; should not treat submodule files as untracked or in the way' '
	test_when_finished "git -C directory-submodule/path reset --hard" &&
	test_when_finished "git -C directory-submodule reset --hard" &&
	(
		cd directory-submodule &&

		git checkout A^0 &&
		test_must_fail git merge B2^0 >out 2>err &&

		# We do not want files within the submodule to prevent the
		# merge from starting; we should not be writing to such paths
		# anyway.
		test_i18ngrep ! "refusing to lose untracked file at" err
	)
'

test_expect_failure 'directory/submodule conflict; merge --abort works afterward' '
	test_when_finished "git -C directory-submodule/path reset --hard" &&
	test_when_finished "git -C directory-submodule reset --hard" &&
	(
		cd directory-submodule &&

		git checkout A^0 &&
		test_must_fail git merge B2^0 &&
		test_path_is_file .git/MERGE_HEAD &&

		# merge --abort should succeed, should clear .git/MERGE_HEAD,
		# and should not leave behind any conflicted files
		git merge --abort &&
		test_path_is_missing .git/MERGE_HEAD &&
		git ls-files -u >conflicts &&
		test_must_be_empty conflicts
	)
'

test_done