Skip to content

Commit 0b4a671

Browse files
dschovdye
authored andcommitted
pre-command: always respect core.hooksPath
We need to respect that config setting even if we already know that we have a repository, but have not yet read the config. The regression test was written by Alejandro Pauly. 2021-10-30: Recent movement of find_hook() into hook.c required moving this change from run-command.c. Signed-off-by: Johannes Schindelin <johasc@microsoft.com>
1 parent 281fa4f commit 0b4a671

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

hook.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,20 @@ const char *find_hook(const char *name)
5959
static struct strbuf path = STRBUF_INIT;
6060

6161
strbuf_reset(&path);
62-
if (have_git_dir())
62+
if (have_git_dir()) {
63+
static int forced_config;
64+
65+
if (!forced_config) {
66+
if (!git_hooks_path) {
67+
git_config_get_pathname("core.hookspath",
68+
&git_hooks_path);
69+
UNLEAK(git_hooks_path);
70+
}
71+
forced_config = 1;
72+
}
73+
6374
strbuf_git_path(&path, "hooks/%s", name);
64-
else if (!hook_path_early(name, &path))
75+
} else if (!hook_path_early(name, &path))
6576
return NULL;
6677

6778
if (access(path.buf, X_OK) < 0) {

t/t0400-pre-command-hook.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,15 @@ test_expect_success 'in a subdirectory, using an alias' '
5555
test_line_count = 2 sub/log
5656
'
5757

58+
test_expect_success 'with core.hooksPath' '
59+
mkdir -p .git/alternateHooks &&
60+
write_script .git/alternateHooks/pre-command <<-EOF &&
61+
echo "alternate" >\$(git rev-parse --git-dir)/pre-command.out
62+
EOF
63+
write_script .git/hooks/pre-command <<-EOF &&
64+
echo "original" >\$(git rev-parse --git-dir)/pre-command.out
65+
EOF
66+
git -c core.hooksPath=.git/alternateHooks status &&
67+
test "alternate" = "$(cat .git/pre-command.out)"
68+
'
5869
test_done

0 commit comments

Comments
 (0)