@@ -3,7 +3,7 @@
#include "strbuf.h"
#include "help.h"
#include "compat/compiler.h"
-#include "run-command.h"
+#include "hook.h"
static void get_system_info(struct strbuf *sys_info)
@@ -82,7 +82,7 @@ static void get_populated_hooks(struct strbuf *hook_info, int nongit)
}
for (i = 0; i < ARRAY_SIZE(hook); i++)
- if (find_hook(hook[i]))
+ if (hook_exists(hook[i], configured_hookdir_opt()))
strbuf_addf(hook_info, "%s\n", hook[i]);
}
@@ -225,6 +225,21 @@ void run_hooks_opt_init(struct run_hooks_opt *o)
o->run_hookdir = configured_hookdir_opt();
}
+int hook_exists(const char *hookname, enum hookdir_opt should_run_hookdir)
+{
+ const char *value = NULL; /* throwaway */
+ struct strbuf hook_key = STRBUF_INIT;
+
+ int could_run_hookdir = (should_run_hookdir == hookdir_interactive ||
+ should_run_hookdir == hookdir_warn ||
+ should_run_hookdir == hookdir_yes)
+ && !!find_hook(hookname);
+
+ strbuf_addf(&hook_key, "hook.%s.command", hookname);
+
+ return (!git_config_get_value(hook_key.buf, &value)) || could_run_hookdir;
+}
+
void run_hooks_opt_clear(struct run_hooks_opt *o)
{
strvec_clear(&o->env);
@@ -62,6 +62,15 @@ struct run_hooks_opt
void run_hooks_opt_init(struct run_hooks_opt *o);
void run_hooks_opt_clear(struct run_hooks_opt *o);
+/*
+ * Returns 1 if any hooks are specified in the config or if a hook exists in the
+ * hookdir. Typically, invoke hook_exsts() like:
+ * hook_exists(hookname, configured_hookdir_opt());
+ * Like with run_hooks, if you take a --run-hookdir flag, reflect that
+ * user-specified behavior here instead.
+ */
+int hook_exists(const char *hookname, enum hookdir_opt should_run_hookdir);
+
/*
* Runs all hooks associated to the 'hookname' event in order. Each hook will be
* passed 'env' and 'args'.
Add a helper to easily determine whether any hooks exist for a given hook event. Many callers want to check whether some state could be modified by a hook; that check should include the config-based hooks as well. Optimize by checking the config directly. Since commands which execute hooks might want to take args to replace 'hook.runHookDir', let 'hook_exists()' mirror the behavior of 'hook.runHookDir'. Signed-off-by: Emily Shaffer <emilyshaffer@google.com> --- Notes: Since v4, updated this commit to include bugreport as a builtin instead of as a standalone. Since v4, a little more nuance when deciding whether a hookdir hook can happen. builtin/bugreport.c | 4 ++-- hook.c | 15 +++++++++++++++ hook.h | 9 +++++++++ 3 files changed, 26 insertions(+), 2 deletions(-)