Skip to content

Commit 2eb541e

Browse files
10ne1pks-t
authored andcommitted
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>
1 parent 5e57b20 commit 2eb541e

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2675,7 +2675,7 @@ git$X: git.o GIT-LDFLAGS $(BUILTIN_OBJS) $(GITLIBS)
26752675

26762676
help.sp help.s help.o: command-list.h
26772677
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
26792679

26802680
builtin/help.sp builtin/help.s builtin/help.o: config-list.h GIT-PREFIX
26812681
builtin/help.sp builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \

builtin/hook.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "environment.h"
55
#include "gettext.h"
66
#include "hook.h"
7-
#include "hook-list.h"
87
#include "parse-options.h"
98

109
#define BUILTIN_HOOK_RUN_USAGE \
@@ -13,15 +12,6 @@
1312
#define BUILTIN_HOOK_LIST_USAGE \
1413
N_("git hook list [--allow-unknown-hook-name] [-z] [--show-scope] <hook-name>")
1514

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-
2515
static const char * const builtin_hook_usage[] = {
2616
BUILTIN_HOOK_RUN_USAGE,
2717
BUILTIN_HOOK_LIST_USAGE,

hook.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@
55
#include "environment.h"
66
#include "gettext.h"
77
#include "hook.h"
8+
#include "hook-list.h"
89
#include "parse.h"
910
#include "path.h"
1011
#include "run-command.h"
1112
#include "setup.h"
1213
#include "strbuf.h"
1314
#include "strmap.h"
1415

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+
1525
const char *find_hook(struct repository *r, const char *name)
1626
{
1727
static struct strbuf path = STRBUF_INIT;

hook.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ void hook_free(void *p, const char *str);
234234
*/
235235
void hook_cache_clear(struct strmap *cache);
236236

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+
237243
/**
238244
* Returns the path to the hook file, or NULL if the hook is missing
239245
* or disabled. Note that this points to static storage that will be

meson.build

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,18 @@ libgit_sources += custom_target(
563563
env: script_environment,
564564
)
565565

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+
566578
builtin_sources = [
567579
'builtin/add.c',
568580
'builtin/am.c',
@@ -739,18 +751,6 @@ builtin_sources += custom_target(
739751
env: script_environment,
740752
)
741753

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-
754754
# This contains the variables for GIT-BUILD-OPTIONS, which we use to propagate
755755
# build options to our tests.
756756
build_options_config = configuration_data()

0 commit comments

Comments
 (0)