hook: move is_known_hook() to hook.c for wider use
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>
Adrian Ratiu committed
Apr 10, 2026 at 12:06 UTC
2eb541e8f2a9b0dd923279421c741d0a0c00420d
5 files changed
+29
-23
Makefile
+1
-1
@@ -2675,7 +2675,7 @@ git$X: git.o GIT-LDFLAGS $(BUILTIN_OBJS) $(GITLIBS)
2675
2676
help.sp help.s help.o: command-list.h
2677
builtin/bugreport.sp builtin/bugreport.s builtin/bugreport.o: hook-list.h
2678
-builtin/hook.sp builtin/hook.s builtin/hook.o: hook-list.h
2678
+hook.sp hook.s hook.o: hook-list.h
2679
2680
builtin/help.sp builtin/help.s builtin/help.o: config-list.h GIT-PREFIX
2681
builtin/help.sp builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \
builtin/hook.c
-10
@@ -4,7 +4,6 @@
4
#include "environment.h"
5
#include "gettext.h"
6
#include "hook.h"
7
-#include "hook-list.h"
7
#include "parse-options.h"
8
9
#define BUILTIN_HOOK_RUN_USAGE \
@@ -13,15 +12,6 @@
12
#define BUILTIN_HOOK_LIST_USAGE \
13
N_("git hook list [--allow-unknown-hook-name] [-z] [--show-scope] <hook-name>")
14
16
-static int is_known_hook(const char *name)
17
-{
18
- const char **p;
19
- for (p = hook_name_list; *p; p++)
20
- if (!strcmp(*p, name))
21
- return 1;
22
- return 0;
23
-}
24
-
15
static const char * const builtin_hook_usage[] = {
16
BUILTIN_HOOK_RUN_USAGE,
17
BUILTIN_HOOK_LIST_USAGE,
hook.c
+10
@@ -5,6 +5,7 @@
5
#include "environment.h"
6
#include "gettext.h"
7
#include "hook.h"
8
+#include "hook-list.h"
9
#include "parse.h"
10
#include "path.h"
11
#include "run-command.h"
@@ -12,6 +13,15 @@
13
#include "strbuf.h"
14
#include "strmap.h"
15
16
+bool is_known_hook(const char *name)
17
+{
18
+ const char **h;
19
+ for (h = hook_name_list; *h; h++)
20
+ if (!strcmp(*h, name))
21
+ return true;
22
+ return false;
23
+}
24
+
25
const char *find_hook(struct repository *r, const char *name)
26
{
27
static struct strbuf path = STRBUF_INIT;
hook.h
+6
@@ -234,6 +234,12 @@ void hook_free(void *p, const char *str);
234
*/
235
void hook_cache_clear(struct strmap *cache);
236
237
+/**
238
+ * Returns true if `name` is a recognized hook event name
239
+ * (e.g. "pre-commit", "post-receive").
240
+ */
241
+bool is_known_hook(const char *name);
242
+
243
/**
244
* Returns the path to the hook file, or NULL if the hook is missing
245
* or disabled. Note that this points to static storage that will be
meson.build
+12
-12
@@ -563,6 +563,18 @@ libgit_sources += custom_target(
563
env: script_environment,
564
)
565
566
+libgit_sources += custom_target(
567
+ input: 'Documentation/githooks.adoc',
568
+ output: 'hook-list.h',
569
+ command: [
570
+ shell,
571
+ meson.current_source_dir() + '/tools/generate-hooklist.sh',
572
+ meson.current_source_dir(),
573
+ '@OUTPUT@',
574
+ ],
575
+ env: script_environment,
576
+)
577
+
578
builtin_sources = [
579
'builtin/add.c',
580
'builtin/am.c',
@@ -739,18 +751,6 @@ builtin_sources += custom_target(
751
env: script_environment,
752
)
753
742
-builtin_sources += custom_target(
743
- input: 'Documentation/githooks.adoc',
744
- output: 'hook-list.h',
745
- command: [
746
- shell,
747
- meson.current_source_dir() + '/tools/generate-hooklist.sh',
748
- meson.current_source_dir(),
749
- '@OUTPUT@',
750
- ],
751
- env: script_environment,
752
-)
753
-
754
# This contains the variables for GIT-BUILD-OPTIONS, which we use to propagate
755
# build options to our tests.
756
build_options_config = configuration_data()