Skip to content

Commit 52c0241

Browse files
committed
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 03e838b commit 52c0241

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
@@ -61,9 +61,20 @@ const char *find_hook(const char *name)
6161
static struct strbuf path = STRBUF_INIT;
6262

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

6980
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)