Skip to content

Commit b5e57ab

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 506300a commit b5e57ab

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

hook.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,18 @@ const char *find_hook(struct repository *r, const char *name)
6868
int found_hook;
6969

7070
strbuf_reset(&path);
71-
if (have_git_dir())
71+
if (have_git_dir()) {
72+
static int forced_config;
73+
74+
if (!forced_config) {
75+
if (!git_hooks_path)
76+
git_config_get_pathname("core.hookspath",
77+
&git_hooks_path);
78+
forced_config = 1;
79+
}
80+
7281
strbuf_repo_git_path(&path, r, "hooks/%s", name);
73-
else if (!hook_path_early(name, &path))
82+
} else if (!hook_path_early(name, &path))
7483
return NULL;
7584

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