diff options
| author | Adrian Ratiu <adrian.ratiu@collabora.com> | 2026-02-19 00:23:45 +0200 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2026-02-19 13:23:40 -0800 |
| commit | ee2fbfd6b28fba20bc936ad1c2cb2617ba251025 (patch) | |
| tree | b956dd564d34592160e4ee9a507d2b15138b9fd0 /hook.c | |
| parent | 468b5f75c3e277d84e2d739e642fbad27ccdb666 (diff) | |
| download | git-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.c')
| -rw-r--r-- | hook.c | 13 |
1 files changed, 13 insertions, 0 deletions
@@ -133,6 +133,8 @@ static int notify_hook_finished(int result, static void run_hooks_opt_clear(struct run_hooks_opt *options) { + if (options->feed_pipe_cb_data_free) + options->feed_pipe_cb_data_free(options->feed_pipe_cb_data); strvec_clear(&options->env); strvec_clear(&options->args); } @@ -172,6 +174,17 @@ int run_hooks_opt(struct repository *r, const char *hook_name, if (!options->jobs) BUG("run_hooks_opt must be called with options.jobs >= 1"); + /* + * Ensure cb_data copy and free functions are either provided together, + * or neither one is provided. + */ + if ((options->feed_pipe_cb_data_alloc && !options->feed_pipe_cb_data_free) || + (!options->feed_pipe_cb_data_alloc && options->feed_pipe_cb_data_free)) + BUG("feed_pipe_cb_data_alloc and feed_pipe_cb_data_free must be set together"); + + if (options->feed_pipe_cb_data_alloc) + options->feed_pipe_cb_data = options->feed_pipe_cb_data_alloc(options->feed_pipe_ctx); + if (options->invoked_hook) *options->invoked_hook = 0; |
