1#!/bin/sh
2#
3# Copyright (c) 2007 Lars Hjemli
4#
5
6test_description='git merge
7
8Testing basic merge operations/option parsing.'
9
10. ./test-lib.sh
11
12cat >file <<EOF
131
142
153
164
175
186
197
208
219
22EOF
23
24cat >file.1 <<EOF
251 X
262
273
284
295
306
317
328
339
34EOF
35
36cat >file.5 <<EOF
371
382
393
404
415 X
426
437
448
459
46EOF
47
48cat >file.9 <<EOF
491
502
513
524
535
546
557
568
579 X
58EOF
59
60cat >result.1 <<EOF
611 X
622
633
644
655
666
677
688
699
70EOF
71
72cat >result.1-5 <<EOF
731 X
742
753
764
775 X
786
797
808
819
82EOF
83
84cat >result.1-5-9 <<EOF
851 X
862
873
884
895 X
906
917
928
939 X
94EOF
95
96create_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
114verify_diff() {
115 if ! test_cmp "$1" "$2"
116 then
117 echo "$3"
118 false
119 fi
120}
121
122verify_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 test_must_fail 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
141verify_head() {
142 if test "$1" != "$(git rev-parse HEAD)"
143 then
144 echo "[OOPS] HEAD != $1"
145 false
146 fi
147}
148
149verify_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
163verify_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
183verify_no_mergehead() {
184 if test -f .git/MERGE_HEAD
185 then
186 echo "[OOPS] MERGE_HEAD exists"
187 false
188 fi
189}
190
191
192test_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
222test_debug 'gitk --all'
223
224test_expect_success 'test option parsing' '
225 test_must_fail git merge -$ c1 &&
226 test_must_fail git merge --no-such c1 &&
227 test_must_fail git merge -s foobar c1 &&
228 test_must_fail git merge -s=foobar c1 &&
229 test_must_fail git merge -m &&
230 test_must_fail git merge
231'
232
233test_expect_success 'merge c0 with c1' '
234 git reset --hard c0 &&
235 git merge c1 &&
236 verify_merge file result.1 &&
237 verify_head "$c1"
238'
239
240test_debug 'gitk --all'
241
242test_expect_success 'merge c1 with c2' '
243 git reset --hard c1 &&
244 test_tick &&
245 git merge c2 &&
246 verify_merge file result.1-5 msg.1-5 &&
247 verify_parents $c1 $c2
248'
249
250test_debug 'gitk --all'
251
252test_expect_success 'merge c1 with c2 and c3' '
253 git reset --hard c1 &&
254 test_tick &&
255 git merge c2 c3 &&
256 verify_merge file result.1-5-9 msg.1-5-9 &&
257 verify_parents $c1 $c2 $c3
258'
259
260test_debug 'gitk --all'
261
262test_expect_success 'merge c0 with c1 (no-commit)' '
263 git reset --hard c0 &&
264 git merge --no-commit c1 &&
265 verify_merge file result.1 &&
266 verify_head $c1
267'
268
269test_debug 'gitk --all'
270
271test_expect_success 'merge c1 with c2 (no-commit)' '
272 git reset --hard c1 &&
273 git merge --no-commit c2 &&
274 verify_merge file result.1-5 &&
275 verify_head $c1 &&
276 verify_mergeheads $c2
277'
278
279test_debug 'gitk --all'
280
281test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
282 git reset --hard c1 &&
283 git merge --no-commit c2 c3 &&
284 verify_merge file result.1-5-9 &&
285 verify_head $c1 &&
286 verify_mergeheads $c2 $c3
287'
288
289test_debug 'gitk --all'
290
291test_expect_success 'merge c0 with c1 (squash)' '
292 git reset --hard c0 &&
293 git merge --squash c1 &&
294 verify_merge file result.1 &&
295 verify_head $c0 &&
296 verify_no_mergehead &&
297 verify_diff squash.1 .git/SQUASH_MSG "[OOPS] bad squash message"
298'
299
300test_debug 'gitk --all'
301
302test_expect_success 'merge c1 with c2 (squash)' '
303 git reset --hard c1 &&
304 git merge --squash c2 &&
305 verify_merge file result.1-5 &&
306 verify_head $c1 &&
307 verify_no_mergehead &&
308 verify_diff squash.1-5 .git/SQUASH_MSG "[OOPS] bad squash message"
309'
310
311test_debug 'gitk --all'
312
313test_expect_success 'merge c1 with c2 and c3 (squash)' '
314 git reset --hard c1 &&
315 git merge --squash c2 c3 &&
316 verify_merge file result.1-5-9 &&
317 verify_head $c1 &&
318 verify_no_mergehead &&
319 verify_diff squash.1-5-9 .git/SQUASH_MSG "[OOPS] bad squash message"
320'
321
322test_debug 'gitk --all'
323
324test_expect_success 'merge c1 with c2 (no-commit in config)' '
325 git reset --hard c1 &&
326 git config branch.master.mergeoptions "--no-commit" &&
327 git merge c2 &&
328 verify_merge file result.1-5 &&
329 verify_head $c1 &&
330 verify_mergeheads $c2
331'
332
333test_debug 'gitk --all'
334
335test_expect_success 'merge c1 with c2 (squash in config)' '
336 git reset --hard c1 &&
337 git config branch.master.mergeoptions "--squash" &&
338 git merge c2 &&
339 verify_merge file result.1-5 &&
340 verify_head $c1 &&
341 verify_no_mergehead &&
342 verify_diff squash.1-5 .git/SQUASH_MSG "[OOPS] bad squash message"
343'
344
345test_debug 'gitk --all'
346
347test_expect_success 'override config option -n with --summary' '
348 git reset --hard c1 &&
349 git config branch.master.mergeoptions "-n" &&
350 test_tick &&
351 git merge --summary c2 >diffstat.txt &&
352 verify_merge file result.1-5 msg.1-5 &&
353 verify_parents $c1 $c2 &&
354 if ! grep "^ file | *2 +-$" diffstat.txt
355 then
356 echo "[OOPS] diffstat was not generated with --summary"
357 false
358 fi
359'
360
361test_expect_success 'override config option -n with --stat' '
362 git reset --hard c1 &&
363 git config branch.master.mergeoptions "-n" &&
364 test_tick &&
365 git merge --stat c2 >diffstat.txt &&
366 verify_merge file result.1-5 msg.1-5 &&
367 verify_parents $c1 $c2 &&
368 if ! grep "^ file | *2 +-$" diffstat.txt
369 then
370 echo "[OOPS] diffstat was not generated with --stat"
371 false
372 fi
373'
374
375test_debug 'gitk --all'
376
377test_expect_success 'override config option --stat' '
378 git reset --hard c1 &&
379 git config branch.master.mergeoptions "--stat" &&
380 test_tick &&
381 git merge -n c2 >diffstat.txt &&
382 verify_merge file result.1-5 msg.1-5 &&
383 verify_parents $c1 $c2 &&
384 if grep "^ file | *2 +-$" diffstat.txt
385 then
386 echo "[OOPS] diffstat was generated"
387 false
388 fi
389'
390
391test_debug 'gitk --all'
392
393test_expect_success 'merge c1 with c2 (override --no-commit)' '
394 git reset --hard c1 &&
395 git config branch.master.mergeoptions "--no-commit" &&
396 test_tick &&
397 git merge --commit c2 &&
398 verify_merge file result.1-5 msg.1-5 &&
399 verify_parents $c1 $c2
400'
401
402test_debug 'gitk --all'
403
404test_expect_success 'merge c1 with c2 (override --squash)' '
405 git reset --hard c1 &&
406 git config branch.master.mergeoptions "--squash" &&
407 test_tick &&
408 git merge --no-squash c2 &&
409 verify_merge file result.1-5 msg.1-5 &&
410 verify_parents $c1 $c2
411'
412
413test_debug 'gitk --all'
414
415test_expect_success 'merge c0 with c1 (no-ff)' '
416 git reset --hard c0 &&
417 git config branch.master.mergeoptions "" &&
418 test_tick &&
419 git merge --no-ff c1 &&
420 verify_merge file result.1 &&
421 verify_parents $c0 $c1
422'
423
424test_debug 'gitk --all'
425
426test_expect_success 'combining --squash and --no-ff is refused' '
427 test_must_fail git merge --squash --no-ff c1 &&
428 test_must_fail git merge --no-ff --squash c1
429'
430
431test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
432 git reset --hard c0 &&
433 git config branch.master.mergeoptions "--no-ff" &&
434 git merge --ff c1 &&
435 verify_merge file result.1 &&
436 verify_head $c1
437'
438
439test_expect_success 'merge log message' '
440 git reset --hard c0 &&
441 git merge --no-log c2 &&
442 git show -s --pretty=format:%b HEAD >msg.act &&
443 verify_diff msg.nolog msg.act "[OOPS] bad merge log message" &&
444
445 git merge --log c3 &&
446 git show -s --pretty=format:%b HEAD >msg.act &&
447 verify_diff msg.log msg.act "[OOPS] bad merge log message" &&
448
449 git reset --hard HEAD^ &&
450 git config merge.log yes &&
451 git merge c3 &&
452 git show -s --pretty=format:%b HEAD >msg.act &&
453 verify_diff msg.log msg.act "[OOPS] bad merge log message"
454'
455
456test_debug 'gitk --all'
457
458test_expect_success 'merge c1 with c0, c2, c0, and c1' '
459 git reset --hard c1 &&
460 git config branch.master.mergeoptions "" &&
461 test_tick &&
462 git merge c0 c2 c0 c1 &&
463 verify_merge file result.1-5 &&
464 verify_parents $c1 $c2
465'
466
467test_debug 'gitk --all'
468
469test_expect_success 'merge c1 with c0, c2, c0, and c1' '
470 git reset --hard c1 &&
471 git config branch.master.mergeoptions "" &&
472 test_tick &&
473 git merge c0 c2 c0 c1 &&
474 verify_merge file result.1-5 &&
475 verify_parents $c1 $c2
476'
477
478test_debug 'gitk --all'
479
480test_expect_success 'merge c1 with c1 and c2' '
481 git reset --hard c1 &&
482 git config branch.master.mergeoptions "" &&
483 test_tick &&
484 git merge c1 c2 &&
485 verify_merge file result.1-5 &&
486 verify_parents $c1 $c2
487'
488
489test_debug 'gitk --all'
490
491test_expect_success 'merge fast-forward in a dirty tree' '
492 git reset --hard c0 &&
493 mv file file1 &&
494 cat file1 >file &&
495 rm -f file1 &&
496 git merge c2
497'
498
499test_debug 'gitk --all'
500
501test_expect_success 'in-index merge' '
502 git reset --hard c0 &&
503 git merge --no-ff -s resolve c1 > out &&
504 grep "Wonderful." out &&
505 verify_parents $c0 $c1
506'
507
508test_debug 'gitk --all'
509
510test_done