aboutsummaryrefslogtreecommitdiff
path: root/hook.h
diff options
context:
space:
mode:
authorAdrian Ratiu <adrian.ratiu@collabora.com>2026-02-19 00:23:45 +0200
committerJunio C Hamano <gitster@pobox.com>2026-02-19 13:23:40 -0800
commitee2fbfd6b28fba20bc936ad1c2cb2617ba251025 (patch)
treeb956dd564d34592160e4ee9a507d2b15138b9fd0 /hook.h
parent468b5f75c3e277d84e2d739e642fbad27ccdb666 (diff)
downloadgit-ee2fbfd6b28fba20bc936ad1c2cb2617ba251025.tar.xz
hook: add internal state alloc/free callbacks
Some hooks use opaque structs to keep internal state between callbacks. Because hooks ran sequentially (jobs == 1) with one command per hook, these internal states could be allocated on the stack for each hook run. Next commits add the ability to run multiple commands for each hook, so the states cannot be shared or stored on the stack anymore, especially since down the line we will also enable parallel execution (jobs > 1). Add alloc/free helpers for each hook, doing a "deep" alloc/init & free of their internal opaque struct. The alloc callback takes a context pointer, to initialize the struct at at the time of resource acquisition. These callbacks must always be provided together: no alloc without free and no free without alloc, otherwise a BUG() is triggered. Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'hook.h')
-rw-r--r--hook.h25
1 files changed, 24 insertions, 1 deletions
diff --git a/hook.h b/hook.h
index 20eb56fd63..a6bdc6f90f 100644
--- a/hook.h
+++ b/hook.h
@@ -5,6 +5,9 @@
struct repository;
+typedef void (*cb_data_free_fn)(void *data);
+typedef void *(*cb_data_alloc_fn)(void *init_ctx);
+
struct run_hooks_opt
{
/* Environment vars to be set for each hook */
@@ -88,10 +91,30 @@ struct run_hooks_opt
* It can be accessed directly via the third callback arg 'pp_task_cb':
* struct ... *state = pp_task_cb;
*
- * The caller is responsible for managing the memory for this data.
+ * The caller is responsible for managing the memory for this data by
+ * providing alloc/free callbacks to `run_hooks_opt`.
+ *
* Only useful when using `run_hooks_opt.feed_pipe`, otherwise ignore it.
*/
void *feed_pipe_cb_data;
+
+ /**
+ * Some hooks need to create a fresh `feed_pipe_cb_data` internal state,
+ * so they can keep track of progress without affecting one another.
+ *
+ * If provided, this function will be called to alloc & initialize the
+ * `feed_pipe_cb_data` for each hook.
+ *
+ * The `feed_pipe_ctx` pointer can be used to pass initialization data.
+ */
+ cb_data_alloc_fn feed_pipe_cb_data_alloc;
+
+ /**
+ * Called to free the memory initialized by `feed_pipe_cb_data_alloc`.
+ *
+ * Must always be provided when `feed_pipe_cb_data_alloc` is provided.
+ */
+ cb_data_free_fn feed_pipe_cb_data_free;
};
#define RUN_HOOKS_OPT_INIT { \