]> git.ipfire.org Git - thirdparty/git.git/commitdiff
hook: move is_known_hook() to hook.c for wider use
authorAdrian Ratiu <adrian.ratiu@collabora.com>
Fri, 10 Apr 2026 09:06:05 +0000 (12:06 +0300)
committerJunio C Hamano <gitster@pobox.com>
Fri, 10 Apr 2026 14:58:54 +0000 (07:58 -0700)
Move is_known_hook() from builtin/hook.c (static) into hook.c and
export it via hook.h so it can be reused.

Make it return bool and the iterator `h` for clarity (iterate hooks).

Both meson.build and the Makefile are updated to reflect that the
header is now used by libgit, not the builtin sources.

The next commit will use this to reject hook friendly-names that
collide with known event names.

Co-authored-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Makefile
builtin/hook.c
hook.c
hook.h
meson.build

index 5d22394c2ec1a628e4aa7ecbc8a6e5af72da750e..c4e83823e4a54766fddfc0e594df101fb231d186 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2675,7 +2675,7 @@ git$X: git.o GIT-LDFLAGS $(BUILTIN_OBJS) $(GITLIBS)
 
 help.sp help.s help.o: command-list.h
 builtin/bugreport.sp builtin/bugreport.s builtin/bugreport.o: hook-list.h
-builtin/hook.sp builtin/hook.s builtin/hook.o: hook-list.h
+hook.sp hook.s hook.o: hook-list.h
 
 builtin/help.sp builtin/help.s builtin/help.o: config-list.h GIT-PREFIX
 builtin/help.sp builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \
index bea0668b47593136ed517a81e786590d8f576fa7..1839412dca3edcffd9adaa957e3174899bd9e227 100644 (file)
@@ -4,7 +4,6 @@
 #include "environment.h"
 #include "gettext.h"
 #include "hook.h"
-#include "hook-list.h"
 #include "parse-options.h"
 
 #define BUILTIN_HOOK_RUN_USAGE \
 #define BUILTIN_HOOK_LIST_USAGE \
        N_("git hook list [--allow-unknown-hook-name] [-z] [--show-scope] <hook-name>")
 
-static int is_known_hook(const char *name)
-{
-       const char **p;
-       for (p = hook_name_list; *p; p++)
-               if (!strcmp(*p, name))
-                       return 1;
-       return 0;
-}
-
 static const char * const builtin_hook_usage[] = {
        BUILTIN_HOOK_RUN_USAGE,
        BUILTIN_HOOK_LIST_USAGE,
diff --git a/hook.c b/hook.c
index 0493993bbe6738f40709989eb790661694260d40..19076f8f2baba69617c66e5669aab64948b4802b 100644 (file)
--- a/hook.c
+++ b/hook.c
@@ -5,6 +5,7 @@
 #include "environment.h"
 #include "gettext.h"
 #include "hook.h"
+#include "hook-list.h"
 #include "parse.h"
 #include "path.h"
 #include "run-command.h"
 #include "strbuf.h"
 #include "strmap.h"
 
+bool is_known_hook(const char *name)
+{
+       const char **h;
+       for (h = hook_name_list; *h; h++)
+               if (!strcmp(*h, name))
+                       return true;
+       return false;
+}
+
 const char *find_hook(struct repository *r, const char *name)
 {
        static struct strbuf path = STRBUF_INIT;
diff --git a/hook.h b/hook.h
index 01db4226a60306583cdb9aba831748547ebc55c2..5a93f56618e1236881b0f0203c9bba9e1c9dc7ce 100644 (file)
--- a/hook.h
+++ b/hook.h
@@ -234,6 +234,12 @@ void hook_free(void *p, const char *str);
  */
 void hook_cache_clear(struct strmap *cache);
 
+/**
+ * Returns true if `name` is a recognized hook event name
+ * (e.g. "pre-commit", "post-receive").
+ */
+bool is_known_hook(const char *name);
+
 /**
  * Returns the path to the hook file, or NULL if the hook is missing
  * or disabled. Note that this points to static storage that will be
index 8309942d184847ea6434bae30e81dbbbf175192d..f438d5545dafb758c9eb4be8be44e8e997fa57db 100644 (file)
@@ -563,6 +563,18 @@ libgit_sources += custom_target(
   env: script_environment,
 )
 
+libgit_sources += custom_target(
+  input: 'Documentation/githooks.adoc',
+  output: 'hook-list.h',
+  command: [
+    shell,
+    meson.current_source_dir() + '/tools/generate-hooklist.sh',
+    meson.current_source_dir(),
+    '@OUTPUT@',
+  ],
+  env: script_environment,
+)
+
 builtin_sources = [
   'builtin/add.c',
   'builtin/am.c',
@@ -739,18 +751,6 @@ builtin_sources += custom_target(
   env: script_environment,
 )
 
-builtin_sources += custom_target(
-  input: 'Documentation/githooks.adoc',
-  output: 'hook-list.h',
-  command: [
-    shell,
-    meson.current_source_dir() + '/tools/generate-hooklist.sh',
-    meson.current_source_dir(),
-    '@OUTPUT@',
-  ],
-  env: script_environment,
-)
-
 # This contains the variables for GIT-BUILD-OPTIONS, which we use to propagate
 # build options to our tests.
 build_options_config = configuration_data()