mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 17:14:43 +04:00
Merge pull request #326 from pre-commit/fix_commit_am_322
Fix #322 by only removing git environment variables while cloning
This commit is contained in:
commit
fadeb509ad
6 changed files with 42 additions and 14 deletions
|
|
@ -15,6 +15,8 @@ build: false
|
||||||
before_test:
|
before_test:
|
||||||
- git config --global user.name "AppVeyor CI"
|
- git config --global user.name "AppVeyor CI"
|
||||||
- git config --global user.email "user@example.com"
|
- git config --global user.email "user@example.com"
|
||||||
|
# Shut up CRLF messages
|
||||||
|
- git config --global core.safecrlf false
|
||||||
|
|
||||||
test_script: tox
|
test_script: tox
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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
|
# to install packages to the wrong place. We don't want anything to deal with
|
||||||
# pyvenv
|
# pyvenv
|
||||||
os.environ.pop('__PYVENV_LAUNCHER__', None)
|
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):
|
def main(argv=None):
|
||||||
|
|
|
||||||
|
|
@ -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 clean_path_on_failure
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
from pre_commit.util import cwd
|
from pre_commit.util import cwd
|
||||||
|
from pre_commit.util import no_git_env
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger('pre_commit')
|
logger = logging.getLogger('pre_commit')
|
||||||
|
|
@ -114,9 +115,11 @@ class Store(object):
|
||||||
|
|
||||||
dir = tempfile.mkdtemp(prefix='repo', dir=self.directory)
|
dir = tempfile.mkdtemp(prefix='repo', dir=self.directory)
|
||||||
with clean_path_on_failure(dir):
|
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):
|
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
|
# Update our db with the created repo
|
||||||
with sqlite3.connect(self.db_path) as db:
|
with sqlite3.connect(self.db_path) as db:
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,20 @@ def shell_escape(arg):
|
||||||
return "'" + arg.replace("'", "'\"'\"'".strip()) + "'"
|
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
|
@contextlib.contextmanager
|
||||||
def tarfile_open(*args, **kwargs):
|
def tarfile_open(*args, **kwargs):
|
||||||
"""Compatibility layer because python2.6"""
|
"""Compatibility layer because python2.6"""
|
||||||
|
|
|
||||||
|
|
@ -133,7 +133,7 @@ def _get_commit_output(
|
||||||
home = home or tempdir_factory.get()
|
home = home or tempdir_factory.get()
|
||||||
env = dict(env_base, PRE_COMMIT_HOME=home)
|
env = dict(env_base, PRE_COMMIT_HOME=home)
|
||||||
return cmd_output(
|
return cmd_output(
|
||||||
'git', 'commit', '-m', 'Commit!', '--allow-empty',
|
'git', 'commit', '-am', 'Commit!', '--allow-empty',
|
||||||
# git commit puts pre-commit to stderr
|
# git commit puts pre-commit to stderr
|
||||||
stderr=subprocess.STDOUT,
|
stderr=subprocess.STDOUT,
|
||||||
env=env,
|
env=env,
|
||||||
|
|
@ -175,7 +175,7 @@ def test_install_in_submodule_and_run(tempdir_factory):
|
||||||
parent_path = git_dir(tempdir_factory)
|
parent_path = git_dir(tempdir_factory)
|
||||||
with cwd(parent_path):
|
with cwd(parent_path):
|
||||||
cmd_output('git', 'submodule', 'add', src_path, 'sub')
|
cmd_output('git', 'submodule', 'add', src_path, 'sub')
|
||||||
cmd_output('git', 'commit', '-am', 'foo')
|
cmd_output('git', 'commit', '-m', 'foo')
|
||||||
|
|
||||||
sub_pth = os.path.join(parent_path, 'sub')
|
sub_pth = os.path.join(parent_path, 'sub')
|
||||||
with cwd(sub_pth):
|
with cwd(sub_pth):
|
||||||
|
|
@ -185,6 +185,23 @@ def test_install_in_submodule_and_run(tempdir_factory):
|
||||||
assert NORMAL_PRE_COMMIT_RUN.match(output)
|
assert NORMAL_PRE_COMMIT_RUN.match(output)
|
||||||
|
|
||||||
|
|
||||||
|
def test_commit_am(tempdir_factory):
|
||||||
|
"""Regression test for #322."""
|
||||||
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
|
with cwd(path):
|
||||||
|
# Make an unstaged change
|
||||||
|
open('unstaged', 'w').close()
|
||||||
|
cmd_output('git', 'add', '.')
|
||||||
|
cmd_output('git', 'commit', '-m', 'foo')
|
||||||
|
with io.open('unstaged', 'w') as foo_file:
|
||||||
|
foo_file.write('Oh hai')
|
||||||
|
|
||||||
|
assert install(Runner(path)) == 0
|
||||||
|
|
||||||
|
ret, output = _get_commit_output(tempdir_factory)
|
||||||
|
assert ret == 0
|
||||||
|
|
||||||
|
|
||||||
def test_install_idempotent(tempdir_factory):
|
def test_install_idempotent(tempdir_factory):
|
||||||
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
|
||||||
with cwd(path):
|
with cwd(path):
|
||||||
|
|
|
||||||
|
|
@ -377,6 +377,7 @@ def test_additional_dependencies_roll_forward(tempdir_factory, store):
|
||||||
assert 'mccabe' in output
|
assert 'mccabe' in output
|
||||||
|
|
||||||
|
|
||||||
|
@skipif_slowtests_false
|
||||||
@xfailif_windows_no_ruby
|
@xfailif_windows_no_ruby
|
||||||
@pytest.mark.integration
|
@pytest.mark.integration
|
||||||
def test_additional_ruby_dependencies_installed(
|
def test_additional_ruby_dependencies_installed(
|
||||||
|
|
@ -392,6 +393,7 @@ def test_additional_ruby_dependencies_installed(
|
||||||
assert 'thread_safe' in output
|
assert 'thread_safe' in output
|
||||||
|
|
||||||
|
|
||||||
|
@skipif_slowtests_false
|
||||||
@xfailif_windows_no_node
|
@xfailif_windows_no_node
|
||||||
@pytest.mark.integration
|
@pytest.mark.integration
|
||||||
def test_additional_node_dependencies_installed(
|
def test_additional_node_dependencies_installed(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue