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"

0 commit comments

Comments
 (0)