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

[PATCH v5 8/8] hook: replace find_hook() with hook_exists()

From
Emily Shaffer <emilyshaffer@google.com>
Date
Oct 14, 2020, 23:24 UTC
Message-ID
<20201014232447.3050579-9-emilyshaffer@google.com>
In-Reply-To
<20201014232447.3050579-1-emilyshaffer@google.com>

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, a little more nuance when deciding whether a hookdir hook can happen.
 hook.c | 14 ++++++++++++++
 hook.h |  9 +++++++++
 2 files changed, 23 insertions(+)
diff --git a/hook.c b/hook.c
index 1494a32c1a..e3d289d0e9 100644
--- a/hook.c
+++ b/hook.c
@@ -218,6 +218,20 @@ struct list_head* hook_list(const struct strbuf* hookname)
 	return hook_head;
 }
 
+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;
+}
 
 int run_hooks(const char *const *env, const char *hookname,
 	      const struct strvec *args, enum hookdir_opt run_hookdir)
diff --git a/hook.h b/hook.h
index 6eb1dc99c4..bf8ea3ee11 100644
--- a/hook.h
+++ b/hook.h
@@ -36,6 +36,15 @@ enum hookdir_opt
  * command line arguments.
  */
 enum hookdir_opt configured_hookdir_opt(void);
+
+/*
+ * 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'.
-- 
2.28.0.rc0.142.g3c755180ce-goog
Previous: Emily ShafferNext: Emily Shaffer
Message 4 of 12 in “propose config-based hooks (part I)”
  1. 0/8 propose config-based hooks (part I)Emily Shaffer, Oct 14, 2020
  2. 3/8 hook: add list commandEmily Shaffer, Oct 14, 2020
  3. 6/8 parse-options: parse into strvecEmily Shaffer, Oct 14, 2020
  4. 8/8 hook: replace find_hook() with hook_exists()Emily Shaffer, Oct 14, 2020
  5. 1/8 doc: propose hooks managed by the configEmily Shaffer, Oct 14, 2020
  6. Ævar Arnfjörð BjarmasonOct 15, 2020
  7. Junio C HamanoOct 16, 2020
  8. Emily ShafferOct 21, 2020
  9. 5/8 hook: implement hookcmd.<name>.skipEmily Shaffer, Oct 14, 2020
  10. 4/8 hook: include hookdir hook in listEmily Shaffer, Oct 14, 2020
  11. 2/8 hook: scaffolding for git-hook subcommandEmily Shaffer, Oct 14, 2020
  12. 7/8 hook: add 'run' subcommandEmily Shaffer, Oct 14, 2020

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.