Skip to content

Commit 517213c

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 <[email protected]>
1 parent 0c9bf08 commit 517213c

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
@@ -66,9 +66,20 @@ const char *find_hook(const char *name)
6666
int found_hook;
6767

6868
strbuf_reset(&path);
69-
if (have_git_dir())
69+
if (have_git_dir()) {
70+
static int forced_config;
71+
72+
if (!forced_config) {
73+
if (!git_hooks_path) {
74+
git_config_get_pathname("core.hookspath",
75+
&git_hooks_path);
76+
UNLEAK(git_hooks_path);
77+
}
78+
forced_config = 1;
79+
}
80+
7081
strbuf_git_path(&path, "hooks/%s", name);
71-
else if (!hook_path_early(name, &path))
82+
} else if (!hook_path_early(name, &path))
7283
return NULL;
7384

7485
found_hook = 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)