Fix #322 by only removing git environment variables while cloning

This commit is contained in:
Anthony Sottile
2015-12-22 18:36:12 -08:00
parent c16479b94a
commit 495fefd316
6 changed files with 42 additions and 14 deletions

View File

@@ -22,16 +22,6 @@ from pre_commit.runner import Runner
# to install packages to the wrong place. We don't want anything to deal with
# pyvenv
os.environ.pop('__PYVENV_LAUNCHER__', None)
# https://github.com/pre-commit/pre-commit/issues/300
# In git 2.6.3 (maybe others), git exports this while running pre-commit hooks
os.environ.pop('GIT_WORK_TREE', None)
# In git 1.9.1 (maybe others), git exports these while running pre-commit hooks
# in submodules. In the general case this causes problems.
# These are covered by test_install_in_submodule_and_run
# Causes git clone to clone wrong thing
os.environ.pop('GIT_DIR', None)
# Causes 'error invalid object ...' during commit
os.environ.pop('GIT_INDEX_FILE', None)
def main(argv=None):

View File

@@ -14,6 +14,7 @@ from pre_commit.prefixed_command_runner import PrefixedCommandRunner
from pre_commit.util import clean_path_on_failure
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from pre_commit.util import no_git_env
logger = logging.getLogger('pre_commit')
@@ -114,9 +115,11 @@ class Store(object):
dir = tempfile.mkdtemp(prefix='repo', dir=self.directory)
with clean_path_on_failure(dir):
cmd_output('git', 'clone', '--no-checkout', url, dir)
cmd_output(
'git', 'clone', '--no-checkout', url, dir, env=no_git_env(),
)
with cwd(dir):
cmd_output('git', 'reset', sha, '--hard')
cmd_output('git', 'reset', sha, '--hard', env=no_git_env())
# Update our db with the created repo
with sqlite3.connect(self.db_path) as db:

View File

@@ -71,6 +71,20 @@ def shell_escape(arg):
return "'" + arg.replace("'", "'\"'\"'".strip()) + "'"
def no_git_env():
# Too many bugs dealing with environment variables and GIT:
# https://github.com/pre-commit/pre-commit/issues/300
# In git 2.6.3 (maybe others), git exports GIT_WORK_TREE while running
# pre-commit hooks
# In git 1.9.1 (maybe others), git exports GIT_DIR and GIT_INDEX_FILE
# while running pre-commit hooks in submodules.
# GIT_DIR: Causes git clone to clone wrong thing
# GIT_INDEX_FILE: Causes 'error invalid object ...' during commit
return dict(
(k, v) for k, v in os.environ.items() if not k.startswith('GIT_')
)
@contextlib.contextmanager
def tarfile_open(*args, **kwargs):
"""Compatibility layer because python2.6"""