| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5bc2560 commit 406b98e
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -64,6 +64,16 @@ def hook_path(name: str, git_dir: PathLike) -> str: | |||
| 64 | 64 | return osp.join(git_dir, "hooks", name) | |
| 65 | 65 | ||
| 66 | 66 | ||
| 67 | + def _commit_hook_path(name: str, index: "IndexFile") -> str: | ||
| 68 | + """:return: path to the named commit hook, respecting Git's core.hooksPath.""" | ||
| 69 | + hp = index.repo.git.rev_parse("--git-path", f"hooks/{name}") | ||
| 70 | + if osp.isabs(hp): | ||
| 71 | + return hp | ||
| 72 | + | ||
| 73 | + base_dir = index.repo.working_dir or index.repo.git_dir | ||
| 74 | + return osp.abspath(osp.join(base_dir, hp)) | ||
| 75 | + | ||
| 76 | + | ||
| 67 | 77 | def _has_file_extension(path: str) -> str: | |
| 68 | 78 | return osp.splitext(path)[1] | |
| 69 | 79 | ||
@@ -82,7 +92,7 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None: | |||
| 82 | 92 | ||
| 83 | 93 | :raise git.exc.HookExecutionError: | |
| 84 | 94 | """ | |
| 85 | - hp = hook_path(name, index.repo.git_dir) | ||
| 95 | + hp = _commit_hook_path(name, index) | ||
| 86 | 96 | if not os.access(hp, os.X_OK): | |
| 87 | 97 | return | |
| 88 | 98 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1160,6 +1160,32 @@ def test_pre_commit_hook_success(self, rw_repo): | |||
| 1160 | 1160 | _make_hook(index.repo.git_dir, "pre-commit", "exit 0") | |
| 1161 | 1161 | index.commit("This should not fail") | |
| 1162 | 1162 | ||
| 1163 | + @pytest.mark.xfail( | ||
| 1164 | + type(_win_bash_status) is WinBashStatus.Absent, | ||
| 1165 | + reason="Can't run a hook on Windows without bash.exe.", | ||
| 1166 | + raises=HookExecutionError, | ||
| 1167 | + ) | ||
| 1168 | + @pytest.mark.xfail( | ||
| 1169 | + type(_win_bash_status) is WinBashStatus.WslNoDistro, | ||
| 1170 | + reason="Currently uses the bash.exe of WSL, even with no WSL distro installed", | ||
| 1171 | + raises=HookExecutionError, | ||
| 1172 | + ) | ||
| 1173 | + @with_rw_repo("HEAD", bare=True) | ||
| 1174 | + def test_pre_commit_hook_respects_core_hooks_path(self, rw_repo): | ||
| 1175 | + index = rw_repo.index | ||
| 1176 | + hooks_dir = Path(index.repo.git_dir, "custom-hooks") | ||
| 1177 | + hooks_dir.mkdir() | ||
| 1178 | + hp = hooks_dir / "pre-commit" | ||
| 1179 | + hp.write_text(HOOKS_SHEBANG + "echo 'ran custom hook' >custom-hook-output.txt", encoding="utf-8") | ||
| 1180 | + os.chmod(hp, 0o744) | ||
| 1181 | + | ||
| 1182 | + with index.repo.config_writer() as writer: | ||
| 1183 | + writer.set_value("core", "hooksPath", "custom-hooks") | ||
| 1184 | + | ||
| 1185 | + index.commit("This should run the custom hook") | ||
| 1186 | + output = Path(rw_repo.git_dir, "custom-hook-output.txt").read_text(encoding="utf-8") | ||
| 1187 | + self.assertEqual(output, "ran custom hook\n") | ||
| 1188 | + | ||
| 1163 | 1189 | @pytest.mark.xfail( | |
| 1164 | 1190 | type(_win_bash_status) is WinBashStatus.WslNoDistro, | |
| 1165 | 1191 | reason="Currently uses the bash.exe of WSL, even with no WSL distro installed", | |
| Back | FazBrowse Home | New Git URL |
0 commit comments