From 02cbbfa6bbfd3b79ee7edea7734507f21b3c18ac Mon Sep 17 00:00:00 2001 From: ncttjz Date: Wed, 15 Jul 2026 23:44:22 +0700 Subject: [PATCH] fix: respect core.hooksPath git config in commit hooks --- git/index/fun.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/git/index/fun.py b/git/index/fun.py index 629c19b1e..e8d7c8b08 100644 --- a/git/index/fun.py +++ b/git/index/fun.py @@ -81,8 +81,21 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None: Arguments passed to hook file. :raise git.exc.HookExecutionError: + + :note: + Respects the ``core.hooksPath`` git configuration option. When set, hooks are + resolved relative to that path instead of the default ``.git/hooks`` directory. """ - hp = hook_path(name, index.repo.git_dir) + hooks_path = index.repo.config_reader().get( + "core", "hooksPath", fallback=None + ) + if hooks_path is not None: + hp = osp.join(hooks_path, name) + if not osp.isabs(hooks_path): + # Relative hooksPath is resolved against the working tree root. + hp = osp.join(index.repo.working_dir, hp) + else: + hp = hook_path(name, index.repo.git_dir) if not os.access(hp, os.X_OK): return