| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 04960cf commit 7131bcc
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -79,6 +79,21 @@ def _has_file_extension(path: str) -> str: | |||
| 79 | 79 | return osp.splitext(path)[1] | |
| 80 | 80 | ||
| 81 | 81 | ||
| 82 | + def _which_from_path(command: str) -> Union[str, None]: | ||
| 83 | + """Resolve an executable 'command' from PATH without considering the current directory.""" | ||
| 84 | + cwd = osp.normcase(osp.abspath(os.curdir)) | ||
| 85 | + for directory in os.get_exec_path(): | ||
| 86 | + if not directory: | ||
| 87 | + continue | ||
| 88 | + directory = osp.abspath(directory) | ||
| 89 | + if osp.normcase(directory) == cwd: | ||
| 90 | + continue | ||
| 91 | + candidate = osp.join(directory, command) | ||
| 92 | + if osp.isfile(candidate) and os.access(candidate, os.X_OK): | ||
| 93 | + return candidate | ||
| 94 | + return None | ||
| 95 | + | ||
| 96 | + | ||
| 82 | 97 | def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None: | |
| 83 | 98 | """Run the commit hook of the given name. Silently ignore hooks that do not exist. | |
| 84 | 99 | ||
@@ -112,7 +127,11 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None: | |||
| 112 | 127 | # an absolute path in this form, although a relative path is preferable | |
| 113 | 128 | # because it also works with the Windows Subsystem for Linux wrapper. | |
| 114 | 129 | bash_hp = hp | |
| 115 | - cmd = ["bash.exe", Path(bash_hp).as_posix()] | ||
| 130 | + # Resolve through PATH before spawning. On Windows, CreateProcess searches | ||
| 131 | + # the current and system directories before PATH for a bare executable name, | ||
| 132 | + # which can otherwise select an impostor or the WSL launcher instead of Git | ||
| 133 | + # for Windows' Bash. | ||
| 134 | + cmd = [_which_from_path("bash.exe") or "bash.exe", Path(bash_hp).as_posix()] | ||
| 116 | 135 | ||
| 117 | 136 | process = safer_popen( | |
| 118 | 137 | cmd + list(args), | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1128,16 +1128,20 @@ def test_run_commit_hook_outside_worktree_on_windows(self, rw_dir): | |||
| 1128 | 1128 | repo = Repo.init(root / "repo") | |
| 1129 | 1129 | hooks_dir = root / "hooks" | |
| 1130 | 1130 | _make_hook(root, "fake-hook", "exit 0") | |
| 1131 | + bash = root / "git" / "bin" / "bash.exe" | ||
| 1131 | 1132 | with repo.config_writer() as writer: | |
| 1132 | 1133 | writer.set_value("core", "hooksPath", str(hooks_dir)) | |
| 1133 | 1134 | ||
| 1134 | - with mock.patch("git.index.fun.sys.platform", "win32"): | ||
| 1135 | + with mock.patch("git.index.fun.sys.platform", "win32"), mock.patch( | ||
| 1136 | + "git.index.fun._which_from_path", return_value=str(bash) | ||
| 1137 | + ) as which: | ||
| 1135 | 1138 | with mock.patch("git.index.fun.safer_popen") as popen, mock.patch("git.index.fun.handle_process_output"): | |
| 1136 | 1139 | popen.return_value.returncode = 0 | |
| 1137 | 1140 | run_commit_hook("fake-hook", repo.index) | |
| 1138 | 1141 | ||
| 1142 | + which.assert_called_once_with("bash.exe") | ||
| 1139 | 1143 | command = popen.call_args[0][0] | |
| 1140 | - self.assertEqual(command, ["bash.exe", "../hooks/fake-hook"]) | ||
| 1144 | + self.assertEqual(command, [str(bash), "../hooks/fake-hook"]) | ||
| 1141 | 1145 | ||
| 1142 | 1146 | @ddt.data((False,), (True,)) | |
| 1143 | 1147 | @with_rw_directory | |
| Back | FazBrowse Home | New Git URL |
0 commit comments