commit-graph: when incompatible with graphs, indicate why
[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 ! [c0] commit 0
11  ! [c1] commit 1
12   ! [c2] commit 2
13    ! [c3] commit 3
14     ! [c4] c4
15      ! [c5] c5
16       ! [c6] c6
17        * [master] Merge commit 'c1'
18 --------
19        - [master] Merge commit 'c1'
20  +     * [c1] commit 1
21       +  [c6] c6
22      +   [c5] c5
23     ++   [c4] c4
24    ++++  [c3] commit 3
25   +      [c2] commit 2
26 +++++++* [c0] commit 0
27 '
28
29 . ./test-lib.sh
30 . "$TEST_DIRECTORY"/lib-gpg.sh
31
32 test_write_lines 1 2 3 4 5 6 7 8 9 >file
33 cp file file.orig
34 test_write_lines '1 X' 2 3 4 5 6 7 8 9 >file.1
35 test_write_lines 1 2 '3 X' 4 5 6 7 8 9 >file.3
36 test_write_lines 1 2 3 4 '5 X' 6 7 8 9 >file.5
37 test_write_lines 1 2 3 4 5 6 7 8 '9 X' >file.9
38 test_write_lines 1 2 3 4 5 6 7 8 '9 Y' >file.9y
39 test_write_lines '1 X' 2 3 4 5 6 7 8 9 >result.1
40 test_write_lines '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
41 test_write_lines '1 X' 2 3 4 5 6 7 8 '9 X' >result.1-9
42 test_write_lines '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
43 test_write_lines '1 X' 2 '3 X' 4 '5 X' 6 7 8 '9 X' >result.1-3-5-9
44 test_write_lines 1 2 3 4 5 6 7 8 '9 Z' >result.9z
45
46 create_merge_msgs () {
47         echo "Merge tag 'c2'" >msg.1-5 &&
48         echo "Merge tags 'c2' and 'c3'" >msg.1-5-9 &&
49         {
50                 echo "Squashed commit of the following:" &&
51                 echo &&
52                 git log --no-merges ^HEAD c1
53         } >squash.1 &&
54         {
55                 echo "Squashed commit of the following:" &&
56                 echo &&
57                 git log --no-merges ^HEAD c2
58         } >squash.1-5 &&
59         {
60                 echo "Squashed commit of the following:" &&
61                 echo &&
62                 git log --no-merges ^HEAD c2 c3
63         } >squash.1-5-9 &&
64         {
65                 echo "* tag 'c3':" &&
66                 echo "  commit 3"
67         } >msg.log
68 }
69
70 verify_merge () {
71         test_cmp "$2" "$1" &&
72         git update-index --refresh &&
73         git diff --exit-code &&
74         if test -n "$3"
75         then
76                 git show -s --pretty=tformat:%s HEAD >msg.act &&
77                 test_cmp "$3" msg.act
78         fi
79 }
80
81 verify_head () {
82         echo "$1" >head.expected &&
83         git rev-parse HEAD >head.actual &&
84         test_cmp head.expected head.actual
85 }
86
87 verify_parents () {
88         test_write_lines "$@" >parents.expected &&
89         >parents.actual &&
90         i=1 &&
91         while test $i -le $#
92         do
93                 git rev-parse HEAD^$i >>parents.actual &&
94                 i=$(expr $i + 1) ||
95                 return 1
96         done &&
97         test_must_fail git rev-parse --verify "HEAD^$i" &&
98         test_cmp parents.expected parents.actual
99 }
100
101 verify_mergeheads () {
102         test_write_lines "$@" >mergehead.expected &&
103         while read sha1 rest
104         do
105                 git rev-parse $sha1
106         done <.git/MERGE_HEAD >mergehead.actual &&
107         test_cmp mergehead.expected mergehead.actual
108 }
109
110 verify_no_mergehead () {
111         ! test -e .git/MERGE_HEAD
112 }
113
114 test_expect_success 'setup' '
115         git add file &&
116         test_tick &&
117         git commit -m "commit 0" &&
118         git tag c0 &&
119         c0=$(git rev-parse HEAD) &&
120         cp file.1 file &&
121         git add file &&
122         test_tick &&
123         git commit -m "commit 1" &&
124         git tag c1 &&
125         c1=$(git rev-parse HEAD) &&
126         git reset --hard "$c0" &&
127         cp file.5 file &&
128         git add file &&
129         test_tick &&
130         git commit -m "commit 2" &&
131         git tag c2 &&
132         c2=$(git rev-parse HEAD) &&
133         git reset --hard "$c0" &&
134         cp file.9y file &&
135         git add file &&
136         test_tick &&
137         git commit -m "commit 7" &&
138         git tag c7 &&
139         git reset --hard "$c0" &&
140         cp file.9 file &&
141         git add file &&
142         test_tick &&
143         git commit -m "commit 3" &&
144         git tag c3 &&
145         c3=$(git rev-parse HEAD) &&
146         git reset --hard "$c0" &&
147         create_merge_msgs
148 '
149
150 test_debug 'git log --graph --decorate --oneline --all'
151
152 test_expect_success 'test option parsing' '
153         test_must_fail git merge -$ c1 &&
154         test_must_fail git merge --no-such c1 &&
155         test_must_fail git merge -s foobar c1 &&
156         test_must_fail git merge -s=foobar c1 &&
157         test_must_fail git merge -m &&
158         test_must_fail git merge --abort foobar &&
159         test_must_fail git merge --abort --quiet &&
160         test_must_fail git merge --continue foobar &&
161         test_must_fail git merge --continue --quiet &&
162         test_must_fail git merge
163 '
164
165 test_expect_success 'merge -h with invalid index' '
166         mkdir broken &&
167         (
168                 cd broken &&
169                 git init &&
170                 >.git/index &&
171                 test_expect_code 129 git merge -h 2>usage
172         ) &&
173         test_i18ngrep "[Uu]sage: git merge" broken/usage
174 '
175
176 test_expect_success 'reject non-strategy with a git-merge-foo name' '
177         test_must_fail git merge -s index c1
178 '
179
180 test_expect_success 'merge c0 with c1' '
181         echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
182
183         git reset --hard c0 &&
184         git merge c1 &&
185         verify_merge file result.1 &&
186         verify_head "$c1" &&
187
188         git reflog -1 >reflog.actual &&
189         sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
190         test_cmp reflog.expected reflog.fuzzy
191 '
192
193 test_debug 'git log --graph --decorate --oneline --all'
194
195 test_expect_success 'merge c0 with c1 with --ff-only' '
196         git reset --hard c0 &&
197         git merge --ff-only c1 &&
198         git merge --ff-only HEAD c0 c1 &&
199         verify_merge file result.1 &&
200         verify_head "$c1"
201 '
202
203 test_debug 'git log --graph --decorate --oneline --all'
204
205 test_expect_success 'merge from unborn branch' '
206         git checkout -f master &&
207         test_might_fail git branch -D kid &&
208
209         echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
210
211         git checkout --orphan kid &&
212         test_when_finished "git checkout -f master" &&
213         git rm -fr . &&
214         test_tick &&
215         git merge --ff-only c1 &&
216         verify_merge file result.1 &&
217         verify_head "$c1" &&
218
219         git reflog -1 >reflog.actual &&
220         sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
221         test_cmp reflog.expected reflog.fuzzy
222 '
223
224 test_debug 'git log --graph --decorate --oneline --all'
225
226 test_expect_success 'merge c1 with c2' '
227         git reset --hard c1 &&
228         test_tick &&
229         git merge c2 &&
230         verify_merge file result.1-5 msg.1-5 &&
231         verify_parents $c1 $c2
232 '
233
234 test_expect_success 'merge --squash c3 with c7' '
235         git reset --hard c3 &&
236         test_must_fail git merge --squash c7 &&
237         cat result.9z >file &&
238         git commit --no-edit -a &&
239
240         cat >expect <<-EOF &&
241         Squashed commit of the following:
242
243         $(git show -s c7)
244
245         # Conflicts:
246         #       file
247         EOF
248         git cat-file commit HEAD >raw &&
249         sed -e "1,/^$/d" raw >actual &&
250         test_cmp expect actual
251 '
252
253 test_expect_success 'merge c3 with c7 with commit.cleanup = scissors' '
254         git config commit.cleanup scissors &&
255         git reset --hard c3 &&
256         test_must_fail git merge c7 &&
257         cat result.9z >file &&
258         git commit --no-edit -a &&
259
260         cat >expect <<-\EOF &&
261         Merge tag '"'"'c7'"'"'
262
263         # ------------------------ >8 ------------------------
264         # Do not modify or remove the line above.
265         # Everything below it will be ignored.
266         #
267         # Conflicts:
268         #       file
269         EOF
270         git cat-file commit HEAD >raw &&
271         sed -e "1,/^$/d" raw >actual &&
272         test_i18ncmp expect actual
273 '
274
275 test_expect_success 'merge c3 with c7 with --squash commit.cleanup = scissors' '
276         git config commit.cleanup scissors &&
277         git reset --hard c3 &&
278         test_must_fail git merge --squash c7 &&
279         cat result.9z >file &&
280         git commit --no-edit -a &&
281
282         cat >expect <<-EOF &&
283         Squashed commit of the following:
284
285         $(git show -s c7)
286
287         # ------------------------ >8 ------------------------
288         # Do not modify or remove the line above.
289         # Everything below it will be ignored.
290         #
291         # Conflicts:
292         #       file
293         EOF
294         git cat-file commit HEAD >raw &&
295         sed -e "1,/^$/d" raw >actual &&
296         test_i18ncmp expect actual
297 '
298
299 test_debug 'git log --graph --decorate --oneline --all'
300
301 test_expect_success 'merge c1 with c2 and c3' '
302         git reset --hard c1 &&
303         test_tick &&
304         git merge c2 c3 &&
305         verify_merge file result.1-5-9 msg.1-5-9 &&
306         verify_parents $c1 $c2 $c3
307 '
308
309 test_debug 'git log --graph --decorate --oneline --all'
310
311 test_expect_success 'merges with --ff-only' '
312         git reset --hard c1 &&
313         test_tick &&
314         test_must_fail git merge --ff-only c2 &&
315         test_must_fail git merge --ff-only c3 &&
316         test_must_fail git merge --ff-only c2 c3 &&
317         git reset --hard c0 &&
318         git merge c3 &&
319         verify_head $c3
320 '
321
322 test_expect_success 'merges with merge.ff=only' '
323         git reset --hard c1 &&
324         test_tick &&
325         test_config merge.ff "only" &&
326         test_must_fail git merge c2 &&
327         test_must_fail git merge c3 &&
328         test_must_fail git merge c2 c3 &&
329         git reset --hard c0 &&
330         git merge c3 &&
331         verify_head $c3
332 '
333
334 test_expect_success 'merge c0 with c1 (no-commit)' '
335         git reset --hard c0 &&
336         git merge --no-commit c1 &&
337         verify_merge file result.1 &&
338         verify_head $c1
339 '
340
341 test_debug 'git log --graph --decorate --oneline --all'
342
343 test_expect_success 'merge c1 with c2 (no-commit)' '
344         git reset --hard c1 &&
345         git merge --no-commit c2 &&
346         verify_merge file result.1-5 &&
347         verify_head $c1 &&
348         verify_mergeheads $c2
349 '
350
351 test_debug 'git log --graph --decorate --oneline --all'
352
353 test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
354         git reset --hard c1 &&
355         git merge --no-commit c2 c3 &&
356         verify_merge file result.1-5-9 &&
357         verify_head $c1 &&
358         verify_mergeheads $c2 $c3
359 '
360
361 test_debug 'git log --graph --decorate --oneline --all'
362
363 test_expect_success 'merge c0 with c1 (squash)' '
364         git reset --hard c0 &&
365         git merge --squash c1 &&
366         verify_merge file result.1 &&
367         verify_head $c0 &&
368         verify_no_mergehead &&
369         test_cmp squash.1 .git/SQUASH_MSG
370 '
371
372 test_debug 'git log --graph --decorate --oneline --all'
373
374 test_expect_success 'merge c0 with c1 (squash, ff-only)' '
375         git reset --hard c0 &&
376         git merge --squash --ff-only c1 &&
377         verify_merge file result.1 &&
378         verify_head $c0 &&
379         verify_no_mergehead &&
380         test_cmp squash.1 .git/SQUASH_MSG
381 '
382
383 test_debug 'git log --graph --decorate --oneline --all'
384
385 test_expect_success 'merge c1 with c2 (squash)' '
386         git reset --hard c1 &&
387         git merge --squash c2 &&
388         verify_merge file result.1-5 &&
389         verify_head $c1 &&
390         verify_no_mergehead &&
391         test_cmp squash.1-5 .git/SQUASH_MSG
392 '
393
394 test_debug 'git log --graph --decorate --oneline --all'
395
396 test_expect_success 'unsuccessful merge of c1 with c2 (squash, ff-only)' '
397         git reset --hard c1 &&
398         test_must_fail git merge --squash --ff-only c2
399 '
400
401 test_debug 'git log --graph --decorate --oneline --all'
402
403 test_expect_success 'merge c1 with c2 and c3 (squash)' '
404         git reset --hard c1 &&
405         git merge --squash c2 c3 &&
406         verify_merge file result.1-5-9 &&
407         verify_head $c1 &&
408         verify_no_mergehead &&
409         test_cmp squash.1-5-9 .git/SQUASH_MSG
410 '
411
412 test_debug 'git log --graph --decorate --oneline --all'
413
414 test_expect_success 'merge c1 with c2 (no-commit in config)' '
415         git reset --hard c1 &&
416         test_config branch.master.mergeoptions "--no-commit" &&
417         git merge c2 &&
418         verify_merge file result.1-5 &&
419         verify_head $c1 &&
420         verify_mergeheads $c2
421 '
422
423 test_debug 'git log --graph --decorate --oneline --all'
424
425 test_expect_success 'merge c1 with c2 (log in config)' '
426         git reset --hard c1 &&
427         git merge --log c2 &&
428         git show -s --pretty=tformat:%s%n%b >expect &&
429
430         test_config branch.master.mergeoptions "--log" &&
431         git reset --hard c1 &&
432         git merge c2 &&
433         git show -s --pretty=tformat:%s%n%b >actual &&
434
435         test_cmp expect actual
436 '
437
438 test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
439         git reset --hard c1 &&
440         git merge c2 &&
441         git show -s --pretty=tformat:%s%n%b >expect &&
442
443         test_config branch.master.mergeoptions "--no-log" &&
444         test_config merge.log "true" &&
445         git reset --hard c1 &&
446         git merge c2 &&
447         git show -s --pretty=tformat:%s%n%b >actual &&
448
449         test_cmp expect actual
450 '
451
452 test_expect_success 'merge c1 with c2 (squash in config)' '
453         git reset --hard c1 &&
454         test_config branch.master.mergeoptions "--squash" &&
455         git merge c2 &&
456         verify_merge file result.1-5 &&
457         verify_head $c1 &&
458         verify_no_mergehead &&
459         test_cmp squash.1-5 .git/SQUASH_MSG
460 '
461
462 test_debug 'git log --graph --decorate --oneline --all'
463
464 test_expect_success 'override config option -n with --summary' '
465         git reset --hard c1 &&
466         test_config branch.master.mergeoptions "-n" &&
467         test_tick &&
468         git merge --summary c2 >diffstat.txt &&
469         verify_merge file result.1-5 msg.1-5 &&
470         verify_parents $c1 $c2 &&
471         if ! grep "^ file |  *2 +-$" diffstat.txt
472         then
473                 echo "[OOPS] diffstat was not generated with --summary"
474                 false
475         fi
476 '
477
478 test_expect_success 'override config option -n with --stat' '
479         git reset --hard c1 &&
480         test_config branch.master.mergeoptions "-n" &&
481         test_tick &&
482         git merge --stat c2 >diffstat.txt &&
483         verify_merge file result.1-5 msg.1-5 &&
484         verify_parents $c1 $c2 &&
485         if ! grep "^ file |  *2 +-$" diffstat.txt
486         then
487                 echo "[OOPS] diffstat was not generated with --stat"
488                 false
489         fi
490 '
491
492 test_debug 'git log --graph --decorate --oneline --all'
493
494 test_expect_success 'override config option --stat' '
495         git reset --hard c1 &&
496         test_config branch.master.mergeoptions "--stat" &&
497         test_tick &&
498         git merge -n c2 >diffstat.txt &&
499         verify_merge file result.1-5 msg.1-5 &&
500         verify_parents $c1 $c2 &&
501         if grep "^ file |  *2 +-$" diffstat.txt
502         then
503                 echo "[OOPS] diffstat was generated"
504                 false
505         fi
506 '
507
508 test_debug 'git log --graph --decorate --oneline --all'
509
510 test_expect_success 'merge c1 with c2 (override --no-commit)' '
511         git reset --hard c1 &&
512         test_config branch.master.mergeoptions "--no-commit" &&
513         test_tick &&
514         git merge --commit c2 &&
515         verify_merge file result.1-5 msg.1-5 &&
516         verify_parents $c1 $c2
517 '
518
519 test_debug 'git log --graph --decorate --oneline --all'
520
521 test_expect_success 'merge c1 with c2 (override --squash)' '
522         git reset --hard c1 &&
523         test_config branch.master.mergeoptions "--squash" &&
524         test_tick &&
525         git merge --no-squash c2 &&
526         verify_merge file result.1-5 msg.1-5 &&
527         verify_parents $c1 $c2
528 '
529
530 test_debug 'git log --graph --decorate --oneline --all'
531
532 test_expect_success 'merge c0 with c1 (no-ff)' '
533         git reset --hard c0 &&
534         test_tick &&
535         git merge --no-ff c1 &&
536         verify_merge file result.1 &&
537         verify_parents $c0 $c1
538 '
539
540 test_debug 'git log --graph --decorate --oneline --all'
541
542 test_expect_success 'merge c0 with c1 (merge.ff=false)' '
543         git reset --hard c0 &&
544         test_config merge.ff "false" &&
545         test_tick &&
546         git merge c1 &&
547         verify_merge file result.1 &&
548         verify_parents $c0 $c1
549 '
550 test_debug 'git log --graph --decorate --oneline --all'
551
552 test_expect_success 'combine branch.master.mergeoptions with merge.ff' '
553         git reset --hard c0 &&
554         test_config branch.master.mergeoptions "--ff" &&
555         test_config merge.ff "false" &&
556         test_tick &&
557         git merge c1 &&
558         verify_merge file result.1 &&
559         verify_parents "$c0"
560 '
561
562 test_expect_success 'tolerate unknown values for merge.ff' '
563         git reset --hard c0 &&
564         test_config merge.ff "something-new" &&
565         test_tick &&
566         git merge c1 2>message &&
567         verify_head "$c1" &&
568         test_must_be_empty message
569 '
570
571 test_expect_success 'combining --squash and --no-ff is refused' '
572         git reset --hard c0 &&
573         test_must_fail git merge --squash --no-ff c1 &&
574         test_must_fail git merge --no-ff --squash c1
575 '
576
577 test_expect_success 'combining --squash and --commit is refused' '
578         git reset --hard c0 &&
579         test_must_fail git merge --squash --commit c1 &&
580         test_must_fail git merge --commit --squash c1
581 '
582
583 test_expect_success 'option --ff-only overwrites --no-ff' '
584         git merge --no-ff --ff-only c1 &&
585         test_must_fail git merge --no-ff --ff-only c2
586 '
587
588 test_expect_success 'option --no-ff overrides merge.ff=only config' '
589         git reset --hard c0 &&
590         test_config merge.ff only &&
591         git merge --no-ff c1
592 '
593
594 test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
595         git reset --hard c0 &&
596         test_config branch.master.mergeoptions "--no-ff" &&
597         git merge --ff c1 &&
598         verify_merge file result.1 &&
599         verify_head $c1
600 '
601
602 test_expect_success 'merge log message' '
603         git reset --hard c0 &&
604         git merge --no-log c2 &&
605         git show -s --pretty=format:%b HEAD >msg.act &&
606         test_must_be_empty msg.act &&
607
608         git reset --hard c0 &&
609         test_config branch.master.mergeoptions "--no-ff" &&
610         git merge --no-log c2 &&
611         git show -s --pretty=format:%b HEAD >msg.act &&
612         test_must_be_empty msg.act &&
613
614         git merge --log c3 &&
615         git show -s --pretty=format:%b HEAD >msg.act &&
616         test_cmp msg.log msg.act &&
617
618         git reset --hard HEAD^ &&
619         test_config merge.log "yes" &&
620         git merge c3 &&
621         git show -s --pretty=format:%b HEAD >msg.act &&
622         test_cmp msg.log msg.act
623 '
624
625 test_debug 'git log --graph --decorate --oneline --all'
626
627 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
628        git reset --hard c1 &&
629        test_tick &&
630        git merge c0 c2 c0 c1 &&
631        verify_merge file result.1-5 &&
632        verify_parents $c1 $c2
633 '
634
635 test_debug 'git log --graph --decorate --oneline --all'
636
637 test_expect_success 'merge c1 with c0, c2, c0, and c1' '
638        git reset --hard c1 &&
639        test_tick &&
640        git merge c0 c2 c0 c1 &&
641        verify_merge file result.1-5 &&
642        verify_parents $c1 $c2
643 '
644
645 test_debug 'git log --graph --decorate --oneline --all'
646
647 test_expect_success 'merge c1 with c1 and c2' '
648        git reset --hard c1 &&
649        test_tick &&
650        git merge c1 c2 &&
651        verify_merge file result.1-5 &&
652        verify_parents $c1 $c2
653 '
654
655 test_debug 'git log --graph --decorate --oneline --all'
656
657 test_expect_success 'merge fast-forward in a dirty tree' '
658        git reset --hard c0 &&
659        mv file file1 &&
660        cat file1 >file &&
661        rm -f file1 &&
662        git merge c2
663 '
664
665 test_debug 'git log --graph --decorate --oneline --all'
666
667 test_expect_success 'in-index merge' '
668         git reset --hard c0 &&
669         git merge --no-ff -s resolve c1 >out &&
670         test_i18ngrep "Wonderful." out &&
671         verify_parents $c0 $c1
672 '
673
674 test_debug 'git log --graph --decorate --oneline --all'
675
676 test_expect_success 'refresh the index before merging' '
677         git reset --hard c1 &&
678         cp file file.n && mv -f file.n file &&
679         git merge c3
680 '
681
682 test_expect_success 'merge with --autostash' '
683         git reset --hard c1 &&
684         git merge-file file file.orig file.9 &&
685         git merge --autostash c2 2>err &&
686         test_i18ngrep "Applied autostash." err &&
687         git show HEAD:file >merge-result &&
688         test_cmp result.1-5 merge-result &&
689         test_cmp result.1-5-9 file
690 '
691
692 test_expect_success 'merge with merge.autoStash' '
693         test_config merge.autoStash true &&
694         git reset --hard c1 &&
695         git merge-file file file.orig file.9 &&
696         git merge c2 2>err &&
697         test_i18ngrep "Applied autostash." err &&
698         git show HEAD:file >merge-result &&
699         test_cmp result.1-5 merge-result &&
700         test_cmp result.1-5-9 file
701 '
702
703 test_expect_success 'fast-forward merge with --autostash' '
704         git reset --hard c0 &&
705         git merge-file file file.orig file.5 &&
706         git merge --autostash c1 2>err &&
707         test_i18ngrep "Applied autostash." err &&
708         test_cmp result.1-5 file
709 '
710
711 test_expect_success 'octopus merge with --autostash' '
712         git reset --hard c1 &&
713         git merge-file file file.orig file.3 &&
714         git merge --autostash c2 c3 2>err &&
715         test_i18ngrep "Applied autostash." err &&
716         git show HEAD:file >merge-result &&
717         test_cmp result.1-5-9 merge-result &&
718         test_cmp result.1-3-5-9 file
719 '
720
721 test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
722         git reset --hard c3 &&
723         cp file.1 file &&
724         test_must_fail git merge --autostash c7 &&
725         git merge --abort 2>err &&
726         test_i18ngrep "Applied autostash." err &&
727         test_cmp file.1 file
728 '
729
730 test_expect_success 'completed merge (git commit) with --no-commit and --autostash' '
731         git reset --hard c1 &&
732         git merge-file file file.orig file.9 &&
733         git diff >expect &&
734         git merge --no-commit --autostash c2 &&
735         git stash show -p MERGE_AUTOSTASH >actual &&
736         test_cmp expect actual &&
737         git commit 2>err &&
738         test_i18ngrep "Applied autostash." err &&
739         git show HEAD:file >merge-result &&
740         test_cmp result.1-5 merge-result &&
741         test_cmp result.1-5-9 file
742 '
743
744 test_expect_success 'completed merge (git merge --continue) with --no-commit and --autostash' '
745         git reset --hard c1 &&
746         git merge-file file file.orig file.9 &&
747         git diff >expect &&
748         git merge --no-commit --autostash c2 &&
749         git stash show -p MERGE_AUTOSTASH >actual &&
750         test_cmp expect actual &&
751         git merge --continue 2>err &&
752         test_i18ngrep "Applied autostash." err &&
753         git show HEAD:file >merge-result &&
754         test_cmp result.1-5 merge-result &&
755         test_cmp result.1-5-9 file
756 '
757
758 test_expect_success 'aborted merge (merge --abort) with --no-commit and --autostash' '
759         git reset --hard c1 &&
760         git merge-file file file.orig file.9 &&
761         git diff >expect &&
762         git merge --no-commit --autostash c2 &&
763         git stash show -p MERGE_AUTOSTASH >actual &&
764         test_cmp expect actual &&
765         git merge --abort 2>err &&
766         test_i18ngrep "Applied autostash." err &&
767         git diff >actual &&
768         test_cmp expect actual
769 '
770
771 test_expect_success 'aborted merge (reset --hard) with --no-commit and --autostash' '
772         git reset --hard c1 &&
773         git merge-file file file.orig file.9 &&
774         git diff >expect &&
775         git merge --no-commit --autostash c2 &&
776         git stash show -p MERGE_AUTOSTASH >actual &&
777         test_cmp expect actual &&
778         git reset --hard 2>err &&
779         test_i18ngrep "Autostash exists; creating a new stash entry." err &&
780         git diff --exit-code
781 '
782
783 test_expect_success 'quit merge with --no-commit and --autostash' '
784         git reset --hard c1 &&
785         git merge-file file file.orig file.9 &&
786         git diff >expect &&
787         git merge --no-commit --autostash c2 &&
788         git stash show -p MERGE_AUTOSTASH >actual &&
789         test_cmp expect actual &&
790         git diff HEAD >expect &&
791         git merge --quit 2>err &&
792         test_i18ngrep "Autostash exists; creating a new stash entry." err &&
793         git diff HEAD >actual &&
794         test_cmp expect actual
795 '
796
797 test_expect_success 'merge with conflicted --autostash changes' '
798         git reset --hard c1 &&
799         git merge-file file file.orig file.9y &&
800         git diff >expect &&
801         test_when_finished "test_might_fail git stash drop" &&
802         git merge --autostash c3 2>err &&
803         test_i18ngrep "Applying autostash resulted in conflicts." err &&
804         git show HEAD:file >merge-result &&
805         test_cmp result.1-9 merge-result &&
806         git stash show -p >actual &&
807         test_cmp expect actual
808 '
809
810 cat >expected.branch <<\EOF
811 Merge branch 'c5-branch' (early part)
812 EOF
813 cat >expected.tag <<\EOF
814 Merge commit 'c5~1'
815 EOF
816
817 test_expect_success 'merge early part of c2' '
818         git reset --hard c3 &&
819         echo c4 >c4.c &&
820         git add c4.c &&
821         git commit -m c4 &&
822         git tag c4 &&
823         echo c5 >c5.c &&
824         git add c5.c &&
825         git commit -m c5 &&
826         git tag c5 &&
827         git reset --hard c3 &&
828         echo c6 >c6.c &&
829         git add c6.c &&
830         git commit -m c6 &&
831         git tag c6 &&
832         git branch -f c5-branch c5 &&
833         git merge c5-branch~1 &&
834         git show -s --pretty=tformat:%s HEAD >actual.branch &&
835         git reset --keep HEAD^ &&
836         git merge c5~1 &&
837         git show -s --pretty=tformat:%s HEAD >actual.tag &&
838         test_cmp expected.branch actual.branch &&
839         test_cmp expected.tag actual.tag
840 '
841
842 test_debug 'git log --graph --decorate --oneline --all'
843
844 test_expect_success 'merge --no-ff --no-commit && commit' '
845         git reset --hard c0 &&
846         git merge --no-ff --no-commit c1 &&
847         EDITOR=: git commit &&
848         verify_parents $c0 $c1
849 '
850
851 test_debug 'git log --graph --decorate --oneline --all'
852
853 test_expect_success 'amending no-ff merge commit' '
854         EDITOR=: git commit --amend &&
855         verify_parents $c0 $c1
856 '
857
858 test_debug 'git log --graph --decorate --oneline --all'
859
860 cat >editor <<\EOF
861 #!/bin/sh
862 # Add a new message string that was not in the template
863 (
864         echo "Merge work done on the side branch c1"
865         echo
866         cat "$1"
867 ) >"$1.tmp" && mv "$1.tmp" "$1"
868 # strip comments and blank lines from end of message
869 sed -e '/^#/d' "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' >expected
870 EOF
871 chmod 755 editor
872
873 test_expect_success 'merge --no-ff --edit' '
874         git reset --hard c0 &&
875         EDITOR=./editor git merge --no-ff --edit c1 &&
876         verify_parents $c0 $c1 &&
877         git cat-file commit HEAD >raw &&
878         grep "work done on the side branch" raw &&
879         sed "1,/^$/d" >actual raw &&
880         test_cmp expected actual
881 '
882
883 test_expect_success 'merge annotated/signed tag w/o tracking' '
884         test_when_finished "rm -rf dst; git tag -d anno1" &&
885         git tag -a -m "anno c1" anno1 c1 &&
886         git init dst &&
887         git rev-parse c1 >dst/expect &&
888         (
889                 # c0 fast-forwards to c1 but because this repository
890                 # is not a "downstream" whose refs/tags follows along
891                 # tag from the "upstream", this pull defaults to --no-ff
892                 cd dst &&
893                 git pull .. c0 &&
894                 git pull .. anno1 &&
895                 git rev-parse HEAD^2 >actual &&
896                 test_cmp expect actual
897         )
898 '
899
900 test_expect_success 'merge annotated/signed tag w/ tracking' '
901         test_when_finished "rm -rf dst; git tag -d anno1" &&
902         git tag -a -m "anno c1" anno1 c1 &&
903         git init dst &&
904         git rev-parse c1 >dst/expect &&
905         (
906                 # c0 fast-forwards to c1 and because this repository
907                 # is a "downstream" whose refs/tags follows along
908                 # tag from the "upstream", this pull defaults to --ff
909                 cd dst &&
910                 git remote add origin .. &&
911                 git pull origin c0 &&
912                 git fetch origin &&
913                 git merge anno1 &&
914                 git rev-parse HEAD >actual &&
915                 test_cmp expect actual
916         )
917 '
918
919 test_expect_success GPG 'merge --ff-only tag' '
920         git reset --hard c0 &&
921         git commit --allow-empty -m "A newer commit" &&
922         git tag -s -m "A newer commit" signed &&
923         git reset --hard c0 &&
924
925         git merge --ff-only signed &&
926         git rev-parse signed^0 >expect &&
927         git rev-parse HEAD >actual &&
928         test_cmp expect actual
929 '
930
931 test_expect_success GPG 'merge --no-edit tag should skip editor' '
932         git reset --hard c0 &&
933         git commit --allow-empty -m "A newer commit" &&
934         git tag -f -s -m "A newer commit" signed &&
935         git reset --hard c0 &&
936
937         EDITOR=false git merge --no-edit --no-ff signed &&
938         git rev-parse signed^0 >expect &&
939         git rev-parse HEAD^2 >actual &&
940         test_cmp expect actual
941 '
942
943 test_expect_success 'set up mod-256 conflict scenario' '
944         # 256 near-identical stanzas...
945         for i in $(test_seq 1 256); do
946                 for j in 1 2 3 4 5; do
947                         echo $i-$j
948                 done
949         done >file &&
950         git add file &&
951         git commit -m base &&
952
953         # one side changes the first line of each to "master"
954         sed s/-1/-master/ file >tmp &&
955         mv tmp file &&
956         git commit -am master &&
957
958         # and the other to "side"; merging the two will
959         # yield 256 separate conflicts
960         git checkout -b side HEAD^ &&
961         sed s/-1/-side/ file >tmp &&
962         mv tmp file &&
963         git commit -am side
964 '
965
966 test_expect_success 'merge detects mod-256 conflicts (recursive)' '
967         git reset --hard &&
968         test_must_fail git merge -s recursive master
969 '
970
971 test_expect_success 'merge detects mod-256 conflicts (resolve)' '
972         git reset --hard &&
973         test_must_fail git merge -s resolve master
974 '
975
976 test_expect_success 'merge nothing into void' '
977         git init void &&
978         (
979                 cd void &&
980                 git remote add up .. &&
981                 git fetch up &&
982                 test_must_fail git merge FETCH_HEAD
983         )
984 '
985
986 test_expect_success 'merge can be completed with --continue' '
987         git reset --hard c0 &&
988         git merge --no-ff --no-commit c1 &&
989         git merge --continue &&
990         verify_parents $c0 $c1
991 '
992
993 write_script .git/FAKE_EDITOR <<EOF
994 # kill -TERM command added below.
995 EOF
996
997 test_expect_success EXECKEEPSPID 'killed merge can be completed with --continue' '
998         git reset --hard c0 &&
999         ! "$SHELL_PATH" -c '\''
1000           echo kill -TERM $$ >>.git/FAKE_EDITOR
1001           GIT_EDITOR=.git/FAKE_EDITOR
1002           export GIT_EDITOR
1003           exec git merge --no-ff --edit c1'\'' &&
1004         git merge --continue &&
1005         verify_parents $c0 $c1
1006 '
1007
1008 test_expect_success 'merge --quit' '
1009         git init merge-quit &&
1010         (
1011                 cd merge-quit &&
1012                 test_commit base &&
1013                 echo one >>base.t &&
1014                 git commit -am one &&
1015                 git branch one &&
1016                 git checkout base &&
1017                 echo two >>base.t &&
1018                 git commit -am two &&
1019                 test_must_fail git -c rerere.enabled=true merge one &&
1020                 test_path_is_file .git/MERGE_HEAD &&
1021                 test_path_is_file .git/MERGE_MODE &&
1022                 test_path_is_file .git/MERGE_MSG &&
1023                 git rerere status >rerere.before &&
1024                 git merge --quit &&
1025                 test_path_is_missing .git/MERGE_HEAD &&
1026                 test_path_is_missing .git/MERGE_MODE &&
1027                 test_path_is_missing .git/MERGE_MSG &&
1028                 git rerere status >rerere.after &&
1029                 test_must_be_empty rerere.after &&
1030                 ! test_cmp rerere.after rerere.before
1031         )
1032 '
1033
1034 test_expect_success 'merge suggests matching remote refname' '
1035         git commit --allow-empty -m not-local &&
1036         git update-ref refs/remotes/origin/not-local HEAD &&
1037         git reset --hard HEAD^ &&
1038
1039         # This is white-box testing hackery; we happen to know
1040         # that reading packed refs is more picky about the memory
1041         # ownership of strings we pass to for_each_ref() callbacks.
1042         git pack-refs --all --prune &&
1043
1044         test_must_fail git merge not-local 2>stderr &&
1045         grep origin/not-local stderr
1046 '
1047
1048 test_expect_success 'suggested names are not ambiguous' '
1049         git update-ref refs/heads/origin/not-local HEAD &&
1050         test_must_fail git merge not-local 2>stderr &&
1051         grep remotes/origin/not-local stderr
1052 '
1053
1054 test_done