1#!/bin/sh
2
3test_description='basic commit reachability tests'
4
5. ./test-lib.sh
6
7# Construct a grid-like commit graph with points (x,y)
8# with 1 <= x <= 10, 1 <= y <= 10, where (x,y) has
9# parents (x-1, y) and (x, y-1), keeping in mind that
10# we drop a parent if a coordinate is nonpositive.
11#
12# (10,10)
13# / \
14# (10,9) (9,10)
15# / \ / \
16# (10,8) (9,9) (8,10)
17# / \ / \ / \
18# ( continued...)
19# \ / \ / \ /
20# (3,1) (2,2) (1,3)
21# \ / \ /
22# (2,1) (2,1)
23# \ /
24# (1,1)
25#
26# We use branch 'commit-x-y' to refer to (x,y).
27# This grid allows interesting reachability and
28# non-reachability queries: (x,y) can reach (x',y')
29# if and only if x' <= x and y' <= y.
30test_expect_success 'setup' '
31 for i in $(test_seq 1 10)
32 do
33 test_commit "1-$i" &&
34 git branch -f commit-1-$i
35 done &&
36 for j in $(test_seq 1 9)
37 do
38 git reset --hard commit-$j-1 &&
39 x=$(($j + 1)) &&
40 test_commit "$x-1" &&
41 git branch -f commit-$x-1 &&
42
43 for i in $(test_seq 2 10)
44 do
45 git merge commit-$j-$i -m "$x-$i" &&
46 git branch -f commit-$x-$i
47 done
48 done &&
49 git commit-graph write --reachable &&
50 mv .git/objects/info/commit-graph commit-graph-full &&
51 git show-ref -s commit-5-5 | git commit-graph write --stdin-commits &&
52 mv .git/objects/info/commit-graph commit-graph-half &&
53 git config core.commitGraph true
54'
55
56run_three_modes () {
57 test_when_finished rm -rf .git/objects/info/commit-graph &&
58 "$@" <input >actual &&
59 test_cmp expect actual &&
60 cp commit-graph-full .git/objects/info/commit-graph &&
61 "$@" <input >actual &&
62 test_cmp expect actual &&
63 cp commit-graph-half .git/objects/info/commit-graph &&
64 "$@" <input >actual &&
65 test_cmp expect actual
66}
67
68test_three_modes () {
69 run_three_modes test-tool reach "$@"
70}
71
72test_expect_success 'ref_newer:miss' '
73 cat >input <<-\EOF &&
74 A:commit-5-7
75 B:commit-4-9
76 EOF
77 echo "ref_newer(A,B):0" >expect &&
78 test_three_modes ref_newer
79'
80
81test_expect_success 'ref_newer:hit' '
82 cat >input <<-\EOF &&
83 A:commit-5-7
84 B:commit-2-3
85 EOF
86 echo "ref_newer(A,B):1" >expect &&
87 test_three_modes ref_newer
88'
89
90test_expect_success 'in_merge_bases:hit' '
91 cat >input <<-\EOF &&
92 A:commit-5-7
93 B:commit-8-8
94 EOF
95 echo "in_merge_bases(A,B):1" >expect &&
96 test_three_modes in_merge_bases
97'
98
99test_expect_success 'in_merge_bases:miss' '
100 cat >input <<-\EOF &&
101 A:commit-6-8
102 B:commit-5-9
103 EOF
104 echo "in_merge_bases(A,B):0" >expect &&
105 test_three_modes in_merge_bases
106'
107
108test_expect_success 'is_descendant_of:hit' '
109 cat >input <<-\EOF &&
110 A:commit-5-7
111 X:commit-4-8
112 X:commit-6-6
113 X:commit-1-1
114 EOF
115 echo "is_descendant_of(A,X):1" >expect &&
116 test_three_modes is_descendant_of
117'
118
119test_expect_success 'is_descendant_of:miss' '
120 cat >input <<-\EOF &&
121 A:commit-6-8
122 X:commit-5-9
123 X:commit-4-10
124 X:commit-7-6
125 EOF
126 echo "is_descendant_of(A,X):0" >expect &&
127 test_three_modes is_descendant_of
128'
129
130test_expect_success 'get_merge_bases_many' '
131 cat >input <<-\EOF &&
132 A:commit-5-7
133 X:commit-4-8
134 X:commit-6-6
135 X:commit-8-3
136 EOF
137 {
138 echo "get_merge_bases_many(A,X):" &&
139 git rev-parse commit-5-6 \
140 commit-4-7 | sort
141 } >expect &&
142 test_three_modes get_merge_bases_many
143'
144
145test_expect_success 'reduce_heads' '
146 cat >input <<-\EOF &&
147 X:commit-1-10
148 X:commit-2-8
149 X:commit-3-6
150 X:commit-4-4
151 X:commit-1-7
152 X:commit-2-5
153 X:commit-3-3
154 X:commit-5-1
155 EOF
156 {
157 echo "reduce_heads(X):" &&
158 git rev-parse commit-5-1 \
159 commit-4-4 \
160 commit-3-6 \
161 commit-2-8 \
162 commit-1-10 | sort
163 } >expect &&
164 test_three_modes reduce_heads
165'
166
167test_expect_success 'can_all_from_reach:hit' '
168 cat >input <<-\EOF &&
169 X:commit-2-10
170 X:commit-3-9
171 X:commit-4-8
172 X:commit-5-7
173 X:commit-6-6
174 X:commit-7-5
175 X:commit-8-4
176 X:commit-9-3
177 Y:commit-1-9
178 Y:commit-2-8
179 Y:commit-3-7
180 Y:commit-4-6
181 Y:commit-5-5
182 Y:commit-6-4
183 Y:commit-7-3
184 Y:commit-8-1
185 EOF
186 echo "can_all_from_reach(X,Y):1" >expect &&
187 test_three_modes can_all_from_reach
188'
189
190test_expect_success 'can_all_from_reach:miss' '
191 cat >input <<-\EOF &&
192 X:commit-2-10
193 X:commit-3-9
194 X:commit-4-8
195 X:commit-5-7
196 X:commit-6-6
197 X:commit-7-5
198 X:commit-8-4
199 X:commit-9-3
200 Y:commit-1-9
201 Y:commit-2-8
202 Y:commit-3-7
203 Y:commit-4-6
204 Y:commit-5-5
205 Y:commit-6-4
206 Y:commit-8-5
207 EOF
208 echo "can_all_from_reach(X,Y):0" >expect &&
209 test_three_modes can_all_from_reach
210'
211
212test_expect_success 'commit_contains:hit' '
213 cat >input <<-\EOF &&
214 A:commit-7-7
215 X:commit-2-10
216 X:commit-3-9
217 X:commit-4-8
218 X:commit-5-7
219 X:commit-6-6
220 X:commit-7-5
221 X:commit-8-4
222 X:commit-9-3
223 EOF
224 echo "commit_contains(_,A,X,_):1" >expect &&
225 test_three_modes commit_contains &&
226 test_three_modes commit_contains --tag
227'
228
229test_expect_success 'commit_contains:miss' '
230 cat >input <<-\EOF &&
231 A:commit-6-5
232 X:commit-2-10
233 X:commit-3-9
234 X:commit-4-8
235 X:commit-5-7
236 X:commit-6-6
237 X:commit-7-5
238 X:commit-8-4
239 X:commit-9-3
240 EOF
241 echo "commit_contains(_,A,X,_):0" >expect &&
242 test_three_modes commit_contains &&
243 test_three_modes commit_contains --tag
244'
245
246test_expect_success 'rev-list: basic topo-order' '
247 git rev-parse \
248 commit-6-6 commit-5-6 commit-4-6 commit-3-6 commit-2-6 commit-1-6 \
249 commit-6-5 commit-5-5 commit-4-5 commit-3-5 commit-2-5 commit-1-5 \
250 commit-6-4 commit-5-4 commit-4-4 commit-3-4 commit-2-4 commit-1-4 \
251 commit-6-3 commit-5-3 commit-4-3 commit-3-3 commit-2-3 commit-1-3 \
252 commit-6-2 commit-5-2 commit-4-2 commit-3-2 commit-2-2 commit-1-2 \
253 commit-6-1 commit-5-1 commit-4-1 commit-3-1 commit-2-1 commit-1-1 \
254 >expect &&
255 run_three_modes git rev-list --topo-order commit-6-6
256'
257
258test_expect_success 'rev-list: first-parent topo-order' '
259 git rev-parse \
260 commit-6-6 \
261 commit-6-5 \
262 commit-6-4 \
263 commit-6-3 \
264 commit-6-2 \
265 commit-6-1 commit-5-1 commit-4-1 commit-3-1 commit-2-1 commit-1-1 \
266 >expect &&
267 run_three_modes git rev-list --first-parent --topo-order commit-6-6
268'
269
270test_expect_success 'rev-list: range topo-order' '
271 git rev-parse \
272 commit-6-6 commit-5-6 commit-4-6 commit-3-6 commit-2-6 commit-1-6 \
273 commit-6-5 commit-5-5 commit-4-5 commit-3-5 commit-2-5 commit-1-5 \
274 commit-6-4 commit-5-4 commit-4-4 commit-3-4 commit-2-4 commit-1-4 \
275 commit-6-3 commit-5-3 commit-4-3 \
276 commit-6-2 commit-5-2 commit-4-2 \
277 commit-6-1 commit-5-1 commit-4-1 \
278 >expect &&
279 run_three_modes git rev-list --topo-order commit-3-3..commit-6-6
280'
281
282test_expect_success 'rev-list: range topo-order' '
283 git rev-parse \
284 commit-6-6 commit-5-6 commit-4-6 \
285 commit-6-5 commit-5-5 commit-4-5 \
286 commit-6-4 commit-5-4 commit-4-4 \
287 commit-6-3 commit-5-3 commit-4-3 \
288 commit-6-2 commit-5-2 commit-4-2 \
289 commit-6-1 commit-5-1 commit-4-1 \
290 >expect &&
291 run_three_modes git rev-list --topo-order commit-3-8..commit-6-6
292'
293
294test_expect_success 'rev-list: first-parent range topo-order' '
295 git rev-parse \
296 commit-6-6 \
297 commit-6-5 \
298 commit-6-4 \
299 commit-6-3 \
300 commit-6-2 \
301 commit-6-1 commit-5-1 commit-4-1 \
302 >expect &&
303 run_three_modes git rev-list --first-parent --topo-order commit-3-8..commit-6-6
304'
305
306test_expect_success 'rev-list: ancestry-path topo-order' '
307 git rev-parse \
308 commit-6-6 commit-5-6 commit-4-6 commit-3-6 \
309 commit-6-5 commit-5-5 commit-4-5 commit-3-5 \
310 commit-6-4 commit-5-4 commit-4-4 commit-3-4 \
311 commit-6-3 commit-5-3 commit-4-3 \
312 >expect &&
313 run_three_modes git rev-list --topo-order --ancestry-path commit-3-3..commit-6-6
314'
315
316test_expect_success 'rev-list: symmetric difference topo-order' '
317 git rev-parse \
318 commit-6-6 commit-5-6 commit-4-6 \
319 commit-6-5 commit-5-5 commit-4-5 \
320 commit-6-4 commit-5-4 commit-4-4 \
321 commit-6-3 commit-5-3 commit-4-3 \
322 commit-6-2 commit-5-2 commit-4-2 \
323 commit-6-1 commit-5-1 commit-4-1 \
324 commit-3-8 commit-2-8 commit-1-8 \
325 commit-3-7 commit-2-7 commit-1-7 \
326 >expect &&
327 run_three_modes git rev-list --topo-order commit-3-8...commit-6-6
328'
329
330test_done