reduce_heads(): protect from duplicate input
[git/git.git] / t / t7600-merge.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Lars Hjemli
4 #
5
6 test_description='git-merge
7
8 Testing basic merge operations/option parsing.'
9
10 . ./test-lib.sh
11
12 cat >file <<EOF
13 1
14 2
15 3
16 4
17 5
18 6
19 7
20 8
21 9
22 EOF
23
24 cat >file.1 <<EOF
25 1 X
26 2
27 3
28 4
29 5
30 6
31 7
32 8
33 9
34 EOF
35
36 cat >file.5 <<EOF
37 1
38 2
39 3
40 4
41 5 X
42 6
43 7
44 8
45 9
46 EOF
47
48 cat >file.9 <<EOF
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9 X
58 EOF
59
60 cat >result.1 <<EOF
61 1 X
62 2
63 3
64 4
65 5
66 6
67 7
68 8
69 9
70 EOF
71
72 cat >result.1-5 <<EOF
73 1 X
74 2
75 3
76 4
77 5 X
78 6
79 7
80 8
81 9
82 EOF
83
84 cat >result.1-5-9 <<EOF
85 1 X
86 2
87 3
88 4
89 5 X
90 6
91 7
92 8
93 9 X
94 EOF
95
96 create_merge_msgs() {
97 echo "Merge commit 'c2'" >msg.1-5 &&
98 echo "Merge commit 'c2'; commit 'c3'" >msg.1-5-9 &&
99 echo "Squashed commit of the following:" >squash.1 &&
100 echo >>squash.1 &&
101 git log --no-merges ^HEAD c1 >>squash.1 &&
102 echo "Squashed commit of the following:" >squash.1-5 &&
103 echo >>squash.1-5 &&
104 git log --no-merges ^HEAD c2 >>squash.1-5 &&
105 echo "Squashed commit of the following:" >squash.1-5-9 &&
106 echo >>squash.1-5-9 &&
107 git log --no-merges ^HEAD c2 c3 >>squash.1-5-9 &&
108 echo > msg.nolog &&
109 echo "* commit 'c3':" >msg.log &&
110 echo " commit 3" >>msg.log &&
111 echo >>msg.log
112 }
113
114 verify_diff() {
115 if ! test_cmp "$1" "$2"
116 then
117 echo "$3"
118 false
119 fi
120 }
121
122 verify_merge() {
123 verify_diff "$2" "$1" "[OOPS] bad merge result" &&
124 if test $(git ls-files -u | wc -l) -gt 0
125 then
126 echo "[OOPS] unmerged files"
127 false
128 fi &&
129 if ! git diff --exit-code
130 then
131 echo "[OOPS] working tree != index"
132 false
133 fi &&
134 if test -n "$3"
135 then
136 git show -s --pretty=format:%s HEAD >msg.act &&
137 verify_diff "$3" msg.act "[OOPS] bad merge message"
138 fi
139 }
140
141 verify_head() {
142 if test "$1" != "$(git rev-parse HEAD)"
143 then
144 echo "[OOPS] HEAD != $1"
145 false
146 fi
147 }
148
149 verify_parents() {
150 i=1
151 while test $# -gt 0
152 do
153 if test "$1" != "$(git rev-parse HEAD^$i)"
154 then
155 echo "[OOPS] HEAD^$i != $1"
156 return 1
157 fi
158 i=$(expr $i + 1)
159 shift
160 done
161 }
162
163 verify_mergeheads() {
164 i=1
165 if ! test -f .git/MERGE_HEAD
166 then
167 echo "[OOPS] MERGE_HEAD is missing"
168 false
169 fi &&
170 while test $# -gt 0
171 do
172 head=$(head -n $i .git/MERGE_HEAD | sed -ne \$p)
173 if test "$1" != "$head"
174 then
175 echo "[OOPS] MERGE_HEAD $i != $1"
176 return 1
177 fi
178 i=$(expr $i + 1)
179 shift
180 done
181 }
182
183 verify_no_mergehead() {
184 if test -f .git/MERGE_HEAD
185 then
186 echo "[OOPS] MERGE_HEAD exists"
187 false
188 fi
189 }
190
191
192 test_expect_success 'setup' '
193 git add file &&
194 test_tick &&
195 git commit -m "commit 0" &&
196 git tag c0 &&
197 c0=$(git rev-parse HEAD) &&
198 cp file.1 file &&
199 git add file &&
200 test_tick &&
201 git commit -m "commit 1" &&
202 git tag c1 &&
203 c1=$(git rev-parse HEAD) &&
204 git reset --hard "$c0" &&
205 cp file.5 file &&
206 git add file &&
207 test_tick &&
208 git commit -m "commit 2" &&
209 git tag c2 &&
210 c2=$(git rev-parse HEAD) &&
211 git reset --hard "$c0" &&
212 cp file.9 file &&
213 git add file &&
214 test_tick &&
215 git commit -m "commit 3" &&
216 git tag c3 &&
217 c3=$(git rev-parse HEAD)
218 git reset --hard "$c0" &&
219 create_merge_msgs
220 '
221
222 test_debug 'gitk --all'
223
224 test_expect_success 'test option parsing' '
225 if git merge -$ c1
226 then
227 echo "[OOPS] -$ accepted"
228 false
229 fi &&
230 if git merge --no-such c1
231 then
232 echo "[OOPS] --no-such accepted"
233 false
234 fi &&
235 if git merge -s foobar c1
236 then
237 echo "[OOPS] -s foobar accepted"
238 false
239 fi &&
240 if git merge -s=foobar c1
241 then
242 echo "[OOPS] -s=foobar accepted"
243 false
244 fi &&
245 if git merge -m
246 then
247 echo "[OOPS] missing commit msg accepted"
248 false
249 fi &&
250 if git merge
251 then
252 echo "[OOPS] missing commit references accepted"
253 false
254 fi
255 '
256
257 test_expect_success 'merge c0 with c1' '
258 git reset --hard c0 &&
259 git merge c1 &&
260 verify_merge file result.1 &&
261 verify_head "$c1"
262 '
263
264 test_debug 'gitk --all'
265
266 test_expect_success 'merge c1 with c2' '
267 git reset --hard c1 &&
268 test_tick &&
269 git merge c2 &&
270 verify_merge file result.1-5 msg.1-5 &&
271 verify_parents $c1 $c2
272 '
273
274 test_debug 'gitk --all'
275
276 test_expect_success 'merge c1 with c2 and c3' '
277 git reset --hard c1 &&
278 test_tick &&
279 git merge c2 c3 &&
280 verify_merge file result.1-5-9 msg.1-5-9 &&
281 verify_parents $c1 $c2 $c3
282 '
283
284 test_debug 'gitk --all'
285
286 test_expect_success 'merge c0 with c1 (no-commit)' '
287 git reset --hard c0 &&
288 git merge --no-commit c1 &&
289 verify_merge file result.1 &&
290 verify_head $c1
291 '
292
293 test_debug 'gitk --all'
294
295 test_expect_success 'merge c1 with c2 (no-commit)' '
296 git reset --hard c1 &&
297 git merge --no-commit c2 &&
298 verify_merge file result.1-5 &&
299 verify_head $c1 &&
300 verify_mergeheads $c2
301 '
302
303 test_debug 'gitk --all'
304
305 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
306 git reset --hard c1 &&
307 git merge --no-commit c2 c3 &&
308 verify_merge file result.1-5-9 &&
309 verify_head $c1 &&
310 verify_mergeheads $c2 $c3
311 '
312
313 test_debug 'gitk --all'
314
315 test_expect_success 'merge c0 with c1 (squash)' '
316 git reset --hard c0 &&
317 git merge --squash c1 &&
318 verify_merge file result.1 &&
319 verify_head $c0 &&
320 verify_no_mergehead &&
321 verify_diff squash.1 .git/SQUASH_MSG "[OOPS] bad squash message"
322 '
323
324 test_debug 'gitk --all'
325
326 test_expect_success 'merge c1 with c2 (squash)' '
327 git reset --hard c1 &&
328 git merge --squash c2 &&
329 verify_merge file result.1-5 &&
330 verify_head $c1 &&
331 verify_no_mergehead &&
332 verify_diff squash.1-5 .git/SQUASH_MSG "[OOPS] bad squash message"
333 '
334
335 test_debug 'gitk --all'
336
337 test_expect_success 'merge c1 with c2 and c3 (squash)' '
338 git reset --hard c1 &&
339 git merge --squash c2 c3 &&
340 verify_merge file result.1-5-9 &&
341 verify_head $c1 &&
342 verify_no_mergehead &&
343 verify_diff squash.1-5-9 .git/SQUASH_MSG "[OOPS] bad squash message"
344 '
345
346 test_debug 'gitk --all'
347
348 test_expect_success 'merge c1 with c2 (no-commit in config)' '
349 git reset --hard c1 &&
350 git config branch.master.mergeoptions "--no-commit" &&
351 git merge c2 &&
352 verify_merge file result.1-5 &&
353 verify_head $c1 &&
354 verify_mergeheads $c2
355 '
356
357 test_debug 'gitk --all'
358
359 test_expect_success 'merge c1 with c2 (squash in config)' '
360 git reset --hard c1 &&
361 git config branch.master.mergeoptions "--squash" &&
362 git merge c2 &&
363 verify_merge file result.1-5 &&
364 verify_head $c1 &&
365 verify_no_mergehead &&
366 verify_diff squash.1-5 .git/SQUASH_MSG "[OOPS] bad squash message"
367 '
368
369 test_debug 'gitk --all'
370
371 test_expect_success 'override config option -n with --summary' '
372 git reset --hard c1 &&
373 git config branch.master.mergeoptions "-n" &&
374 test_tick &&
375 git merge --summary c2 >diffstat.txt &&
376 verify_merge file result.1-5 msg.1-5 &&
377 verify_parents $c1 $c2 &&
378 if ! grep "^ file | *2 +-$" diffstat.txt
379 then
380 echo "[OOPS] diffstat was not generated with --summary"
381 false
382 fi
383 '
384
385 test_expect_success 'override config option -n with --stat' '
386 git reset --hard c1 &&
387 git config branch.master.mergeoptions "-n" &&
388 test_tick &&
389 git merge --stat c2 >diffstat.txt &&
390 verify_merge file result.1-5 msg.1-5 &&
391 verify_parents $c1 $c2 &&
392 if ! grep "^ file | *2 +-$" diffstat.txt
393 then
394 echo "[OOPS] diffstat was not generated with --stat"
395 false
396 fi
397 '
398
399 test_debug 'gitk --all'
400
401 test_expect_success 'override config option --stat' '
402 git reset --hard c1 &&
403 git config branch.master.mergeoptions "--stat" &&
404 test_tick &&
405 git merge -n c2 >diffstat.txt &&
406 verify_merge file result.1-5 msg.1-5 &&
407 verify_parents $c1 $c2 &&
408 if grep "^ file | *2 +-$" diffstat.txt
409 then
410 echo "[OOPS] diffstat was generated"
411 false
412 fi
413 '
414
415 test_debug 'gitk --all'
416
417 test_expect_success 'merge c1 with c2 (override --no-commit)' '
418 git reset --hard c1 &&
419 git config branch.master.mergeoptions "--no-commit" &&
420 test_tick &&
421 git merge --commit c2 &&
422 verify_merge file result.1-5 msg.1-5 &&
423 verify_parents $c1 $c2
424 '
425
426 test_debug 'gitk --all'
427
428 test_expect_success 'merge c1 with c2 (override --squash)' '
429 git reset --hard c1 &&
430 git config branch.master.mergeoptions "--squash" &&
431 test_tick &&
432 git merge --no-squash c2 &&
433 verify_merge file result.1-5 msg.1-5 &&
434 verify_parents $c1 $c2
435 '
436
437 test_debug 'gitk --all'
438
439 test_expect_success 'merge c0 with c1 (no-ff)' '
440 git reset --hard c0 &&
441 git config branch.master.mergeoptions "" &&
442 test_tick &&
443 git merge --no-ff c1 &&
444 verify_merge file result.1 &&
445 verify_parents $c0 $c1
446 '
447
448 test_debug 'gitk --all'
449
450 test_expect_success 'combining --squash and --no-ff is refused' '
451 test_must_fail git merge --squash --no-ff c1 &&
452 test_must_fail git merge --no-ff --squash c1
453 '
454
455 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
456 git reset --hard c0 &&
457 git config branch.master.mergeoptions "--no-ff" &&
458 git merge --ff c1 &&
459 verify_merge file result.1 &&
460 verify_head $c1
461 '
462
463 test_expect_success 'merge log message' '
464 git reset --hard c0 &&
465 git merge --no-log c2 &&
466 git show -s --pretty=format:%b HEAD >msg.act &&
467 verify_diff msg.nolog msg.act "[OOPS] bad merge log message" &&
468
469 git merge --log c3 &&
470 git show -s --pretty=format:%b HEAD >msg.act &&
471 verify_diff msg.log msg.act "[OOPS] bad merge log message" &&
472
473 git reset --hard HEAD^ &&
474 git config merge.log yes &&
475 git merge c3 &&
476 git show -s --pretty=format:%b HEAD >msg.act &&
477 verify_diff msg.log msg.act "[OOPS] bad merge log message"
478 '
479
480 test_debug 'gitk --all'
481
482 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
483 git reset --hard c1 &&
484 git config branch.master.mergeoptions "" &&
485 test_tick &&
486 git merge c0 c2 c0 c1 &&
487 verify_merge file result.1-5 &&
488 verify_parents $c1 $c2
489 '
490
491 test_debug 'gitk --all'
492
493 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
494 git reset --hard c1 &&
495 git config branch.master.mergeoptions "" &&
496 test_tick &&
497 git merge c0 c2 c0 c1 &&
498 verify_merge file result.1-5 &&
499 verify_parents $c1 $c2
500 '
501
502 test_debug 'gitk --all'
503
504 test_expect_success 'merge c1 with c1 and c2' '
505 git reset --hard c1 &&
506 git config branch.master.mergeoptions "" &&
507 test_tick &&
508 git merge c1 c2 &&
509 verify_merge file result.1-5 &&
510 verify_parents $c1 $c2
511 '
512
513 test_debug 'gitk --all'
514
515 test_done