[PATCH v3 07/12] hook: allow event = "" to overwrite previous values
- From
Adrian Ratiu <adrian.ratiu@collabora.com>
- Date
- Mar 1, 2026, 18:44 UTC
- Message-ID
- <20260301184500.1488433-8-adrian.ratiu@collabora.com>
- In-Reply-To
- <20260301184500.1488433-1-adrian.ratiu@collabora.com>
Add the ability for empty events to clear previously set multivalue variables, so the newly added "hook.*.event" behave like the other multivalued keys.
Suggested-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> --- Documentation/config/hook.adoc | 4 +++- hook.c | 31 ++++++++++++++++++++----------- t/t1800-hook.sh | 12 ++++++++++++ 3 files changed, 35 insertions(+), 12 deletions(-)
diff --git a/Documentation/config/hook.adoc b/Documentation/config/hook.adoc index 4bbda5636d..d0023b2deb 100644 --- a/Documentation/config/hook.adoc +++ b/Documentation/config/hook.adoc @@ -12,7 +12,9 @@ hook.<friendly-name>.event:: linkgit:githooks[5] for a complete list of hook events.) On the specified event, the associated `hook.<friendly-name>.command` is executed. This is a multi-valued key. To run `hook.<friendly-name>` on multiple - events, specify the key more than once. See linkgit:git-hook[1]. + events, specify the key more than once. An empty value resets + the list of events, clearing any previously defined events for + `hook.<friendly-name>`. See linkgit:git-hook[1]. hook.<friendly-name>.enabled:: Whether the hook `hook.<friendly-name>` is enabled. Defaults to `true`. diff --git a/hook.c b/hook.c index 24eb330cac..696919e703 100644 --- a/hook.c +++ b/hook.c @@ -133,18 +133,27 @@ static int hook_config_lookup_all(const char *key, const char *value, hook_name = xmemdupz(name, name_len); if (!strcmp(subkey, "event")) { - struct string_list *hooks = - strmap_get(&data->event_hooks, value); + if (!*value) { + /* Empty values reset previous events for this hook. */ + struct hashmap_iter iter; + struct strmap_entry *e; + + strmap_for_each_entry(&data->event_hooks, &iter, e) + unsorted_string_list_remove(e->value, hook_name, 0); + } else { + struct string_list *hooks = + strmap_get(&data->event_hooks, value); + + if (!hooks) { + CALLOC_ARRAY(hooks, 1); + string_list_init_dup(hooks); + strmap_put(&data->event_hooks, value, hooks); + } - if (!hooks) { - hooks = xcalloc(1, sizeof(*hooks)); - string_list_init_dup(hooks); - strmap_put(&data->event_hooks, value, hooks); + /* Re-insert if necessary to preserve last-seen order. */ + unsorted_string_list_remove(hooks, hook_name, 0); + string_list_append(hooks, hook_name); } - - /* Re-insert if necessary to preserve last-seen order. */ - unsorted_string_list_remove(hooks, hook_name, 0); - string_list_append(hooks, hook_name); } else if (!strcmp(subkey, "command")) { /* Store command overwriting the old value */ char *old = strmap_put(&data->commands, hook_name, @@ -160,7 +169,7 @@ static int hook_config_lookup_all(const char *key, const char *value, break; case 1: /* enabled: undo a prior disabled entry */ unsorted_string_list_remove(&data->disabled_hooks, - hook_name); + hook_name, 0); break; default: break; /* ignore unrecognised values */ diff --git a/t/t1800-hook.sh b/t/t1800-hook.sh index fb8cfc8137..c14ec661b9 100755 --- a/t/t1800-hook.sh +++ b/t/t1800-hook.sh @@ -225,6 +225,18 @@ test_expect_success 'git hook list reorders on duplicate event declarations' ' test_cmp expected actual ' +test_expect_success 'git hook list: empty event value resets events' ' + setup_hooks && + + # ghi is configured for pre-commit; reset it with an empty value + test_config hook.ghi.event "" --add && + + # only def should remain for pre-commit + echo "def" >expected && + git hook list pre-commit >actual && + test_cmp expected actual +' + test_expect_success 'hook can be configured for multiple events' ' setup_hooks &&
-- 2.52.0.732.gb351b5166d.dirty