3 test_description
='merge-recursive backend test'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9 .
"$TEST_DIRECTORY"/lib-merge.sh
11 test_expect_success
'setup 1' '
14 o0=$(git hash-object a) &&
22 git commit -m initial &&
23 c0=$(git rev-parse --verify HEAD) &&
32 git branch rename-ln &&
36 o1=$(git hash-object a) &&
41 git commit -m "main modifies a and d/e" &&
42 c1=$(git rev-parse --verify HEAD) &&
43 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
45 echo "100644 blob $o1 a" &&
46 echo "100644 blob $o0 b" &&
47 echo "100644 blob $o0 c" &&
48 echo "100644 blob $o1 d/e" &&
49 echo "100644 $o1 0 a" &&
50 echo "100644 $o0 0 b" &&
51 echo "100644 $o0 0 c" &&
52 echo "100644 $o1 0 d/e"
54 test_cmp expected actual
57 test_expect_success
'setup 2' '
61 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
63 echo "100644 blob $o0 a" &&
64 echo "100644 blob $o0 b" &&
65 echo "100644 blob $o0 c" &&
66 echo "100644 blob $o0 d/e" &&
67 echo "100644 $o0 0 a" &&
68 echo "100644 $o0 0 b" &&
69 echo "100644 $o0 0 c" &&
70 echo "100644 $o0 0 d/e"
72 test_cmp expected actual &&
75 o2=$(git hash-object a) &&
80 git commit -m "side modifies a" &&
81 c2=$(git rev-parse --verify HEAD) &&
82 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
84 echo "100644 blob $o2 a" &&
85 echo "100644 blob $o0 b" &&
86 echo "100644 blob $o0 c" &&
87 echo "100644 blob $o0 d/e" &&
88 echo "100644 $o2 0 a" &&
89 echo "100644 $o0 0 b" &&
90 echo "100644 $o0 0 c" &&
91 echo "100644 $o0 0 d/e"
93 test_cmp expected actual
96 test_expect_success
'setup 3' '
100 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
102 echo "100644 blob $o0 a" &&
103 echo "100644 blob $o0 b" &&
104 echo "100644 blob $o0 c" &&
105 echo "100644 blob $o0 d/e" &&
106 echo "100644 $o0 0 a" &&
107 echo "100644 $o0 0 b" &&
108 echo "100644 $o0 0 c" &&
109 echo "100644 $o0 0 d/e"
111 test_cmp expected actual &&
113 rm -f b && mkdir b && echo df-1 >b/c && git add b/c &&
114 o3=$(git hash-object b/c) &&
117 git commit -m "df-1 makes b/c" &&
118 c3=$(git rev-parse --verify HEAD) &&
119 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
121 echo "100644 blob $o0 a" &&
122 echo "100644 blob $o3 b/c" &&
123 echo "100644 blob $o0 c" &&
124 echo "100644 blob $o0 d/e" &&
125 echo "100644 $o0 0 a" &&
126 echo "100644 $o3 0 b/c" &&
127 echo "100644 $o0 0 c" &&
128 echo "100644 $o0 0 d/e"
130 test_cmp expected actual
133 test_expect_success
'setup 4' '
137 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
139 echo "100644 blob $o0 a" &&
140 echo "100644 blob $o0 b" &&
141 echo "100644 blob $o0 c" &&
142 echo "100644 blob $o0 d/e" &&
143 echo "100644 $o0 0 a" &&
144 echo "100644 $o0 0 b" &&
145 echo "100644 $o0 0 c" &&
146 echo "100644 $o0 0 d/e"
148 test_cmp expected actual &&
150 rm -f a && mkdir a && echo df-2 >a/c && git add a/c &&
151 o4=$(git hash-object a/c) &&
154 git commit -m "df-2 makes a/c" &&
155 c4=$(git rev-parse --verify HEAD) &&
156 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
158 echo "100644 blob $o4 a/c" &&
159 echo "100644 blob $o0 b" &&
160 echo "100644 blob $o0 c" &&
161 echo "100644 blob $o0 d/e" &&
162 echo "100644 $o4 0 a/c" &&
163 echo "100644 $o0 0 b" &&
164 echo "100644 $o0 0 c" &&
165 echo "100644 $o0 0 d/e"
167 test_cmp expected actual
170 test_expect_success
'setup 5' '
173 git checkout remove &&
174 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
176 echo "100644 blob $o0 a" &&
177 echo "100644 blob $o0 b" &&
178 echo "100644 blob $o0 c" &&
179 echo "100644 blob $o0 d/e" &&
180 echo "100644 $o0 0 a" &&
181 echo "100644 $o0 0 b" &&
182 echo "100644 $o0 0 c" &&
183 echo "100644 $o0 0 d/e"
185 test_cmp expected actual &&
188 echo remove-conflict >a &&
192 o5=$(git hash-object a) &&
195 git commit -m "remove removes b and modifies a" &&
196 c5=$(git rev-parse --verify HEAD) &&
197 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
199 echo "100644 blob $o5 a" &&
200 echo "100644 blob $o0 c" &&
201 echo "100644 blob $o0 d/e" &&
202 echo "100644 $o5 0 a" &&
203 echo "100644 $o0 0 c" &&
204 echo "100644 $o0 0 d/e"
206 test_cmp expected actual
210 test_expect_success
'setup 6' '
214 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
216 echo "100644 blob $o0 a" &&
217 echo "100644 blob $o0 b" &&
218 echo "100644 blob $o0 c" &&
219 echo "100644 blob $o0 d/e" &&
220 echo "100644 $o0 0 a" &&
221 echo "100644 $o0 0 b" &&
222 echo "100644 $o0 0 c" &&
223 echo "100644 $o0 0 d/e"
225 test_cmp expected actual &&
227 rm -fr d && echo df-3 >d && git add d &&
228 o6=$(git hash-object d) &&
231 git commit -m "df-3 makes d" &&
232 c6=$(git rev-parse --verify HEAD) &&
233 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
235 echo "100644 blob $o0 a" &&
236 echo "100644 blob $o0 b" &&
237 echo "100644 blob $o0 c" &&
238 echo "100644 blob $o6 d" &&
239 echo "100644 $o0 0 a" &&
240 echo "100644 $o0 0 b" &&
241 echo "100644 $o0 0 c" &&
242 echo "100644 $o6 0 d"
244 test_cmp expected actual
247 test_expect_success
'setup 7' '
249 git checkout submod &&
252 git commit -m "remove d/e" &&
253 git update-index --add --cacheinfo 160000 $c1 d &&
255 git commit -m "make d/ a submodule"
258 test_expect_success
'setup 8' '
259 git checkout rename &&
263 git commit -m "rename a->e" &&
264 c7=$(git rev-parse --verify HEAD) &&
265 git checkout rename-ln &&
269 git commit -m "rename a->e, symlink a->e" &&
270 oln=$(printf e | git hash-object --stdin)
273 test_expect_success
'setup 9' '
278 git commit -m "copy a->e"
281 test_expect_success
'merge-recursive simple' '
284 git checkout -f "$c2" &&
286 test_expect_code 1 git merge-recursive "$c0" -- "$c2" "$c1"
289 test_expect_success
'merge-recursive result' '
291 git ls-files -s >actual &&
293 echo "100644 $o0 1 a" &&
294 echo "100644 $o2 2 a" &&
295 echo "100644 $o1 3 a" &&
296 echo "100644 $o0 0 b" &&
297 echo "100644 $o0 0 c" &&
298 echo "100644 $o1 0 d/e"
300 test_cmp expected actual
304 test_expect_success
'fail if the index has unresolved entries' '
307 git checkout -f "$c1" &&
309 test_must_fail git merge "$c5" &&
310 test_must_fail git merge "$c5" 2> out &&
311 test_i18ngrep "not possible because you have unmerged files" out &&
313 test_must_fail git merge "$c5" 2> out &&
314 test_i18ngrep "You have not concluded your merge" out &&
315 rm -f .git/MERGE_HEAD &&
316 test_must_fail git merge "$c5" 2> out &&
317 test_i18ngrep "Your local changes to the following files would be overwritten by merge:" out
320 test_expect_success
'merge-recursive remove conflict' '
323 git checkout -f "$c1" &&
325 test_expect_code 1 git merge-recursive "$c0" -- "$c1" "$c5"
328 test_expect_success
'merge-recursive remove conflict' '
330 git ls-files -s >actual &&
332 echo "100644 $o0 1 a" &&
333 echo "100644 $o1 2 a" &&
334 echo "100644 $o5 3 a" &&
335 echo "100644 $o0 0 c" &&
336 echo "100644 $o1 0 d/e"
338 test_cmp expected actual
342 test_expect_success
'merge-recursive d/f simple' '
345 git checkout -f "$c1" &&
347 git merge-recursive "$c0" -- "$c1" "$c3"
350 test_expect_success
'merge-recursive result' '
352 git ls-files -s >actual &&
354 echo "100644 $o1 0 a" &&
355 echo "100644 $o3 0 b/c" &&
356 echo "100644 $o0 0 c" &&
357 echo "100644 $o1 0 d/e"
359 test_cmp expected actual
363 test_expect_success
'merge-recursive d/f conflict' '
367 git checkout -f "$c1" &&
369 test_expect_code 1 git merge-recursive "$c0" -- "$c1" "$c4"
372 test_expect_success
'merge-recursive d/f conflict result' '
374 git ls-files -s >actual &&
376 echo "100644 $o0 1 a" &&
377 echo "100644 $o1 2 a" &&
378 echo "100644 $o4 0 a/c" &&
379 echo "100644 $o0 0 b" &&
380 echo "100644 $o0 0 c" &&
381 echo "100644 $o1 0 d/e"
383 test_cmp expected actual
387 test_expect_success
'merge-recursive d/f conflict the other way' '
391 git checkout -f "$c4" &&
393 test_expect_code 1 git merge-recursive "$c0" -- "$c4" "$c1"
396 test_expect_success
'merge-recursive d/f conflict result the other way' '
398 git ls-files -s >actual &&
400 echo "100644 $o0 1 a" &&
401 echo "100644 $o1 3 a" &&
402 echo "100644 $o4 0 a/c" &&
403 echo "100644 $o0 0 b" &&
404 echo "100644 $o0 0 c" &&
405 echo "100644 $o1 0 d/e"
407 test_cmp expected actual
411 test_expect_success
'merge-recursive d/f conflict' '
415 git checkout -f "$c1" &&
417 test_expect_code 1 git merge-recursive "$c0" -- "$c1" "$c6"
420 test_expect_success
'merge-recursive d/f conflict result' '
422 git ls-files -s >actual &&
424 echo "100644 $o1 0 a" &&
425 echo "100644 $o0 0 b" &&
426 echo "100644 $o0 0 c" &&
427 echo "100644 $o6 3 d" &&
428 echo "100644 $o0 1 d/e" &&
429 echo "100644 $o1 2 d/e"
431 test_cmp expected actual
435 test_expect_success
'merge-recursive d/f conflict' '
439 git checkout -f "$c6" &&
441 test_expect_code 1 git merge-recursive "$c0" -- "$c6" "$c1"
444 test_expect_success
'merge-recursive d/f conflict result' '
446 git ls-files -s >actual &&
448 echo "100644 $o1 0 a" &&
449 echo "100644 $o0 0 b" &&
450 echo "100644 $o0 0 c" &&
451 echo "100644 $o6 2 d" &&
452 echo "100644 $o0 1 d/e" &&
453 echo "100644 $o1 3 d/e"
455 test_cmp expected actual
459 test_expect_success SYMLINKS
'dir in working tree with symlink ancestor does not produce d/f conflict' '
466 git add foo bar/file &&
467 git commit -m "foo symlink" &&
469 git checkout -b branch1 &&
470 git commit --allow-empty -m "empty commit" &&
477 git commit -m "replace foo symlink with real foo dir and foo/bar file" &&
479 git checkout branch1 &&
481 git cherry-pick main &&
482 test_path_is_dir foo &&
483 test_path_is_file foo/bar
487 test_expect_success
'reset and 3-way merge' '
489 git reset --hard "$c2" &&
490 git read-tree -m "$c0" "$c2" "$c1"
494 test_expect_success
'reset and bind merge' '
496 git reset --hard main &&
497 git read-tree --prefix=M/ main &&
498 git ls-files -s >actual &&
500 echo "100644 $o1 0 M/a" &&
501 echo "100644 $o0 0 M/b" &&
502 echo "100644 $o0 0 M/c" &&
503 echo "100644 $o1 0 M/d/e" &&
504 echo "100644 $o1 0 a" &&
505 echo "100644 $o0 0 b" &&
506 echo "100644 $o0 0 c" &&
507 echo "100644 $o1 0 d/e"
509 test_cmp expected actual &&
511 git read-tree --prefix=a1/ main &&
512 git ls-files -s >actual &&
514 echo "100644 $o1 0 M/a" &&
515 echo "100644 $o0 0 M/b" &&
516 echo "100644 $o0 0 M/c" &&
517 echo "100644 $o1 0 M/d/e" &&
518 echo "100644 $o1 0 a" &&
519 echo "100644 $o1 0 a1/a" &&
520 echo "100644 $o0 0 a1/b" &&
521 echo "100644 $o0 0 a1/c" &&
522 echo "100644 $o1 0 a1/d/e" &&
523 echo "100644 $o0 0 b" &&
524 echo "100644 $o0 0 c" &&
525 echo "100644 $o1 0 d/e"
527 test_cmp expected actual &&
529 git read-tree --prefix=z/ main &&
530 git ls-files -s >actual &&
532 echo "100644 $o1 0 M/a" &&
533 echo "100644 $o0 0 M/b" &&
534 echo "100644 $o0 0 M/c" &&
535 echo "100644 $o1 0 M/d/e" &&
536 echo "100644 $o1 0 a" &&
537 echo "100644 $o1 0 a1/a" &&
538 echo "100644 $o0 0 a1/b" &&
539 echo "100644 $o0 0 a1/c" &&
540 echo "100644 $o1 0 a1/d/e" &&
541 echo "100644 $o0 0 b" &&
542 echo "100644 $o0 0 c" &&
543 echo "100644 $o1 0 d/e" &&
544 echo "100644 $o1 0 z/a" &&
545 echo "100644 $o0 0 z/b" &&
546 echo "100644 $o0 0 z/c" &&
547 echo "100644 $o1 0 z/d/e"
549 test_cmp expected actual
553 test_expect_success
'merge-recursive w/ empty work tree - ours has rename' '
555 GIT_WORK_TREE="$PWD/ours-has-rename-work" &&
556 export GIT_WORK_TREE &&
557 GIT_INDEX_FILE="$PWD/ours-has-rename-index" &&
558 export GIT_INDEX_FILE &&
559 mkdir "$GIT_WORK_TREE" &&
560 git read-tree -i -m $c7 2>actual-err &&
561 test_must_be_empty actual-err &&
562 git update-index --ignore-missing --refresh 2>actual-err &&
563 test_must_be_empty actual-err &&
564 git merge-recursive $c0 -- $c7 $c3 2>actual-err &&
565 test_must_be_empty actual-err &&
566 git ls-files -s >actual-files 2>actual-err &&
567 test_must_be_empty actual-err
569 cat >expected-files <<-EOF &&
575 test_cmp expected-files actual-files
578 test_expect_success
'merge-recursive w/ empty work tree - theirs has rename' '
580 GIT_WORK_TREE="$PWD/theirs-has-rename-work" &&
581 export GIT_WORK_TREE &&
582 GIT_INDEX_FILE="$PWD/theirs-has-rename-index" &&
583 export GIT_INDEX_FILE &&
584 mkdir "$GIT_WORK_TREE" &&
585 git read-tree -i -m $c3 2>actual-err &&
586 test_must_be_empty actual-err &&
587 git update-index --ignore-missing --refresh 2>actual-err &&
588 test_must_be_empty actual-err &&
589 git merge-recursive $c0 -- $c3 $c7 2>actual-err &&
590 test_must_be_empty actual-err &&
591 git ls-files -s >actual-files 2>actual-err &&
592 test_must_be_empty actual-err
594 cat >expected-files <<-EOF &&
600 test_cmp expected-files actual-files
603 test_expect_success
'merge removes empty directories' '
605 git reset --hard main &&
606 git checkout -b rm &&
608 git commit -mremoved-d/e &&
610 git merge -s recursive rm &&
611 test_path_is_missing d
614 test_expect_success
'merge-recursive simple w/submodule' '
616 git checkout submod &&
620 test_expect_success
'merge-recursive simple w/submodule result' '
622 git ls-files -s >actual &&
624 echo "100644 $o5 0 a" &&
625 echo "100644 $o0 0 c" &&
626 echo "160000 $c1 0 d"
628 test_cmp expected actual
631 test_expect_success
'merge-recursive copy vs. rename' '
632 git checkout -f copy &&
634 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
636 echo "100644 blob $o0 b" &&
637 echo "100644 blob $o0 c" &&
638 echo "100644 blob $o0 d/e" &&
639 echo "100644 blob $o0 e" &&
640 echo "100644 $o0 0 b" &&
641 echo "100644 $o0 0 c" &&
642 echo "100644 $o0 0 d/e" &&
643 echo "100644 $o0 0 e"
645 test_cmp expected actual
648 test_expect_merge_algorithm failure success
'merge-recursive rename vs. rename/symlink' '
650 git checkout -f rename &&
651 git merge rename-ln &&
652 ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
654 echo "120000 blob $oln a" &&
655 echo "100644 blob $o0 b" &&
656 echo "100644 blob $o0 c" &&
657 echo "100644 blob $o0 d/e" &&
658 echo "100644 blob $o0 e" &&
659 echo "120000 $oln 0 a" &&
660 echo "100644 $o0 0 b" &&
661 echo "100644 $o0 0 c" &&
662 echo "100644 $o0 0 d/e" &&
663 echo "100644 $o0 0 e"
665 test_cmp expected actual
668 test_expect_success
'merging with triple rename across D/F conflict' '
669 git reset --hard HEAD &&
670 git checkout -b topic &&
673 echo "just a file" >sub1 &&
675 echo content1 >sub2/file1 &&
676 echo content2 >sub2/file2 &&
677 echo content3 >sub2/file3 &&
679 echo base >simple/bar &&
682 git commit -m base &&
684 git checkout -b other &&
685 echo more >>simple/bar &&
687 git commit -a -m changesimplefile &&
689 git checkout topic &&
693 git commit -m changefiletodir &&
699 test_expect_success
'merge-recursive remembers the names of all base trees' '
700 git reset --hard HEAD &&
702 # make the index match $c1 so that merge-recursive below does not
704 git diff --binary HEAD $c1 -- | git apply --cached &&
706 # more trees than static slots used by oid_to_hex()
707 for commit in $c0 $c2 $c4 $c5 $c6 $c7
709 git rev-parse "$commit^{tree}"
712 # ignore the return code; it only fails because the input is weird...
713 test_must_fail git -c merge.verbosity=5 merge-recursive $(cat trees) -- $c1 $c3 >out &&
715 # ...but make sure it fails in the expected way
716 test_i18ngrep CONFLICT.*rename/rename out &&
718 # merge-recursive prints in reverse order, but we do not care
719 sort <trees >expect &&
720 sed -n "s/^virtual //p" out | sort >actual &&
721 test_cmp expect actual
724 test_expect_success
'merge-recursive internal merge resolves to the sameness' '
725 git reset --hard HEAD &&
727 # We are going to create a history leading to two criss-cross
728 # branches A and B. The common ancestor at the bottom, O0,
729 # has two child commits O1 and O2, both of which will be merge
730 # base between A and B, like so:
738 # The recently added "check to see if the index is different from
739 # the tree into which something else is getting merged" check must
740 # NOT kick in when an inner merge between O1 and O2 is made. Both
741 # O1 and O2 happen to have the same tree as O0 in this test to
742 # trigger the bug---whether the inner merge is made by merging O2
743 # into O1 or O1 into O2, their common ancestor O0 and the branch
744 # being merged have the same tree. We should not trigger the "is
745 # the index dirty?" check in this case.
750 git commit -m "O0" &&
751 O0=$(git rev-parse HEAD) &&
754 git commit --allow-empty -m "O1" &&
755 O1=$(git rev-parse HEAD) &&
757 git reset --hard $O0 &&
759 git commit --allow-empty -m "O2" &&
760 O2=$(git rev-parse HEAD) &&
763 git merge -s ours $O1 &&
764 B=$(git rev-parse HEAD) &&
766 git reset --hard $O1 &&
768 git merge -s ours $O2 &&
769 A=$(git rev-parse HEAD) &&