The 19th batch
[git.git] / t / t6010-merge-base.sh
blobf96ea82e78636fd410b30f0b7846552dccd1cd86
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano
6 test_description='Merge base and parent list computation.
9 TEST_PASSES_SANITIZE_LEAK=true
10 . ./test-lib.sh
12 M=1130000000
13 Z=+0000
15 GIT_COMMITTER_EMAIL=git@comm.iter.xz
16 GIT_COMMITTER_NAME='C O Mmiter'
17 GIT_AUTHOR_NAME='A U Thor'
18 GIT_AUTHOR_EMAIL=git@au.thor.xz
19 export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
21 doit () {
22 OFFSET=$1 &&
23 NAME=$2 &&
24 shift 2 &&
26 PARENTS= &&
27 for P
29 PARENTS="${PARENTS}-p $P "
30 done &&
32 GIT_COMMITTER_DATE="$(($M + $OFFSET)) $Z" &&
33 GIT_AUTHOR_DATE=$GIT_COMMITTER_DATE &&
34 export GIT_COMMITTER_DATE GIT_AUTHOR_DATE &&
36 commit=$(echo $NAME | git commit-tree $T $PARENTS) &&
38 git update-ref "refs/tags/$NAME" "$commit" &&
39 echo $commit
42 test_expect_success 'setup' '
43 T=$(git mktree </dev/null)
46 test_expect_success 'set up G and H' '
47 # E---D---C---B---A
48 # \"-_ \ \
49 # \ `---------G \
50 # \ \
51 # F----------------H
52 E=$(doit 5 E) &&
53 D=$(doit 4 D $E) &&
54 F=$(doit 6 F $E) &&
55 C=$(doit 3 C $D) &&
56 B=$(doit 2 B $C) &&
57 A=$(doit 1 A $B) &&
58 G=$(doit 7 G $B $E) &&
59 H=$(doit 8 H $A $F)
62 test_expect_success 'merge-base G H' '
63 git name-rev $B >expected &&
65 MB=$(git merge-base G H) &&
66 git name-rev "$MB" >actual.single &&
68 MB=$(git merge-base --all G H) &&
69 git name-rev "$MB" >actual.all &&
71 MB=$(git show-branch --merge-base G H) &&
72 git name-rev "$MB" >actual.sb &&
74 test_cmp expected actual.single &&
75 test_cmp expected actual.all &&
76 test_cmp expected actual.sb
79 test_expect_success 'merge-base/show-branch --independent' '
80 git name-rev "$H" >expected1 &&
81 git name-rev "$H" "$G" >expected2 &&
83 parents=$(git merge-base --independent H) &&
84 git name-rev $parents >actual1.mb &&
85 parents=$(git merge-base --independent A H G) &&
86 git name-rev $parents >actual2.mb &&
88 parents=$(git show-branch --independent H) &&
89 git name-rev $parents >actual1.sb &&
90 parents=$(git show-branch --independent A H G) &&
91 git name-rev $parents >actual2.sb &&
93 test_cmp expected1 actual1.mb &&
94 test_cmp expected2 actual2.mb &&
95 test_cmp expected1 actual1.sb &&
96 test_cmp expected2 actual2.sb
99 test_expect_success 'unsynchronized clocks' '
100 # This test is to demonstrate that relying on timestamps in a distributed
101 # SCM to provide a _consistent_ partial ordering of commits leads to
102 # insanity.
104 # Relative
105 # Structure timestamps
107 # PL PR +4 +4
108 # / \/ \ / \/ \
109 # L2 C2 R2 +3 -1 +3
110 # | | | | | |
111 # L1 C1 R1 +2 -2 +2
112 # | | | | | |
113 # L0 C0 R0 +1 -3 +1
114 # \ | / \ | /
115 # S 0
117 # The left and right chains of commits can be of any length and complexity as
118 # long as all of the timestamps are greater than that of S.
120 S=$(doit 0 S) &&
122 C0=$(doit -3 C0 $S) &&
123 C1=$(doit -2 C1 $C0) &&
124 C2=$(doit -1 C2 $C1) &&
126 L0=$(doit 1 L0 $S) &&
127 L1=$(doit 2 L1 $L0) &&
128 L2=$(doit 3 L2 $L1) &&
130 R0=$(doit 1 R0 $S) &&
131 R1=$(doit 2 R1 $R0) &&
132 R2=$(doit 3 R2 $R1) &&
134 PL=$(doit 4 PL $L2 $C2) &&
135 PR=$(doit 4 PR $C2 $R2) &&
137 git name-rev $C2 >expected &&
139 MB=$(git merge-base PL PR) &&
140 git name-rev "$MB" >actual.single &&
142 MB=$(git merge-base --all PL PR) &&
143 git name-rev "$MB" >actual.all &&
145 test_cmp expected actual.single &&
146 test_cmp expected actual.all
149 test_expect_success '--independent with unsynchronized clocks' '
150 IB=$(doit 0 IB) &&
151 I1=$(doit -10 I1 $IB) &&
152 I2=$(doit -9 I2 $I1) &&
153 I3=$(doit -8 I3 $I2) &&
154 I4=$(doit -7 I4 $I3) &&
155 I5=$(doit -6 I5 $I4) &&
156 I6=$(doit -5 I6 $I5) &&
157 I7=$(doit -4 I7 $I6) &&
158 I8=$(doit -3 I8 $I7) &&
159 IH=$(doit -2 IH $I8) &&
161 echo $IH >expected &&
162 git merge-base --independent IB IH >actual &&
163 test_cmp expected actual
166 test_expect_success 'merge-base for octopus-step (setup)' '
167 # Another set to demonstrate base between one commit and a merge
168 # in the documentation.
170 # * C (MMC) * B (MMB) * A (MMA)
171 # * o * o * o
172 # * o * o * o
173 # * o * o * o
174 # * o | _______/
175 # | |/
176 # | * 1 (MM1)
177 # | _______/
178 # |/
179 # * root (MMR)
181 test_commit MMR &&
182 test_commit MM1 &&
183 test_commit MM-o &&
184 test_commit MM-p &&
185 test_commit MM-q &&
186 test_commit MMA &&
187 git checkout MM1 &&
188 test_commit MM-r &&
189 test_commit MM-s &&
190 test_commit MM-t &&
191 test_commit MMB &&
192 git checkout MMR &&
193 test_commit MM-u &&
194 test_commit MM-v &&
195 test_commit MM-w &&
196 test_commit MM-x &&
197 test_commit MMC
200 test_expect_success 'merge-base A B C' '
201 git rev-parse --verify MM1 >expected &&
202 git rev-parse --verify MMR >expected.sb &&
204 git merge-base --all MMA MMB MMC >actual &&
205 git merge-base --all --octopus MMA MMB MMC >actual.common &&
206 git show-branch --merge-base MMA MMB MMC >actual.sb &&
208 test_cmp expected actual &&
209 test_cmp expected.sb actual.common &&
210 test_cmp expected.sb actual.sb
213 test_expect_success 'criss-cross merge-base for octopus-step' '
214 git reset --hard MMR &&
215 test_commit CC1 &&
216 git reset --hard E &&
217 test_commit CC2 &&
218 test_tick &&
219 # E is a root commit unrelated to MMR root on which CC1 is based
220 git merge -s ours --allow-unrelated-histories CC1 &&
221 test_commit CC-o &&
222 test_commit CCB &&
223 git reset --hard CC1 &&
224 # E is a root commit unrelated to MMR root on which CC1 is based
225 git merge -s ours --allow-unrelated-histories CC2 &&
226 test_commit CCA &&
228 git rev-parse CC1 CC2 >expected &&
229 git merge-base --all CCB CCA^^ CCA^^2 >actual &&
231 sort expected >expected.sorted &&
232 sort actual >actual.sorted &&
233 test_cmp expected.sorted actual.sorted
236 test_expect_success 'using reflog to find the fork point' '
237 git reset --hard &&
238 git checkout -b base $E &&
241 for count in 1 2 3
243 git commit --allow-empty -m "Base commit #$count" &&
244 git rev-parse HEAD >expect$count &&
245 git checkout -B derived &&
246 git commit --allow-empty -m "Derived #$count" &&
247 git rev-parse HEAD >derived$count &&
248 git checkout -B base $E || exit 1
249 done &&
251 for count in 1 2 3
253 git merge-base --fork-point base $(cat derived$count) >actual &&
254 test_cmp expect$count actual || exit 1
255 done
257 ) &&
258 # check that we correctly default to HEAD
259 git checkout derived &&
260 git merge-base --fork-point base >actual &&
261 test_cmp expect3 actual
264 test_expect_success '--fork-point works with empty reflog' '
265 git -c core.logallrefupdates=false branch no-reflog base &&
266 git merge-base --fork-point no-reflog derived &&
267 test_cmp expect3 actual
270 test_expect_success 'merge-base --octopus --all for complex tree' '
271 # Best common ancestor for JE, JAA and JDD is JC
272 # JE
273 # / |
274 # / |
275 # / |
276 # JAA / |
277 # |\ / |
278 # | \ | JDD |
279 # | \ |/ | |
280 # | JC JD |
281 # | | /| |
282 # | |/ | |
283 # JA | | |
284 # |\ /| | |
285 # X JB | X X
286 # \ \ | / /
287 # \__\|/___/
289 test_commit J &&
290 test_commit JB &&
291 git reset --hard J &&
292 test_commit JC &&
293 git reset --hard J &&
294 test_commit JTEMP1 &&
295 test_merge JA JB &&
296 test_merge JAA JC &&
297 git reset --hard J &&
298 test_commit JTEMP2 &&
299 test_merge JD JB &&
300 test_merge JDD JC &&
301 git reset --hard J &&
302 test_commit JTEMP3 &&
303 test_merge JE JC &&
304 git rev-parse JC >expected &&
305 git merge-base --all --octopus JAA JDD JE >actual &&
306 test_cmp expected actual
309 test_done