commit: add commit_stack_grow()

Add a function for increasing the capacity of a commit_stack. It is useful for reducing reallocations when the target size is known in advance. 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 958a816794b9382fe90585b674ee8b96ed6aa8bf
2 files changed +7 -1
commit.c
+6 -1
@@ -1988,9 +1988,14 @@ void commit_stack_init(struct commit_stack *stack)
1988 stack->nr = stack->alloc = 0;
1989 }
1990
1991 +void commit_stack_grow(struct commit_stack *stack, size_t extra)
1992 +{
1993 + ALLOC_GROW(stack->items, st_add(stack->nr, extra), stack->alloc);
1994 +}
1995 +
1996 void commit_stack_push(struct commit_stack *stack, struct commit *commit)
1997 {
1993 - ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
1998 + commit_stack_grow(stack, 1);
1999 stack->items[stack->nr++] = commit;
2000 }
2001
commit.h
+1
@@ -388,6 +388,7 @@ struct commit_stack {
388 #define COMMIT_STACK_INIT { 0 }
389
390 void commit_stack_init(struct commit_stack *);
391 +void commit_stack_grow(struct commit_stack *, size_t);
392 void commit_stack_push(struct commit_stack *, struct commit *);
393 struct commit *commit_stack_pop(struct commit_stack *);
394 void commit_stack_clear(struct commit_stack *);