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}
109
110verify_diff() {
111 if ! diff -u "$1" "$2"
112 then
113 echo "$3"
114 false
115 fi
116}
117
118verify_merge() {
119 verify_diff "$2" "$1" "[OOPS] bad merge result" &&
120 if test $(git ls-files -u | wc -l) -gt 0
121 then
122 echo "[OOPS] unmerged files"
123 false
124 fi &&
125 if ! git diff --exit-code
126 then
127 echo "[OOPS] working tree != index"
128 false
129 fi &&
130 if test -n "$3"
131 then
132 git show -s --pretty=format:%s HEAD >msg.act &&
133 verify_diff "$3" msg.act "[OOPS] bad merge message"
134 fi
135}
136
137verify_head() {
138 if test "$1" != "$(git rev-parse HEAD)"
139 then
140 echo "[OOPS] HEAD != $1"
141 false
142 fi
143}
144
145verify_parents() {
146 i=1
147 while test $# -gt 0
148 do
149 if test "$1" != "$(git rev-parse HEAD^$i)"
150 then
151 echo "[OOPS] HEAD^$i != $1"
152 return 1
153 fi
154 i=$(expr $i + 1)
155 shift
156 done
157}
158
159verify_mergeheads() {
160 i=1
161 if ! test -f .git/MERGE_HEAD
162 then
163 echo "[OOPS] MERGE_HEAD is missing"
164 false
165 fi &&
166 while test $# -gt 0
167 do
168 head=$(head -n $i .git/MERGE_HEAD | tail -n 1)
169 if test "$1" != "$head"
170 then
171 echo "[OOPS] MERGE_HEAD $i != $1"
172 return 1
173 fi
174 i=$(expr $i + 1)
175 shift
176 done
177}
178
179verify_no_mergehead() {
180 if test -f .git/MERGE_HEAD
181 then
182 echo "[OOPS] MERGE_HEAD exists"
183 false
184 fi
185}
186
187
188test_expect_success 'setup' '
189 git add file &&
190 test_tick &&
191 git commit -m "commit 0" &&
192 git tag c0 &&
193 c0=$(git rev-parse HEAD) &&
194 cp file.1 file &&
195 git add file &&
196 test_tick &&
197 git commit -m "commit 1" &&
198 git tag c1 &&
199 c1=$(git rev-parse HEAD) &&
200 git reset --hard "$c0" &&
201 cp file.5 file &&
202 git add file &&
203 test_tick &&
204 git commit -m "commit 2" &&
205 git tag c2 &&
206 c2=$(git rev-parse HEAD) &&
207 git reset --hard "$c0" &&
208 cp file.9 file &&
209 git add file &&
210 test_tick &&
211 git commit -m "commit 3" &&
212 git tag c3 &&
213 c3=$(git rev-parse HEAD)
214 git reset --hard "$c0" &&
215 create_merge_msgs
216'
217
218test_debug 'gitk --all'
219
220test_expect_success 'test option parsing' '
221 if git merge -$ c1
222 then
223 echo "[OOPS] -$ accepted"
224 false
225 fi &&
226 if git merge --no-such c1
227 then
228 echo "[OOPS] --no-such accepted"
229 false
230 fi &&
231 if git merge -s foobar c1
232 then
233 echo "[OOPS] -s foobar accepted"
234 false
235 fi &&
236 if git merge -s=foobar c1
237 then
238 echo "[OOPS] -s=foobar accepted"
239 false
240 fi &&
241 if git merge -m
242 then
243 echo "[OOPS] missing commit msg accepted"
244 false
245 fi &&
246 if git merge
247 then
248 echo "[OOPS] missing commit references accepted"
249 false
250 fi
251'
252
253test_expect_success 'merge c0 with c1' '
254 git reset --hard c0 &&
255 git merge c1 &&
256 verify_merge file result.1 &&
257 verify_head "$c1"
258'
259
260test_debug 'gitk --all'
261
262test_expect_success 'merge c1 with c2' '
263 git reset --hard c1 &&
264 test_tick &&
265 git merge c2 &&
266 verify_merge file result.1-5 msg.1-5 &&
267 verify_parents $c1 $c2
268'
269
270test_debug 'gitk --all'
271
272test_expect_success 'merge c1 with c2 and c3' '
273 git reset --hard c1 &&
274 test_tick &&
275 git merge c2 c3 &&
276 verify_merge file result.1-5-9 msg.1-5-9 &&
277 verify_parents $c1 $c2 $c3
278'
279
280test_debug 'gitk --all'
281
282test_expect_success 'merge c0 with c1 (no-commit)' '
283 git reset --hard c0 &&
284 git merge --no-commit c1 &&
285 verify_merge file result.1 &&
286 verify_head $c1
287'
288
289test_debug 'gitk --all'
290
291test_expect_success 'merge c1 with c2 (no-commit)' '
292 git reset --hard c1 &&
293 git merge --no-commit c2 &&
294 verify_merge file result.1-5 &&
295 verify_head $c1 &&
296 verify_mergeheads $c2
297'
298
299test_debug 'gitk --all'
300
301test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
302 git reset --hard c1 &&
303 git merge --no-commit c2 c3 &&
304 verify_merge file result.1-5-9 &&
305 verify_head $c1 &&
306 verify_mergeheads $c2 $c3
307'
308
309test_debug 'gitk --all'
310
311test_expect_success 'merge c0 with c1 (squash)' '
312 git reset --hard c0 &&
313 git merge --squash c1 &&
314 verify_merge file result.1 &&
315 verify_head $c0 &&
316 verify_no_mergehead &&
317 verify_diff squash.1 .git/SQUASH_MSG "[OOPS] bad squash message"
318'
319
320test_debug 'gitk --all'
321
322test_expect_success 'merge c1 with c2 (squash)' '
323 git reset --hard c1 &&
324 git merge --squash c2 &&
325 verify_merge file result.1-5 &&
326 verify_head $c1 &&
327 verify_no_mergehead &&
328 verify_diff squash.1-5 .git/SQUASH_MSG "[OOPS] bad squash message"
329'
330
331test_debug 'gitk --all'
332
333test_expect_success 'merge c1 with c2 and c3 (squash)' '
334 git reset --hard c1 &&
335 git merge --squash c2 c3 &&
336 verify_merge file result.1-5-9 &&
337 verify_head $c1 &&
338 verify_no_mergehead &&
339 verify_diff squash.1-5-9 .git/SQUASH_MSG "[OOPS] bad squash message"
340'
341
342test_debug 'gitk --all'
343
344test_expect_success 'merge c1 with c2 (no-commit in config)' '
345 git reset --hard c1 &&
346 git config branch.master.mergeoptions "--no-commit" &&
347 git merge c2 &&
348 verify_merge file result.1-5 &&
349 verify_head $c1 &&
350 verify_mergeheads $c2
351'
352
353test_debug 'gitk --all'
354
355test_expect_success 'merge c1 with c2 (squash in config)' '
356 git reset --hard c1 &&
357 git config branch.master.mergeoptions "--squash" &&
358 git merge c2 &&
359 verify_merge file result.1-5 &&
360 verify_head $c1 &&
361 verify_no_mergehead &&
362 verify_diff squash.1-5 .git/SQUASH_MSG "[OOPS] bad squash message"
363'
364
365test_debug 'gitk --all'
366
367test_expect_success 'override config option -n' '
368 git reset --hard c1 &&
369 git config branch.master.mergeoptions "-n" &&
370 test_tick &&
371 git merge --summary c2 >diffstat.txt &&
372 verify_merge file result.1-5 msg.1-5 &&
373 verify_parents $c1 $c2 &&
374 if ! grep -e "^ file | *2 +-$" diffstat.txt
375 then
376 echo "[OOPS] diffstat was not generated"
377 fi
378'
379
380test_debug 'gitk --all'
381
382test_expect_success 'override config option --summary' '
383 git reset --hard c1 &&
384 git config branch.master.mergeoptions "--summary" &&
385 test_tick &&
386 git merge -n c2 >diffstat.txt &&
387 verify_merge file result.1-5 msg.1-5 &&
388 verify_parents $c1 $c2 &&
389 if grep -e "^ file | *2 +-$" diffstat.txt
390 then
391 echo "[OOPS] diffstat was generated"
392 false
393 fi
394'
395
396test_debug 'gitk --all'
397
398test_expect_success 'merge c1 with c2 (override --no-commit)' '
399 git reset --hard c1 &&
400 git config branch.master.mergeoptions "--no-commit" &&
401 test_tick &&
402 git merge --commit c2 &&
403 verify_merge file result.1-5 msg.1-5 &&
404 verify_parents $c1 $c2
405'
406
407test_debug 'gitk --all'
408
409test_expect_success 'merge c1 with c2 (override --squash)' '
410 git reset --hard c1 &&
411 git config branch.master.mergeoptions "--squash" &&
412 test_tick &&
413 git merge --no-squash c2 &&
414 verify_merge file result.1-5 msg.1-5 &&
415 verify_parents $c1 $c2
416'
417
418test_debug 'gitk --all'
419
420test_expect_success 'merge c0 with c1 (no-ff)' '
421 git reset --hard c0 &&
422 git config branch.master.mergeoptions "" &&
423 test_tick &&
424 git merge --no-ff c1 &&
425 verify_merge file result.1 &&
426 verify_parents $c0 $c1
427'
428
429test_debug 'gitk --all'
430
431test_expect_success 'combining --squash and --no-ff is refused' '
432 test_must_fail git merge --squash --no-ff c1 &&
433 test_must_fail git merge --no-ff --squash c1
434'
435
436test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
437 git reset --hard c0 &&
438 git config branch.master.mergeoptions "--no-ff" &&
439 git merge --ff c1 &&
440 verify_merge file result.1 &&
441 verify_head $c1
442'
443
444test_debug 'gitk --all'
445
446test_done