| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent bcf9f1c commit 2a21f09
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1 +1 @@ | |||
| 1 | - 2.1.14 | ||
| 1 | + 2.1.15 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,11 @@ | |||
| 2 | 2 | Changelog | |
| 3 | 3 | ========= | |
| 4 | 4 | ||
| 5 | + 2.1.15 - Bugfixes | ||
| 6 | + ================= | ||
| 7 | + | ||
| 8 | + Fix requirements.txt formatting and version lock gitdb2 to <3 for Python 2 compatibility. | ||
| 9 | + | ||
| 5 | 10 | 2.1.14 - Bugfixes | |
| 6 | 11 | ================= | |
| 7 | 12 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -124,6 +124,59 @@ def pump_stream(cmdline, name, stream, is_decode, handler): | |||
| 124 | 124 | return finalizer(process) | |
| 125 | 125 | ||
| 126 | 126 | ||
| 127 | + def _safer_popen_windows(command, shell, env=None, **kwargs): | ||
| 128 | + """Call :class:`subprocess.Popen` on Windows but don't include a CWD in the search. | ||
| 129 | + This avoids an untrusted search path condition where a file like ``git.exe`` in a | ||
| 130 | + malicious repository would be run when GitPython operates on the repository. The | ||
| 131 | + process using GitPython may have an untrusted repository's working tree as its | ||
| 132 | + current working directory. Some operations may temporarily change to that directory | ||
| 133 | + before running a subprocess. In addition, while by default GitPython does not run | ||
| 134 | + external commands with a shell, it can be made to do so, in which case the CWD of | ||
| 135 | + the subprocess, which GitPython usually sets to a repository working tree, can | ||
| 136 | + itself be searched automatically by the shell. This wrapper covers all those cases. | ||
| 137 | + :note: This currently works by setting the ``NoDefaultCurrentDirectoryInExePath`` | ||
| 138 | + environment variable during subprocess creation. It also takes care of passing | ||
| 139 | + Windows-specific process creation flags, but that is unrelated to path search. | ||
| 140 | + :note: The current implementation contains a race condition on :attr:`os.environ`. | ||
| 141 | + GitPython isn't thread-safe, but a program using it on one thread should ideally | ||
| 142 | + be able to mutate :attr:`os.environ` on another, without unpredictable results. | ||
| 143 | + See comments in https://github.com/gitpython-developers/GitPython/pull/1650. | ||
| 144 | + """ | ||
| 145 | + # CREATE_NEW_PROCESS_GROUP is needed for some ways of killing it afterwards. See: | ||
| 146 | + # https://docs.python.org/3/library/subprocess.html#subprocess.Popen.send_signal | ||
| 147 | + # https://docs.python.org/3/library/subprocess.html#subprocess.CREATE_NEW_PROCESS_GROUP | ||
| 148 | + creationflags = subprocess.CREATE_NO_WINDOW | subprocess.CREATE_NEW_PROCESS_GROUP | ||
| 149 | + | ||
| 150 | + # When using a shell, the shell is the direct subprocess, so the variable must be | ||
| 151 | + # set in its environment, to affect its search behavior. (The "1" can be any value.) | ||
| 152 | + if shell: | ||
| 153 | + safer_env = {} if env is None else dict(env) | ||
| 154 | + safer_env["NoDefaultCurrentDirectoryInExePath"] = "1" | ||
| 155 | + else: | ||
| 156 | + safer_env = env | ||
| 157 | + | ||
| 158 | + # When not using a shell, the current process does the search in a CreateProcessW | ||
| 159 | + # API call, so the variable must be set in our environment. With a shell, this is | ||
| 160 | + # unnecessary, in versions where https://github.com/python/cpython/issues/101283 is | ||
| 161 | + # patched. If not, in the rare case the ComSpec environment variable is unset, the | ||
| 162 | + # shell is searched for unsafely. Setting NoDefaultCurrentDirectoryInExePath in all | ||
| 163 | + # cases, as here, is simpler and protects against that. (The "1" can be any value.) | ||
| 164 | + with patch_env("NoDefaultCurrentDirectoryInExePath", "1"): | ||
| 165 | + return Popen( | ||
| 166 | + command, | ||
| 167 | + shell=shell, | ||
| 168 | + env=safer_env, | ||
| 169 | + creationflags=creationflags, | ||
| 170 | + **kwargs | ||
| 171 | + ) | ||
| 172 | + | ||
| 173 | + | ||
| 174 | + if os.name == "nt": | ||
| 175 | + safer_popen = _safer_popen_windows | ||
| 176 | + else: | ||
| 177 | + safer_popen = Popen | ||
| 178 | + | ||
| 179 | + | ||
| 127 | 180 | def dashify(string): | |
| 128 | 181 | return string.replace('_', '-') | |
| 129 | 182 | ||
@@ -144,11 +197,6 @@ def dict_to_slots_and__excluded_are_none(self, d, excluded=()): | |||
| 144 | 197 | # value of Windows process creation flag taken from MSDN | |
| 145 | 198 | CREATE_NO_WINDOW = 0x08000000 | |
| 146 | 199 | ||
| 147 | - ## CREATE_NEW_PROCESS_GROUP is needed to allow killing it afterwards, | ||
| 148 | - # see https://docs.python.org/3/library/subprocess.html#subprocess.Popen.send_signal | ||
| 149 | - PROC_CREATIONFLAGS = (CREATE_NO_WINDOW | subprocess.CREATE_NEW_PROCESS_GROUP | ||
| 150 | - if is_win else 0) | ||
| 151 | - | ||
| 152 | 200 | ||
| 153 | 201 | class Git(LazyMixin): | |
| 154 | 202 | ||
@@ -721,19 +769,18 @@ def execute(self, command, | |||
| 721 | 769 | log.debug("Popen(%s, cwd=%s, universal_newlines=%s, shell=%s, istream=%s)", | |
| 722 | 770 | command, cwd, universal_newlines, shell, istream_ok) | |
| 723 | 771 | try: | |
| 724 | - proc = Popen(command, | ||
| 725 | - env=env, | ||
| 726 | - cwd=cwd, | ||
| 727 | - bufsize=-1, | ||
| 728 | - stdin=istream, | ||
| 729 | - stderr=PIPE, | ||
| 730 | - stdout=stdout_sink, | ||
| 731 | - shell=shell is not None and shell or self.USE_SHELL, | ||
| 732 | - close_fds=is_posix, # unsupported on windows | ||
| 733 | - universal_newlines=universal_newlines, | ||
| 734 | - creationflags=PROC_CREATIONFLAGS, | ||
| 735 | - **subprocess_kwargs | ||
| 736 | - ) | ||
| 772 | + proc = safer_popen( | ||
| 773 | + command, | ||
| 774 | + env=env, | ||
| 775 | + cwd=cwd, | ||
| 776 | + bufsize=-1, | ||
| 777 | + stdin=istream, | ||
| 778 | + stderr=PIPE, | ||
| 779 | + stdout=stdout_sink, | ||
| 780 | + shell=shell is not None and shell or self.USE_SHELL, | ||
| 781 | + universal_newlines=universal_newlines, | ||
| 782 | + **subprocess_kwargs | ||
| 783 | + ) | ||
| 737 | 784 | except cmd_not_found_exception as err: | |
| 738 | 785 | raise GitCommandNotFound(command, err) | |
| 739 | 786 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,7 +13,7 @@ | |||
| 13 | 13 | ) | |
| 14 | 14 | import subprocess | |
| 15 | 15 | ||
| 16 | - from git.cmd import PROC_CREATIONFLAGS, handle_process_output | ||
| 16 | + from git.cmd import handle_process_output, safer_popen | ||
| 17 | 17 | from git.compat import ( | |
| 18 | 18 | PY3, | |
| 19 | 19 | defenc, | |
@@ -76,13 +76,12 @@ def run_commit_hook(name, index, *args): | |||
| 76 | 76 | env['GIT_INDEX_FILE'] = safe_decode(index.path) if PY3 else safe_encode(index.path) | |
| 77 | 77 | env['GIT_EDITOR'] = ':' | |
| 78 | 78 | try: | |
| 79 | - cmd = subprocess.Popen([hp] + list(args), | ||
| 79 | + cmd = safer_open([hp] + list(args), | ||
| 80 | 80 | env=env, | |
| 81 | 81 | stdout=subprocess.PIPE, | |
| 82 | 82 | stderr=subprocess.PIPE, | |
| 83 | 83 | cwd=index.repo.working_dir, | |
| 84 | - close_fds=is_posix, | ||
| 85 | - creationflags=PROC_CREATIONFLAGS,) | ||
| 84 | + close_fds=is_posix) | ||
| 86 | 85 | except Exception as ex: | |
| 87 | 86 | raise HookExecutionError(hp, ex) | |
| 88 | 87 | else: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,6 +16,7 @@ | |||
| 16 | 16 | import textwrap | |
| 17 | 17 | import time | |
| 18 | 18 | import unittest | |
| 19 | + import venv | ||
| 19 | 20 | ||
| 20 | 21 | from git.compat import string_types, is_win | |
| 21 | 22 | from git.util import rmtree, cwd | |
@@ -36,7 +37,8 @@ | |||
| 36 | 37 | __all__ = ( | |
| 37 | 38 | 'fixture_path', 'fixture', 'StringProcessAdapter', | |
| 38 | 39 | 'with_rw_directory', 'with_rw_repo', 'with_rw_and_rw_remote_repo', | |
| 39 | - 'TestBase', 'TestCase', | ||
| 40 | + 'TestBase', 'VirtualEnvironment', | ||
| 41 | + 'TestCase', | ||
| 40 | 42 | 'SkipTest', 'skipIf', | |
| 41 | 43 | 'GIT_REPO', 'GIT_DAEMON_PORT' | |
| 42 | 44 | ) | |
@@ -83,13 +85,13 @@ def with_rw_directory(func): | |||
| 83 | 85 | test succeeds, but leave it otherwise to aid additional debugging""" | |
| 84 | 86 | ||
| 85 | 87 | @wraps(func) | |
| 86 | - def wrapper(self): | ||
| 88 | + def wrapper(self, *args, **kwargs): | ||
| 87 | 89 | path = tempfile.mktemp(prefix=func.__name__) | |
| 88 | 90 | os.mkdir(path) | |
| 89 | 91 | keep = False | |
| 90 | 92 | try: | |
| 91 | 93 | try: | |
| 92 | - return func(self, path) | ||
| 94 | + return func(self, path, *args, **kwargs) | ||
| 93 | 95 | except Exception: | |
| 94 | 96 | log.info("Test %s.%s failed, output is at %r\n", | |
| 95 | 97 | type(self).__name__, func.__name__, path) | |
@@ -379,3 +381,46 @@ def _make_file(self, rela_path, data, repo=None): | |||
| 379 | 381 | with open(abs_path, "w") as fp: | |
| 380 | 382 | fp.write(data) | |
| 381 | 383 | return abs_path | |
| 384 | + | ||
| 385 | + | ||
| 386 | + class VirtualEnvironment: | ||
| 387 | + """A newly created Python virtual environment for use in a test.""" | ||
| 388 | + | ||
| 389 | + __slots__ = ("_env_dir",) | ||
| 390 | + | ||
| 391 | + def __init__(self, env_dir, with_pip): | ||
| 392 | + if os.name == "nt": | ||
| 393 | + self._env_dir = osp.realpath(env_dir) | ||
| 394 | + venv.create(self.env_dir, symlinks=False, with_pip=with_pip) | ||
| 395 | + else: | ||
| 396 | + self._env_dir = env_dir | ||
| 397 | + venv.create(self.env_dir, symlinks=True, with_pip=with_pip) | ||
| 398 | + | ||
| 399 | + @property | ||
| 400 | + def env_dir(self): | ||
| 401 | + """The top-level directory of the environment.""" | ||
| 402 | + return self._env_dir | ||
| 403 | + | ||
| 404 | + @property | ||
| 405 | + def python(self): | ||
| 406 | + """Path to the Python executable in the environment.""" | ||
| 407 | + return self._executable("python") | ||
| 408 | + | ||
| 409 | + @property | ||
| 410 | + def pip(self): | ||
| 411 | + """Path to the pip executable in the environment, or RuntimeError if absent.""" | ||
| 412 | + return self._executable("pip") | ||
| 413 | + | ||
| 414 | + @property | ||
| 415 | + def sources(self): | ||
| 416 | + """Path to a src directory in the environment, which may not exist yet.""" | ||
| 417 | + return os.path.join(self.env_dir, "src") | ||
| 418 | + | ||
| 419 | + def _executable(self, basename): | ||
| 420 | + if os.name == "nt": | ||
| 421 | + path = osp.join(self.env_dir, "Scripts", basename + ".exe") | ||
| 422 | + else: | ||
| 423 | + path = osp.join(self.env_dir, "bin", basename) | ||
| 424 | + if osp.isfile(path) or osp.islink(path): | ||
| 425 | + return path | ||
| 426 | + raise RuntimeError("no regular file or symlink " + str(path)) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -38,9 +38,30 @@ | |||
| 38 | 38 | except ImportError: | |
| 39 | 39 | import mock | |
| 40 | 40 | ||
| 41 | + try: | ||
| 42 | + from pathlib import Path | ||
| 43 | + except ImportError: | ||
| 44 | + from pathlib2 import Path | ||
| 45 | + | ||
| 41 | 46 | from git.compat import is_win | |
| 42 | 47 | ||
| 43 | 48 | ||
| 49 | + @contextlib.contextmanager | ||
| 50 | + def _patch_out_env(name): | ||
| 51 | + try: | ||
| 52 | + old_value = os.environ[name] | ||
| 53 | + except KeyError: | ||
| 54 | + old_value = None | ||
| 55 | + else: | ||
| 56 | + del os.environ[name] | ||
| 57 | + try: | ||
| 58 | + yield | ||
| 59 | + finally: | ||
| 60 | + if old_value is not None: | ||
| 61 | + os.environ[name] = old_value | ||
| 62 | + | ||
| 63 | + | ||
| 64 | + | ||
| 44 | 65 | class TestGit(TestBase): | |
| 45 | 66 | ||
| 46 | 67 | @classmethod | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -192,6 +192,17 @@ def _get_exe_extensions(): | |||
| 192 | 192 | ||
| 193 | 193 | ||
| 194 | 194 | def py_where(program, path=None): | |
| 195 | + """Perform a path search to assist :func:`is_cygwin_git`. | ||
| 196 | + This is not robust for general use. It is an implementation detail of | ||
| 197 | + :func:`is_cygwin_git`. When a search following all shell rules is needed, | ||
| 198 | + :func:`shutil.which` can be used instead. | ||
| 199 | + :note: Neither this function nor :func:`shutil.which` will predict the effect of an | ||
| 200 | + executable search on a native Windows system due to a :class:`subprocess.Popen` | ||
| 201 | + call without ``shell=True``, because shell and non-shell executable search on | ||
| 202 | + Windows differ considerably. | ||
| 203 | + """ | ||
| 204 | + | ||
| 205 | + | ||
| 195 | 206 | # From: http://stackoverflow.com/a/377028/548792 | |
| 196 | 207 | winprog_exts = _get_exe_extensions() | |
| 197 | 208 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | - ddt>=1.1.1 | ||
| 2 | 1 | coverage | |
| 2 | + ddt>=1.1.1 | ||
| 3 | 3 | flake8 | |
| 4 | + mock; python_version=='2.7' | ||
| 4 | 5 | nose | |
| 5 | 6 | tox | |
| 6 | - mock; python_version=='2.7' | ||
| 7 | + virtualenv | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments