aboutsummaryrefslogtreecommitdiff
path: root/hook.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2026-04-03 13:01:08 -0700
committerJunio C Hamano <gitster@pobox.com>2026-04-03 13:01:09 -0700
commit0cd4fb9f46eb0ebd0d243a886ce9a52210e0723e (patch)
tree8069cdba6fa414dc0cb1e11c5c7de7446ef6071f /hook.c
parent4e5821732e684f21a35288d8e67f453ca2595083 (diff)
parent5c58dbc887a1f3530cb29c995f63675beebb22e9 (diff)
downloadgit-0cd4fb9f46eb0ebd0d243a886ce9a52210e0723e.tar.xz
Merge branch 'ar/config-hook-cleanups'
Code clean-up around the recent "hooks defined in config" topic. * ar/config-hook-cleanups: hook: reject unknown hook names in git-hook(1) hook: show disabled hooks in "git hook list" hook: show config scope in git hook list hook: introduce hook_config_cache_entry for per-hook data t1800: add test to verify hook execution ordering hook: make consistent use of friendly-name in docs hook: replace hook_list_clear() -> string_list_clear_func() hook: detect & emit two more bugs hook: rename cb_data_free/alloc -> hook_data_free/alloc hook: fix minor style issues builtin/receive-pack: properly init receive_hook strbuf hook: move unsorted_string_list_remove() to string-list.[ch]
Diffstat (limited to 'hook.c')
-rw-r--r--hook.c190
1 files changed, 122 insertions, 68 deletions
diff --git a/hook.c b/hook.c
index 2c8252b2c4..cc23276d27 100644
--- a/hook.c
+++ b/hook.c
@@ -1,16 +1,16 @@
#include "git-compat-util.h"
#include "abspath.h"
#include "advice.h"
+#include "config.h"
+#include "environment.h"
#include "gettext.h"
#include "hook.h"
-#include "path.h"
#include "parse.h"
+#include "path.h"
#include "run-command.h"
-#include "config.h"
+#include "setup.h"
#include "strbuf.h"
#include "strmap.h"
-#include "environment.h"
-#include "setup.h"
const char *find_hook(struct repository *r, const char *name)
{
@@ -52,34 +52,26 @@ const char *find_hook(struct repository *r, const char *name)
return path.buf;
}
-static void hook_clear(struct hook *h, cb_data_free_fn cb_data_free)
+void hook_free(void *p, const char *str UNUSED)
{
+ struct hook *h = p;
+
if (!h)
return;
- if (h->kind == HOOK_TRADITIONAL)
+ if (h->kind == HOOK_TRADITIONAL) {
free((void *)h->u.traditional.path);
- else if (h->kind == HOOK_CONFIGURED) {
+ } else if (h->kind == HOOK_CONFIGURED) {
free((void *)h->u.configured.friendly_name);
free((void *)h->u.configured.command);
}
- if (cb_data_free)
- cb_data_free(h->feed_pipe_cb_data);
+ if (h->data_free && h->feed_pipe_cb_data)
+ h->data_free(h->feed_pipe_cb_data);
free(h);
}
-void hook_list_clear(struct string_list *hooks, cb_data_free_fn cb_data_free)
-{
- struct string_list_item *item;
-
- for_each_string_list_item(item, hooks)
- hook_clear(item->util, cb_data_free);
-
- string_list_clear(hooks, 0);
-}
-
/* Helper to detect and add default "traditional" hooks from the hookdir. */
static void list_hooks_add_default(struct repository *r, const char *hookname,
struct string_list *hook_list,
@@ -91,7 +83,7 @@ static void list_hooks_add_default(struct repository *r, const char *hookname,
if (!hook_path)
return;
- h = xcalloc(1, sizeof(struct hook));
+ CALLOC_ARRAY(h, 1);
/*
* If the hook is to run in a specific dir, a relative path can
@@ -100,9 +92,15 @@ static void list_hooks_add_default(struct repository *r, const char *hookname,
if (options && options->dir)
hook_path = absolute_path(hook_path);
- /* Setup per-hook internal state cb data */
- if (options && options->feed_pipe_cb_data_alloc)
+ /*
+ * Setup per-hook internal state callback data.
+ * When provided, the alloc/free callbacks are always provided
+ * together, so use them to alloc/free the internal hook state.
+ */
+ if (options && options->feed_pipe_cb_data_alloc) {
h->feed_pipe_cb_data = options->feed_pipe_cb_data_alloc(options->feed_pipe_ctx);
+ h->data_free = options->feed_pipe_cb_data_free;
+ }
h->kind = HOOK_TRADITIONAL;
h->u.traditional.path = xstrdup(hook_path);
@@ -110,19 +108,21 @@ 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);
-}
+/*
+ * Cache entry stored as the .util pointer of string_list items inside the
+ * hook config cache.
+ */
+struct hook_config_cache_entry {
+ char *command;
+ enum config_scope scope;
+ bool disabled;
+};
/*
* 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.
+ * disabled_hooks: set of friendly-names with hook.<friendly-name>.enabled = false.
*/
struct hook_all_config_cb {
struct strmap commands;
@@ -132,7 +132,7 @@ struct hook_all_config_cb {
/* repo_config() callback that collects all hook.* configuration in one pass. */
static int hook_config_lookup_all(const char *key, const char *value,
- const struct config_context *ctx UNUSED,
+ const struct config_context *ctx,
void *cb_data)
{
struct hook_all_config_cb *data = cb_data;
@@ -156,20 +156,32 @@ static int hook_config_lookup_all(const char *key, const char *value,
struct strmap_entry *e;
strmap_for_each_entry(&data->event_hooks, &iter, e)
- unsorted_string_list_remove(e->value, hook_name);
+ unsorted_string_list_remove(e->value, hook_name, 0);
} else {
struct string_list *hooks =
strmap_get(&data->event_hooks, value);
if (!hooks) {
- hooks = xcalloc(1, sizeof(*hooks));
+ CALLOC_ARRAY(hooks, 1);
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);
- string_list_append(hooks, hook_name);
+ unsorted_string_list_remove(hooks, hook_name, 0);
+
+ if (!ctx->kvi)
+ BUG("hook config callback called without key-value info");
+
+ /*
+ * Stash the config scope in the util pointer for
+ * later retrieval in build_hook_config_map(). This
+ * intermediate struct is transient and never leaves
+ * that function, so we pack the enum value into the
+ * pointer rather than heap-allocating a wrapper.
+ */
+ string_list_append(hooks, hook_name)->util =
+ (void *)(uintptr_t)ctx->kvi->scope;
}
} else if (!strcmp(subkey, "command")) {
/* Store command overwriting the old value */
@@ -186,7 +198,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 */
@@ -202,8 +214,10 @@ static int hook_config_lookup_all(const char *key, const char *value,
* every item's string is the hook's friendly-name and its util pointer is
* the corresponding command string. Both strings are owned by the map.
*
- * Disabled hooks and hooks missing a command are already filtered out at
- * parse time, so callers can iterate the list directly.
+ * Disabled hooks are kept in the cache with entry->disabled set, so that
+ * "git hook list" can display them. A non-disabled hook missing a command
+ * is fatal; a disabled hook missing a command emits a warning and is kept
+ * in the cache with entry->command = NULL.
*/
void hook_cache_clear(struct strmap *cache)
{
@@ -212,7 +226,12 @@ void hook_cache_clear(struct strmap *cache)
strmap_for_each_entry(cache, &iter, e) {
struct string_list *hooks = e->value;
- string_list_clear(hooks, 1); /* free util (command) pointers */
+ for (size_t i = 0; i < hooks->nr; i++) {
+ struct hook_config_cache_entry *entry = hooks->items[i].util;
+ free(entry->command);
+ free(entry);
+ }
+ string_list_clear(hooks, 0);
free(hooks);
}
strmap_clear(cache, 0);
@@ -235,28 +254,39 @@ static void build_hook_config_map(struct repository *r, struct strmap *cache)
/* 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));
+ struct string_list *hooks;
+ CALLOC_ARRAY(hooks, 1);
string_list_init_dup(hooks);
for (size_t i = 0; i < hook_names->nr; i++) {
const char *hname = hook_names->items[i].string;
+ enum config_scope scope =
+ (enum config_scope)(uintptr_t)hook_names->items[i].util;
+ struct hook_config_cache_entry *entry;
char *command;
- /* filter out disabled hooks */
- if (unsorted_string_list_lookup(&cb_data.disabled_hooks,
- hname))
- continue;
+ bool is_disabled =
+ !!unsorted_string_list_lookup(
+ &cb_data.disabled_hooks, hname);
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);
+ if (!command) {
+ if (is_disabled)
+ warning(_("disabled hook '%s' has no "
+ "command configured"), hname);
+ else
+ 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);
+ /* util stores a cache entry; owned by the cache. */
+ CALLOC_ARRAY(entry, 1);
+ entry->command = xstrdup_or_null(command);
+ entry->scope = scope;
+ entry->disabled = is_disabled;
+ string_list_append(hooks, hname)->util = entry;
}
strmap_put(cache, e->key, hooks);
@@ -289,7 +319,7 @@ static struct strmap *get_hook_config_cache(struct repository *r)
* it just once on the first call.
*/
if (!r->hook_config_cache) {
- r->hook_config_cache = xcalloc(1, sizeof(*cache));
+ CALLOC_ARRAY(r->hook_config_cache, 1);
strmap_init(r->hook_config_cache);
build_hook_config_map(r, r->hook_config_cache);
}
@@ -297,9 +327,9 @@ static struct strmap *get_hook_config_cache(struct repository *r)
} else {
/*
* Out-of-repo calls (no gitdir) allocate and return a temporary
- * map cache which gets free'd immediately by the caller.
+ * cache which gets freed immediately by the caller.
*/
- cache = xcalloc(1, sizeof(*cache));
+ CALLOC_ARRAY(cache, 1);
strmap_init(cache);
build_hook_config_map(r, cache);
}
@@ -318,17 +348,28 @@ static void list_hooks_add_configured(struct repository *r,
/* 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));
+ struct hook_config_cache_entry *entry = configured_hooks->items[i].util;
+ struct hook *hook;
- if (options && options->feed_pipe_cb_data_alloc)
+ CALLOC_ARRAY(hook, 1);
+
+ /*
+ * When provided, the alloc/free callbacks are always provided
+ * together, so use them to alloc/free the internal hook state.
+ */
+ if (options && options->feed_pipe_cb_data_alloc) {
hook->feed_pipe_cb_data =
options->feed_pipe_cb_data_alloc(
options->feed_pipe_ctx);
+ hook->data_free = options->feed_pipe_cb_data_free;
+ }
hook->kind = HOOK_CONFIGURED;
hook->u.configured.friendly_name = xstrdup(friendly_name);
- hook->u.configured.command = xstrdup(command);
+ hook->u.configured.command =
+ entry->command ? xstrdup(entry->command) : NULL;
+ hook->u.configured.scope = entry->scope;
+ hook->u.configured.disabled = entry->disabled;
string_list_append(list, friendly_name)->util = hook;
}
@@ -351,7 +392,7 @@ struct string_list *list_hooks(struct repository *r, const char *hookname,
if (!hookname)
BUG("null hookname was provided to hook_list()!");
- hook_head = xmalloc(sizeof(struct string_list));
+ CALLOC_ARRAY(hook_head, 1);
string_list_init_dup(hook_head);
/* Add hooks from the config, e.g. hook.myhook.event = pre-commit */
@@ -366,8 +407,17 @@ struct string_list *list_hooks(struct repository *r, const char *hookname,
int hook_exists(struct repository *r, const char *name)
{
struct string_list *hooks = list_hooks(r, name, NULL);
- int exists = hooks->nr > 0;
- hook_list_clear(hooks, NULL);
+ int exists = 0;
+
+ for (size_t i = 0; i < hooks->nr; i++) {
+ struct hook *h = hooks->items[i].util;
+ if (h->kind == HOOK_TRADITIONAL ||
+ !h->u.configured.disabled) {
+ exists = 1;
+ break;
+ }
+ }
+ string_list_clear_func(hooks, hook_free);
free(hooks);
return exists;
}
@@ -381,10 +431,11 @@ static int pick_next_hook(struct child_process *cp,
struct string_list *hook_list = hook_cb->hook_command_list;
struct hook *h;
- if (hook_cb->hook_to_run_index >= hook_list->nr)
- return 0;
-
- h = hook_list->items[hook_cb->hook_to_run_index++].util;
+ do {
+ if (hook_cb->hook_to_run_index >= hook_list->nr)
+ return 0;
+ h = hook_list->items[hook_cb->hook_to_run_index++].util;
+ } while (h->kind == HOOK_CONFIGURED && h->u.configured.disabled);
cp->no_stdin = 1;
strvec_pushv(&cp->env, hook_cb->options->env.v);
@@ -414,7 +465,11 @@ static int pick_next_hook(struct child_process *cp,
} else if (h->kind == HOOK_CONFIGURED) {
/* to enable oneliners, let config-specified hooks run in shell. */
cp->use_shell = true;
+ if (!h->u.configured.command)
+ BUG("non-disabled HOOK_CONFIGURED hook has no command");
strvec_push(&cp->args, h->u.configured.command);
+ } else {
+ BUG("unknown hook kind");
}
if (!cp->args.nr)
@@ -501,8 +556,7 @@ int run_hooks_opt(struct repository *r, const char *hook_name,
* 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))
+ if (!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->invoked_hook)
@@ -518,7 +572,7 @@ int run_hooks_opt(struct repository *r, const char *hook_name,
run_processes_parallel(&opts);
ret = cb_data.rc;
cleanup:
- hook_list_clear(cb_data.hook_command_list, options->feed_pipe_cb_data_free);
+ string_list_clear_func(cb_data.hook_command_list, hook_free);
free(cb_data.hook_command_list);
run_hooks_opt_clear(options);
return ret;