]>
Commit | Line | Data |
---|---|---|
a85d1b69 LH |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2007 Lars Hjemli | |
4 | # | |
5 | ||
47a528ad | 6 | test_description='git merge |
a85d1b69 LH |
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 && | |
efb779f8 SG |
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 | |
a85d1b69 LH |
112 | } |
113 | ||
114 | verify_diff() { | |
82ebb0b6 | 115 | if ! test_cmp "$1" "$2" |
a85d1b69 LH |
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 && | |
d492b31c | 129 | if test_must_fail git diff --exit-code |
a85d1b69 LH |
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 | |
b4ce54fc | 172 | head=$(head -n $i .git/MERGE_HEAD | sed -ne \$p) |
a85d1b69 LH |
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 | ||
74f5b7fb | 224 | test_expect_success 'test option parsing' ' |
01941bd5 JS |
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 | |
a85d1b69 LH |
231 | ' |
232 | ||
dce61e72 MV |
233 | test_expect_success 'reject non-strategy with a git-merge-foo name' ' |
234 | test_must_fail git merge -s index c1 | |
235 | ' | |
236 | ||
a85d1b69 LH |
237 | test_expect_success 'merge c0 with c1' ' |
238 | git reset --hard c0 && | |
239 | git merge c1 && | |
240 | verify_merge file result.1 && | |
241 | verify_head "$c1" | |
242 | ' | |
243 | ||
244 | test_debug 'gitk --all' | |
245 | ||
246 | test_expect_success 'merge c1 with c2' ' | |
247 | git reset --hard c1 && | |
248 | test_tick && | |
249 | git merge c2 && | |
250 | verify_merge file result.1-5 msg.1-5 && | |
251 | verify_parents $c1 $c2 | |
252 | ' | |
253 | ||
254 | test_debug 'gitk --all' | |
255 | ||
256 | test_expect_success 'merge c1 with c2 and c3' ' | |
257 | git reset --hard c1 && | |
258 | test_tick && | |
259 | git merge c2 c3 && | |
260 | verify_merge file result.1-5-9 msg.1-5-9 && | |
261 | verify_parents $c1 $c2 $c3 | |
262 | ' | |
263 | ||
264 | test_debug 'gitk --all' | |
265 | ||
266 | test_expect_success 'merge c0 with c1 (no-commit)' ' | |
267 | git reset --hard c0 && | |
268 | git merge --no-commit c1 && | |
269 | verify_merge file result.1 && | |
270 | verify_head $c1 | |
271 | ' | |
272 | ||
273 | test_debug 'gitk --all' | |
274 | ||
275 | test_expect_success 'merge c1 with c2 (no-commit)' ' | |
276 | git reset --hard c1 && | |
277 | git merge --no-commit c2 && | |
278 | verify_merge file result.1-5 && | |
279 | verify_head $c1 && | |
280 | verify_mergeheads $c2 | |
281 | ' | |
282 | ||
283 | test_debug 'gitk --all' | |
284 | ||
285 | test_expect_success 'merge c1 with c2 and c3 (no-commit)' ' | |
286 | git reset --hard c1 && | |
287 | git merge --no-commit c2 c3 && | |
288 | verify_merge file result.1-5-9 && | |
289 | verify_head $c1 && | |
290 | verify_mergeheads $c2 $c3 | |
291 | ' | |
292 | ||
293 | test_debug 'gitk --all' | |
294 | ||
295 | test_expect_success 'merge c0 with c1 (squash)' ' | |
296 | git reset --hard c0 && | |
297 | git merge --squash c1 && | |
298 | verify_merge file result.1 && | |
299 | verify_head $c0 && | |
300 | verify_no_mergehead && | |
301 | verify_diff squash.1 .git/SQUASH_MSG "[OOPS] bad squash message" | |
302 | ' | |
303 | ||
304 | test_debug 'gitk --all' | |
305 | ||
306 | test_expect_success 'merge c1 with c2 (squash)' ' | |
307 | git reset --hard c1 && | |
308 | git merge --squash c2 && | |
309 | verify_merge file result.1-5 && | |
310 | verify_head $c1 && | |
311 | verify_no_mergehead && | |
312 | verify_diff squash.1-5 .git/SQUASH_MSG "[OOPS] bad squash message" | |
313 | ' | |
314 | ||
315 | test_debug 'gitk --all' | |
316 | ||
317 | test_expect_success 'merge c1 with c2 and c3 (squash)' ' | |
318 | git reset --hard c1 && | |
319 | git merge --squash c2 c3 && | |
320 | verify_merge file result.1-5-9 && | |
321 | verify_head $c1 && | |
322 | verify_no_mergehead && | |
323 | verify_diff squash.1-5-9 .git/SQUASH_MSG "[OOPS] bad squash message" | |
324 | ' | |
325 | ||
326 | test_debug 'gitk --all' | |
327 | ||
aec7b362 LH |
328 | test_expect_success 'merge c1 with c2 (no-commit in config)' ' |
329 | git reset --hard c1 && | |
330 | git config branch.master.mergeoptions "--no-commit" && | |
331 | git merge c2 && | |
332 | verify_merge file result.1-5 && | |
333 | verify_head $c1 && | |
334 | verify_mergeheads $c2 | |
335 | ' | |
336 | ||
337 | test_debug 'gitk --all' | |
338 | ||
339 | test_expect_success 'merge c1 with c2 (squash in config)' ' | |
340 | git reset --hard c1 && | |
341 | git config branch.master.mergeoptions "--squash" && | |
342 | git merge c2 && | |
343 | verify_merge file result.1-5 && | |
344 | verify_head $c1 && | |
345 | verify_no_mergehead && | |
346 | verify_diff squash.1-5 .git/SQUASH_MSG "[OOPS] bad squash message" | |
347 | ' | |
348 | ||
349 | test_debug 'gitk --all' | |
350 | ||
d8abe148 | 351 | test_expect_success 'override config option -n with --summary' ' |
aec7b362 LH |
352 | git reset --hard c1 && |
353 | git config branch.master.mergeoptions "-n" && | |
354 | test_tick && | |
355 | git merge --summary c2 >diffstat.txt && | |
356 | verify_merge file result.1-5 msg.1-5 && | |
357 | verify_parents $c1 $c2 && | |
aadbe44f | 358 | if ! grep "^ file | *2 +-$" diffstat.txt |
aec7b362 | 359 | then |
d8abe148 SG |
360 | echo "[OOPS] diffstat was not generated with --summary" |
361 | false | |
362 | fi | |
363 | ' | |
364 | ||
365 | test_expect_success 'override config option -n with --stat' ' | |
366 | git reset --hard c1 && | |
367 | git config branch.master.mergeoptions "-n" && | |
368 | test_tick && | |
369 | git merge --stat c2 >diffstat.txt && | |
370 | verify_merge file result.1-5 msg.1-5 && | |
371 | verify_parents $c1 $c2 && | |
372 | if ! grep "^ file | *2 +-$" diffstat.txt | |
373 | then | |
374 | echo "[OOPS] diffstat was not generated with --stat" | |
375 | false | |
aec7b362 LH |
376 | fi |
377 | ' | |
378 | ||
379 | test_debug 'gitk --all' | |
380 | ||
d8abe148 | 381 | test_expect_success 'override config option --stat' ' |
aec7b362 | 382 | git reset --hard c1 && |
d8abe148 | 383 | git config branch.master.mergeoptions "--stat" && |
aec7b362 LH |
384 | test_tick && |
385 | git merge -n c2 >diffstat.txt && | |
386 | verify_merge file result.1-5 msg.1-5 && | |
387 | verify_parents $c1 $c2 && | |
aadbe44f | 388 | if grep "^ file | *2 +-$" diffstat.txt |
aec7b362 LH |
389 | then |
390 | echo "[OOPS] diffstat was generated" | |
391 | false | |
392 | fi | |
393 | ' | |
394 | ||
395 | test_debug 'gitk --all' | |
396 | ||
d08af0ad LH |
397 | test_expect_success 'merge c1 with c2 (override --no-commit)' ' |
398 | git reset --hard c1 && | |
399 | git config branch.master.mergeoptions "--no-commit" && | |
400 | test_tick && | |
401 | git merge --commit c2 && | |
402 | verify_merge file result.1-5 msg.1-5 && | |
403 | verify_parents $c1 $c2 | |
404 | ' | |
405 | ||
406 | test_debug 'gitk --all' | |
407 | ||
408 | test_expect_success 'merge c1 with c2 (override --squash)' ' | |
409 | git reset --hard c1 && | |
410 | git config branch.master.mergeoptions "--squash" && | |
411 | test_tick && | |
412 | git merge --no-squash c2 && | |
413 | verify_merge file result.1-5 msg.1-5 && | |
414 | verify_parents $c1 $c2 | |
415 | ' | |
416 | ||
417 | test_debug 'gitk --all' | |
418 | ||
d66424c4 LH |
419 | test_expect_success 'merge c0 with c1 (no-ff)' ' |
420 | git reset --hard c0 && | |
e6d1f76c | 421 | git config branch.master.mergeoptions "" && |
d66424c4 LH |
422 | test_tick && |
423 | git merge --no-ff c1 && | |
424 | verify_merge file result.1 && | |
425 | verify_parents $c0 $c1 | |
426 | ' | |
427 | ||
428 | test_debug 'gitk --all' | |
429 | ||
e6d1f76c GP |
430 | test_expect_success 'combining --squash and --no-ff is refused' ' |
431 | test_must_fail git merge --squash --no-ff c1 && | |
432 | test_must_fail git merge --no-ff --squash c1 | |
433 | ' | |
434 | ||
d66424c4 LH |
435 | test_expect_success 'merge c0 with c1 (ff overrides no-ff)' ' |
436 | git reset --hard c0 && | |
437 | git config branch.master.mergeoptions "--no-ff" && | |
438 | git merge --ff c1 && | |
439 | verify_merge file result.1 && | |
440 | verify_head $c1 | |
441 | ' | |
442 | ||
efb779f8 SG |
443 | test_expect_success 'merge log message' ' |
444 | git reset --hard c0 && | |
445 | git merge --no-log c2 && | |
446 | git show -s --pretty=format:%b HEAD >msg.act && | |
447 | verify_diff msg.nolog msg.act "[OOPS] bad merge log message" && | |
4393c237 | 448 | |
efb779f8 SG |
449 | git merge --log c3 && |
450 | git show -s --pretty=format:%b HEAD >msg.act && | |
4393c237 JH |
451 | verify_diff msg.log msg.act "[OOPS] bad merge log message" && |
452 | ||
453 | git reset --hard HEAD^ && | |
454 | git config merge.log yes && | |
455 | git merge c3 && | |
456 | git show -s --pretty=format:%b HEAD >msg.act && | |
efb779f8 SG |
457 | verify_diff msg.log msg.act "[OOPS] bad merge log message" |
458 | ' | |
459 | ||
d66424c4 LH |
460 | test_debug 'gitk --all' |
461 | ||
3d1dd472 SHJ |
462 | test_expect_success 'merge c1 with c0, c2, c0, and c1' ' |
463 | git reset --hard c1 && | |
464 | git config branch.master.mergeoptions "" && | |
465 | test_tick && | |
466 | git merge c0 c2 c0 c1 && | |
467 | verify_merge file result.1-5 && | |
468 | verify_parents $c1 $c2 | |
469 | ' | |
470 | ||
471 | test_debug 'gitk --all' | |
472 | ||
711f6b29 JH |
473 | test_expect_success 'merge c1 with c0, c2, c0, and c1' ' |
474 | git reset --hard c1 && | |
475 | git config branch.master.mergeoptions "" && | |
476 | test_tick && | |
477 | git merge c0 c2 c0 c1 && | |
478 | verify_merge file result.1-5 && | |
479 | verify_parents $c1 $c2 | |
480 | ' | |
481 | ||
482 | test_debug 'gitk --all' | |
483 | ||
484 | test_expect_success 'merge c1 with c1 and c2' ' | |
485 | git reset --hard c1 && | |
486 | git config branch.master.mergeoptions "" && | |
487 | test_tick && | |
488 | git merge c1 c2 && | |
489 | verify_merge file result.1-5 && | |
490 | verify_parents $c1 $c2 | |
491 | ' | |
492 | ||
493 | test_debug 'gitk --all' | |
494 | ||
9ca8f607 JH |
495 | test_expect_success 'merge fast-forward in a dirty tree' ' |
496 | git reset --hard c0 && | |
497 | mv file file1 && | |
498 | cat file1 >file && | |
499 | rm -f file1 && | |
500 | git merge c2 | |
501 | ' | |
502 | ||
503 | test_debug 'gitk --all' | |
504 | ||
446247db JH |
505 | test_expect_success 'in-index merge' ' |
506 | git reset --hard c0 && | |
507 | git merge --no-ff -s resolve c1 > out && | |
508 | grep "Wonderful." out && | |
509 | verify_parents $c0 $c1 | |
510 | ' | |
511 | ||
512 | test_debug 'gitk --all' | |
513 | ||
668f26ff MV |
514 | test_expect_success 'refresh the index before merging' ' |
515 | git reset --hard c1 && | |
516 | sleep 1 && | |
517 | touch file && | |
518 | git merge c3 | |
519 | ' | |
520 | ||
4e6d4bc0 MV |
521 | cat >expected <<EOF |
522 | Merge branch 'c5' (early part) | |
523 | EOF | |
524 | ||
525 | test_expect_success 'merge early part of c2' ' | |
526 | git reset --hard c3 && | |
527 | echo c4 > c4.c && | |
528 | git add c4.c && | |
529 | git commit -m c4 && | |
530 | git tag c4 && | |
531 | echo c5 > c5.c && | |
532 | git add c5.c && | |
533 | git commit -m c5 && | |
534 | git tag c5 && | |
535 | git reset --hard c3 && | |
536 | echo c6 > c6.c && | |
537 | git add c6.c && | |
538 | git commit -m c6 && | |
539 | git tag c6 && | |
540 | git merge c5~1 && | |
541 | git show -s --pretty=format:%s HEAD > actual && | |
542 | test_cmp actual expected | |
543 | ' | |
544 | ||
668f26ff MV |
545 | test_debug 'gitk --all' |
546 | ||
cf10f9fd MV |
547 | test_expect_success 'merge --no-ff --no-commit && commit' ' |
548 | git reset --hard c0 && | |
549 | git merge --no-ff --no-commit c1 && | |
550 | EDITOR=: git commit && | |
551 | verify_parents $c0 $c1 | |
552 | ' | |
553 | ||
554 | test_debug 'gitk --all' | |
555 | ||
556 | test_expect_success 'amending no-ff merge commit' ' | |
557 | EDITOR=: git commit --amend && | |
558 | verify_parents $c0 $c1 | |
559 | ' | |
560 | ||
561 | test_debug 'gitk --all' | |
562 | ||
a85d1b69 | 563 | test_done |