提交 90f64b41 编写于 作者: X Xavier Fernandez 提交者: GitHub

Merge pull request #4666 from cjerdonek/issue-1130-git-dir-env-vars

Address issue #1130 (GIT_DIR and GIT_WORK_TREE).
Allow pip to work if the ``GIT_DIR`` and ``GIT_WORK_TREE`` environment
variables are set.
......@@ -627,7 +627,14 @@ def unpack_file(filename, location, content_type, link):
def call_subprocess(cmd, show_stdout=True, cwd=None,
on_returncode='raise',
command_desc=None,
extra_environ=None, spinner=None):
extra_environ=None, unset_environ=None, spinner=None):
"""
Args:
unset_environ: an iterable of environment variable names to unset
prior to calling subprocess.Popen().
"""
if unset_environ is None:
unset_environ = []
# This function's handling of subprocess output is confusing and I
# previously broke it terribly, so as penance I will write a long comment
# explaining things.
......@@ -664,6 +671,8 @@ def call_subprocess(cmd, show_stdout=True, cwd=None,
env = os.environ.copy()
if extra_environ:
env.update(extra_environ)
for name in unset_environ:
env.pop(name, None)
try:
proc = subprocess.Popen(
cmd, stderr=subprocess.STDOUT, stdin=None, stdout=stdout,
......
......@@ -166,6 +166,8 @@ class VersionControl(object):
dirname = ''
# List of supported schemes for this Version Control
schemes = () # type: Tuple[str, ...]
# Iterable of environment variable names to pass to call_subprocess().
unset_environ = () # type: Tuple[str, ...]
default_arg_rev = None # type: Optional[str]
def __init__(self, url=None, *args, **kwargs):
......@@ -422,7 +424,8 @@ class VersionControl(object):
return call_subprocess(cmd, show_stdout, cwd,
on_returncode,
command_desc, extra_environ,
spinner)
unset_environ=self.unset_environ,
spinner=spinner)
except OSError as e:
# errno.ENOENT = no such file or directory
# In other words, the VCS executable isn't available
......
......@@ -27,6 +27,9 @@ class Git(VersionControl):
schemes = (
'git', 'git+http', 'git+https', 'git+ssh', 'git+git', 'git+file',
)
# Prevent the user's environment variables from interfering with pip:
# https://github.com/pypa/pip/issues/1130
unset_environ = ('GIT_DIR', 'GIT_WORK_TREE')
default_arg_rev = 'origin/HEAD'
def __init__(self, url=None, *args, **kwargs):
......
import os
from pip._internal.utils.temp_dir import TempDirectory
from pip._internal.vcs.git import Git
def test_git_dir_ignored():
"""
Test that a GIT_DIR environment variable is ignored.
"""
git = Git()
with TempDirectory() as temp:
temp_dir = temp.path
env = {'GIT_DIR': 'foo'}
# If GIT_DIR is not ignored, then os.listdir() will return ['foo'].
git.run_command(['init', temp_dir], cwd=temp_dir, extra_environ=env)
assert os.listdir(temp_dir) == ['.git']
def test_git_work_tree_ignored():
"""
Test that a GIT_WORK_TREE environment variable is ignored.
"""
git = Git()
with TempDirectory() as temp:
temp_dir = temp.path
git.run_command(['init', temp_dir], cwd=temp_dir)
# Choose a directory relative to the cwd that does not exist.
# If GIT_WORK_TREE is not ignored, then the command will error out
# with: "fatal: This operation must be run in a work tree".
env = {'GIT_WORK_TREE': 'foo'}
git.run_command(['status', temp_dir], extra_environ=env, cwd=temp_dir)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册