hook.[ch]: move find_hook() from run-command.c to hook.c

Move the find_hook() function from run-command.c to a new hook.c library. This change establishes a stub library that's pretty pointless right now, but will see much wider use with Emily Shaffer's upcoming "configuration-based hooks" series. Eventually all the hook related code will live in hook.[ch]. Let's start that process by moving the simple find_hook() function over as-is. Signed-off-by: Emily Shaffer <emilyshaffer@google.com> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Sep 26, 2021 at 21:03 UTC 5e3aba33da26803e48b0099c9dabfd327f7f8b8b
14 files changed +59 -43
Makefile
+1
@@ -904,6 +904,7 @@ LIB_OBJS += hash-lookup.o
904 LIB_OBJS += hashmap.o
905 LIB_OBJS += help.o
906 LIB_OBJS += hex.o
907 +LIB_OBJS += hook.o
908 LIB_OBJS += ident.o
909 LIB_OBJS += json-writer.o
910 LIB_OBJS += kwset.o
builtin/am.c
+1
@@ -11,6 +11,7 @@
11 #include "parse-options.h"
12 #include "dir.h"
13 #include "run-command.h"
14 +#include "hook.h"
15 #include "quote.h"
16 #include "tempfile.h"
17 #include "lockfile.h"
builtin/bugreport.c
+1 -1
@@ -3,7 +3,7 @@
3 #include "strbuf.h"
4 #include "help.h"
5 #include "compat/compiler.h"
6 -#include "run-command.h"
6 +#include "hook.h"
7
8
9 static void get_system_info(struct strbuf *sys_info)
builtin/commit.c
+1
@@ -19,6 +19,7 @@
19 #include "revision.h"
20 #include "wt-status.h"
21 #include "run-command.h"
22 +#include "hook.h"
23 #include "refs.h"
24 #include "log-tree.h"
25 #include "strbuf.h"
builtin/merge.c
+1
@@ -13,6 +13,7 @@
13 #include "builtin.h"
14 #include "lockfile.h"
15 #include "run-command.h"
16 +#include "hook.h"
17 #include "diff.h"
18 #include "diff-merges.h"
19 #include "refs.h"
builtin/receive-pack.c
+1
@@ -7,6 +7,7 @@
7 #include "pkt-line.h"
8 #include "sideband.h"
9 #include "run-command.h"
10 +#include "hook.h"
11 #include "exec-cmd.h"
12 #include "commit.h"
13 #include "object.h"
builtin/worktree.c
+1
@@ -8,6 +8,7 @@
8 #include "branch.h"
9 #include "refs.h"
10 #include "run-command.h"
11 +#include "hook.h"
12 #include "sigchain.h"
13 #include "submodule.h"
14 #include "utf8.h"
hook.c new
+37
@@ -0,0 +1,37 @@
1 +#include "cache.h"
2 +#include "hook.h"
3 +#include "run-command.h"
4 +
5 +const char *find_hook(const char *name)
6 +{
7 + static struct strbuf path = STRBUF_INIT;
8 +
9 + strbuf_reset(&path);
10 + strbuf_git_path(&path, "hooks/%s", name);
11 + if (access(path.buf, X_OK) < 0) {
12 + int err = errno;
13 +
14 +#ifdef STRIP_EXTENSION
15 + strbuf_addstr(&path, STRIP_EXTENSION);
16 + if (access(path.buf, X_OK) >= 0)
17 + return path.buf;
18 + if (errno == EACCES)
19 + err = errno;
20 +#endif
21 +
22 + if (err == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) {
23 + static struct string_list advise_given = STRING_LIST_INIT_DUP;
24 +
25 + if (!string_list_lookup(&advise_given, name)) {
26 + string_list_insert(&advise_given, name);
27 + advise(_("The '%s' hook was ignored because "
28 + "it's not set as executable.\n"
29 + "You can disable this warning with "
30 + "`git config advice.ignoredHook false`."),
31 + path.buf);
32 + }
33 + }
34 + return NULL;
35 + }
36 + return path.buf;
37 +}
hook.h new
+11
@@ -0,0 +1,11 @@
1 +#ifndef HOOK_H
2 +#define HOOK_H
3 +
4 +/*
5 + * Returns the path to the hook file, or NULL if the hook is missing
6 + * or disabled. Note that this points to static storage that will be
7 + * overwritten by further calls to find_hook and run_hook_*.
8 + */
9 +const char *find_hook(const char *name);
10 +
11 +#endif
refs.c
+1
@@ -10,6 +10,7 @@
10 #include "refs.h"
11 #include "refs/refs-internal.h"
12 #include "run-command.h"
13 +#include "hook.h"
14 #include "object-store.h"
15 #include "object.h"
16 #include "tag.h"
run-command.c
+1 -34
@@ -9,6 +9,7 @@
9 #include "quote.h"
10 #include "config.h"
11 #include "packfile.h"
12 +#include "hook.h"
13
14 void child_process_init(struct child_process *child)
15 {
@@ -1322,40 +1323,6 @@ int async_with_fork(void)
1323 #endif
1324 }
1325
1325 -const char *find_hook(const char *name)
1326 -{
1327 - static struct strbuf path = STRBUF_INIT;
1328 -
1329 - strbuf_reset(&path);
1330 - strbuf_git_path(&path, "hooks/%s", name);
1331 - if (access(path.buf, X_OK) < 0) {
1332 - int err = errno;
1333 -
1334 -#ifdef STRIP_EXTENSION
1335 - strbuf_addstr(&path, STRIP_EXTENSION);
1336 - if (access(path.buf, X_OK) >= 0)
1337 - return path.buf;
1338 - if (errno == EACCES)
1339 - err = errno;
1340 -#endif
1341 -
1342 - if (err == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) {
1343 - static struct string_list advise_given = STRING_LIST_INIT_DUP;
1344 -
1345 - if (!string_list_lookup(&advise_given, name)) {
1346 - string_list_insert(&advise_given, name);
1347 - advise(_("The '%s' hook was ignored because "
1348 - "it's not set as executable.\n"
1349 - "You can disable this warning with "
1350 - "`git config advice.ignoredHook false`."),
1351 - path.buf);
1352 - }
1353 - }
1354 - return NULL;
1355 - }
1356 - return path.buf;
1357 -}
1358 -
1326 int run_hook_ve(const char *const *env, const char *name, va_list args)
1327 {
1328 struct child_process hook = CHILD_PROCESS_INIT;
run-command.h
-7
@@ -224,13 +224,6 @@ int finish_command_in_signal(struct child_process *);
224 */
225 int run_command(struct child_process *);
226
227 -/*
228 - * Returns the path to the hook file, or NULL if the hook is missing
229 - * or disabled. Note that this points to static storage that will be
230 - * overwritten by further calls to find_hook and run_hook_*.
231 - */
232 -const char *find_hook(const char *name);
233 -
227 /**
228 * Run a hook.
229 * The first argument is a pathname to an index file, or NULL
sequencer.c
+1
@@ -8,6 +8,7 @@
8 #include "sequencer.h"
9 #include "tag.h"
10 #include "run-command.h"
11 +#include "hook.h"
12 #include "exec-cmd.h"
13 #include "utf8.h"
14 #include "cache-tree.h"
transport.c
+1 -1
@@ -1,7 +1,7 @@
1 #include "cache.h"
2 #include "config.h"
3 #include "transport.h"
4 -#include "run-command.h"
4 +#include "hook.h"
5 #include "pkt-line.h"
6 #include "fetch-pack.h"
7 #include "remote.h"