]> git.ipfire.org Git - thirdparty/git.git/commitdiff
test-reach: use commit_stack
authorRené Scharfe <l.s.r@web.de>
Wed, 24 Dec 2025 17:03:21 +0000 (18:03 +0100)
committerJunio C Hamano <gitster@pobox.com>
Wed, 24 Dec 2025 23:29:28 +0000 (08:29 +0900)
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>
t/helper/test-reach.c

index c58c93800f32320eb83ea192196776a9cc4223f1..feabeb29c25d898786159cfee153ddc559aa85b6 100644 (file)
@@ -34,8 +34,8 @@ int cmd__reach(int ac, const char **av)
        struct commit *A, *B;
        struct commit_list *X, *Y;
        struct object_array X_obj = OBJECT_ARRAY_INIT;
-       struct commit **X_array, **Y_array;
-       size_t X_nr, X_alloc, Y_nr, Y_alloc;
+       struct commit_stack X_stack = COMMIT_STACK_INIT;
+       struct commit_stack Y_stack = COMMIT_STACK_INIT;
        struct strbuf buf = STRBUF_INIT;
        struct repository *r = the_repository;
 
@@ -46,10 +46,6 @@ int cmd__reach(int ac, const char **av)
 
        A = B = NULL;
        X = Y = NULL;
-       X_nr = Y_nr = 0;
-       X_alloc = Y_alloc = 16;
-       ALLOC_ARRAY(X_array, X_alloc);
-       ALLOC_ARRAY(Y_array, Y_alloc);
 
        while (strbuf_getline(&buf, stdin) != EOF) {
                struct object_id oid;
@@ -88,15 +84,13 @@ int cmd__reach(int ac, const char **av)
 
                        case 'X':
                                commit_list_insert(c, &X);
-                               ALLOC_GROW(X_array, X_nr + 1, X_alloc);
-                               X_array[X_nr++] = c;
+                               commit_stack_push(&X_stack, c);
                                add_object_array(orig, NULL, &X_obj);
                                break;
 
                        case 'Y':
                                commit_list_insert(c, &Y);
-                               ALLOC_GROW(Y_array, Y_nr + 1, Y_alloc);
-                               Y_array[Y_nr++] = c;
+                               commit_stack_push(&Y_stack, c);
                                break;
 
                        default:
@@ -112,16 +106,16 @@ int cmd__reach(int ac, const char **av)
                       repo_in_merge_bases(the_repository, A, B));
        else if (!strcmp(av[1], "in_merge_bases_many"))
                printf("%s(A,X):%d\n", av[1],
-                      repo_in_merge_bases_many(the_repository, A, X_nr, X_array, 0));
+                      repo_in_merge_bases_many(the_repository, A, X_stack.nr, X_stack.items, 0));
        else if (!strcmp(av[1], "is_descendant_of"))
                printf("%s(A,X):%d\n", av[1], repo_is_descendant_of(r, A, X));
        else if (!strcmp(av[1], "get_branch_base_for_tip"))
-               printf("%s(A,X):%d\n", av[1], get_branch_base_for_tip(r, A, X_array, X_nr));
+               printf("%s(A,X):%d\n", av[1], get_branch_base_for_tip(r, A, X_stack.items, X_stack.nr));
        else if (!strcmp(av[1], "get_merge_bases_many")) {
                struct commit_list *list = NULL;
                if (repo_get_merge_bases_many(the_repository,
-                                             A, X_nr,
-                                             X_array,
+                                             A, X_stack.nr,
+                                             X_stack.items,
                                              &list) < 0)
                        exit(128);
                printf("%s(A,X):\n", av[1]);
@@ -159,8 +153,8 @@ int cmd__reach(int ac, const char **av)
                const int reachable_flag = 1;
                int count = 0;
                struct commit_list *current;
-               struct commit_list *list = get_reachable_subset(X_array, X_nr,
-                                                               Y_array, Y_nr,
+               struct commit_list *list = get_reachable_subset(X_stack.items, X_stack.nr,
+                                                               Y_stack.items, Y_stack.nr,
                                                                reachable_flag);
                printf("get_reachable_subset(X,Y)\n");
                for (current = list; current; current = current->next) {
@@ -169,8 +163,8 @@ int cmd__reach(int ac, const char **av)
                                    oid_to_hex(&list->item->object.oid));
                        count++;
                }
-               for (size_t i = 0; i < Y_nr; i++) {
-                       if (Y_array[i]->object.flags & reachable_flag)
+               for (size_t i = 0; i < Y_stack.nr; i++) {
+                       if (Y_stack.items[i]->object.flags & reachable_flag)
                                count--;
                }
 
@@ -185,7 +179,7 @@ int cmd__reach(int ac, const char **av)
        strbuf_release(&buf);
        free_commit_list(X);
        free_commit_list(Y);
-       free(X_array);
-       free(Y_array);
+       commit_stack_clear(&X_stack);
+       commit_stack_clear(&Y_stack);
        return 0;
 }