git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 4/8] hook: include hooks from the config

From
Adrian Ratiu <adrian.ratiu@collabora.com>
Date
Feb 20, 2026, 14:31 UTC
Message-ID
<87tsvbwkpd.fsf@collabora.com>
In-Reply-To
<aZhXm1-jw_Mi8-vL@pks.im>
On Fri, 20 Feb 2026, Patrick Steinhardt <ps@pks.im> wrote:
Show 15 quoted lines
> On Thu, Feb 19, 2026 at 12:23:48AM +0200, Adrian Ratiu wrote:
>> diff --git a/builtin/hook.c b/builtin/hook.c
>> index 51660c4941..e151bb2cd1 100644
>> --- a/builtin/hook.c
>> +++ b/builtin/hook.c
>> @@ -54,6 +56,10 @@ static void hook_clear(struct hook *h, cb_data_free_fn cb_data_free)
>>  
>>  	if (h->kind == HOOK_TRADITIONAL)
>>  		free((void *)h->u.traditional.path);
>> +	else if (h->kind == HOOK_CONFIGURED) {
>> +		free((void *)h->u.configured.friendly_name);
>> +		free((void *)h->u.configured.command);
>> +	}
>
> The `if` branch also needs curly braces now.
Ack, will fix.
Show 14 quoted lines
>> @@ -101,6 +107,187 @@ static void list_hooks_add_default(struct repository *r, const char *hookname,
>>  	string_list_append(hook_list, hook_path)->util = h;
>>  }
>>  
>> +static void unsorted_string_list_remove(struct string_list *list,
>> +					const char *str)
>> +{
>> +	struct string_list_item *item = unsorted_string_list_lookup(list, str);
>> +	if (item)
>> +		unsorted_string_list_delete_item(list, item - list->items, 0);
>> +}
>
> This looks like a function that could reasonably be added to
> "string-list.{c,h}".
Yes, I'll move the helper there.
Show 14 quoted lines
>> +/*
>> + * Callback struct to collect all hook.* keys in a single config pass.
>> + * commands: friendly-name to command map.
>> + * event_hooks: event-name to list of friendly-names map.
>> + * disabled_hooks: set of friendly-names with hook.name.enabled = false.
>> + */
>> +struct hook_all_config_cb {
>> +	struct strmap commands;
>> +	struct strmap event_hooks;
>
> Hm, curious that we've got two maps. I'd have expected to have a single
> map from "friendly name" or "hook name" to `struct hook`. But maybe
> we'll assemble these structs for those maps later on.
>
Exactly. I'll explain below.
Show 5 quoted lines
>> +	struct string_list disabled_hooks;
>> +};
>
> We don't have support for disabled hooks yet. I assume this'll be added
> by a later commit, only.
Good catch.
I need to move this definition to the later commit.
Show 83 quoted lines
>
> [snip]
>> +/* Populate `cache` with the complete hook configuration */
>> +static void build_hook_config_map(struct repository *r, struct strmap *cache)
>> +{
>> +	struct hook_all_config_cb cb_data;
>> +	struct hashmap_iter iter;
>> +	struct strmap_entry *e;
>> +
>> +	strmap_init(&cb_data.commands);
>> +	strmap_init(&cb_data.event_hooks);
>> +	string_list_init_dup(&cb_data.disabled_hooks);
>> +
>> +	/* Parse all configs in one run. */
>> +	repo_config(r, hook_config_lookup_all, &cb_data);
>> +
>> +	/* Construct the cache from parsed configs. */
>> +	strmap_for_each_entry(&cb_data.event_hooks, &iter, e) {
>> +		struct string_list *hook_names = e->value;
>> +		struct string_list *hooks = xcalloc(1, sizeof(*hooks));
>> +
>> +		string_list_init_dup(hooks);
>> +
>> +		for (size_t i = 0; i < hook_names->nr; i++) {
>> +			const char *hname = hook_names->items[i].string;
>> +			char *command;
>> +
>> +			command = strmap_get(&cb_data.commands, hname);
>> +			if (!command)
>> +				die(_("'hook.%s.command' must be configured or "
>> +				      "'hook.%s.event' must be removed;"
>> +				      " aborting."), hname, hname);
>> +
>> +			/* util stores the command; owned by the cache. */
>> +			string_list_append(hooks, hname)->util =
>> +				xstrdup(command);
>> +		}
>> +
>> +		strmap_put(cache, e->key, hooks);
>> +	}
>> +
>> +	strmap_clear(&cb_data.commands, 1);
>> +	string_list_clear(&cb_data.disabled_hooks, 0);
>> +	strmap_for_each_entry(&cb_data.event_hooks, &iter, e) {
>> +		string_list_clear(e->value, 0);
>> +		free(e->value);
>> +	}
>> +	strmap_clear(&cb_data.event_hooks, 0);
>> +}
>
> Okay, this is where we assemble the hooks. Still curious that the result
> isn't a `struct hook` for each configured hook.
>
> [snip]
>> +static void list_hooks_add_configured(struct repository *r,
>> +				      const char *hookname,
>> +				      struct string_list *list,
>> +				      struct run_hooks_opt *options)
>> +{
>> +	struct strmap *cache = get_hook_config_cache(r);
>> +	struct string_list *configured_hooks = strmap_get(cache, hookname);
>> +
>> +	/* Iterate through configured hooks and initialize internal states */
>> +	for (size_t i = 0; configured_hooks && i < configured_hooks->nr; i++) {
>> +		const char *friendly_name = configured_hooks->items[i].string;
>> +		const char *command = configured_hooks->items[i].util;
>> +		struct hook *hook = xcalloc(1, sizeof(struct hook));
>> +
>> +		if (options && options->feed_pipe_cb_data_alloc)
>> +			hook->feed_pipe_cb_data =
>> +				options->feed_pipe_cb_data_alloc(
>> +					options->feed_pipe_ctx);
>> +
>> +		hook->kind = HOOK_CONFIGURED;
>> +		hook->u.configured.friendly_name = xstrdup(friendly_name);
>> +		hook->u.configured.command = xstrdup(command);
>> +
>> +		string_list_append(list, friendly_name)->util = hook;
>> +	}
>> +}
>
> Okay, here we finally create the hook structures. Is there any specific
> reason why we don't cache these structures directly?
That is exactly what my first local cache implementation attempt did.
I doesn't work because:
1. struct hook contains the internal hook state which cannot be reused
   betwee hook calls (nor can it be cached).
2. struct hook requires the initialization ctx passed to
   feed_pipe_cb_data_alloc() which is different depending on the hook.
So 1 and 2 above make struct hook non-cacheable.
Maybe the following explains it better. :)
We have 3 different operations:
1. Parsing the config file once via hook_config_lookup_all()
2. Storing/reusing the config cache via build_hook_config_map()
3. Creating the struct hooks for execution via
   list_hooks_add_configured().

Caching can only work on the parsed config data, at step 2. Hope this all makes sense.

Maybe we could rename these steps/functions to better reflect their purpouse, or document this better somewhere in the source code?

Previous: Patrick SteinhardtNext: Adrian Ratiu
Message 42 of 71 in “Specify hooks via configs”
  1. 0/4 Specify hooks via configsAdrian Ratiu, Feb 4, 2026
  2. 2/4 hook: introduce "git hook list"Adrian Ratiu, Feb 4, 2026
  3. Patrick SteinhardtFeb 9, 2026
  4. Adrian RatiuFeb 9, 2026
  5. 3/4 hook: include hooks from the configAdrian Ratiu, Feb 4, 2026
  6. Patrick SteinhardtFeb 9, 2026
  7. Adrian RatiuFeb 9, 2026
  8. Patrick SteinhardtFeb 10, 2026
  9. Adrian RatiuFeb 10, 2026
  10. 1/4 hook: run a list of hooksAdrian Ratiu, Feb 4, 2026
  11. Junio C HamanoFeb 5, 2026
  12. Adrian RatiuFeb 6, 2026
  13. Patrick SteinhardtFeb 9, 2026
  14. Adrian RatiuFeb 9, 2026
  15. Patrick SteinhardtFeb 10, 2026
  16. 4/4 hook: allow out-of-repo 'git hook' invocationsAdrian Ratiu, Feb 4, 2026
  17. Junio C HamanoFeb 6, 2026
  18. 0/8 Specify hooks via configsAdrian Ratiu, Feb 18, 2026
  19. 2/8 hook: run a list of hooks to prepare for multihook supportAdrian Ratiu, Feb 18, 2026
  20. Patrick SteinhardtFeb 20, 2026
  21. Adrian RatiuFeb 20, 2026
  22. 1/8 hook: add internal state alloc/free callbacksAdrian Ratiu, Feb 18, 2026
  23. Junio C HamanoFeb 19, 2026
  24. Adrian RatiuFeb 20, 2026
  25. Junio C HamanoFeb 20, 2026
  26. Adrian RatiuFeb 20, 2026
  27. Patrick SteinhardtFeb 20, 2026
  28. Adrian RatiuFeb 20, 2026
  29. 3/8 hook: add "git hook list" commandAdrian Ratiu, Feb 18, 2026
  30. Patrick SteinhardtFeb 20, 2026
  31. Adrian RatiuFeb 20, 2026
  32. 5/8 hook: allow disabling config hooksAdrian Ratiu, Feb 18, 2026
  33. Patrick SteinhardtFeb 20, 2026
  34. Adrian RatiuFeb 20, 2026
  35. Patrick SteinhardtFeb 20, 2026
  36. Junio C HamanoFeb 20, 2026
  37. 6/8 hook: allow event = "" to overwrite previous valuesAdrian Ratiu, Feb 18, 2026
  38. 4/8 hook: include hooks from the configAdrian Ratiu, Feb 18, 2026
  39. Junio C HamanoFeb 19, 2026
  40. Adrian RatiuFeb 20, 2026
  41. Patrick SteinhardtFeb 20, 2026
  42. Adrian RatiuFeb 20, 2026
  43. 7/8 hook: allow out-of-repo 'git hook' invocationsAdrian Ratiu, Feb 18, 2026
  44. 8/8 hook: add -z option to "git hook list"Adrian Ratiu, Feb 18, 2026
  45. Junio C HamanoFeb 19, 2026
  46. Adrian RatiuFeb 20, 2026
  47. brian m. carlsonFeb 20, 2026
  48. Adrian RatiuFeb 21, 2026
  49. Adrian RatiuFeb 22, 2026
  50. Junio C HamanoFeb 25, 2026
  51. Adrian RatiuFeb 26, 2026
  52. brian m. carlsonFeb 25, 2026
  53. Adrian RatiuFeb 26, 2026
  54. 00/12 [next] Specify hooks via configsAdrian Ratiu, Mar 1, 2026
  55. 02/12 hook: run a list of hooks to prepare for multihook supportAdrian Ratiu, Mar 1, 2026
  56. 03/12 hook: add "git hook list" commandAdrian Ratiu, Mar 1, 2026
  57. 01/12 hook: add internal state alloc/free callbacksAdrian Ratiu, Mar 1, 2026
  58. 04/12 string-list: add unsorted_string_list_remove()Adrian Ratiu, Mar 1, 2026
  59. 06/12 hook: allow disabling config hooksAdrian Ratiu, Mar 1, 2026
  60. 05/12 hook: include hooks from the configAdrian Ratiu, Mar 1, 2026
  61. SZEDER GáborApr 6, 2026
  62. Adrian RatiuApr 8, 2026
  63. 07/12 hook: allow event = "" to overwrite previous valuesAdrian Ratiu, Mar 1, 2026
  64. 08/12 hook: allow out-of-repo 'git hook' invocationsAdrian Ratiu, Mar 1, 2026
  65. 09/12 hook: add -z option to "git hook list"Adrian Ratiu, Mar 1, 2026
  66. 10/12 hook: refactor hook_config_cache from strmap to named structAdrian Ratiu, Mar 1, 2026
  67. 11/12 hook: store and display scope for configured hooks in git hook listAdrian Ratiu, Mar 1, 2026
  68. 12/12 hook: show disabled hooks in "git hook list"Adrian Ratiu, Mar 1, 2026
  69. Junio C HamanoMar 2, 2026
  70. Adrian RatiuMar 2, 2026
  71. Junio C HamanoMar 2, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.