3 # Copyright (c) 2010 Will Palmer
6 test_description
='git merge-tree'
8 TEST_PASSES_SANITIZE_LEAK
=true
11 test_expect_success setup
'
12 test_commit "initial" "initial-file" "initial"
15 test_expect_success
'file add A, !B' '
16 git reset --hard initial &&
17 test_commit "add-a-not-b" "ONE" "AAA" &&
18 git merge-tree initial initial add-a-not-b >actual &&
19 cat >expected <<EXPECTED &&
21 their 100644 $(git rev-parse HEAD:ONE) ONE
26 test_cmp expected actual
29 test_expect_success
'file add !A, B' '
30 git reset --hard initial &&
31 test_commit "add-not-a-b" "ONE" "AAA" &&
32 git merge-tree initial add-not-a-b initial >actual &&
33 test_must_be_empty actual
36 test_expect_success
'file add A, B (same)' '
37 git reset --hard initial &&
38 test_commit "add-a-b-same-A" "ONE" "AAA" &&
39 git reset --hard initial &&
40 test_commit "add-a-b-same-B" "ONE" "AAA" &&
41 git merge-tree initial add-a-b-same-A add-a-b-same-B >actual &&
42 test_must_be_empty actual
45 test_expect_success
'file add A, B (different)' '
46 git reset --hard initial &&
47 test_commit "add-a-b-diff-A" "ONE" "AAA" &&
48 git reset --hard initial &&
49 test_commit "add-a-b-diff-B" "ONE" "BBB" &&
50 git merge-tree initial add-a-b-diff-A add-a-b-diff-B >actual &&
51 cat >expected <<EXPECTED &&
53 our 100644 $(git rev-parse add-a-b-diff-A:ONE) ONE
54 their 100644 $(git rev-parse add-a-b-diff-B:ONE) ONE
63 test_cmp expected actual
66 test_expect_success
'file change A, !B' '
67 git reset --hard initial &&
68 test_commit "change-a-not-b" "initial-file" "BBB" &&
69 git merge-tree initial change-a-not-b initial >actual &&
70 test_must_be_empty actual
73 test_expect_success
'file change !A, B' '
74 git reset --hard initial &&
75 test_commit "change-not-a-b" "initial-file" "BBB" &&
76 git merge-tree initial initial change-not-a-b >actual &&
77 cat >expected <<EXPECTED &&
79 result 100644 $(git rev-parse change-a-not-b:initial-file) initial-file
80 our 100644 $(git rev-parse initial:initial-file ) initial-file
86 test_cmp expected actual
89 test_expect_success
'3-way merge with --attr-source' '
90 test_when_finished rm -rf 3-way &&
94 test_commit initial file1 foo &&
95 base=$(git rev-parse HEAD) &&
96 git checkout -b brancha &&
98 git commit -am "adding bar" &&
99 source=$(git rev-parse HEAD) &&
100 git checkout @{-1} &&
101 git checkout -b branchb &&
103 git commit -am "adding baz" &&
104 merge=$(git rev-parse HEAD) &&
105 git checkout -b gitattributes &&
106 test_commit "gitattributes" .gitattributes "file1 merge=union" &&
107 git checkout @{-1} &&
108 tree=$(git --attr-source=gitattributes merge-tree --write-tree \
109 --merge-base "$base" --end-of-options "$source" "$merge") &&
110 test_write_lines foo bar baz >expect &&
111 git cat-file -p "$tree:file1" >actual &&
112 test_cmp expect actual
116 test_expect_success
'file change A, B (same)' '
117 git reset --hard initial &&
118 test_commit "change-a-b-same-A" "initial-file" "AAA" &&
119 git reset --hard initial &&
120 test_commit "change-a-b-same-B" "initial-file" "AAA" &&
121 git merge-tree initial change-a-b-same-A change-a-b-same-B >actual &&
122 test_must_be_empty actual
125 test_expect_success
'file change A, B (different)' '
126 git reset --hard initial &&
127 test_commit "change-a-b-diff-A" "initial-file" "AAA" &&
128 git reset --hard initial &&
129 test_commit "change-a-b-diff-B" "initial-file" "BBB" &&
130 git merge-tree initial change-a-b-diff-A change-a-b-diff-B >actual &&
131 cat >expected <<EXPECTED &&
133 base 100644 $(git rev-parse initial:initial-file ) initial-file
134 our 100644 $(git rev-parse change-a-b-diff-A:initial-file) initial-file
135 their 100644 $(git rev-parse change-a-b-diff-B:initial-file) initial-file
144 test_cmp expected actual
147 test_expect_success
'file change A, B (mixed)' '
148 git reset --hard initial &&
149 test_commit "change-a-b-mix-base" "ONE" "
165 test_commit "change-a-b-mix-A" "ONE" \
166 "$(sed -e "1{s/AAA/BBB/;}" -e "10{s/AAA/BBB/;}" <ONE)" &&
167 git reset --hard change-a-b-mix-base &&
168 test_commit "change-a-b-mix-B" "ONE" \
169 "$(sed -e "1{s/AAA/BBB/;}" -e "10{s/AAA/CCC/;}" <ONE)" &&
170 git merge-tree change-a-b-mix-base change-a-b-mix-A change-a-b-mix-B \
173 cat >expected <<EXPECTED &&
175 base 100644 $(git rev-parse change-a-b-mix-base:ONE) ONE
176 our 100644 $(git rev-parse change-a-b-mix-A:ONE ) ONE
177 their 100644 $(git rev-parse change-a-b-mix-B:ONE ) ONE
192 test_cmp expected actual
195 test_expect_success
'file remove A, !B' '
196 git reset --hard initial &&
197 test_commit "rm-a-not-b-base" "ONE" "AAA" &&
199 git commit -m "rm-a-not-b" &&
200 git tag "rm-a-not-b" &&
201 git merge-tree rm-a-not-b-base rm-a-not-b rm-a-not-b-base >actual &&
202 test_must_be_empty actual
205 test_expect_success
'file remove !A, B' '
206 git reset --hard initial &&
207 test_commit "rm-not-a-b-base" "ONE" "AAA" &&
209 git commit -m "rm-not-a-b" &&
210 git tag "rm-not-a-b" &&
211 git merge-tree rm-a-not-b-base rm-a-not-b-base rm-a-not-b >actual &&
212 cat >expected <<EXPECTED &&
214 base 100644 $(git rev-parse rm-a-not-b-base:ONE) ONE
215 our 100644 $(git rev-parse rm-a-not-b-base:ONE) ONE
220 test_cmp expected actual
223 test_expect_success
'file remove A, B (same)' '
224 git reset --hard initial &&
225 test_commit "rm-a-b-base" "ONE" "AAA" &&
227 git commit -m "rm-a-b" &&
229 git merge-tree rm-a-b-base rm-a-b rm-a-b >actual &&
230 test_must_be_empty actual
233 test_expect_success
'file change A, remove B' '
234 git reset --hard initial &&
235 test_commit "change-a-rm-b-base" "ONE" "AAA" &&
236 test_commit "change-a-rm-b-A" "ONE" "BBB" &&
237 git reset --hard change-a-rm-b-base &&
239 git commit -m "change-a-rm-b-B" &&
240 git tag "change-a-rm-b-B" &&
241 git merge-tree change-a-rm-b-base change-a-rm-b-A change-a-rm-b-B \
243 cat >expected <<EXPECTED &&
245 base 100644 $(git rev-parse change-a-rm-b-base:ONE) ONE
246 our 100644 $(git rev-parse change-a-rm-b-A:ONE ) ONE
251 test_cmp expected actual
254 test_expect_success
'file remove A, change B' '
255 git reset --hard initial &&
256 test_commit "rm-a-change-b-base" "ONE" "AAA" &&
259 git commit -m "rm-a-change-b-A" &&
260 git tag "rm-a-change-b-A" &&
261 git reset --hard rm-a-change-b-base &&
262 test_commit "rm-a-change-b-B" "ONE" "BBB" &&
263 git merge-tree rm-a-change-b-base rm-a-change-b-A rm-a-change-b-B \
265 cat >expected <<EXPECTED &&
267 base 100644 $(git rev-parse rm-a-change-b-base:ONE) ONE
268 their 100644 $(git rev-parse rm-a-change-b-B:ONE ) ONE
270 test_cmp expected actual
273 test_expect_success
'tree add A, B (same)' '
274 git reset --hard initial &&
276 test_commit "add sub/file" "sub/file" "file" add-tree-A &&
277 git merge-tree initial add-tree-A add-tree-A >actual &&
278 test_must_be_empty actual
281 test_expect_success
'tree add A, B (different)' '
282 git reset --hard initial &&
284 test_commit "add sub/file" "sub/file" "AAA" add-tree-a-b-A &&
285 git reset --hard initial &&
287 test_commit "add sub/file" "sub/file" "BBB" add-tree-a-b-B &&
288 git merge-tree initial add-tree-a-b-A add-tree-a-b-B >actual &&
289 cat >expect <<-EOF &&
291 our 100644 $(git rev-parse add-tree-a-b-A:sub/file) sub/file
292 their 100644 $(git rev-parse add-tree-a-b-B:sub/file) sub/file
300 test_cmp expect actual
303 test_expect_success
'tree unchanged A, removed B' '
304 git reset --hard initial &&
306 test_commit "add sub/file" "sub/file" "AAA" tree-remove-b-initial &&
309 git commit -m "remove sub/file" &&
310 git tag tree-remove-b-B &&
311 git merge-tree tree-remove-b-initial tree-remove-b-initial tree-remove-b-B >actual &&
312 cat >expect <<-EOF &&
314 base 100644 $(git rev-parse tree-remove-b-initial:sub/file) sub/file
315 our 100644 $(git rev-parse tree-remove-b-initial:sub/file) sub/file
319 test_cmp expect actual
322 test_expect_success
'turn file to tree' '
323 git reset --hard initial &&
325 mkdir initial-file &&
326 test_commit "turn-file-to-tree" "initial-file/ONE" "CCC" &&
327 git merge-tree initial initial turn-file-to-tree >actual &&
328 cat >expect <<-EOF &&
330 their 100644 $(git rev-parse turn-file-to-tree:initial-file/ONE) initial-file/ONE
334 base 100644 $(git rev-parse initial:initial-file) initial-file
335 our 100644 $(git rev-parse initial:initial-file) initial-file
339 test_cmp expect actual
342 test_expect_success
'turn tree to file' '
343 git reset --hard initial &&
345 test_commit "add-tree" "dir/path" "AAA" &&
346 test_commit "add-another-tree" "dir/another" "BBB" &&
348 test_commit "make-file" "dir" "CCC" &&
349 git merge-tree add-tree add-another-tree make-file >actual &&
350 cat >expect <<-EOF &&
352 base 100644 $(git rev-parse add-tree:dir/path) dir/path
353 our 100644 $(git rev-parse add-tree:dir/path) dir/path
357 their 100644 $(git rev-parse make-file:dir) dir
361 test_cmp expect actual
364 test_expect_success
'merge-tree respects core.useReplaceRefs=false' '
365 test_commit merge-to &&
366 test_commit valid base &&
367 git reset --hard HEAD^ &&
368 test_commit malicious base &&
370 test_when_finished "git replace -d $(git rev-parse valid^0)" &&
371 git replace valid^0 malicious^0 &&
373 tree=$(git -c core.useReplaceRefs=true merge-tree --write-tree merge-to valid) &&
374 merged=$(git cat-file -p $tree:base) &&
375 test malicious = $merged &&
377 tree=$(git -c core.useReplaceRefs=false merge-tree --write-tree merge-to valid) &&
378 merged=$(git cat-file -p $tree:base) &&