commit: add commit_stack_init()

Add a function for initializing a struct commit_stack, for when static initialization is not possible or impractical. 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 2ebaa2b45e752088fd03d03c484fe43a653deba8
2 files changed +9 -2
commit.c
+8 -2
@@ -1982,6 +1982,12 @@ int run_commit_hook(int editor_is_used, const char *index_file,
1982 return run_hooks_opt(the_repository, name, &opt);
1983 }
1984
1985 +void commit_stack_init(struct commit_stack *stack)
1986 +{
1987 + stack->items = NULL;
1988 + stack->nr = stack->alloc = 0;
1989 +}
1990 +
1991 void commit_stack_push(struct commit_stack *stack, struct commit *commit)
1992 {
1993 ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc);
@@ -1995,6 +2001,6 @@ struct commit *commit_stack_pop(struct commit_stack *stack)
2001
2002 void commit_stack_clear(struct commit_stack *stack)
2003 {
1998 - FREE_AND_NULL(stack->items);
1999 - stack->nr = stack->alloc = 0;
2004 + free(stack->items);
2005 + commit_stack_init(stack);
2006 }
commit.h
+1
@@ -387,6 +387,7 @@ struct commit_stack {
387 };
388 #define COMMIT_STACK_INIT { 0 }
389
390 +void commit_stack_init(struct commit_stack *);
391 void commit_stack_push(struct commit_stack *, struct commit *);
392 struct commit *commit_stack_pop(struct commit_stack *);
393 void commit_stack_clear(struct commit_stack *);