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>
This commit is contained in:
Johannes Schindelin 2017-08-08 00:27:50 +02:00 коммит произвёл Victoria Dye
Родитель 50188158b8
Коммит de68b1159c
2 изменённых файлов: 24 добавлений и 2 удалений

15
hook.c
Просмотреть файл

@ -59,9 +59,20 @@ const char *find_hook(const char *name)
static struct strbuf path = STRBUF_INIT;
strbuf_reset(&path);
if (have_git_dir())
if (have_git_dir()) {
static int forced_config;
if (!forced_config) {
if (!git_hooks_path) {
git_config_get_pathname("core.hookspath",
&git_hooks_path);
UNLEAK(git_hooks_path);
}
forced_config = 1;
}
strbuf_git_path(&path, "hooks/%s", name);
else if (!hook_path_early(name, &path))
} else if (!hook_path_early(name, &path))
return NULL;
if (access(path.buf, X_OK) < 0) {

Просмотреть файл

@ -55,4 +55,15 @@ test_expect_success 'in a subdirectory, using an alias' '
test_line_count = 2 sub/log
'
test_expect_success 'with core.hooksPath' '
mkdir -p .git/alternateHooks &&
write_script .git/alternateHooks/pre-command <<-EOF &&
echo "alternate" >\$(git rev-parse --git-dir)/pre-command.out
EOF
write_script .git/hooks/pre-command <<-EOF &&
echo "original" >\$(git rev-parse --git-dir)/pre-command.out
EOF
git -c core.hooksPath=.git/alternateHooks status &&
test "alternate" = "$(cat .git/pre-command.out)"
'
test_done