git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 08/14] test-reach: use commit_stack

From
René Scharfe <l.s.r@web.de>
Date
Dec 24, 2025, 17:03 UTC
Message-ID
<20251224170327.68049-9-l.s.r@web.de>
In-Reply-To
<20251224170327.68049-1-l.s.r@web.de>
Use commit_stack instead of open-coding it.
Signed-off-by: René Scharfe <l.s.r@web.de>
---
 t/helper/test-reach.c | 34 ++++++++++++++--------------------
 1 file changed, 14 insertions(+), 20 deletions(-)
diff --git a/t/helper/test-reach.c b/t/helper/test-reach.c
index c58c93800f..feabeb29c2 100644
--- a/t/helper/test-reach.c
+++ b/t/helper/test-reach.c
@@ -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;
 }
-- 
2.52.0
Previous: René ScharfeNext: René Scharfe
Message 4 of 15 in “export and use commit_stack”
  1. 00/14 export and use commit_stackRené Scharfe, Dec 24, 2025
  2. 01/14 revision: export commit_stackRené Scharfe, Dec 24, 2025
  3. 05/14 remote: use commit_stack for local_commitsRené Scharfe, Dec 24, 2025
  4. 08/14 test-reach: use commit_stackRené Scharfe, Dec 24, 2025
  5. 04/14 name-rev: use commit_stackRené Scharfe, Dec 24, 2025
  6. 02/14 log: use commit_stackRené Scharfe, Dec 24, 2025
  7. 07/14 remote: use commit_stack for src_commitsRené Scharfe, Dec 24, 2025
  8. 03/14 midx: use commit_stackRené Scharfe, Dec 24, 2025
  9. 06/14 remote: use commit_stack for sent_tipsRené Scharfe, Dec 24, 2025
  10. 11/14 shallow: use commit_stackRené Scharfe, Dec 24, 2025
  11. 10/14 pack-bitmap-write: use commit_stackRené Scharfe, Dec 24, 2025
  12. 09/14 commit: add commit_stack_init()René Scharfe, Dec 24, 2025
  13. 13/14 commit-graph: use commit_stackRené Scharfe, Dec 24, 2025
  14. 14/14 commit-reach: use commit_stackRené Scharfe, Dec 24, 2025
  15. 12/14 commit: add commit_stack_grow()René Scharfe, Dec 24, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.