3 test_description='merge-recursive backend test'
7 test_expect_success 'setup 1' '
10 o0=$(git hash-object a) &&
18 git commit -m initial &&
19 c0=$(git rev-parse --verify HEAD) &&
28 git branch rename-ln &&
32 o1=$(git hash-object a) &&
37 git commit -m "master modifies a and d/e" &&
38 c1=$(git rev-parse --verify HEAD) &&
39 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
41 echo "100644 blob $o1 a" &&
42 echo "100644 blob $o0 b" &&
43 echo "100644 blob $o0 c" &&
44 echo "100644 blob $o1 d/e" &&
45 echo "100644 $o1 0 a" &&
46 echo "100644 $o0 0 b" &&
47 echo "100644 $o0 0 c" &&
48 echo "100644 $o1 0 d/e"
50 test_cmp expected actual
53 test_expect_success 'setup 2' '
57 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
59 echo "100644 blob $o0 a" &&
60 echo "100644 blob $o0 b" &&
61 echo "100644 blob $o0 c" &&
62 echo "100644 blob $o0 d/e" &&
63 echo "100644 $o0 0 a" &&
64 echo "100644 $o0 0 b" &&
65 echo "100644 $o0 0 c" &&
66 echo "100644 $o0 0 d/e"
68 test_cmp expected actual &&
71 o2=$(git hash-object a) &&
76 git commit -m "side modifies a" &&
77 c2=$(git rev-parse --verify HEAD) &&
78 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
80 echo "100644 blob $o2 a" &&
81 echo "100644 blob $o0 b" &&
82 echo "100644 blob $o0 c" &&
83 echo "100644 blob $o0 d/e" &&
84 echo "100644 $o2 0 a" &&
85 echo "100644 $o0 0 b" &&
86 echo "100644 $o0 0 c" &&
87 echo "100644 $o0 0 d/e"
89 test_cmp expected actual
92 test_expect_success 'setup 3' '
96 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
98 echo "100644 blob $o0 a" &&
99 echo "100644 blob $o0 b" &&
100 echo "100644 blob $o0 c" &&
101 echo "100644 blob $o0 d/e" &&
102 echo "100644 $o0 0 a" &&
103 echo "100644 $o0 0 b" &&
104 echo "100644 $o0 0 c" &&
105 echo "100644 $o0 0 d/e"
107 test_cmp expected actual &&
109 rm -f b && mkdir b && echo df-1 >b/c && git add b/c &&
110 o3=$(git hash-object b/c) &&
113 git commit -m "df-1 makes b/c" &&
114 c3=$(git rev-parse --verify HEAD) &&
115 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
117 echo "100644 blob $o0 a" &&
118 echo "100644 blob $o3 b/c" &&
119 echo "100644 blob $o0 c" &&
120 echo "100644 blob $o0 d/e" &&
121 echo "100644 $o0 0 a" &&
122 echo "100644 $o3 0 b/c" &&
123 echo "100644 $o0 0 c" &&
124 echo "100644 $o0 0 d/e"
126 test_cmp expected actual
129 test_expect_success 'setup 4' '
133 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
135 echo "100644 blob $o0 a" &&
136 echo "100644 blob $o0 b" &&
137 echo "100644 blob $o0 c" &&
138 echo "100644 blob $o0 d/e" &&
139 echo "100644 $o0 0 a" &&
140 echo "100644 $o0 0 b" &&
141 echo "100644 $o0 0 c" &&
142 echo "100644 $o0 0 d/e"
144 test_cmp expected actual &&
146 rm -f a && mkdir a && echo df-2 >a/c && git add a/c &&
147 o4=$(git hash-object a/c) &&
150 git commit -m "df-2 makes a/c" &&
151 c4=$(git rev-parse --verify HEAD) &&
152 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
154 echo "100644 blob $o4 a/c" &&
155 echo "100644 blob $o0 b" &&
156 echo "100644 blob $o0 c" &&
157 echo "100644 blob $o0 d/e" &&
158 echo "100644 $o4 0 a/c" &&
159 echo "100644 $o0 0 b" &&
160 echo "100644 $o0 0 c" &&
161 echo "100644 $o0 0 d/e"
163 test_cmp expected actual
166 test_expect_success 'setup 5' '
169 git checkout remove &&
170 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
172 echo "100644 blob $o0 a" &&
173 echo "100644 blob $o0 b" &&
174 echo "100644 blob $o0 c" &&
175 echo "100644 blob $o0 d/e" &&
176 echo "100644 $o0 0 a" &&
177 echo "100644 $o0 0 b" &&
178 echo "100644 $o0 0 c" &&
179 echo "100644 $o0 0 d/e"
181 test_cmp expected actual &&
184 echo remove-conflict >a &&
188 o5=$(git hash-object a) &&
191 git commit -m "remove removes b and modifies a" &&
192 c5=$(git rev-parse --verify HEAD) &&
193 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
195 echo "100644 blob $o5 a" &&
196 echo "100644 blob $o0 c" &&
197 echo "100644 blob $o0 d/e" &&
198 echo "100644 $o5 0 a" &&
199 echo "100644 $o0 0 c" &&
200 echo "100644 $o0 0 d/e"
202 test_cmp expected actual
206 test_expect_success 'setup 6' '
210 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
212 echo "100644 blob $o0 a" &&
213 echo "100644 blob $o0 b" &&
214 echo "100644 blob $o0 c" &&
215 echo "100644 blob $o0 d/e" &&
216 echo "100644 $o0 0 a" &&
217 echo "100644 $o0 0 b" &&
218 echo "100644 $o0 0 c" &&
219 echo "100644 $o0 0 d/e"
221 test_cmp expected actual &&
223 rm -fr d && echo df-3 >d && git add d &&
224 o6=$(git hash-object d) &&
227 git commit -m "df-3 makes d" &&
228 c6=$(git rev-parse --verify HEAD) &&
229 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
231 echo "100644 blob $o0 a" &&
232 echo "100644 blob $o0 b" &&
233 echo "100644 blob $o0 c" &&
234 echo "100644 blob $o6 d" &&
235 echo "100644 $o0 0 a" &&
236 echo "100644 $o0 0 b" &&
237 echo "100644 $o0 0 c" &&
238 echo "100644 $o6 0 d"
240 test_cmp expected actual
243 test_expect_success 'setup 7' '
245 git checkout submod &&
248 git commit -m "remove d/e" &&
249 git update-index --add --cacheinfo 160000 $c1 d &&
251 git commit -m "make d/ a submodule"
254 test_expect_success 'setup 8' '
255 git checkout rename &&
259 git commit -m "rename a->e" &&
260 c7=$(git rev-parse --verify HEAD) &&
261 git checkout rename-ln &&
265 git commit -m "rename a->e, symlink a->e" &&
266 oln=$(printf e | git hash-object --stdin)
269 test_expect_success 'setup 9' '
274 git commit -m "copy a->e"
277 test_expect_success 'merge-recursive simple' '
280 git checkout -f "$c2" &&
282 test_expect_code 1 git merge-recursive "$c0" -- "$c2" "$c1"
285 test_expect_success 'merge-recursive result' '
287 git ls-files -s >actual &&
289 echo "100644 $o0 1 a" &&
290 echo "100644 $o2 2 a" &&
291 echo "100644 $o1 3 a" &&
292 echo "100644 $o0 0 b" &&
293 echo "100644 $o0 0 c" &&
294 echo "100644 $o1 0 d/e"
296 test_cmp expected actual
300 test_expect_success 'fail if the index has unresolved entries' '
303 git checkout -f "$c1" &&
305 test_must_fail git merge "$c5" &&
306 test_must_fail git merge "$c5" 2> out &&
307 test_i18ngrep "not possible because you have unmerged files" out &&
309 test_must_fail git merge "$c5" 2> out &&
310 test_i18ngrep "You have not concluded your merge" out &&
311 rm -f .git/MERGE_HEAD &&
312 test_must_fail git merge "$c5" 2> out &&
313 test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
316 test_expect_success 'merge-recursive remove conflict' '
319 git checkout -f "$c1" &&
321 test_expect_code 1 git merge-recursive "$c0" -- "$c1" "$c5"
324 test_expect_success 'merge-recursive remove conflict' '
326 git ls-files -s >actual &&
328 echo "100644 $o0 1 a" &&
329 echo "100644 $o1 2 a" &&
330 echo "100644 $o5 3 a" &&
331 echo "100644 $o0 0 c" &&
332 echo "100644 $o1 0 d/e"
334 test_cmp expected actual
338 test_expect_success 'merge-recursive d/f simple' '
341 git checkout -f "$c1" &&
343 git merge-recursive "$c0" -- "$c1" "$c3"
346 test_expect_success 'merge-recursive result' '
348 git ls-files -s >actual &&
350 echo "100644 $o1 0 a" &&
351 echo "100644 $o3 0 b/c" &&
352 echo "100644 $o0 0 c" &&
353 echo "100644 $o1 0 d/e"
355 test_cmp expected actual
359 test_expect_success 'merge-recursive d/f conflict' '
363 git checkout -f "$c1" &&
365 test_expect_code 1 git merge-recursive "$c0" -- "$c1" "$c4"
368 test_expect_success 'merge-recursive d/f conflict result' '
370 git ls-files -s >actual &&
372 echo "100644 $o0 1 a" &&
373 echo "100644 $o1 2 a" &&
374 echo "100644 $o4 0 a/c" &&
375 echo "100644 $o0 0 b" &&
376 echo "100644 $o0 0 c" &&
377 echo "100644 $o1 0 d/e"
379 test_cmp expected actual
383 test_expect_success 'merge-recursive d/f conflict the other way' '
387 git checkout -f "$c4" &&
389 test_expect_code 1 git merge-recursive "$c0" -- "$c4" "$c1"
392 test_expect_success 'merge-recursive d/f conflict result the other way' '
394 git ls-files -s >actual &&
396 echo "100644 $o0 1 a" &&
397 echo "100644 $o1 3 a" &&
398 echo "100644 $o4 0 a/c" &&
399 echo "100644 $o0 0 b" &&
400 echo "100644 $o0 0 c" &&
401 echo "100644 $o1 0 d/e"
403 test_cmp expected actual
407 test_expect_success 'merge-recursive d/f conflict' '
411 git checkout -f "$c1" &&
413 test_expect_code 1 git merge-recursive "$c0" -- "$c1" "$c6"
416 test_expect_success 'merge-recursive d/f conflict result' '
418 git ls-files -s >actual &&
420 echo "100644 $o1 0 a" &&
421 echo "100644 $o0 0 b" &&
422 echo "100644 $o0 0 c" &&
423 echo "100644 $o6 3 d" &&
424 echo "100644 $o0 1 d/e" &&
425 echo "100644 $o1 2 d/e"
427 test_cmp expected actual
431 test_expect_success 'merge-recursive d/f conflict' '
435 git checkout -f "$c6" &&
437 test_expect_code 1 git merge-recursive "$c0" -- "$c6" "$c1"
440 test_expect_success 'merge-recursive d/f conflict result' '
442 git ls-files -s >actual &&
444 echo "100644 $o1 0 a" &&
445 echo "100644 $o0 0 b" &&
446 echo "100644 $o0 0 c" &&
447 echo "100644 $o6 2 d" &&
448 echo "100644 $o0 1 d/e" &&
449 echo "100644 $o1 3 d/e"
451 test_cmp expected actual
455 test_expect_success SYMLINKS 'dir in working tree with symlink ancestor does not produce d/f conflict' '
462 git add foo bar/file &&
463 git commit -m "foo symlink" &&
465 git checkout -b branch1 &&
466 git commit --allow-empty -m "empty commit" &&
468 git checkout master &&
473 git commit -m "replace foo symlink with real foo dir and foo/bar file" &&
475 git checkout branch1 &&
477 git cherry-pick master &&
478 test_path_is_dir foo &&
479 test_path_is_file foo/bar
483 test_expect_success 'reset and 3-way merge' '
485 git reset --hard "$c2" &&
486 git read-tree -m "$c0" "$c2" "$c1"
490 test_expect_success 'reset and bind merge' '
492 git reset --hard master &&
493 git read-tree --prefix=M/ master &&
494 git ls-files -s >actual &&
496 echo "100644 $o1 0 M/a" &&
497 echo "100644 $o0 0 M/b" &&
498 echo "100644 $o0 0 M/c" &&
499 echo "100644 $o1 0 M/d/e" &&
500 echo "100644 $o1 0 a" &&
501 echo "100644 $o0 0 b" &&
502 echo "100644 $o0 0 c" &&
503 echo "100644 $o1 0 d/e"
505 test_cmp expected actual &&
507 git read-tree --prefix=a1/ master &&
508 git ls-files -s >actual &&
510 echo "100644 $o1 0 M/a" &&
511 echo "100644 $o0 0 M/b" &&
512 echo "100644 $o0 0 M/c" &&
513 echo "100644 $o1 0 M/d/e" &&
514 echo "100644 $o1 0 a" &&
515 echo "100644 $o1 0 a1/a" &&
516 echo "100644 $o0 0 a1/b" &&
517 echo "100644 $o0 0 a1/c" &&
518 echo "100644 $o1 0 a1/d/e" &&
519 echo "100644 $o0 0 b" &&
520 echo "100644 $o0 0 c" &&
521 echo "100644 $o1 0 d/e"
523 test_cmp expected actual &&
525 git read-tree --prefix=z/ master &&
526 git ls-files -s >actual &&
528 echo "100644 $o1 0 M/a" &&
529 echo "100644 $o0 0 M/b" &&
530 echo "100644 $o0 0 M/c" &&
531 echo "100644 $o1 0 M/d/e" &&
532 echo "100644 $o1 0 a" &&
533 echo "100644 $o1 0 a1/a" &&
534 echo "100644 $o0 0 a1/b" &&
535 echo "100644 $o0 0 a1/c" &&
536 echo "100644 $o1 0 a1/d/e" &&
537 echo "100644 $o0 0 b" &&
538 echo "100644 $o0 0 c" &&
539 echo "100644 $o1 0 d/e" &&
540 echo "100644 $o1 0 z/a" &&
541 echo "100644 $o0 0 z/b" &&
542 echo "100644 $o0 0 z/c" &&
543 echo "100644 $o1 0 z/d/e"
545 test_cmp expected actual
549 test_expect_success 'merge-recursive w/ empty work tree - ours has rename' '
551 GIT_WORK_TREE="$PWD/ours-has-rename-work" &&
552 export GIT_WORK_TREE &&
553 GIT_INDEX_FILE="$PWD/ours-has-rename-index" &&
554 export GIT_INDEX_FILE &&
555 mkdir "$GIT_WORK_TREE" &&
556 git read-tree -i -m $c7 2>actual-err &&
557 test_must_be_empty actual-err &&
558 git update-index --ignore-missing --refresh 2>actual-err &&
559 test_must_be_empty actual-err &&
560 git merge-recursive $c0 -- $c7 $c3 2>actual-err &&
561 test_must_be_empty actual-err &&
562 git ls-files -s >actual-files 2>actual-err &&
563 test_must_be_empty actual-err
565 cat >expected-files <<-EOF &&
571 test_cmp expected-files actual-files
574 test_expect_success 'merge-recursive w/ empty work tree - theirs has rename' '
576 GIT_WORK_TREE="$PWD/theirs-has-rename-work" &&
577 export GIT_WORK_TREE &&
578 GIT_INDEX_FILE="$PWD/theirs-has-rename-index" &&
579 export GIT_INDEX_FILE &&
580 mkdir "$GIT_WORK_TREE" &&
581 git read-tree -i -m $c3 2>actual-err &&
582 test_must_be_empty actual-err &&
583 git update-index --ignore-missing --refresh 2>actual-err &&
584 test_must_be_empty actual-err &&
585 git merge-recursive $c0 -- $c3 $c7 2>actual-err &&
586 test_must_be_empty actual-err &&
587 git ls-files -s >actual-files 2>actual-err &&
588 test_must_be_empty actual-err
590 cat >expected-files <<-EOF &&
596 test_cmp expected-files actual-files
599 test_expect_success 'merge removes empty directories' '
601 git reset --hard master &&
602 git checkout -b rm &&
604 git commit -mremoved-d/e &&
605 git checkout master &&
606 git merge -s recursive rm &&
607 test_path_is_missing d
610 test_expect_success 'merge-recursive simple w/submodule' '
612 git checkout submod &&
616 test_expect_success 'merge-recursive simple w/submodule result' '
618 git ls-files -s >actual &&
620 echo "100644 $o5 0 a" &&
621 echo "100644 $o0 0 c" &&
622 echo "160000 $c1 0 d"
624 test_cmp expected actual
627 test_expect_success 'merge-recursive copy vs. rename' '
628 git checkout -f copy &&
630 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
632 echo "100644 blob $o0 b" &&
633 echo "100644 blob $o0 c" &&
634 echo "100644 blob $o0 d/e" &&
635 echo "100644 blob $o0 e" &&
636 echo "100644 $o0 0 b" &&
637 echo "100644 $o0 0 c" &&
638 echo "100644 $o0 0 d/e" &&
639 echo "100644 $o0 0 e"
641 test_cmp expected actual
644 test_expect_failure 'merge-recursive rename vs. rename/symlink' '
646 git checkout -f rename &&
647 git merge rename-ln &&
648 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
650 echo "120000 blob $oln a" &&
651 echo "100644 blob $o0 b" &&
652 echo "100644 blob $o0 c" &&
653 echo "100644 blob $o0 d/e" &&
654 echo "100644 blob $o0 e" &&
655 echo "120000 $oln 0 a" &&
656 echo "100644 $o0 0 b" &&
657 echo "100644 $o0 0 c" &&
658 echo "100644 $o0 0 d/e" &&
659 echo "100644 $o0 0 e"
661 test_cmp expected actual
664 test_expect_success 'merging with triple rename across D/F conflict' '
665 git reset --hard HEAD &&
666 git checkout -b topic &&
669 echo "just a file" >sub1 &&
671 echo content1 >sub2/file1 &&
672 echo content2 >sub2/file2 &&
673 echo content3 >sub2/file3 &&
675 echo base >simple/bar &&
678 git commit -m base &&
680 git checkout -b other &&
681 echo more >>simple/bar &&
683 git commit -a -m changesimplefile &&
685 git checkout topic &&
689 git commit -m changefiletodir &&
695 test_expect_success 'merge-recursive remembers the names of all base trees' '
696 git reset --hard HEAD &&
698 # make the index match $c1 so that merge-recursive below does not
700 git diff --binary HEAD $c1 -- | git apply --cached &&
702 # more trees than static slots used by oid_to_hex()
703 for commit in $c0 $c2 $c4 $c5 $c6 $c7
705 git rev-parse "$commit^{tree}"
708 # ignore the return code; it only fails because the input is weird...
709 test_must_fail git -c merge.verbosity=5 merge-recursive $(cat trees) -- $c1 $c3 >out &&
711 # ...but make sure it fails in the expected way
712 test_i18ngrep CONFLICT.*rename/rename out &&
714 # merge-recursive prints in reverse order, but we do not care
715 sort <trees >expect &&
716 sed -n "s/^virtual //p" out | sort >actual &&
717 test_cmp expect actual
720 test_expect_success 'merge-recursive internal merge resolves to the sameness' '
721 git reset --hard HEAD &&
723 # We are going to create a history leading to two criss-cross
724 # branches A and B. The common ancestor at the bottom, O0,
725 # has two child commits O1 and O2, both of which will be merge
726 # base between A and B, like so:
734 # The recently added "check to see if the index is different from
735 # the tree into which something else is getting merged" check must
736 # NOT kick in when an inner merge between O1 and O2 is made. Both
737 # O1 and O2 happen to have the same tree as O0 in this test to
738 # trigger the bug---whether the inner merge is made by merging O2
739 # into O1 or O1 into O2, their common ancestor O0 and the branch
740 # being merged have the same tree. We should not trigger the "is
741 # the index dirty?" check in this case.
746 git commit -m "O0" &&
747 O0=$(git rev-parse HEAD) &&
750 git commit --allow-empty -m "O1" &&
751 O1=$(git rev-parse HEAD) &&
753 git reset --hard $O0 &&
755 git commit --allow-empty -m "O2" &&
756 O2=$(git rev-parse HEAD) &&
759 git merge -s ours $O1 &&
760 B=$(git rev-parse HEAD) &&
762 git reset --hard $O1 &&
764 git merge -s ours $O2 &&
765 A=$(git rev-parse HEAD) &&