Skip to content

Commit 5e3aba3

Browse files
avargitster
authored andcommitted
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 <[email protected]> Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f53df0b commit 5e3aba3

File tree

14 files changed

+59
-43
lines changed

14 files changed

+59
-43
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@ LIB_OBJS += hash-lookup.o
904904
LIB_OBJS += hashmap.o
905905
LIB_OBJS += help.o
906906
LIB_OBJS += hex.o
907+
LIB_OBJS += hook.o
907908
LIB_OBJS += ident.o
908909
LIB_OBJS += json-writer.o
909910
LIB_OBJS += kwset.o

builtin/am.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "parse-options.h"
1212
#include "dir.h"
1313
#include "run-command.h"
14+
#include "hook.h"
1415
#include "quote.h"
1516
#include "tempfile.h"
1617
#include "lockfile.h"

builtin/bugreport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "strbuf.h"
44
#include "help.h"
55
#include "compat/compiler.h"
6-
#include "run-command.h"
6+
#include "hook.h"
77

88

99
static void get_system_info(struct strbuf *sys_info)

builtin/commit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "revision.h"
2020
#include "wt-status.h"
2121
#include "run-command.h"
22+
#include "hook.h"
2223
#include "refs.h"
2324
#include "log-tree.h"
2425
#include "strbuf.h"

builtin/merge.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "builtin.h"
1414
#include "lockfile.h"
1515
#include "run-command.h"
16+
#include "hook.h"
1617
#include "diff.h"
1718
#include "diff-merges.h"
1819
#include "refs.h"

builtin/receive-pack.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "pkt-line.h"
88
#include "sideband.h"
99
#include "run-command.h"
10+
#include "hook.h"
1011
#include "exec-cmd.h"
1112
#include "commit.h"
1213
#include "object.h"

builtin/worktree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "branch.h"
99
#include "refs.h"
1010
#include "run-command.h"
11+
#include "hook.h"
1112
#include "sigchain.h"
1213
#include "submodule.h"
1314
#include "utf8.h"

hook.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "refs.h"
1111
#include "refs/refs-internal.h"
1212
#include "run-command.h"
13+
#include "hook.h"
1314
#include "object-store.h"
1415
#include "object.h"
1516
#include "tag.h"

run-command.c

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "quote.h"
1010
#include "config.h"
1111
#include "packfile.h"
12+
#include "hook.h"
1213

1314
void child_process_init(struct child_process *child)
1415
{
@@ -1322,40 +1323,6 @@ int async_with_fork(void)
13221323
#endif
13231324
}
13241325

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-
13591326
int run_hook_ve(const char *const *env, const char *name, va_list args)
13601327
{
13611328
struct child_process hook = CHILD_PROCESS_INIT;

run-command.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,6 @@ int finish_command_in_signal(struct child_process *);
224224
*/
225225
int run_command(struct child_process *);
226226

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-
234227
/**
235228
* Run a hook.
236229
* The first argument is a pathname to an index file, or NULL

sequencer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "sequencer.h"
99
#include "tag.h"
1010
#include "run-command.h"
11+
#include "hook.h"
1112
#include "exec-cmd.h"
1213
#include "utf8.h"
1314
#include "cache-tree.h"

transport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "cache.h"
22
#include "config.h"
33
#include "transport.h"
4-
#include "run-command.h"
4+
#include "hook.h"
55
#include "pkt-line.h"
66
#include "fetch-pack.h"
77
#include "remote.h"

0 commit comments

Comments
 (0)