From d8a17ef09b8d9fdeb7d22cbc926cbebf3d8a58c9 Mon Sep 17 00:00:00 2001 From: René Scharfe Date: Wed, 24 Dec 2025 18:03:14 +0100 Subject: revision: export commit_stack MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dynamic arrays of commit pointers are used in several places. Some of them use a custom struct to hold array, item count and capacity, others have them as separate variables linked by a common name part. Pick one succinct, clean implementation -- commit_stack -- and convert the different variants to it to reduce code duplication. Signed-off-by: René Scharfe Signed-off-by: Junio C Hamano --- commit.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'commit.c') diff --git a/commit.c b/commit.c index 709c9eed58..f2edafa49c 100644 --- a/commit.c +++ b/commit.c @@ -1981,3 +1981,20 @@ int run_commit_hook(int editor_is_used, const char *index_file, opt.invoked_hook = invoked_hook; return run_hooks_opt(the_repository, name, &opt); } + +void commit_stack_push(struct commit_stack *stack, struct commit *commit) +{ + ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc); + stack->items[stack->nr++] = commit; +} + +struct commit *commit_stack_pop(struct commit_stack *stack) +{ + return stack->nr ? stack->items[--stack->nr] : NULL; +} + +void commit_stack_clear(struct commit_stack *stack) +{ + FREE_AND_NULL(stack->items); + stack->nr = stack->alloc = 0; +} -- cgit v1.3 From 2ebaa2b45e752088fd03d03c484fe43a653deba8 Mon Sep 17 00:00:00 2001 From: René Scharfe Date: Wed, 24 Dec 2025 18:03:22 +0100 Subject: commit: add commit_stack_init() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a function for initializing a struct commit_stack, for when static initialization is not possible or impractical. Signed-off-by: René Scharfe Signed-off-by: Junio C Hamano --- commit.c | 10 ++++++++-- commit.h | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'commit.c') diff --git a/commit.c b/commit.c index f2edafa49c..55b1c8d2f8 100644 --- a/commit.c +++ b/commit.c @@ -1982,6 +1982,12 @@ int run_commit_hook(int editor_is_used, const char *index_file, return run_hooks_opt(the_repository, name, &opt); } +void commit_stack_init(struct commit_stack *stack) +{ + stack->items = NULL; + stack->nr = stack->alloc = 0; +} + void commit_stack_push(struct commit_stack *stack, struct commit *commit) { ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc); @@ -1995,6 +2001,6 @@ struct commit *commit_stack_pop(struct commit_stack *stack) void commit_stack_clear(struct commit_stack *stack) { - FREE_AND_NULL(stack->items); - stack->nr = stack->alloc = 0; + free(stack->items); + commit_stack_init(stack); } diff --git a/commit.h b/commit.h index 81e047f820..7c01a76425 100644 --- a/commit.h +++ b/commit.h @@ -387,6 +387,7 @@ struct commit_stack { }; #define COMMIT_STACK_INIT { 0 } +void commit_stack_init(struct commit_stack *); void commit_stack_push(struct commit_stack *, struct commit *); struct commit *commit_stack_pop(struct commit_stack *); void commit_stack_clear(struct commit_stack *); -- cgit v1.3 From 958a816794b9382fe90585b674ee8b96ed6aa8bf Mon Sep 17 00:00:00 2001 From: René Scharfe Date: Wed, 24 Dec 2025 18:03:25 +0100 Subject: commit: add commit_stack_grow() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Junio C Hamano --- commit.c | 7 ++++++- commit.h | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'commit.c') diff --git a/commit.c b/commit.c index 55b1c8d2f8..28bb5ce029 100644 --- a/commit.c +++ b/commit.c @@ -1988,9 +1988,14 @@ void commit_stack_init(struct commit_stack *stack) stack->nr = stack->alloc = 0; } +void commit_stack_grow(struct commit_stack *stack, size_t extra) +{ + ALLOC_GROW(stack->items, st_add(stack->nr, extra), stack->alloc); +} + void commit_stack_push(struct commit_stack *stack, struct commit *commit) { - ALLOC_GROW(stack->items, stack->nr + 1, stack->alloc); + commit_stack_grow(stack, 1); stack->items[stack->nr++] = commit; } diff --git a/commit.h b/commit.h index 7c01a76425..79a761c37d 100644 --- a/commit.h +++ b/commit.h @@ -388,6 +388,7 @@ struct commit_stack { #define COMMIT_STACK_INIT { 0 } void commit_stack_init(struct commit_stack *); +void commit_stack_grow(struct commit_stack *, size_t); void commit_stack_push(struct commit_stack *, struct commit *); struct commit *commit_stack_pop(struct commit_stack *); void commit_stack_clear(struct commit_stack *); -- cgit v1.3