1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano
4 #
6 test_description='Three way merge with read-tree -m
8 This test tries three-way merge with read-tree -m
10 There is one ancestor (called O for Original) and two branches A
11 and B derived from it. We want to do a 3-way merge between A and
12 B, using O as the common ancestor.
14 merge A O B
16 Decisions are made by comparing contents of O, A and B pathname
17 by pathname. The result is determined by the following guiding
18 principle:
20 - If only A does something to it and B does not touch it, take
21 whatever A does.
23 - If only B does something to it and A does not touch it, take
24 whatever B does.
26 - If both A and B does something but in the same way, take
27 whatever they do.
29 - If A and B does something but different things, we need a
30 3-way merge:
32 - We cannot do anything about the following cases:
34 * O does not have it. A and B both must be adding to the
35 same path independently.
37 * A deletes it. B must be modifying.
39 - Otherwise, A and B are modifying. Run 3-way merge.
41 First, the case matrix.
43 - Vertical axis is for A'\''s actions.
44 - Horizontal axis is for B'\''s actions.
46 .----------------------------------------------------------------.
47 | A B | No Action | Delete | Modify | Add |
48 |------------+------------+------------+------------+------------|
49 | No Action | | | | |
50 | | select O | delete | select B | select B |
51 | | | | | |
52 |------------+------------+------------+------------+------------|
53 | Delete | | | ********** | can |
54 | | delete | delete | merge | not |
55 | | | | | happen |
56 |------------+------------+------------+------------+------------|
57 | Modify | | ********** | ?????????? | can |
58 | | select A | merge | select A=B | not |
59 | | | | merge | happen |
60 |------------+------------+------------+------------+------------|
61 | Add | | can | can | ?????????? |
62 | | select A | not | not | select A=B |
63 | | | happen | happen | merge |
64 .----------------------------------------------------------------.
66 In addition:
68 SS: a special case of MM, where A and B makes the same modification.
69 LL: a special case of AA, where A and B creates the same file.
70 TT: a special case of MM, where A and B makes mergeable changes.
71 DF: a special case, where A makes a directory and B makes a file.
73 '
74 . ./test-lib.sh
75 . ../lib-read-tree-m-3way.sh
77 ################################################################
78 # Trivial "majority when 3 stages exist" merge plus #5ALT trivial
79 # merge.
81 cat >expected <<\EOF
82 100644 X 2 AA
83 100644 X 3 AA
84 100644 X 2 AN
85 100644 X 1 DD
86 100644 X 3 DF
87 100644 X 2 DF/DF
88 100644 X 1 DM
89 100644 X 3 DM
90 100644 X 1 DN
91 100644 X 3 DN
92 100644 X 0 LL
93 100644 X 1 MD
94 100644 X 2 MD
95 100644 X 1 MM
96 100644 X 2 MM
97 100644 X 3 MM
98 100644 X 0 MN
99 100644 X 3 NA
100 100644 X 1 ND
101 100644 X 2 ND
102 100644 X 0 NM
103 100644 X 0 NN
104 100644 X 0 SS
105 100644 X 1 TT
106 100644 X 2 TT
107 100644 X 3 TT
108 100644 X 2 Z/AA
109 100644 X 3 Z/AA
110 100644 X 2 Z/AN
111 100644 X 1 Z/DD
112 100644 X 1 Z/DM
113 100644 X 3 Z/DM
114 100644 X 1 Z/DN
115 100644 X 3 Z/DN
116 100644 X 1 Z/MD
117 100644 X 2 Z/MD
118 100644 X 1 Z/MM
119 100644 X 2 Z/MM
120 100644 X 3 Z/MM
121 100644 X 0 Z/MN
122 100644 X 3 Z/NA
123 100644 X 1 Z/ND
124 100644 X 2 Z/ND
125 100644 X 0 Z/NM
126 100644 X 0 Z/NN
127 EOF
129 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
130 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
132 check_result () {
133 git-ls-files --stage | sed -e 's/ '"$_x40"' / X /' >current &&
134 diff -u expected current
135 }
137 # This is done on an empty work directory, which is the normal
138 # merge person behaviour.
139 test_expect_success \
140 '3-way merge with git-read-tree -m, empty cache' \
141 "rm -fr [NDMALTS][NDMALTSF] Z &&
142 rm .git/index &&
143 git-read-tree -m $tree_O $tree_A $tree_B &&
144 check_result"
146 # This starts out with the first head, which is the normal
147 # patch submitter behaviour.
148 test_expect_success \
149 '3-way merge with git-read-tree -m, match H' \
150 "rm -fr [NDMALTS][NDMALTSF] Z &&
151 rm .git/index &&
152 git-read-tree $tree_A &&
153 git-checkout-cache -f -u -a &&
154 git-read-tree -m $tree_O $tree_A $tree_B &&
155 check_result"
157 : <<\END_OF_CASE_TABLE
159 We have so far tested only empty index and clean-and-matching-A index
160 case which are trivial. Make sure index requirements are also
161 checked. The table also lists alternative semantics which is not
162 currently implemented.
164 "git-diff-tree -m O A B"
166 O A B result index requirements
167 -------------------------------------------------------------------
168 1 missing missing missing - must not exist.
169 ------------------------------------------------------------------
170 2 missing missing exists no merge must not exist.
171 ------------------------------------
172 (ALT) take B* must match B, if exists.
173 ------------------------------------------------------------------
174 3 missing exists missing no merge must match A and be
175 up-to-date, if exists.
176 ------------------------------------
177 (ALT) take A* must match A, if exists.
178 ------------------------------------------------------------------
179 4 missing exists A!=B no merge must match A and be
180 up-to-date, if exists.
181 ------------------------------------------------------------------
182 5 missing exists A==B no merge must match A and be
183 up-to-date, if exists.
184 ------------------------------------
185 (ALT) take A must match A, if exists.
186 ------------------------------------------------------------------
187 6 exists missing missing no merge must not exist.
188 ------------------------------------
189 (ALT) remove must not exist.
190 ------------------------------------------------------------------
191 7 exists missing O!=B no merge must not exist.
192 ------------------------------------------------------------------
193 8 exists missing O==B no merge must not exist.
194 ------------------------------------
195 (ALT) remove must not exist.
196 ------------------------------------------------------------------
197 9 exists O!=A missing no merge must match A and be
198 up-to-date, if exists.
199 ------------------------------------------------------------------
200 10 exists O==A missing no merge must match A and be
201 up-to-date, if exists.
202 ------------------------------------
203 (ALT) remove ditto
204 ------------------------------------------------------------------
205 11 exists O!=A O!=B no merge must match A and be
206 A!=B up-to-date, if exists.
207 ------------------------------------------------------------------
208 12 exists O!=A O!=B take A must match A, if exists.
209 A==B
210 ------------------------------------------------------------------
211 13 exists O!=A O==B take A must match A, if exists.
212 ------------------------------------------------------------------
213 14 exists O==A O!=B take B must match A and be
214 be up-to-date, if exists.
215 ------------------------------------
216 (ALT) take B if exists, must either (1)
217 match A and be up-to-date,
218 or (2) match B.
219 ------------------------------------------------------------------
220 15 exists O==A O==B take B must match A if exists.
221 -------------------------------------------------------------------
223 Note: if we want to implement 2ALT and 3ALT we need to be careful.
224 The tree A may contain DF (file) when tree B require DF to be a
225 directory by having DF/DF (file).
227 END_OF_CASE_TABLE
229 test_expect_failure \
230 '1 - must not have an entry not in A.' \
231 "rm -f .git/index XX &&
232 echo XX >XX &&
233 git-update-cache --add XX &&
234 git-read-tree -m $tree_O $tree_A $tree_B"
236 test_expect_failure \
237 '2 - must not have an entry not in A.' \
238 "rm -f .git/index NA &&
239 cp .orig-B/NA NA &&
240 git-update-cache --add NA &&
241 git-read-tree -m $tree_O $tree_A $tree_B"
243 test_expect_success \
244 '3 - must match and be up-to-date in !O && A && !B case.' \
245 "rm -f .git/index AN &&
246 cp .orig-A/AN AN &&
247 git-update-cache --add AN &&
248 git-read-tree -m $tree_O $tree_A $tree_B &&
249 check_result"
251 test_expect_failure \
252 '3 (fail) - must match and be up-to-date in !O && A && !B case.' \
253 "rm -f .git/index AN &&
254 cp .orig-A/AN AN &&
255 git-update-cache --add AN &&
256 echo extra >>AN &&
257 git-read-tree -m $tree_O $tree_A $tree_B"
259 test_expect_failure \
260 '3 (fail) - must match and be up-to-date in !O && A && !B case.' \
261 "rm -f .git/index AN &&
262 cp .orig-A/AN AN &&
263 echo extra >>AN &&
264 git-update-cache --add AN &&
265 git-read-tree -m $tree_O $tree_A $tree_B"
267 test_expect_success \
268 '4 - must match and be up-to-date in !O && A && B && A!=B case.' \
269 "rm -f .git/index AA &&
270 cp .orig-A/AA AA &&
271 git-update-cache --add AA &&
272 git-read-tree -m $tree_O $tree_A $tree_B &&
273 check_result"
275 test_expect_failure \
276 '4 (fail) - must match and be up-to-date in !O && A && B && A!=B case.' \
277 "rm -f .git/index AA &&
278 cp .orig-A/AA AA &&
279 git-update-cache --add AA &&
280 echo extra >>AA &&
281 git-read-tree -m $tree_O $tree_A $tree_B"
283 test_expect_failure \
284 '4 (fail) - must match and be up-to-date in !O && A && B && A!=B case.' \
285 "rm -f .git/index AA &&
286 cp .orig-A/AA AA &&
287 echo extra >>AA &&
288 git-update-cache --add AA &&
289 git-read-tree -m $tree_O $tree_A $tree_B"
291 test_expect_success \
292 '5 - must match in !O && A && B && A==B case.' \
293 "rm -f .git/index LL &&
294 cp .orig-A/LL LL &&
295 git-update-cache --add LL &&
296 git-read-tree -m $tree_O $tree_A $tree_B &&
297 check_result"
299 test_expect_success \
300 '5 - must match in !O && A && B && A==B case.' \
301 "rm -f .git/index LL &&
302 cp .orig-A/LL LL &&
303 git-update-cache --add LL &&
304 echo extra >>LL &&
305 git-read-tree -m $tree_O $tree_A $tree_B &&
306 check_result"
308 test_expect_failure \
309 '5 (fail) - must match A in !O && A && B && A==B case.' \
310 "rm -f .git/index LL &&
311 cp .orig-A/LL LL &&
312 echo extra >>LL &&
313 git-update-cache --add LL &&
314 git-read-tree -m $tree_O $tree_A $tree_B"
316 test_expect_failure \
317 '6 - must not exist in O && !A && !B case' \
318 "rm -f .git/index DD &&
319 echo DD >DD
320 git-update-cache --add DD &&
321 git-read-tree -m $tree_O $tree_A $tree_B"
323 test_expect_failure \
324 '7 - must not exist in O && !A && B && O!=B case' \
325 "rm -f .git/index DM &&
326 cp .orig-B/DM DM &&
327 git-update-cache --add DM &&
328 git-read-tree -m $tree_O $tree_A $tree_B"
330 test_expect_failure \
331 '8 - must not exist in O && !A && B && O==B case' \
332 "rm -f .git/index DN &&
333 cp .orig-B/DN DN &&
334 git-update-cache --add DN &&
335 git-read-tree -m $tree_O $tree_A $tree_B"
337 test_expect_success \
338 '9 - must match and be up-to-date in O && A && !B && O!=A case' \
339 "rm -f .git/index MD &&
340 cp .orig-A/MD MD &&
341 git-update-cache --add MD &&
342 git-read-tree -m $tree_O $tree_A $tree_B &&
343 check_result"
345 test_expect_failure \
346 '9 (fail) - must match and be up-to-date in O && A && !B && O!=A case' \
347 "rm -f .git/index MD &&
348 cp .orig-A/MD MD &&
349 git-update-cache --add MD &&
350 echo extra >>MD &&
351 git-read-tree -m $tree_O $tree_A $tree_B"
353 test_expect_failure \
354 '9 (fail) - must match and be up-to-date in O && A && !B && O!=A case' \
355 "rm -f .git/index MD &&
356 cp .orig-A/MD MD &&
357 echo extra >>MD &&
358 git-update-cache --add MD &&
359 git-read-tree -m $tree_O $tree_A $tree_B"
361 test_expect_success \
362 '10 - must match and be up-to-date in O && A && !B && O==A case' \
363 "rm -f .git/index ND &&
364 cp .orig-A/ND ND &&
365 git-update-cache --add ND &&
366 git-read-tree -m $tree_O $tree_A $tree_B &&
367 check_result"
369 test_expect_failure \
370 '10 (fail) - must match and be up-to-date in O && A && !B && O==A case' \
371 "rm -f .git/index ND &&
372 cp .orig-A/ND ND &&
373 git-update-cache --add ND &&
374 echo extra >>ND &&
375 git-read-tree -m $tree_O $tree_A $tree_B"
377 test_expect_failure \
378 '10 (fail) - must match and be up-to-date in O && A && !B && O==A case' \
379 "rm -f .git/index ND &&
380 cp .orig-A/ND ND &&
381 echo extra >>ND &&
382 git-update-cache --add ND &&
383 git-read-tree -m $tree_O $tree_A $tree_B"
385 test_expect_success \
386 '11 - must match and be up-to-date in O && A && B && O!=A && O!=B && A!=B case' \
387 "rm -f .git/index MM &&
388 cp .orig-A/MM MM &&
389 git-update-cache --add MM &&
390 git-read-tree -m $tree_O $tree_A $tree_B &&
391 check_result"
393 test_expect_failure \
394 '11 (fail) - must match and be up-to-date in O && A && B && O!=A && O!=B && A!=B case' \
395 "rm -f .git/index MM &&
396 cp .orig-A/MM MM &&
397 git-update-cache --add MM &&
398 echo extra >>MM &&
399 git-read-tree -m $tree_O $tree_A $tree_B"
401 test_expect_failure \
402 '11 (fail) - must match and be up-to-date in O && A && B && O!=A && O!=B && A!=B case' \
403 "rm -f .git/index MM &&
404 cp .orig-A/MM MM &&
405 echo extra >>MM &&
406 git-update-cache --add MM &&
407 git-read-tree -m $tree_O $tree_A $tree_B"
409 test_expect_success \
410 '12 - must match A in O && A && B && O!=A && A==B case' \
411 "rm -f .git/index SS &&
412 cp .orig-A/SS SS &&
413 git-update-cache --add SS &&
414 git-read-tree -m $tree_O $tree_A $tree_B &&
415 check_result"
417 test_expect_success \
418 '12 - must match A in O && A && B && O!=A && A==B case' \
419 "rm -f .git/index SS &&
420 cp .orig-A/SS SS &&
421 git-update-cache --add SS &&
422 echo extra >>SS &&
423 git-read-tree -m $tree_O $tree_A $tree_B &&
424 check_result"
426 test_expect_failure \
427 '12 (fail) - must match A in O && A && B && O!=A && A==B case' \
428 "rm -f .git/index SS &&
429 cp .orig-A/SS SS &&
430 echo extra >>SS &&
431 git-update-cache --add SS &&
432 git-read-tree -m $tree_O $tree_A $tree_B"
434 test_expect_success \
435 '13 - must match A in O && A && B && O!=A && O==B case' \
436 "rm -f .git/index MN &&
437 cp .orig-A/MN MN &&
438 git-update-cache --add MN &&
439 git-read-tree -m $tree_O $tree_A $tree_B &&
440 check_result"
442 test_expect_success \
443 '13 - must match A in O && A && B && O!=A && O==B case' \
444 "rm -f .git/index MN &&
445 cp .orig-A/MN MN &&
446 git-update-cache --add MN &&
447 echo extra >>MN &&
448 git-read-tree -m $tree_O $tree_A $tree_B &&
449 check_result"
451 test_expect_success \
452 '14 - must match and be up-to-date in O && A && B && O==A && O!=B case' \
453 "rm -f .git/index NM &&
454 cp .orig-A/NM NM &&
455 git-update-cache --add NM &&
456 git-read-tree -m $tree_O $tree_A $tree_B &&
457 check_result"
459 test_expect_failure \
460 '14 (fail) - must match and be up-to-date in O && A && B && O==A && O!=B case' \
461 "rm -f .git/index NM &&
462 cp .orig-A/NM NM &&
463 git-update-cache --add NM &&
464 echo extra >>NM &&
465 git-read-tree -m $tree_O $tree_A $tree_B"
467 test_expect_failure \
468 '14 (fail) - must match and be up-to-date in O && A && B && O==A && O!=B case' \
469 "rm -f .git/index NM &&
470 cp .orig-A/NM NM &&
471 echo extra >>NM &&
472 git-update-cache --add NM &&
473 git-read-tree -m $tree_O $tree_A $tree_B"
475 test_expect_success \
476 '15 - must match A in O && A && B && O==A && O==B case' \
477 "rm -f .git/index NN &&
478 cp .orig-A/NN NN &&
479 git-update-cache --add NN &&
480 git-read-tree -m $tree_O $tree_A $tree_B &&
481 check_result"
483 test_expect_success \
484 '15 - must match A in O && A && B && O==A && O==B case' \
485 "rm -f .git/index NN &&
486 cp .orig-A/NN NN &&
487 git-update-cache --add NN &&
488 echo extra >>NN &&
489 git-read-tree -m $tree_O $tree_A $tree_B &&
490 check_result"
492 test_expect_failure \
493 '15 (fail) - must match A in O && A && B && O==A && O==B case' \
494 "rm -f .git/index NN &&
495 cp .orig-A/NN NN &&
496 echo extra >>NN &&
497 git-update-cache --add NN &&
498 git-read-tree -m $tree_O $tree_A $tree_B"
500 test_done