Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion git/index/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ def hook_path(name: str, git_dir: PathLike) -> str:
return osp.join(git_dir, "hooks", name)


def _commit_hook_path(name: str, index: "IndexFile") -> str:
""":return: path to the named commit hook, respecting Git's core.hooksPath."""
hp = index.repo.git.rev_parse("--git-path", f"hooks/{name}")
if osp.isabs(hp):
return hp

base_dir = index.repo.working_dir or index.repo.git_dir
return osp.abspath(osp.join(base_dir, hp))


def _has_file_extension(path: str) -> str:
return osp.splitext(path)[1]

Expand All @@ -82,7 +92,7 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None:

:raise git.exc.HookExecutionError:
"""
hp = hook_path(name, index.repo.git_dir)
hp = _commit_hook_path(name, index)
if not os.access(hp, os.X_OK):
return

Expand Down
26 changes: 26 additions & 0 deletions test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,32 @@ def test_pre_commit_hook_success(self, rw_repo):
_make_hook(index.repo.git_dir, "pre-commit", "exit 0")
index.commit("This should not fail")

@pytest.mark.xfail(
type(_win_bash_status) is WinBashStatus.Absent,
reason="Can't run a hook on Windows without bash.exe.",
raises=HookExecutionError,
)
@pytest.mark.xfail(
type(_win_bash_status) is WinBashStatus.WslNoDistro,
reason="Currently uses the bash.exe of WSL, even with no WSL distro installed",
raises=HookExecutionError,
)
@with_rw_repo("HEAD", bare=True)
def test_pre_commit_hook_respects_core_hooks_path(self, rw_repo):
index = rw_repo.index
hooks_dir = Path(index.repo.git_dir, "custom-hooks")
hooks_dir.mkdir()
hp = hooks_dir / "pre-commit"
hp.write_text(HOOKS_SHEBANG + "echo 'ran custom hook' >custom-hook-output.txt", encoding="utf-8")
os.chmod(hp, 0o744)

with index.repo.config_writer() as writer:
writer.set_value("core", "hooksPath", "custom-hooks")

index.commit("This should run the custom hook")
output = Path(rw_repo.git_dir, "custom-hook-output.txt").read_text(encoding="utf-8")
self.assertEqual(output, "ran custom hook\n")

@pytest.mark.xfail(
type(_win_bash_status) is WinBashStatus.WslNoDistro,
reason="Currently uses the bash.exe of WSL, even with no WSL distro installed",
Expand Down
Loading