Skip to content

Commit 05f36ba

Browse files
10ne1gitster
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). The next commit will use this to reject hook friendly-names that collide with known event names. Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 3cd828e commit 05f36ba

4 files changed

Lines changed: 17 additions & 11 deletions

File tree

Makefile

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

26642664
help.sp help.s help.o: command-list.h
26652665
builtin/bugreport.sp builtin/bugreport.s builtin/bugreport.o: hook-list.h
2666-
builtin/hook.sp builtin/hook.s builtin/hook.o: hook-list.h
2666+
hook.sp hook.s hook.o: hook-list.h
26672667

26682668
builtin/help.sp builtin/help.s builtin/help.o: config-list.h GIT-PREFIX
26692669
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

0 commit comments

Comments
 (0)