|  | 
| 2 | 2 | 
 | 
| 3 | 3 | import os | 
| 4 | 4 | import re | 
|  | 5 | +import shutil | 
|  | 6 | +import sys | 
| 5 | 7 | import tempfile | 
| 6 | 8 | from io import open | 
| 7 | 9 | 
 | 
| @@ -90,16 +92,43 @@ def get_hook(repository, hook_name, extra_env=None): | 
| 90 | 92 |     extra_env = add_dict(extra_env, {'GIT_PREFIX': prefix}) | 
| 91 | 93 | 
 | 
| 92 | 94 |     def hook(*parameters): | 
| 93 |  | -        argv = [hook_path] | 
| 94 |  | -        argv.extend(parameters) | 
|  | 95 | +        if sys.platform == 'win32': | 
|  | 96 | +            # On Windows, run the hook using "bash" explicitly. | 
|  | 97 | +            # Try to locate bash.exe in user's PATH, but avoid the WSL | 
|  | 98 | +            # shim/bootstrapper %SYSTEMROOT%/System32/bash.exe | 
|  | 99 | +            systemroot = os.environ.get('SYSTEMROOT') | 
|  | 100 | +            if systemroot: | 
|  | 101 | +                system32 = os.path.normcase(os.path.join(systemroot, 'system32')) | 
|  | 102 | +                path = os.pathsep.join( | 
|  | 103 | +                    p | 
|  | 104 | +                    for p in os.environ.get('PATH', '').split(os.pathsep) | 
|  | 105 | +                    if os.path.normcase(p) != system32 | 
|  | 106 | +                ) | 
|  | 107 | +            else: | 
|  | 108 | +                path = None | 
|  | 109 | + | 
|  | 110 | +            # Find bash with user's path (sans System32). | 
|  | 111 | +            bash_exe = shutil.which('bash.exe', path=path) | 
|  | 112 | +            if not bash_exe: | 
|  | 113 | +                # Next try finding the bash.exe that came with Git for Windows. | 
|  | 114 | +                git_exe = shutil.which('git.exe', path=path) | 
|  | 115 | +                if not git_exe: | 
|  | 116 | +                    raise StgException('Failed to locate either bash.exe or git.exe') | 
|  | 117 | +                bash_exe = os.path.join( | 
|  | 118 | +                    os.path.dirname(os.path.dirname(git_exe)), | 
|  | 119 | +                    'bin', | 
|  | 120 | +                    'bash.exe', | 
|  | 121 | +                ) | 
|  | 122 | + | 
|  | 123 | +            argv = [bash_exe, hook_path] | 
|  | 124 | +        else: | 
|  | 125 | +            argv = [hook_path] | 
| 95 | 126 | 
 | 
| 96 |  | -        # On Windows, run the hook using "bash" explicitly | 
| 97 |  | -        if os.name != 'posix': | 
| 98 |  | -            argv.insert(0, 'bash') | 
|  | 127 | +        argv.extend(parameters) | 
| 99 | 128 | 
 | 
| 100 | 129 |         repository.default_iw.run(argv, extra_env).run() | 
| 101 | 130 | 
 | 
| 102 |  | -    hook.__name__ = str(hook_name) | 
|  | 131 | +    hook.__name__ = hook_name | 
| 103 | 132 |     return hook | 
| 104 | 133 | 
 | 
| 105 | 134 | 
 | 
|  | 
0 commit comments