| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fac6037 commit b719e18
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ | |||
| 3 | 3 | # NOTE: Autodoc hates it if this is a docstring | |
| 4 | 4 | ||
| 5 | 5 | from io import BytesIO | |
| 6 | + from pathlib import Path | ||
| 6 | 7 | import os | |
| 7 | 8 | from stat import ( | |
| 8 | 9 | S_IFDIR, | |
@@ -21,6 +22,7 @@ | |||
| 21 | 22 | force_text, | |
| 22 | 23 | force_bytes, | |
| 23 | 24 | is_posix, | |
| 25 | + is_win, | ||
| 24 | 26 | safe_decode, | |
| 25 | 27 | ) | |
| 26 | 28 | from git.exc import ( | |
@@ -76,6 +78,10 @@ def hook_path(name: str, git_dir: PathLike) -> str: | |||
| 76 | 78 | return osp.join(git_dir, 'hooks', name) | |
| 77 | 79 | ||
| 78 | 80 | ||
| 81 | + def _has_file_extension(path): | ||
| 82 | + return osp.splitext(path)[1] | ||
| 83 | + | ||
| 84 | + | ||
| 79 | 85 | def run_commit_hook(name: str, index: 'IndexFile', *args: str) -> None: | |
| 80 | 86 | """Run the commit hook of the given name. Silently ignores hooks that do not exist. | |
| 81 | 87 | :param name: name of hook, like 'pre-commit' | |
@@ -89,8 +95,15 @@ def run_commit_hook(name: str, index: 'IndexFile', *args: str) -> None: | |||
| 89 | 95 | env = os.environ.copy() | |
| 90 | 96 | env['GIT_INDEX_FILE'] = safe_decode(str(index.path)) | |
| 91 | 97 | env['GIT_EDITOR'] = ':' | |
| 98 | + cmd = [hp] | ||
| 92 | 99 | try: | |
| 93 | - cmd = subprocess.Popen([hp] + list(args), | ||
| 100 | + if is_win and not _has_file_extension(hp): | ||
| 101 | + # Windows only uses extensions to determine how to open files | ||
| 102 | + # (doesn't understand shebangs). Try using bash to run the hook. | ||
| 103 | + relative_hp = Path(hp).relative_to(index.repo.working_dir).as_posix() | ||
| 104 | + cmd = ["bash.exe", relative_hp] | ||
| 105 | + | ||
| 106 | + cmd = subprocess.Popen(cmd + list(args), | ||
| 94 | 107 | env=env, | |
| 95 | 108 | stdout=subprocess.PIPE, | |
| 96 | 109 | stderr=subprocess.PIPE, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,6 +13,7 @@ | |||
| 13 | 13 | ) | |
| 14 | 14 | import tempfile | |
| 15 | 15 | from unittest import skipIf | |
| 16 | + import shutil | ||
| 16 | 17 | ||
| 17 | 18 | from git import ( | |
| 18 | 19 | IndexFile, | |
@@ -52,8 +53,9 @@ | |||
| 52 | 53 | ||
| 53 | 54 | HOOKS_SHEBANG = "#!/usr/bin/env sh\n" | |
| 54 | 55 | ||
| 56 | + is_win_without_bash = is_win and not shutil.which('bash.exe') | ||
| 57 | + | ||
| 55 | 58 | ||
| 56 | - @skipIf(HIDE_WINDOWS_KNOWN_ERRORS, "TODO: fix hooks execution on Windows: #703") | ||
| 57 | 59 | def _make_hook(git_dir, name, content, make_exec=True): | |
| 58 | 60 | """A helper to create a hook""" | |
| 59 | 61 | hp = hook_path(name, git_dir) | |
@@ -881,7 +883,7 @@ def test_pre_commit_hook_fail(self, rw_repo): | |||
| 881 | 883 | try: | |
| 882 | 884 | index.commit("This should fail") | |
| 883 | 885 | except HookExecutionError as err: | |
| 884 | - if is_win: | ||
| 886 | + if is_win_without_bash: | ||
| 885 | 887 | self.assertIsInstance(err.status, OSError) | |
| 886 | 888 | self.assertEqual(err.command, [hp]) | |
| 887 | 889 | self.assertEqual(err.stdout, '') | |
@@ -896,6 +898,7 @@ def test_pre_commit_hook_fail(self, rw_repo): | |||
| 896 | 898 | else: | |
| 897 | 899 | raise AssertionError("Should have caught a HookExecutionError") | |
| 898 | 900 | ||
| 901 | + @skipIf(HIDE_WINDOWS_KNOWN_ERRORS, "TODO: fix hooks execution on Windows: #703") | ||
| 899 | 902 | @with_rw_repo('HEAD', bare=True) | |
| 900 | 903 | def test_commit_msg_hook_success(self, rw_repo): | |
| 901 | 904 | commit_message = "commit default head by Frèderic Çaufl€" | |
@@ -920,7 +923,7 @@ def test_commit_msg_hook_fail(self, rw_repo): | |||
| 920 | 923 | try: | |
| 921 | 924 | index.commit("This should fail") | |
| 922 | 925 | except HookExecutionError as err: | |
| 923 | - if is_win: | ||
| 926 | + if is_win_without_bash: | ||
| 924 | 927 | self.assertIsInstance(err.status, OSError) | |
| 925 | 928 | self.assertEqual(err.command, [hp]) | |
| 926 | 929 | self.assertEqual(err.stdout, '') | |
| Back | FazBrowse Home | New Git URL |
0 commit comments