test-reach: use commit_stack
Use commit_stack instead of open-coding it. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Dec 24, 2025 at 18:03 UTC
64dbeefbd24e02eb05ae3250e118c9552b7690fb
1 file changed
+14
-20
t/helper/test-reach.c
+14
-20
@@ -34,8 +34,8 @@ int cmd__reach(int ac, const char **av)
34
struct commit *A, *B;
35
struct commit_list *X, *Y;
36
struct object_array X_obj = OBJECT_ARRAY_INIT;
37
- struct commit **X_array, **Y_array;
38
- size_t X_nr, X_alloc, Y_nr, Y_alloc;
37
+ struct commit_stack X_stack = COMMIT_STACK_INIT;
38
+ struct commit_stack Y_stack = COMMIT_STACK_INIT;
39
struct strbuf buf = STRBUF_INIT;
40
struct repository *r = the_repository;
41
@@ -46,10 +46,6 @@ int cmd__reach(int ac, const char **av)
46
47
A = B = NULL;
48
X = Y = NULL;
49
- X_nr = Y_nr = 0;
50
- X_alloc = Y_alloc = 16;
51
- ALLOC_ARRAY(X_array, X_alloc);
52
- ALLOC_ARRAY(Y_array, Y_alloc);
49
50
while (strbuf_getline(&buf, stdin) != EOF) {
51
struct object_id oid;
@@ -88,15 +84,13 @@ int cmd__reach(int ac, const char **av)
84
85
case 'X':
86
commit_list_insert(c, &X);
91
- ALLOC_GROW(X_array, X_nr + 1, X_alloc);
92
- X_array[X_nr++] = c;
87
+ commit_stack_push(&X_stack, c);
88
add_object_array(orig, NULL, &X_obj);
89
break;
90
91
case 'Y':
92
commit_list_insert(c, &Y);
98
- ALLOC_GROW(Y_array, Y_nr + 1, Y_alloc);
99
- Y_array[Y_nr++] = c;
93
+ commit_stack_push(&Y_stack, c);
94
break;
95
96
default:
@@ -112,16 +106,16 @@ int cmd__reach(int ac, const char **av)
106
repo_in_merge_bases(the_repository, A, B));
107
else if (!strcmp(av[1], "in_merge_bases_many"))
108
printf("%s(A,X):%d\n", av[1],
115
- repo_in_merge_bases_many(the_repository, A, X_nr, X_array, 0));
109
+ repo_in_merge_bases_many(the_repository, A, X_stack.nr, X_stack.items, 0));
110
else if (!strcmp(av[1], "is_descendant_of"))
111
printf("%s(A,X):%d\n", av[1], repo_is_descendant_of(r, A, X));
112
else if (!strcmp(av[1], "get_branch_base_for_tip"))
119
- printf("%s(A,X):%d\n", av[1], get_branch_base_for_tip(r, A, X_array, X_nr));
113
+ printf("%s(A,X):%d\n", av[1], get_branch_base_for_tip(r, A, X_stack.items, X_stack.nr));
114
else if (!strcmp(av[1], "get_merge_bases_many")) {
115
struct commit_list *list = NULL;
116
if (repo_get_merge_bases_many(the_repository,
123
- A, X_nr,
124
- X_array,
117
+ A, X_stack.nr,
118
+ X_stack.items,
119
&list) < 0)
120
exit(128);
121
printf("%s(A,X):\n", av[1]);
@@ -159,8 +153,8 @@ int cmd__reach(int ac, const char **av)
153
const int reachable_flag = 1;
154
int count = 0;
155
struct commit_list *current;
162
- struct commit_list *list = get_reachable_subset(X_array, X_nr,
163
- Y_array, Y_nr,
156
+ struct commit_list *list = get_reachable_subset(X_stack.items, X_stack.nr,
157
+ Y_stack.items, Y_stack.nr,
158
reachable_flag);
159
printf("get_reachable_subset(X,Y)\n");
160
for (current = list; current; current = current->next) {
@@ -169,8 +163,8 @@ int cmd__reach(int ac, const char **av)
163
oid_to_hex(&list->item->object.oid));
164
count++;
165
}
172
- for (size_t i = 0; i < Y_nr; i++) {
173
- if (Y_array[i]->object.flags & reachable_flag)
166
+ for (size_t i = 0; i < Y_stack.nr; i++) {
167
+ if (Y_stack.items[i]->object.flags & reachable_flag)
168
count--;
169
}
170
@@ -185,7 +179,7 @@ int cmd__reach(int ac, const char **av)
179
strbuf_release(&buf);
180
free_commit_list(X);
181
free_commit_list(Y);
188
- free(X_array);
189
- free(Y_array);
182
+ commit_stack_clear(&X_stack);
183
+ commit_stack_clear(&Y_stack);
184
return 0;
185
}