test-reach: test get_reachable_subset

The get_reachable_subset() method returns the list of commits in the 'to' array that are reachable from at least one commit in the 'from' array. Add tests that check this method works in a few cases: 1. All commits in the 'to' list are reachable. This exercises the early-termination condition. 2. Some commits in the 'to' list are reachable. This exercises the loop-termination condition. 3. No commits in the 'to' list are reachable. This exercises the NULL return condition. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Nov 2, 2018 at 06:14 UTC 4c7bb45269ccd1fdb15347a675ff16e8a8534f68
2 files changed +82 -4
t/helper/test-reach.c
+30 -4
@@ -32,8 +32,8 @@ int cmd__reach(int ac, const char **av)
32 struct commit *A, *B;
33 struct commit_list *X, *Y;
34 struct object_array X_obj = OBJECT_ARRAY_INIT;
35 - struct commit **X_array;
36 - int X_nr, X_alloc;
35 + struct commit **X_array, **Y_array;
36 + int X_nr, X_alloc, Y_nr, Y_alloc;
37 struct strbuf buf = STRBUF_INIT;
38 struct repository *r = the_repository;
39
@@ -44,9 +44,10 @@ int cmd__reach(int ac, const char **av)
44
45 A = B = NULL;
46 X = Y = NULL;
47 - X_nr = 0;
48 - X_alloc = 16;
47 + X_nr = Y_nr = 0;
48 + X_alloc = Y_alloc = 16;
49 ALLOC_ARRAY(X_array, X_alloc);
50 + ALLOC_ARRAY(Y_array, Y_alloc);
51
52 while (strbuf_getline(&buf, stdin) != EOF) {
53 struct object_id oid;
@@ -92,6 +93,8 @@ int cmd__reach(int ac, const char **av)
93
94 case 'Y':
95 commit_list_insert(c, &Y);
96 + ALLOC_GROW(Y_array, Y_nr + 1, Y_alloc);
97 + Y_array[Y_nr++] = c;
98 break;
99
100 default:
@@ -136,6 +139,29 @@ int cmd__reach(int ac, const char **av)
139 filter.with_commit_tag_algo = 0;
140
141 printf("%s(_,A,X,_):%d\n", av[1], commit_contains(&filter, A, X, &cache));
142 + } else if (!strcmp(av[1], "get_reachable_subset")) {
143 + const int reachable_flag = 1;
144 + int i, count = 0;
145 + struct commit_list *current;
146 + struct commit_list *list = get_reachable_subset(X_array, X_nr,
147 + Y_array, Y_nr,
148 + reachable_flag);
149 + printf("get_reachable_subset(X,Y)\n");
150 + for (current = list; current; current = current->next) {
151 + if (!(list->item->object.flags & reachable_flag))
152 + die(_("commit %s is not marked reachable"),
153 + oid_to_hex(&list->item->object.oid));
154 + count++;
155 + }
156 + for (i = 0; i < Y_nr; i++) {
157 + if (Y_array[i]->object.flags & reachable_flag)
158 + count--;
159 + }
160 +
161 + if (count < 0)
162 + die(_("too many commits marked reachable"));
163 +
164 + print_sorted_commit_ids(list);
165 }
166
167 exit(0);
t/t6600-test-reach.sh
+52
@@ -265,4 +265,56 @@ test_expect_success 'commit_contains:miss' '
265 test_three_modes commit_contains --tag
266 '
267
268 +test_expect_success 'get_reachable_subset:all' '
269 + cat >input <<-\EOF &&
270 + X:commit-9-1
271 + X:commit-8-3
272 + X:commit-7-5
273 + X:commit-6-6
274 + X:commit-1-7
275 + Y:commit-3-3
276 + Y:commit-1-7
277 + Y:commit-5-6
278 + EOF
279 + (
280 + echo "get_reachable_subset(X,Y)" &&
281 + git rev-parse commit-3-3 \
282 + commit-1-7 \
283 + commit-5-6 | sort
284 + ) >expect &&
285 + test_three_modes get_reachable_subset
286 +'
287 +
288 +test_expect_success 'get_reachable_subset:some' '
289 + cat >input <<-\EOF &&
290 + X:commit-9-1
291 + X:commit-8-3
292 + X:commit-7-5
293 + X:commit-1-7
294 + Y:commit-3-3
295 + Y:commit-1-7
296 + Y:commit-5-6
297 + EOF
298 + (
299 + echo "get_reachable_subset(X,Y)" &&
300 + git rev-parse commit-3-3 \
301 + commit-1-7 | sort
302 + ) >expect &&
303 + test_three_modes get_reachable_subset
304 +'
305 +
306 +test_expect_success 'get_reachable_subset:none' '
307 + cat >input <<-\EOF &&
308 + X:commit-9-1
309 + X:commit-8-3
310 + X:commit-7-5
311 + X:commit-1-7
312 + Y:commit-9-3
313 + Y:commit-7-6
314 + Y:commit-2-8
315 + EOF
316 + echo "get_reachable_subset(X,Y)" >expect &&
317 + test_three_modes get_reachable_subset
318 +'
319 +
320 test_done