Skip to content

Commit

Permalink
pre-command: always respect core.hooksPath
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
dscho committed Aug 16, 2023
1 parent 044b044 commit 13c0f30
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
15 changes: 13 additions & 2 deletions hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,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) {
Expand Down
11 changes: 11 additions & 0 deletions t/t0400-pre-command-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 13c0f30

Please sign in to comment.