From: Adrian Ratiu <adrian.ratiu@collabora.com>
To: Patrick Steinhardt <ps@pks.im>
Cc: git@vger.kernel.org, Jeff King <peff@peff.net>,
Emily Shaffer <emilyshaffer@google.com>,
Junio C Hamano <gitster@pobox.com>,
Josh Steadmon <steadmon@google.com>,
Kristoffer Haugsbakk <kristofferhaugsbakk@fastmail.com>
Subject: Re: [PATCH v2 1/8] hook: add internal state alloc/free callbacks
Date: Fri, 20 Feb 2026 15:40:30 +0200 [thread overview]
Message-ID: <87342vy1m9.fsf@gentoo.mail-host-address-is-not-set> (raw)
In-Reply-To: <aZhXh6aqlY0VMgEG@pks.im>
On Fri, 20 Feb 2026, Patrick Steinhardt <ps@pks.im> wrote:
> On Thu, Feb 19, 2026 at 12:23:45AM +0200, Adrian Ratiu wrote:
>> diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
>> index 2d2b33d73d..f23772bc56 100644
>> --- a/builtin/receive-pack.c
>> +++ b/builtin/receive-pack.c
>> @@ -901,6 +901,26 @@ static int feed_receive_hook_cb(int hook_stdin_fd, void *pp_cb UNUSED, void *pp_
>> return state->cmd ? 0 : 1; /* 0 = more to come, 1 = EOF */
>> }
>>
>> +static void *receive_hook_feed_state_alloc(void *feed_pipe_ctx)
>> +{
>> + struct receive_hook_feed_state *init_state = feed_pipe_ctx;
>> + struct receive_hook_feed_state *data = xcalloc(1, sizeof(*data));
>
> Tiny nit, not worth addressing: we often use `CALLOC_ARRAY(data, 1)`
> nowadays.
I'll fix this since I'm planning to do a v3 anyway. Thanks for the
pointer.
>
>> + data->report = init_state->report;
>> + data->cmd = init_state->cmd;
>> + data->skip_broken = init_state->skip_broken;
>> + strbuf_init(&data->buf, 0);
>> + return data;
>> +}
>
> Okay, this basically creates the new instance by creating a deep copy of
> the "template" structure.
>
> One could split this up so that we have a "configuration" struct and a
> "data" struct, where we then provide a pointer to the configuration into
> the data structure, as only the buffer needs to change between the
> individual hook invocations. That would avoid some copying around, but
> it feels a bit unnecessary.
Yes, I also thought about this and reached the same conclusion.
>
>> +static void receive_hook_feed_state_free(void *data)
>> +{
>> + struct receive_hook_feed_state *d = data;
>> + if (!d)
>> + return;
>> + strbuf_release(&d->buf);
>> + free(d);
>> +}
>
> I would expect that the hook interfaces know to not call `free()` in
> case `alloc()` wasn't called, but I guess it doesn't hurt to be
> defensive here anyway.
Agreed.
>> @@ -908,7 +928,7 @@ static int run_receive_hook(struct command *commands,
>> {
>> struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
>> struct command *iter = commands;
>> - struct receive_hook_feed_state feed_state;
>> + struct receive_hook_feed_state feed_init_state = { 0 };
>> struct async sideband_async;
>> int sideband_async_started = 0;
>> int saved_stderr = -1;
>> @@ -938,16 +958,15 @@ static int run_receive_hook(struct command *commands,
>> prepare_sideband_async(&sideband_async, &saved_stderr, &sideband_async_started);
>>
>> /* set up stdin callback */
>> - feed_state.cmd = commands;
>> - feed_state.skip_broken = skip_broken;
>> - feed_state.report = NULL;
>> - strbuf_init(&feed_state.buf, 0);
>> - opt.feed_pipe_cb_data = &feed_state;
>> + feed_init_state.cmd = commands;
>> + feed_init_state.skip_broken = skip_broken;
>
> As far as I can see all of the data that we pass to the state struct is
> static, so we might just as well initialize it right away, right?
>
> struct receive_hook_feed_state feed_init_state = {
> .cmd = commands,
> .skip_broken = skip_broken,
> .buf = STRBUF_INIT,
> };
I missed this. Thanks. Will do in v3.
>> diff --git a/hook.c b/hook.c
>> index cde7198412..83ff658866 100644
>> --- a/hook.c
>> +++ b/hook.c
>> @@ -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);
>> }
>
> I guess this here would be where we could skip `free` in case the data
> wasn't even allocated. But as I said further up, I don't care all that
> much.
That is correct. I think I'll just drop that pointer check anyway in v3.
>> 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 */
>
> Do we maybe want to scope these function typedefs to the hooks subsystem
> by calling the `hook_data_free_fn` and `hook_data_alloc_fn`, or
> something like that?
Good idea. I'll rename them in v3.
next prev parent reply other threads:[~2026-02-20 13:40 UTC|newest]
Thread overview: 69+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-04 16:51 [PATCH 0/4] Specify hooks via configs Adrian Ratiu
2026-02-04 16:51 ` [PATCH 1/4] hook: run a list of hooks Adrian Ratiu
2026-02-05 21:59 ` Junio C Hamano
2026-02-06 11:21 ` Adrian Ratiu
2026-02-09 14:27 ` Patrick Steinhardt
2026-02-09 18:16 ` Adrian Ratiu
2026-02-10 13:43 ` Patrick Steinhardt
2026-02-04 16:51 ` [PATCH 2/4] hook: introduce "git hook list" Adrian Ratiu
2026-02-09 14:28 ` Patrick Steinhardt
2026-02-09 18:26 ` Adrian Ratiu
2026-02-04 16:51 ` [PATCH 3/4] hook: include hooks from the config Adrian Ratiu
2026-02-09 14:28 ` Patrick Steinhardt
2026-02-09 19:10 ` Adrian Ratiu
2026-02-10 13:43 ` Patrick Steinhardt
2026-02-10 13:56 ` Adrian Ratiu
2026-02-04 16:51 ` [PATCH 4/4] hook: allow out-of-repo 'git hook' invocations Adrian Ratiu
2026-02-06 16:26 ` [PATCH 0/4] Specify hooks via configs Junio C Hamano
2026-02-18 22:23 ` [PATCH v2 0/8] " Adrian Ratiu
2026-02-18 22:23 ` [PATCH v2 1/8] hook: add internal state alloc/free callbacks Adrian Ratiu
2026-02-19 21:47 ` Junio C Hamano
2026-02-20 12:35 ` Adrian Ratiu
2026-02-20 17:21 ` Junio C Hamano
2026-02-20 12:42 ` Adrian Ratiu
2026-02-20 12:45 ` Patrick Steinhardt
2026-02-20 13:40 ` Adrian Ratiu [this message]
2026-02-18 22:23 ` [PATCH v2 2/8] hook: run a list of hooks to prepare for multihook support Adrian Ratiu
2026-02-20 12:46 ` Patrick Steinhardt
2026-02-20 13:51 ` Adrian Ratiu
2026-02-18 22:23 ` [PATCH v2 3/8] hook: add "git hook list" command Adrian Ratiu
2026-02-20 12:46 ` Patrick Steinhardt
2026-02-20 13:53 ` Adrian Ratiu
2026-02-18 22:23 ` [PATCH v2 4/8] hook: include hooks from the config Adrian Ratiu
2026-02-19 22:16 ` Junio C Hamano
2026-02-20 12:27 ` Adrian Ratiu
2026-02-20 12:46 ` Patrick Steinhardt
2026-02-20 14:31 ` Adrian Ratiu
2026-02-18 22:23 ` [PATCH v2 5/8] hook: allow disabling config hooks Adrian Ratiu
2026-02-20 12:46 ` Patrick Steinhardt
2026-02-20 14:47 ` Adrian Ratiu
2026-02-20 18:40 ` Patrick Steinhardt
2026-02-20 18:45 ` Junio C Hamano
2026-02-18 22:23 ` [PATCH v2 6/8] hook: allow event = "" to overwrite previous values Adrian Ratiu
2026-02-18 22:23 ` [PATCH v2 7/8] hook: allow out-of-repo 'git hook' invocations Adrian Ratiu
2026-02-18 22:23 ` [PATCH v2 8/8] hook: add -z option to "git hook list" Adrian Ratiu
2026-02-19 21:34 ` [PATCH v2 0/8] Specify hooks via configs Junio C Hamano
2026-02-20 12:51 ` Adrian Ratiu
2026-02-20 23:29 ` brian m. carlson
2026-02-21 14:27 ` Adrian Ratiu
2026-02-22 0:39 ` Adrian Ratiu
2026-02-25 18:37 ` Junio C Hamano
2026-02-26 12:21 ` Adrian Ratiu
2026-02-25 22:30 ` brian m. carlson
2026-02-26 12:41 ` Adrian Ratiu
2026-03-01 18:44 ` [PATCH v3 00/12][next] " Adrian Ratiu
2026-03-01 18:44 ` [PATCH v3 01/12] hook: add internal state alloc/free callbacks Adrian Ratiu
2026-03-01 18:44 ` [PATCH v3 02/12] hook: run a list of hooks to prepare for multihook support Adrian Ratiu
2026-03-01 18:44 ` [PATCH v3 03/12] hook: add "git hook list" command Adrian Ratiu
2026-03-01 18:44 ` [PATCH v3 04/12] string-list: add unsorted_string_list_remove() Adrian Ratiu
2026-03-01 18:44 ` [PATCH v3 05/12] hook: include hooks from the config Adrian Ratiu
2026-03-01 18:44 ` [PATCH v3 06/12] hook: allow disabling config hooks Adrian Ratiu
2026-03-01 18:44 ` [PATCH v3 07/12] hook: allow event = "" to overwrite previous values Adrian Ratiu
2026-03-01 18:44 ` [PATCH v3 08/12] hook: allow out-of-repo 'git hook' invocations Adrian Ratiu
2026-03-01 18:44 ` [PATCH v3 09/12] hook: add -z option to "git hook list" Adrian Ratiu
2026-03-01 18:44 ` [PATCH v3 10/12] hook: refactor hook_config_cache from strmap to named struct Adrian Ratiu
2026-03-01 18:44 ` [PATCH v3 11/12] hook: store and display scope for configured hooks in git hook list Adrian Ratiu
2026-03-01 18:45 ` [PATCH v3 12/12] hook: show disabled hooks in "git hook list" Adrian Ratiu
2026-03-02 16:48 ` [PATCH v3 00/12][next] Specify hooks via configs Junio C Hamano
2026-03-02 17:04 ` Adrian Ratiu
2026-03-02 18:48 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87342vy1m9.fsf@gentoo.mail-host-address-is-not-set \
--to=adrian.ratiu@collabora.com \
--cc=emilyshaffer@google.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=kristofferhaugsbakk@fastmail.com \
--cc=peff@peff.net \
--cc=ps@pks.im \
--cc=steadmon@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox