Skip to content

Commit 4c7bb45

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fcb2c07 commit 4c7bb45

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

t/helper/test-reach.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ int cmd__reach(int ac, const char **av)
3232
struct commit *A, *B;
3333
struct commit_list *X, *Y;
3434
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;
3737
struct strbuf buf = STRBUF_INIT;
3838
struct repository *r = the_repository;
3939

@@ -44,9 +44,10 @@ int cmd__reach(int ac, const char **av)
4444

4545
A = B = NULL;
4646
X = Y = NULL;
47-
X_nr = 0;
48-
X_alloc = 16;
47+
X_nr = Y_nr = 0;
48+
X_alloc = Y_alloc = 16;
4949
ALLOC_ARRAY(X_array, X_alloc);
50+
ALLOC_ARRAY(Y_array, Y_alloc);
5051

5152
while (strbuf_getline(&buf, stdin) != EOF) {
5253
struct object_id oid;
@@ -92,6 +93,8 @@ int cmd__reach(int ac, const char **av)
9293

9394
case 'Y':
9495
commit_list_insert(c, &Y);
96+
ALLOC_GROW(Y_array, Y_nr + 1, Y_alloc);
97+
Y_array[Y_nr++] = c;
9598
break;
9699

97100
default:
@@ -136,6 +139,29 @@ int cmd__reach(int ac, const char **av)
136139
filter.with_commit_tag_algo = 0;
137140

138141
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);
139165
}
140166

141167
exit(0);

t/t6600-test-reach.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,56 @@ test_expect_success 'commit_contains:miss' '
265265
test_three_modes commit_contains --tag
266266
'
267267

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+
268320
test_done

0 commit comments

Comments
 (0)