3 test_description
='git merge-tree --write-tree'
5 TEST_PASSES_SANITIZE_LEAK
=true
8 # This test is ort-specific
9 if test "$GIT_TEST_MERGE_ALGORITHM" != "ort"
11 skip_all
="GIT_TEST_MERGE_ALGORITHM != ort"
15 test_expect_success setup
'
16 test_write_lines 1 2 3 4 5 >numbers &&
17 echo hello >greeting &&
19 git add numbers greeting whatever &&
21 git commit -m initial &&
29 test_write_lines 1 2 3 4 5 6 >numbers &&
32 git add numbers greeting whatever &&
34 git commit -m modify-stuff &&
37 test_write_lines 0 1 2 3 4 5 >numbers &&
42 git add numbers greeting whatever/empty &&
44 git commit -m other-modifications &&
47 git mv numbers sequence &&
49 git commit -m rename-numbers &&
52 test_write_lines 0 1 2 3 4 5 >numbers &&
54 git add numbers greeting &&
56 git commit -m other-content-modifications &&
58 git switch --orphan unrelated &&
60 git add something-else &&
62 git commit -m first-commit
65 test_expect_success
'Clean merge' '
66 TREE_OID=$(git merge-tree --write-tree side1 side3) &&
67 q_to_tab <<-EOF >expect &&
68 100644 blob $(git rev-parse side1:greeting)Qgreeting
69 100644 blob $(git rev-parse side1:numbers)Qsequence
70 100644 blob $(git rev-parse side1:whatever)Qwhatever
73 git ls-tree $TREE_OID >actual &&
74 test_cmp expect actual
77 test_expect_success
'Content merge and a few conflicts' '
78 git checkout side1^0 &&
79 test_must_fail git merge side2 &&
80 expected_tree=$(git rev-parse AUTO_MERGE) &&
82 # We will redo the merge, while we are still in a conflicted state!
83 git ls-files -u >conflicted-file-info &&
84 test_when_finished "git reset --hard" &&
86 test_expect_code 1 git merge-tree --write-tree side1 side2 >RESULT &&
87 actual_tree=$(head -n 1 RESULT) &&
89 # Due to differences of e.g. "HEAD" vs "side1", the results will not
90 # exactly match. Dig into individual files.
92 # Numbers should have three-way merged cleanly
93 test_write_lines 0 1 2 3 4 5 6 >expect &&
94 git show ${actual_tree}:numbers >actual &&
95 test_cmp expect actual &&
97 # whatever and whatever~<branch> should have same HASHES
98 git rev-parse ${expected_tree}:whatever ${expected_tree}:whatever~HEAD >expect &&
99 git rev-parse ${actual_tree}:whatever ${actual_tree}:whatever~side1 >actual &&
100 test_cmp expect actual &&
102 # greeting should have a merge conflict
103 git show ${expected_tree}:greeting >tmp &&
104 sed -e s/HEAD/side1/ tmp >expect &&
105 git show ${actual_tree}:greeting >actual &&
106 test_cmp expect actual
109 test_expect_success
'Auto resolve conflicts by "ours" strategy option' '
110 git checkout side1^0 &&
112 # make sure merge conflict exists
113 test_must_fail git merge side4 &&
116 git merge -X ours side4 &&
117 git rev-parse HEAD^{tree} >expected &&
119 git merge-tree -X ours side1 side4 >actual &&
121 test_cmp expected actual
124 test_expect_success
'Barf on misspelled option, with exit code other than 0 or 1' '
125 # Mis-spell with single "s" instead of double "s"
126 test_expect_code 129 git merge-tree --write-tree --mesages FOOBAR side1 side2 2>expect &&
128 grep "error: unknown option.*mesages" expect
131 test_expect_success
'Barf on too many arguments' '
132 test_expect_code 129 git merge-tree --write-tree side1 side2 invalid 2>expect &&
134 grep "^usage: git merge-tree" expect
138 sed -e "s/[0-9a-f]\{40,\}/HASH/g" "$@"
141 test_expect_success
'test conflict notices and such' '
142 test_expect_code 1 git merge-tree --write-tree --name-only side1 side2 >out &&
143 anonymize_hash out >actual &&
146 # "greeting" should merge with conflicts
147 # "numbers" should merge cleanly
148 # "whatever" has *both* a modify/delete and a file/directory conflict
149 cat <<-EOF >expect &&
154 Auto-merging greeting
155 CONFLICT (content): Merge conflict in greeting
157 CONFLICT (file/directory): directory in the way of whatever from side1; moving it to whatever~side1 instead.
158 CONFLICT (modify/delete): whatever~side1 deleted in side2 and modified in side1. Version side1 of whatever~side1 left in tree.
161 test_cmp expect actual
164 # directory rename + content conflict
165 # Commit O: foo, olddir/{a,b,c}
166 # Commit A: modify foo, newdir/{a,b,c}
167 # Commit B: modify foo differently & rename foo -> olddir/bar
168 # Expected: CONFLICT(content) for newdir/bar (not olddir/bar or foo)
170 test_expect_success
'directory rename + content conflict' '
172 git init dir-rename-and-content &&
174 cd dir-rename-and-content &&
175 test_write_lines 1 2 3 4 5 >foo &&
177 for i in a b c; do echo $i >olddir/$i || exit 1; done &&
178 git add foo olddir &&
179 git commit -m "original" &&
186 test_write_lines 1 2 3 4 5 6 >foo &&
188 git mv olddir newdir &&
189 git commit -m "Modify foo, rename olddir to newdir" &&
192 test_write_lines 1 2 3 4 5 six >foo &&
194 git mv foo olddir/bar &&
195 git commit -m "Modify foo & rename foo -> olddir/bar"
199 cd dir-rename-and-content &&
202 git merge-tree -z A^0 B^0 >out &&
204 anonymize_hash out >actual &&
205 q_to_tab <<-\EOF | lf_to_nul >expect &&
207 100644 HASH 1Qnewdir/bar
208 100644 HASH 2Qnewdir/bar
209 100644 HASH 3Qnewdir/bar
212 q_to_nul <<-EOF >>expect &&
213 Q2Qnewdir/barQolddir/barQCONFLICT (directory rename suggested)QCONFLICT (file location): foo renamed to olddir/bar in B^0, inside a directory that was renamed in A^0, suggesting it should perhaps be moved to newdir/bar.
214 Q1Qnewdir/barQAuto-mergingQAuto-merging newdir/bar
215 Q1Qnewdir/barQCONFLICT (contents)QCONFLICT (content): Merge conflict in newdir/bar
218 test_cmp expect actual
222 # rename/delete + modify/delete handling
224 # Commit A: modify foo + rename to bar
225 # Commit B: delete foo
226 # Expected: CONFLICT(rename/delete) + CONFLICT(modify/delete)
228 test_expect_success
'rename/delete handling' '
230 git init rename-delete &&
233 test_write_lines 1 2 3 4 5 >foo &&
235 git commit -m "original" &&
242 test_write_lines 1 2 3 4 5 6 >foo &&
245 git commit -m "Modify foo, rename to bar" &&
249 git commit -m "remove foo"
256 git merge-tree -z A^0 B^0 >out &&
258 anonymize_hash out >actual &&
259 q_to_tab <<-\EOF | lf_to_nul >expect &&
265 q_to_nul <<-EOF >>expect &&
266 Q2QbarQfooQCONFLICT (rename/delete)QCONFLICT (rename/delete): foo renamed to bar in A^0, but deleted in B^0.
267 Q1QbarQCONFLICT (modify/delete)QCONFLICT (modify/delete): bar deleted in B^0 and modified in A^0. Version A^0 of bar left in tree.
270 test_cmp expect actual
274 # rename/add handling
276 # Commit A: modify foo, add different bar
277 # Commit B: modify & rename foo->bar
278 # Expected: CONFLICT(add/add) [via rename collide] for bar
280 test_expect_success
'rename/add handling' '
282 git init rename-add &&
285 test_write_lines original 1 2 3 4 5 >foo &&
287 git commit -m "original" &&
294 test_write_lines 1 2 3 4 5 >foo &&
295 echo "different file" >bar &&
297 git commit -m "Modify foo, add bar" &&
300 test_write_lines original 1 2 3 4 5 6 >foo &&
303 git commit -m "rename foo to bar"
310 git merge-tree -z A^0 B^0 >out &&
314 # First, check that the bar that appears at stage 3 does not
315 # correspond to an individual blob anywhere in history
317 hash=$(tr "\0" "\n" <out | head -n 3 | grep 3.bar | cut -f 2 -d " ") &&
318 git rev-list --objects --all >all_blobs &&
319 ! grep $hash all_blobs &&
322 # Second, check anonymized hash output against expectation
324 anonymize_hash out >actual &&
325 q_to_tab <<-\EOF | lf_to_nul >expect &&
331 q_to_nul <<-EOF >>expect &&
332 Q1QbarQAuto-mergingQAuto-merging bar
333 Q1QbarQCONFLICT (contents)QCONFLICT (add/add): Merge conflict in bar
334 Q1QfooQAuto-mergingQAuto-merging foo
337 test_cmp expect actual
341 # rename/add, where add is a mode conflict
343 # Commit A: modify foo, add symlink bar
344 # Commit B: modify & rename foo->bar
345 # Expected: CONFLICT(distinct modes) for bar
347 test_expect_success SYMLINKS
'rename/add, where add is a mode conflict' '
349 git init rename-add-symlink &&
351 cd rename-add-symlink &&
352 test_write_lines original 1 2 3 4 5 >foo &&
354 git commit -m "original" &&
361 test_write_lines 1 2 3 4 5 >foo &&
364 git commit -m "Modify foo, add symlink bar" &&
367 test_write_lines original 1 2 3 4 5 6 >foo &&
370 git commit -m "rename foo to bar"
374 cd rename-add-symlink &&
377 git merge-tree -z A^0 B^0 >out &&
381 # First, check that the bar that appears at stage 3 does not
382 # correspond to an individual blob anywhere in history
384 hash=$(tr "\0" "\n" <out | head -n 3 | grep 3.bar | cut -f 2 -d " ") &&
385 git rev-list --objects --all >all_blobs &&
386 ! grep $hash all_blobs &&
389 # Second, check anonymized hash output against expectation
391 anonymize_hash out >actual &&
392 q_to_tab <<-\EOF | lf_to_nul >expect &&
395 100644 HASH 3Qbar~B^0
398 q_to_nul <<-EOF >>expect &&
399 Q2QbarQbar~B^0QCONFLICT (distinct modes)QCONFLICT (distinct types): bar had different types on each side; renamed one of them so each can be recorded somewhere.
400 Q1QfooQAuto-mergingQAuto-merging foo
403 test_cmp expect actual
407 # rename/rename(1to2) + content conflict handling
409 # Commit A: modify foo & rename to bar
410 # Commit B: modify foo & rename to baz
411 # Expected: CONFLICT(rename/rename)
413 test_expect_success
'rename/rename + content conflict' '
415 git init rr-plus-content &&
417 cd rr-plus-content &&
418 test_write_lines 1 2 3 4 5 >foo &&
420 git commit -m "original" &&
427 test_write_lines 1 2 3 4 5 six >foo &&
430 git commit -m "Modify foo + rename to bar" &&
433 test_write_lines 1 2 3 4 5 6 >foo &&
436 git commit -m "Modify foo + rename to baz"
440 cd rr-plus-content &&
443 git merge-tree -z A^0 B^0 >out &&
445 anonymize_hash out >actual &&
446 q_to_tab <<-\EOF | lf_to_nul >expect &&
453 q_to_nul <<-EOF >>expect &&
454 Q1QfooQAuto-mergingQAuto-merging foo
455 Q3QfooQbarQbazQCONFLICT (rename/rename)QCONFLICT (rename/rename): foo renamed to bar in A^0 and to baz in B^0.
458 test_cmp expect actual
464 # Commit A: rm foo, add different bar
465 # Commit B: rename foo->bar
466 # Expected: CONFLICT (rename/delete), CONFLICT(add/add) [via rename collide]
469 test_expect_success
'rename/add/delete conflict' '
474 echo "original file" >foo &&
476 git commit -m "original" &&
484 echo "different file" >bar &&
486 git commit -m "Remove foo, add bar" &&
490 git commit -m "rename foo to bar"
497 git merge-tree -z B^0 A^0 >out &&
499 anonymize_hash out >actual &&
501 q_to_tab <<-\EOF | lf_to_nul >expect &&
508 q_to_nul <<-EOF >>expect &&
509 2QbarQfooQCONFLICT (rename/delete)QCONFLICT (rename/delete): foo renamed to bar in B^0, but deleted in A^0.
510 Q1QbarQAuto-mergingQAuto-merging bar
511 Q1QbarQCONFLICT (contents)QCONFLICT (add/add): Merge conflict in bar
514 test_cmp expect actual
518 # rename/rename(2to1)/delete/delete
520 # Commit A: rename foo->baz, rm bar
521 # Commit B: rename bar->baz, rm foo
522 # Expected: 2x CONFLICT (rename/delete), CONFLICT (add/add) via colliding
525 test_expect_success
'rename/rename(2to1)/delete/delete conflict' '
542 git commit -m "Rename foo, remove bar" &&
547 git commit -m "Rename bar, remove foo"
554 git merge-tree -z A^0 B^0 >out &&
556 anonymize_hash out >actual &&
558 q_to_tab <<-\EOF | lf_to_nul >expect &&
565 q_to_nul <<-EOF >>expect &&
566 2QbazQbarQCONFLICT (rename/delete)QCONFLICT (rename/delete): bar renamed to baz in B^0, but deleted in A^0.
567 Q2QbazQfooQCONFLICT (rename/delete)QCONFLICT (rename/delete): foo renamed to baz in A^0, but deleted in B^0.
568 Q1QbazQAuto-mergingQAuto-merging baz
569 Q1QbazQCONFLICT (contents)QCONFLICT (add/add): Merge conflict in baz
572 test_cmp expect actual
576 # mod6: chains of rename/rename(1to2) + add/add via colliding renames
577 # Commit O: one, three, five
578 # Commit A: one->two, three->four, five->six
579 # Commit B: one->six, three->two, five->four
580 # Expected: three CONFLICT(rename/rename) messages + three CONFLICT(add/add)
581 # messages; each path in two of the multi-way merged contents
582 # found in two, four, six
584 test_expect_success
'mod6: chains of rename/rename(1to2) and add/add via colliding renames' '
589 test_seq 11 19 >one &&
590 test_seq 31 39 >three &&
591 test_seq 51 59 >five &&
601 test_seq 10 19 >one &&
612 echo forty >>three &&
614 git add one three five &&
626 git merge-tree -z A^0 B^0 >out &&
630 # First, check that some of the hashes that appear as stage
631 # conflict entries do not appear as individual blobs anywhere
634 hash1=$(tr "\0" "\n" <out | head | grep 2.four | cut -f 2 -d " ") &&
635 hash2=$(tr "\0" "\n" <out | head | grep 3.two | cut -f 2 -d " ") &&
636 git rev-list --objects --all >all_blobs &&
637 ! grep $hash1 all_blobs &&
638 ! grep $hash2 all_blobs &&
641 # Now compare anonymized hash output with expectation
643 anonymize_hash out >actual &&
644 q_to_tab <<-\EOF | lf_to_nul >expect &&
658 q_to_nul <<-EOF >>expect &&
659 3QfiveQsixQfourQCONFLICT (rename/rename)QCONFLICT (rename/rename): five renamed to six in A^0 and to four in B^0.
660 Q1QfourQAuto-mergingQAuto-merging four
661 Q1QfourQCONFLICT (contents)QCONFLICT (add/add): Merge conflict in four
662 Q1QoneQAuto-mergingQAuto-merging one
663 Q3QoneQtwoQsixQCONFLICT (rename/rename)QCONFLICT (rename/rename): one renamed to two in A^0 and to six in B^0.
664 Q1QsixQAuto-mergingQAuto-merging six
665 Q1QsixQCONFLICT (contents)QCONFLICT (add/add): Merge conflict in six
666 Q1QthreeQAuto-mergingQAuto-merging three
667 Q3QthreeQfourQtwoQCONFLICT (rename/rename)QCONFLICT (rename/rename): three renamed to four in A^0 and to two in B^0.
668 Q1QtwoQAuto-mergingQAuto-merging two
669 Q1QtwoQCONFLICT (contents)QCONFLICT (add/add): Merge conflict in two
672 test_cmp expect actual
676 # directory rename + rename/delete + modify/delete + directory/file conflict
677 # Commit O: foo, olddir/{a,b,c}
678 # Commit A: delete foo, rename olddir/ -> newdir/, add newdir/bar/file
679 # Commit B: modify foo & rename foo -> olddir/bar
680 # Expected: CONFLICT(content) for newdir/bar (not olddir/bar or foo)
682 test_expect_success
'directory rename + rename/delete + modify/delete + directory/file conflict' '
684 git init 4-stacked-conflict &&
686 cd 4-stacked-conflict &&
687 test_write_lines 1 2 3 4 5 >foo &&
689 for i in a b c; do echo $i >olddir/$i || exit 1; done &&
690 git add foo olddir &&
691 git commit -m "original" &&
699 git mv olddir newdir &&
702 git add newdir/bar/file &&
703 git commit -m "rm foo, olddir/ -> newdir/, + newdir/bar/file" &&
706 test_write_lines 1 2 3 4 5 6 >foo &&
708 git mv foo olddir/bar &&
709 git commit -m "Modify foo & rename foo -> olddir/bar"
713 cd 4-stacked-conflict &&
716 git merge-tree -z A^0 B^0 >out &&
718 anonymize_hash out >actual &&
720 q_to_tab <<-\EOF | lf_to_nul >expect &&
722 100644 HASH 1Qnewdir/bar~B^0
723 100644 HASH 3Qnewdir/bar~B^0
726 q_to_nul <<-EOF >>expect &&
727 Q2Qnewdir/barQolddir/barQCONFLICT (directory rename suggested)QCONFLICT (file location): foo renamed to olddir/bar in B^0, inside a directory that was renamed in A^0, suggesting it should perhaps be moved to newdir/bar.
728 Q2Qnewdir/barQfooQCONFLICT (rename/delete)QCONFLICT (rename/delete): foo renamed to newdir/bar in B^0, but deleted in A^0.
729 Q2Qnewdir/bar~B^0Qnewdir/barQCONFLICT (file/directory)QCONFLICT (file/directory): directory in the way of newdir/bar from B^0; moving it to newdir/bar~B^0 instead.
730 Q1Qnewdir/bar~B^0QCONFLICT (modify/delete)QCONFLICT (modify/delete): newdir/bar~B^0 deleted in A^0 and modified in B^0. Version B^0 of newdir/bar~B^0 left in tree.
733 test_cmp expect actual
737 for opt
in $
(git merge-tree
--git-completion-helper-all)
739 if test $opt = "--trivial-merge" ||
test $opt = "--write-tree"
744 test_expect_success
"usage: --trivial-merge is incompatible with $opt" '
745 test_expect_code 128 git merge-tree --trivial-merge $opt side1 side2 side3
749 test_expect_success
'Just the conflicted files without the messages' '
750 test_expect_code 1 git merge-tree --write-tree --no-messages --name-only side1 side2 >out &&
751 anonymize_hash out >actual &&
753 test_write_lines HASH greeting whatever~side1 >expect &&
755 test_cmp expect actual
758 test_expect_success
'Check conflicted oids and modes without messages' '
759 test_expect_code 1 git merge-tree --write-tree --no-messages side1 side2 >out &&
760 anonymize_hash out >actual &&
762 # Compare the basic output format
763 q_to_tab >expect <<-\EOF &&
765 100644 HASH 1Qgreeting
766 100644 HASH 2Qgreeting
767 100644 HASH 3Qgreeting
768 100644 HASH 1Qwhatever~side1
769 100644 HASH 2Qwhatever~side1
772 test_cmp expect actual &&
774 # Check the actual hashes against the `ls-files -u` output too
775 tail -n +2 out | sed -e s/side1/HEAD/ >actual &&
776 test_cmp conflicted-file-info actual
779 test_expect_success
'NUL terminated conflicted file "lines"' '
780 git checkout -b tweak1 side1 &&
781 test_write_lines zero 1 2 3 4 5 6 >numbers &&
783 git mv numbers "Αυτά μου φαίνονται κινέζικα" &&
784 git commit -m "Renamed numbers" &&
786 test_expect_code 1 git merge-tree --write-tree -z tweak1 side2 >out &&
788 anonymize_hash out >actual &&
791 # "greeting" should merge with conflicts
792 # "whatever" has *both* a modify/delete and a file/directory conflict
793 # "Αυτά μου φαίνονται κινέζικα" should have a conflict
794 echo HASH | lf_to_nul >expect &&
796 q_to_tab <<-EOF | lf_to_nul >>expect &&
797 100644 HASH 1Qgreeting
798 100644 HASH 2Qgreeting
799 100644 HASH 3Qgreeting
800 100644 HASH 1Qwhatever~tweak1
801 100644 HASH 2Qwhatever~tweak1
802 100644 HASH 1QΑυτά μου φαίνονται κινέζικα
803 100644 HASH 2QΑυτά μου φαίνονται κινέζικα
804 100644 HASH 3QΑυτά μου φαίνονται κινέζικα
808 q_to_nul <<-EOF >>expect &&
809 1QgreetingQAuto-mergingQAuto-merging greeting
810 Q1QgreetingQCONFLICT (contents)QCONFLICT (content): Merge conflict in greeting
811 Q2Qwhatever~tweak1QwhateverQCONFLICT (file/directory)QCONFLICT (file/directory): directory in the way of whatever from tweak1; moving it to whatever~tweak1 instead.
812 Q1Qwhatever~tweak1QCONFLICT (modify/delete)QCONFLICT (modify/delete): whatever~tweak1 deleted in side2 and modified in tweak1. Version tweak1 of whatever~tweak1 left in tree.
813 Q1QΑυτά μου φαίνονται κινέζικαQAuto-mergingQAuto-merging Αυτά μου φαίνονται κινέζικα
814 Q1QΑυτά μου φαίνονται κινέζικαQCONFLICT (contents)QCONFLICT (content): Merge conflict in Αυτά μου φαίνονται κινέζικα
818 test_cmp expect actual
821 test_expect_success
'error out by default for unrelated histories' '
822 test_expect_code 128 git merge-tree --write-tree side1 unrelated 2>error &&
824 grep "refusing to merge unrelated histories" error
827 test_expect_success
'can override merge of unrelated histories' '
828 git merge-tree --write-tree --allow-unrelated-histories side1 unrelated >tree &&
831 git rev-parse side1:numbers side1:greeting side1:whatever unrelated:something-else >expect &&
832 git rev-parse $TREE:numbers $TREE:greeting $TREE:whatever $TREE:something-else >actual &&
834 test_cmp expect actual
837 test_expect_success SANITY
'merge-ort fails gracefully in a read-only repository' '
838 git init --bare read-only &&
839 git push read-only side1 side2 side3 &&
840 test_when_finished "chmod -R u+w read-only" &&
841 chmod -R a-w read-only &&
842 test_must_fail git -C read-only merge-tree side1 side3 &&
843 test_must_fail git -C read-only merge-tree side1 side2
846 test_expect_success
'--stdin with both a successful and a conflicted merge' '
847 printf "side1 side3\nside1 side2" | git merge-tree --stdin >actual &&
849 git checkout side1^0 &&
852 printf "1\0" >expect &&
853 git rev-parse HEAD^{tree} | lf_to_nul >>expect &&
854 printf "\0" >>expect &&
856 git checkout side1^0 &&
857 test_must_fail git merge side2 &&
858 sed s/HEAD/side1/ greeting >tmp &&
861 git mv whatever~HEAD whatever~side1 &&
863 printf "0\0" >>expect &&
864 git write-tree | lf_to_nul >>expect &&
866 cat <<-EOF | q_to_tab | lf_to_nul >>expect &&
867 100644 $(git rev-parse side1~1:greeting) 1Qgreeting
868 100644 $(git rev-parse side1:greeting) 2Qgreeting
869 100644 $(git rev-parse side2:greeting) 3Qgreeting
870 100644 $(git rev-parse side1~1:whatever) 1Qwhatever~side1
871 100644 $(git rev-parse side1:whatever) 2Qwhatever~side1
874 q_to_nul <<-EOF >>expect &&
875 Q1QgreetingQAuto-mergingQAuto-merging greeting
876 Q1QgreetingQCONFLICT (contents)QCONFLICT (content): Merge conflict in greeting
877 Q1QnumbersQAuto-mergingQAuto-merging numbers
878 Q2Qwhatever~side1QwhateverQCONFLICT (file/directory)QCONFLICT (file/directory): directory in the way of whatever from side1; moving it to whatever~side1 instead.
879 Q1Qwhatever~side1QCONFLICT (modify/delete)QCONFLICT (modify/delete): whatever~side1 deleted in side2 and modified in side1. Version side1 of whatever~side1 left in tree.
882 printf "\0\0" >>expect &&
884 test_cmp expect actual
888 test_expect_success
'--merge-base is incompatible with --stdin' '
889 test_must_fail git merge-tree --merge-base=side1 --stdin 2>expect &&
891 grep "^fatal: .*merge-base.*stdin.* cannot be used together" expect
894 # specify merge-base as parent of branch2
895 # git merge-tree --write-tree --merge-base=c2 c1 c3
896 # Commit c1: add file1
897 # Commit c2: add file2 after c1
898 # Commit c3: add file3 after c2
899 # Expected: add file3, and file2 does NOT appear
901 test_expect_success
'specify merge-base as parent of branch2' '
903 test_when_finished "rm -rf base-b2-p" &&
904 git init base-b2-p &&
905 test_commit -C base-b2-p c1 file1 &&
906 test_commit -C base-b2-p c2 file2 &&
907 test_commit -C base-b2-p c3 file3 &&
910 TREE_OID=$(git -C base-b2-p merge-tree --write-tree --merge-base=c2 c1 c3) &&
912 q_to_tab <<-EOF >expect &&
913 100644 blob $(git -C base-b2-p rev-parse c1:file1)Qfile1
914 100644 blob $(git -C base-b2-p rev-parse c3:file3)Qfile3
917 git -C base-b2-p ls-tree $TREE_OID >actual &&
918 test_cmp expect actual
921 # Since the earlier tests have verified that individual merge-tree calls
922 # are doing the right thing, this test case is only used to verify that
923 # we can also trigger merges via --stdin, and that when we do we get
924 # the same answer as running a bunch of separate merges.
926 test_expect_success
'check the input format when --stdin is passed' '
927 test_when_finished "rm -rf repo" &&
929 test_commit -C repo c1 &&
930 test_commit -C repo c2 &&
931 test_commit -C repo c3 &&
932 printf "c1 c3\nc2 -- c1 c3\nc2 c3" | git -C repo merge-tree --stdin >actual &&
934 printf "1\0" >expect &&
935 git -C repo merge-tree --write-tree -z c1 c3 >>expect &&
936 printf "\0" >>expect &&
938 printf "1\0" >>expect &&
939 git -C repo merge-tree --write-tree -z --merge-base=c2 c1 c3 >>expect &&
940 printf "\0" >>expect &&
942 printf "1\0" >>expect &&
943 git -C repo merge-tree --write-tree -z c2 c3 >>expect &&
944 printf "\0" >>expect &&
946 test_cmp expect actual
949 test_expect_success
'--merge-base with tree OIDs' '
950 git merge-tree --merge-base=side1^ side1 side3 >with-commits &&
951 git merge-tree --merge-base=side1^^{tree} side1^{tree} side3^{tree} >with-trees &&
952 test_cmp with-commits with-trees
955 test_expect_success
'error out on missing tree objects' '
956 git init --bare missing-tree.git &&
957 git rev-list side3 >list &&
958 git rev-parse side3^: >>list &&
959 git pack-objects missing-tree.git/objects/pack/side3-tree-is-missing <list &&
960 side3=$(git rev-parse side3) &&
961 test_must_fail git --git-dir=missing-tree.git merge-tree $side3^ $side3 >actual 2>err &&
962 test_grep "Could not read $(git rev-parse $side3:)" err &&
963 test_must_be_empty actual
966 test_expect_success
'error out on missing blob objects' '
967 echo 1 | git hash-object -w --stdin >blob1 &&
968 echo 2 | git hash-object -w --stdin >blob2 &&
969 echo 3 | git hash-object -w --stdin >blob3 &&
970 printf "100644 blob $(cat blob1)\tblob\n" | git mktree >tree1 &&
971 printf "100644 blob $(cat blob2)\tblob\n" | git mktree >tree2 &&
972 printf "100644 blob $(cat blob3)\tblob\n" | git mktree >tree3 &&
973 git init --bare missing-blob.git &&
974 cat blob1 blob3 tree1 tree2 tree3 |
975 git pack-objects missing-blob.git/objects/pack/side1-whatever-is-missing &&
976 test_must_fail git --git-dir=missing-blob.git >actual 2>err \
977 merge-tree --merge-base=$(cat tree1) $(cat tree2) $(cat tree3) &&
978 test_grep "unable to read blob object $(cat blob2)" err &&
979 test_must_be_empty actual
982 test_expect_success
'error out on missing commits as well' '
983 git init --bare missing-commit.git &&
984 git rev-list --objects side1 side3 >list-including-initial &&
985 grep -v ^$(git rev-parse side1^) <list-including-initial >list &&
986 git pack-objects missing-commit.git/objects/pack/missing-initial <list &&
987 side1=$(git rev-parse side1) &&
988 side3=$(git rev-parse side3) &&
989 test_must_fail git --git-dir=missing-commit.git \
990 merge-tree --allow-unrelated-histories $side1 $side3 >actual &&
991 test_must_be_empty actual