3 test_description
='merge-recursive backend test'
6 .
"$TEST_DIRECTORY"/lib-merge.sh
8 test_expect_success
'setup 1' '
11 o0=$(git hash-object a) &&
19 git commit -m initial &&
20 c0=$(git rev-parse --verify HEAD) &&
29 git branch rename-ln &&
33 o1=$(git hash-object a) &&
38 git commit -m "master modifies a and d/e" &&
39 c1=$(git rev-parse --verify HEAD) &&
40 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
42 echo "100644 blob $o1 a" &&
43 echo "100644 blob $o0 b" &&
44 echo "100644 blob $o0 c" &&
45 echo "100644 blob $o1 d/e" &&
46 echo "100644 $o1 0 a" &&
47 echo "100644 $o0 0 b" &&
48 echo "100644 $o0 0 c" &&
49 echo "100644 $o1 0 d/e"
51 test_cmp expected actual
54 test_expect_success
'setup 2' '
58 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
60 echo "100644 blob $o0 a" &&
61 echo "100644 blob $o0 b" &&
62 echo "100644 blob $o0 c" &&
63 echo "100644 blob $o0 d/e" &&
64 echo "100644 $o0 0 a" &&
65 echo "100644 $o0 0 b" &&
66 echo "100644 $o0 0 c" &&
67 echo "100644 $o0 0 d/e"
69 test_cmp expected actual &&
72 o2=$(git hash-object a) &&
77 git commit -m "side modifies a" &&
78 c2=$(git rev-parse --verify HEAD) &&
79 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
81 echo "100644 blob $o2 a" &&
82 echo "100644 blob $o0 b" &&
83 echo "100644 blob $o0 c" &&
84 echo "100644 blob $o0 d/e" &&
85 echo "100644 $o2 0 a" &&
86 echo "100644 $o0 0 b" &&
87 echo "100644 $o0 0 c" &&
88 echo "100644 $o0 0 d/e"
90 test_cmp expected actual
93 test_expect_success
'setup 3' '
97 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
99 echo "100644 blob $o0 a" &&
100 echo "100644 blob $o0 b" &&
101 echo "100644 blob $o0 c" &&
102 echo "100644 blob $o0 d/e" &&
103 echo "100644 $o0 0 a" &&
104 echo "100644 $o0 0 b" &&
105 echo "100644 $o0 0 c" &&
106 echo "100644 $o0 0 d/e"
108 test_cmp expected actual &&
110 rm -f b && mkdir b && echo df-1 >b/c && git add b/c &&
111 o3=$(git hash-object b/c) &&
114 git commit -m "df-1 makes b/c" &&
115 c3=$(git rev-parse --verify HEAD) &&
116 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
118 echo "100644 blob $o0 a" &&
119 echo "100644 blob $o3 b/c" &&
120 echo "100644 blob $o0 c" &&
121 echo "100644 blob $o0 d/e" &&
122 echo "100644 $o0 0 a" &&
123 echo "100644 $o3 0 b/c" &&
124 echo "100644 $o0 0 c" &&
125 echo "100644 $o0 0 d/e"
127 test_cmp expected actual
130 test_expect_success
'setup 4' '
134 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
136 echo "100644 blob $o0 a" &&
137 echo "100644 blob $o0 b" &&
138 echo "100644 blob $o0 c" &&
139 echo "100644 blob $o0 d/e" &&
140 echo "100644 $o0 0 a" &&
141 echo "100644 $o0 0 b" &&
142 echo "100644 $o0 0 c" &&
143 echo "100644 $o0 0 d/e"
145 test_cmp expected actual &&
147 rm -f a && mkdir a && echo df-2 >a/c && git add a/c &&
148 o4=$(git hash-object a/c) &&
151 git commit -m "df-2 makes a/c" &&
152 c4=$(git rev-parse --verify HEAD) &&
153 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
155 echo "100644 blob $o4 a/c" &&
156 echo "100644 blob $o0 b" &&
157 echo "100644 blob $o0 c" &&
158 echo "100644 blob $o0 d/e" &&
159 echo "100644 $o4 0 a/c" &&
160 echo "100644 $o0 0 b" &&
161 echo "100644 $o0 0 c" &&
162 echo "100644 $o0 0 d/e"
164 test_cmp expected actual
167 test_expect_success
'setup 5' '
170 git checkout remove &&
171 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
173 echo "100644 blob $o0 a" &&
174 echo "100644 blob $o0 b" &&
175 echo "100644 blob $o0 c" &&
176 echo "100644 blob $o0 d/e" &&
177 echo "100644 $o0 0 a" &&
178 echo "100644 $o0 0 b" &&
179 echo "100644 $o0 0 c" &&
180 echo "100644 $o0 0 d/e"
182 test_cmp expected actual &&
185 echo remove-conflict >a &&
189 o5=$(git hash-object a) &&
192 git commit -m "remove removes b and modifies a" &&
193 c5=$(git rev-parse --verify HEAD) &&
194 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
196 echo "100644 blob $o5 a" &&
197 echo "100644 blob $o0 c" &&
198 echo "100644 blob $o0 d/e" &&
199 echo "100644 $o5 0 a" &&
200 echo "100644 $o0 0 c" &&
201 echo "100644 $o0 0 d/e"
203 test_cmp expected actual
207 test_expect_success
'setup 6' '
211 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
213 echo "100644 blob $o0 a" &&
214 echo "100644 blob $o0 b" &&
215 echo "100644 blob $o0 c" &&
216 echo "100644 blob $o0 d/e" &&
217 echo "100644 $o0 0 a" &&
218 echo "100644 $o0 0 b" &&
219 echo "100644 $o0 0 c" &&
220 echo "100644 $o0 0 d/e"
222 test_cmp expected actual &&
224 rm -fr d && echo df-3 >d && git add d &&
225 o6=$(git hash-object d) &&
228 git commit -m "df-3 makes d" &&
229 c6=$(git rev-parse --verify HEAD) &&
230 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
232 echo "100644 blob $o0 a" &&
233 echo "100644 blob $o0 b" &&
234 echo "100644 blob $o0 c" &&
235 echo "100644 blob $o6 d" &&
236 echo "100644 $o0 0 a" &&
237 echo "100644 $o0 0 b" &&
238 echo "100644 $o0 0 c" &&
239 echo "100644 $o6 0 d"
241 test_cmp expected actual
244 test_expect_success
'setup 7' '
246 git checkout submod &&
249 git commit -m "remove d/e" &&
250 git update-index --add --cacheinfo 160000 $c1 d &&
252 git commit -m "make d/ a submodule"
255 test_expect_success
'setup 8' '
256 git checkout rename &&
260 git commit -m "rename a->e" &&
261 c7=$(git rev-parse --verify HEAD) &&
262 git checkout rename-ln &&
266 git commit -m "rename a->e, symlink a->e" &&
267 oln=$(printf e | git hash-object --stdin)
270 test_expect_success
'setup 9' '
275 git commit -m "copy a->e"
278 test_expect_success
'merge-recursive simple' '
281 git checkout -f "$c2" &&
283 test_expect_code 1 git merge-recursive "$c0" -- "$c2" "$c1"
286 test_expect_success
'merge-recursive result' '
288 git ls-files -s >actual &&
290 echo "100644 $o0 1 a" &&
291 echo "100644 $o2 2 a" &&
292 echo "100644 $o1 3 a" &&
293 echo "100644 $o0 0 b" &&
294 echo "100644 $o0 0 c" &&
295 echo "100644 $o1 0 d/e"
297 test_cmp expected actual
301 test_expect_success
'fail if the index has unresolved entries' '
304 git checkout -f "$c1" &&
306 test_must_fail git merge "$c5" &&
307 test_must_fail git merge "$c5" 2> out &&
308 test_i18ngrep "not possible because you have unmerged files" out &&
310 test_must_fail git merge "$c5" 2> out &&
311 test_i18ngrep "You have not concluded your merge" out &&
312 rm -f .git/MERGE_HEAD &&
313 test_must_fail git merge "$c5" 2> out &&
314 test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
317 test_expect_success
'merge-recursive remove conflict' '
320 git checkout -f "$c1" &&
322 test_expect_code 1 git merge-recursive "$c0" -- "$c1" "$c5"
325 test_expect_success
'merge-recursive remove conflict' '
327 git ls-files -s >actual &&
329 echo "100644 $o0 1 a" &&
330 echo "100644 $o1 2 a" &&
331 echo "100644 $o5 3 a" &&
332 echo "100644 $o0 0 c" &&
333 echo "100644 $o1 0 d/e"
335 test_cmp expected actual
339 test_expect_success
'merge-recursive d/f simple' '
342 git checkout -f "$c1" &&
344 git merge-recursive "$c0" -- "$c1" "$c3"
347 test_expect_success
'merge-recursive result' '
349 git ls-files -s >actual &&
351 echo "100644 $o1 0 a" &&
352 echo "100644 $o3 0 b/c" &&
353 echo "100644 $o0 0 c" &&
354 echo "100644 $o1 0 d/e"
356 test_cmp expected actual
360 test_expect_success
'merge-recursive d/f conflict' '
364 git checkout -f "$c1" &&
366 test_expect_code 1 git merge-recursive "$c0" -- "$c1" "$c4"
369 test_expect_success
'merge-recursive d/f conflict result' '
371 git ls-files -s >actual &&
373 echo "100644 $o0 1 a" &&
374 echo "100644 $o1 2 a" &&
375 echo "100644 $o4 0 a/c" &&
376 echo "100644 $o0 0 b" &&
377 echo "100644 $o0 0 c" &&
378 echo "100644 $o1 0 d/e"
380 test_cmp expected actual
384 test_expect_success
'merge-recursive d/f conflict the other way' '
388 git checkout -f "$c4" &&
390 test_expect_code 1 git merge-recursive "$c0" -- "$c4" "$c1"
393 test_expect_success
'merge-recursive d/f conflict result the other way' '
395 git ls-files -s >actual &&
397 echo "100644 $o0 1 a" &&
398 echo "100644 $o1 3 a" &&
399 echo "100644 $o4 0 a/c" &&
400 echo "100644 $o0 0 b" &&
401 echo "100644 $o0 0 c" &&
402 echo "100644 $o1 0 d/e"
404 test_cmp expected actual
408 test_expect_success
'merge-recursive d/f conflict' '
412 git checkout -f "$c1" &&
414 test_expect_code 1 git merge-recursive "$c0" -- "$c1" "$c6"
417 test_expect_success
'merge-recursive d/f conflict result' '
419 git ls-files -s >actual &&
421 echo "100644 $o1 0 a" &&
422 echo "100644 $o0 0 b" &&
423 echo "100644 $o0 0 c" &&
424 echo "100644 $o6 3 d" &&
425 echo "100644 $o0 1 d/e" &&
426 echo "100644 $o1 2 d/e"
428 test_cmp expected actual
432 test_expect_success
'merge-recursive d/f conflict' '
436 git checkout -f "$c6" &&
438 test_expect_code 1 git merge-recursive "$c0" -- "$c6" "$c1"
441 test_expect_success
'merge-recursive d/f conflict result' '
443 git ls-files -s >actual &&
445 echo "100644 $o1 0 a" &&
446 echo "100644 $o0 0 b" &&
447 echo "100644 $o0 0 c" &&
448 echo "100644 $o6 2 d" &&
449 echo "100644 $o0 1 d/e" &&
450 echo "100644 $o1 3 d/e"
452 test_cmp expected actual
456 test_expect_success SYMLINKS
'dir in working tree with symlink ancestor does not produce d/f conflict' '
463 git add foo bar/file &&
464 git commit -m "foo symlink" &&
466 git checkout -b branch1 &&
467 git commit --allow-empty -m "empty commit" &&
469 git checkout master &&
474 git commit -m "replace foo symlink with real foo dir and foo/bar file" &&
476 git checkout branch1 &&
478 git cherry-pick master &&
479 test_path_is_dir foo &&
480 test_path_is_file foo/bar
484 test_expect_success
'reset and 3-way merge' '
486 git reset --hard "$c2" &&
487 git read-tree -m "$c0" "$c2" "$c1"
491 test_expect_success
'reset and bind merge' '
493 git reset --hard master &&
494 git read-tree --prefix=M/ master &&
495 git ls-files -s >actual &&
497 echo "100644 $o1 0 M/a" &&
498 echo "100644 $o0 0 M/b" &&
499 echo "100644 $o0 0 M/c" &&
500 echo "100644 $o1 0 M/d/e" &&
501 echo "100644 $o1 0 a" &&
502 echo "100644 $o0 0 b" &&
503 echo "100644 $o0 0 c" &&
504 echo "100644 $o1 0 d/e"
506 test_cmp expected actual &&
508 git read-tree --prefix=a1/ master &&
509 git ls-files -s >actual &&
511 echo "100644 $o1 0 M/a" &&
512 echo "100644 $o0 0 M/b" &&
513 echo "100644 $o0 0 M/c" &&
514 echo "100644 $o1 0 M/d/e" &&
515 echo "100644 $o1 0 a" &&
516 echo "100644 $o1 0 a1/a" &&
517 echo "100644 $o0 0 a1/b" &&
518 echo "100644 $o0 0 a1/c" &&
519 echo "100644 $o1 0 a1/d/e" &&
520 echo "100644 $o0 0 b" &&
521 echo "100644 $o0 0 c" &&
522 echo "100644 $o1 0 d/e"
524 test_cmp expected actual &&
526 git read-tree --prefix=z/ master &&
527 git ls-files -s >actual &&
529 echo "100644 $o1 0 M/a" &&
530 echo "100644 $o0 0 M/b" &&
531 echo "100644 $o0 0 M/c" &&
532 echo "100644 $o1 0 M/d/e" &&
533 echo "100644 $o1 0 a" &&
534 echo "100644 $o1 0 a1/a" &&
535 echo "100644 $o0 0 a1/b" &&
536 echo "100644 $o0 0 a1/c" &&
537 echo "100644 $o1 0 a1/d/e" &&
538 echo "100644 $o0 0 b" &&
539 echo "100644 $o0 0 c" &&
540 echo "100644 $o1 0 d/e" &&
541 echo "100644 $o1 0 z/a" &&
542 echo "100644 $o0 0 z/b" &&
543 echo "100644 $o0 0 z/c" &&
544 echo "100644 $o1 0 z/d/e"
546 test_cmp expected actual
550 test_expect_success
'merge-recursive w/ empty work tree - ours has rename' '
552 GIT_WORK_TREE="$PWD/ours-has-rename-work" &&
553 export GIT_WORK_TREE &&
554 GIT_INDEX_FILE="$PWD/ours-has-rename-index" &&
555 export GIT_INDEX_FILE &&
556 mkdir "$GIT_WORK_TREE" &&
557 git read-tree -i -m $c7 2>actual-err &&
558 test_must_be_empty actual-err &&
559 git update-index --ignore-missing --refresh 2>actual-err &&
560 test_must_be_empty actual-err &&
561 git merge-recursive $c0 -- $c7 $c3 2>actual-err &&
562 test_must_be_empty actual-err &&
563 git ls-files -s >actual-files 2>actual-err &&
564 test_must_be_empty actual-err
566 cat >expected-files <<-EOF &&
572 test_cmp expected-files actual-files
575 test_expect_success
'merge-recursive w/ empty work tree - theirs has rename' '
577 GIT_WORK_TREE="$PWD/theirs-has-rename-work" &&
578 export GIT_WORK_TREE &&
579 GIT_INDEX_FILE="$PWD/theirs-has-rename-index" &&
580 export GIT_INDEX_FILE &&
581 mkdir "$GIT_WORK_TREE" &&
582 git read-tree -i -m $c3 2>actual-err &&
583 test_must_be_empty actual-err &&
584 git update-index --ignore-missing --refresh 2>actual-err &&
585 test_must_be_empty actual-err &&
586 git merge-recursive $c0 -- $c3 $c7 2>actual-err &&
587 test_must_be_empty actual-err &&
588 git ls-files -s >actual-files 2>actual-err &&
589 test_must_be_empty actual-err
591 cat >expected-files <<-EOF &&
597 test_cmp expected-files actual-files
600 test_expect_success
'merge removes empty directories' '
602 git reset --hard master &&
603 git checkout -b rm &&
605 git commit -mremoved-d/e &&
606 git checkout master &&
607 git merge -s recursive rm &&
608 test_path_is_missing d
611 test_expect_success
'merge-recursive simple w/submodule' '
613 git checkout submod &&
617 test_expect_success
'merge-recursive simple w/submodule result' '
619 git ls-files -s >actual &&
621 echo "100644 $o5 0 a" &&
622 echo "100644 $o0 0 c" &&
623 echo "160000 $c1 0 d"
625 test_cmp expected actual
628 test_expect_success
'merge-recursive copy vs. rename' '
629 git checkout -f copy &&
631 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
633 echo "100644 blob $o0 b" &&
634 echo "100644 blob $o0 c" &&
635 echo "100644 blob $o0 d/e" &&
636 echo "100644 blob $o0 e" &&
637 echo "100644 $o0 0 b" &&
638 echo "100644 $o0 0 c" &&
639 echo "100644 $o0 0 d/e" &&
640 echo "100644 $o0 0 e"
642 test_cmp expected actual
645 test_expect_merge_algorithm failure success
'merge-recursive rename vs. rename/symlink' '
647 git checkout -f rename &&
648 git merge rename-ln &&
649 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
651 echo "120000 blob $oln a" &&
652 echo "100644 blob $o0 b" &&
653 echo "100644 blob $o0 c" &&
654 echo "100644 blob $o0 d/e" &&
655 echo "100644 blob $o0 e" &&
656 echo "120000 $oln 0 a" &&
657 echo "100644 $o0 0 b" &&
658 echo "100644 $o0 0 c" &&
659 echo "100644 $o0 0 d/e" &&
660 echo "100644 $o0 0 e"
662 test_cmp expected actual
665 test_expect_success
'merging with triple rename across D/F conflict' '
666 git reset --hard HEAD &&
667 git checkout -b topic &&
670 echo "just a file" >sub1 &&
672 echo content1 >sub2/file1 &&
673 echo content2 >sub2/file2 &&
674 echo content3 >sub2/file3 &&
676 echo base >simple/bar &&
679 git commit -m base &&
681 git checkout -b other &&
682 echo more >>simple/bar &&
684 git commit -a -m changesimplefile &&
686 git checkout topic &&
690 git commit -m changefiletodir &&
696 test_expect_success
'merge-recursive remembers the names of all base trees' '
697 git reset --hard HEAD &&
699 # make the index match $c1 so that merge-recursive below does not
701 git diff --binary HEAD $c1 -- | git apply --cached &&
703 # more trees than static slots used by oid_to_hex()
704 for commit in $c0 $c2 $c4 $c5 $c6 $c7
706 git rev-parse "$commit^{tree}"
709 # ignore the return code; it only fails because the input is weird...
710 test_must_fail git -c merge.verbosity=5 merge-recursive $(cat trees) -- $c1 $c3 >out &&
712 # ...but make sure it fails in the expected way
713 test_i18ngrep CONFLICT.*rename/rename out &&
715 # merge-recursive prints in reverse order, but we do not care
716 sort <trees >expect &&
717 sed -n "s/^virtual //p" out | sort >actual &&
718 test_cmp expect actual
721 test_expect_success
'merge-recursive internal merge resolves to the sameness' '
722 git reset --hard HEAD &&
724 # We are going to create a history leading to two criss-cross
725 # branches A and B. The common ancestor at the bottom, O0,
726 # has two child commits O1 and O2, both of which will be merge
727 # base between A and B, like so:
735 # The recently added "check to see if the index is different from
736 # the tree into which something else is getting merged" check must
737 # NOT kick in when an inner merge between O1 and O2 is made. Both
738 # O1 and O2 happen to have the same tree as O0 in this test to
739 # trigger the bug---whether the inner merge is made by merging O2
740 # into O1 or O1 into O2, their common ancestor O0 and the branch
741 # being merged have the same tree. We should not trigger the "is
742 # the index dirty?" check in this case.
747 git commit -m "O0" &&
748 O0=$(git rev-parse HEAD) &&
751 git commit --allow-empty -m "O1" &&
752 O1=$(git rev-parse HEAD) &&
754 git reset --hard $O0 &&
756 git commit --allow-empty -m "O2" &&
757 O2=$(git rev-parse HEAD) &&
760 git merge -s ours $O1 &&
761 B=$(git rev-parse HEAD) &&
763 git reset --hard $O1 &&
765 git merge -s ours $O2 &&
766 A=$(git rev-parse HEAD) &&