Code

tests: do not use implicit "git diff --no-index"
[git.git] / t / t7600-merge.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Lars Hjemli
4 #
6 test_description='git-merge
8 Testing basic merge operations/option parsing.'
10 . ./test-lib.sh
12 cat >file <<EOF
13 1
14 2
15 3
16 4
17 5
18 6
19 7
20 8
21 9
22 EOF
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
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
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
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
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
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
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
114 verify_diff() {
115         if ! test_cmp "$1" "$2"
116         then
117                 echo "$3"
118                 false
119         fi
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
141 verify_head() {
142         if test "$1" != "$(git rev-parse HEAD)"
143         then
144                 echo "[OOPS] HEAD != $1"
145                 false
146         fi
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
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
183 verify_no_mergehead() {
184         if test -f .git/MERGE_HEAD
185         then
186                 echo "[OOPS] MERGE_HEAD exists"
187                 false
188         fi
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
222 test_debug 'gitk --all'
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
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"
264 test_debug 'gitk --all'
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
274 test_debug 'gitk --all'
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
284 test_debug 'gitk --all'
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
293 test_debug 'gitk --all'
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
303 test_debug 'gitk --all'
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
313 test_debug 'gitk --all'
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"
324 test_debug 'gitk --all'
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"
335 test_debug 'gitk --all'
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"
346 test_debug 'gitk --all'
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
357 test_debug 'gitk --all'
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"
369 test_debug 'gitk --all'
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
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
399 test_debug 'gitk --all'
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
415 test_debug 'gitk --all'
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
426 test_debug 'gitk --all'
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
437 test_debug 'gitk --all'
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
448 test_debug 'gitk --all'
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
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
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         git merge --log c3 &&
469         git show -s --pretty=format:%b HEAD >msg.act &&
470         verify_diff msg.log msg.act "[OOPS] bad merge log message"
473 test_debug 'gitk --all'
475 test_done