Merge branch 'gc/bare-repo-discovery'
[git/debian.git] / t / t4301-merge-tree-write-tree.sh
blobf091259a55eba49fd632694b08495a4b8e8b0429
1 #!/bin/sh
3 test_description='git merge-tree --write-tree'
5 . ./test-lib.sh
7 # This test is ort-specific
8 if test "$GIT_TEST_MERGE_ALGORITHM" != "ort"
9 then
10 skip_all="GIT_TEST_MERGE_ALGORITHM != ort"
11 test_done
14 test_expect_success setup '
15 test_write_lines 1 2 3 4 5 >numbers &&
16 echo hello >greeting &&
17 echo foo >whatever &&
18 git add numbers greeting whatever &&
19 test_tick &&
20 git commit -m initial &&
22 git branch side1 &&
23 git branch side2 &&
24 git branch side3 &&
26 git checkout side1 &&
27 test_write_lines 1 2 3 4 5 6 >numbers &&
28 echo hi >greeting &&
29 echo bar >whatever &&
30 git add numbers greeting whatever &&
31 test_tick &&
32 git commit -m modify-stuff &&
34 git checkout side2 &&
35 test_write_lines 0 1 2 3 4 5 >numbers &&
36 echo yo >greeting &&
37 git rm whatever &&
38 mkdir whatever &&
39 >whatever/empty &&
40 git add numbers greeting whatever/empty &&
41 test_tick &&
42 git commit -m other-modifications &&
44 git checkout side3 &&
45 git mv numbers sequence &&
46 test_tick &&
47 git commit -m rename-numbers &&
49 git switch --orphan unrelated &&
50 >something-else &&
51 git add something-else &&
52 test_tick &&
53 git commit -m first-commit
56 test_expect_success 'Clean merge' '
57 TREE_OID=$(git merge-tree --write-tree side1 side3) &&
58 q_to_tab <<-EOF >expect &&
59 100644 blob $(git rev-parse side1:greeting)Qgreeting
60 100644 blob $(git rev-parse side1:numbers)Qsequence
61 100644 blob $(git rev-parse side1:whatever)Qwhatever
62 EOF
64 git ls-tree $TREE_OID >actual &&
65 test_cmp expect actual
68 test_expect_success 'Content merge and a few conflicts' '
69 git checkout side1^0 &&
70 test_must_fail git merge side2 &&
71 expected_tree=$(git rev-parse AUTO_MERGE) &&
73 # We will redo the merge, while we are still in a conflicted state!
74 git ls-files -u >conflicted-file-info &&
75 test_when_finished "git reset --hard" &&
77 test_expect_code 1 git merge-tree --write-tree side1 side2 >RESULT &&
78 actual_tree=$(head -n 1 RESULT) &&
80 # Due to differences of e.g. "HEAD" vs "side1", the results will not
81 # exactly match. Dig into individual files.
83 # Numbers should have three-way merged cleanly
84 test_write_lines 0 1 2 3 4 5 6 >expect &&
85 git show ${actual_tree}:numbers >actual &&
86 test_cmp expect actual &&
88 # whatever and whatever~<branch> should have same HASHES
89 git rev-parse ${expected_tree}:whatever ${expected_tree}:whatever~HEAD >expect &&
90 git rev-parse ${actual_tree}:whatever ${actual_tree}:whatever~side1 >actual &&
91 test_cmp expect actual &&
93 # greeting should have a merge conflict
94 git show ${expected_tree}:greeting >tmp &&
95 sed -e s/HEAD/side1/ tmp >expect &&
96 git show ${actual_tree}:greeting >actual &&
97 test_cmp expect actual
100 test_expect_success 'Barf on misspelled option, with exit code other than 0 or 1' '
101 # Mis-spell with single "s" instead of double "s"
102 test_expect_code 129 git merge-tree --write-tree --mesages FOOBAR side1 side2 2>expect &&
104 grep "error: unknown option.*mesages" expect
107 test_expect_success 'Barf on too many arguments' '
108 test_expect_code 129 git merge-tree --write-tree side1 side2 invalid 2>expect &&
110 grep "^usage: git merge-tree" expect
113 anonymize_hash() {
114 sed -e "s/[0-9a-f]\{40,\}/HASH/g" "$@"
117 test_expect_success 'test conflict notices and such' '
118 test_expect_code 1 git merge-tree --write-tree --name-only side1 side2 >out &&
119 anonymize_hash out >actual &&
121 # Expected results:
122 # "greeting" should merge with conflicts
123 # "numbers" should merge cleanly
124 # "whatever" has *both* a modify/delete and a file/directory conflict
125 cat <<-EOF >expect &&
126 HASH
127 greeting
128 whatever~side1
130 Auto-merging greeting
131 CONFLICT (content): Merge conflict in greeting
132 Auto-merging numbers
133 CONFLICT (file/directory): directory in the way of whatever from side1; moving it to whatever~side1 instead.
134 CONFLICT (modify/delete): whatever~side1 deleted in side2 and modified in side1. Version side1 of whatever~side1 left in tree.
137 test_cmp expect actual
140 for opt in $(git merge-tree --git-completion-helper-all)
142 if test $opt = "--trivial-merge" || test $opt = "--write-tree"
143 then
144 continue
147 test_expect_success "usage: --trivial-merge is incompatible with $opt" '
148 test_expect_code 128 git merge-tree --trivial-merge $opt side1 side2 side3
150 done
152 test_expect_success 'Just the conflicted files without the messages' '
153 test_expect_code 1 git merge-tree --write-tree --no-messages --name-only side1 side2 >out &&
154 anonymize_hash out >actual &&
156 test_write_lines HASH greeting whatever~side1 >expect &&
158 test_cmp expect actual
161 test_expect_success 'Check conflicted oids and modes without messages' '
162 test_expect_code 1 git merge-tree --write-tree --no-messages side1 side2 >out &&
163 anonymize_hash out >actual &&
165 # Compare the basic output format
166 q_to_tab >expect <<-\EOF &&
167 HASH
168 100644 HASH 1Qgreeting
169 100644 HASH 2Qgreeting
170 100644 HASH 3Qgreeting
171 100644 HASH 1Qwhatever~side1
172 100644 HASH 2Qwhatever~side1
175 test_cmp expect actual &&
177 # Check the actual hashes against the `ls-files -u` output too
178 tail -n +2 out | sed -e s/side1/HEAD/ >actual &&
179 test_cmp conflicted-file-info actual
182 test_expect_success 'NUL terminated conflicted file "lines"' '
183 git checkout -b tweak1 side1 &&
184 test_write_lines zero 1 2 3 4 5 6 >numbers &&
185 git add numbers &&
186 git mv numbers "Αυτά μου φαίνονται κινέζικα" &&
187 git commit -m "Renamed numbers" &&
189 test_expect_code 1 git merge-tree --write-tree -z tweak1 side2 >out &&
190 anonymize_hash out >actual &&
191 printf "\\n" >>actual &&
193 # Expected results:
194 # "greeting" should merge with conflicts
195 # "whatever" has *both* a modify/delete and a file/directory conflict
196 # "Αυτά μου φαίνονται κινέζικα" should have a conflict
197 echo HASH | lf_to_nul >expect &&
199 q_to_tab <<-EOF | lf_to_nul >>expect &&
200 100644 HASH 1Qgreeting
201 100644 HASH 2Qgreeting
202 100644 HASH 3Qgreeting
203 100644 HASH 1Qwhatever~tweak1
204 100644 HASH 2Qwhatever~tweak1
205 100644 HASH 1QΑυτά μου φαίνονται κινέζικα
206 100644 HASH 2QΑυτά μου φαίνονται κινέζικα
207 100644 HASH 3QΑυτά μου φαίνονται κινέζικα
211 q_to_nul <<-EOF >>expect &&
212 1QgreetingQAuto-mergingQAuto-merging greeting
213 Q1QgreetingQCONFLICT (contents)QCONFLICT (content): Merge conflict in greeting
214 Q2Qwhatever~tweak1QwhateverQCONFLICT (file/directory)QCONFLICT (file/directory): directory in the way of whatever from tweak1; moving it to whatever~tweak1 instead.
215 Q1Qwhatever~tweak1QCONFLICT (modify/delete)QCONFLICT (modify/delete): whatever~tweak1 deleted in side2 and modified in tweak1. Version tweak1 of whatever~tweak1 left in tree.
216 Q1QΑυτά μου φαίνονται κινέζικαQAuto-mergingQAuto-merging Αυτά μου φαίνονται κινέζικα
217 Q1QΑυτά μου φαίνονται κινέζικαQCONFLICT (contents)QCONFLICT (content): Merge conflict in Αυτά μου φαίνονται κινέζικα
221 test_cmp expect actual
224 test_expect_success 'error out by default for unrelated histories' '
225 test_expect_code 128 git merge-tree --write-tree side1 unrelated 2>error &&
227 grep "refusing to merge unrelated histories" error
230 test_expect_success 'can override merge of unrelated histories' '
231 git merge-tree --write-tree --allow-unrelated-histories side1 unrelated >tree &&
232 TREE=$(cat tree) &&
234 git rev-parse side1:numbers side1:greeting side1:whatever unrelated:something-else >expect &&
235 git rev-parse $TREE:numbers $TREE:greeting $TREE:whatever $TREE:something-else >actual &&
237 test_cmp expect actual
240 test_done