Skip to content

Commit fadeb50

Browse files
committed
Merge pull request pre-commit#326 from pre-commit/fix_commit_am_322
Fix pre-commit#322 by only removing git environment variables while cloning
2 parents c16479b + 495fefd commit fadeb50

File tree

6 files changed

+42
-14
lines changed

6 files changed

+42
-14
lines changed

appveyor.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ build: false
1515
before_test:
1616
- git config --global user.name "AppVeyor CI"
1717
- git config --global user.email "user@example.com"
18+
# Shut up CRLF messages
19+
- git config --global core.safecrlf false
1820

1921
test_script: tox
2022

pre_commit/main.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,6 @@
2222
# to install packages to the wrong place. We don't want anything to deal with
2323
# pyvenv
2424
os.environ.pop('__PYVENV_LAUNCHER__', None)
25-
# https://github.com/pre-commit/pre-commit/issues/300
26-
# In git 2.6.3 (maybe others), git exports this while running pre-commit hooks
27-
os.environ.pop('GIT_WORK_TREE', None)
28-
# In git 1.9.1 (maybe others), git exports these while running pre-commit hooks
29-
# in submodules. In the general case this causes problems.
30-
# These are covered by test_install_in_submodule_and_run
31-
# Causes git clone to clone wrong thing
32-
os.environ.pop('GIT_DIR', None)
33-
# Causes 'error invalid object ...' during commit
34-
os.environ.pop('GIT_INDEX_FILE', None)
3525

3626

3727
def main(argv=None):

pre_commit/store.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pre_commit.util import clean_path_on_failure
1515
from pre_commit.util import cmd_output
1616
from pre_commit.util import cwd
17+
from pre_commit.util import no_git_env
1718

1819

1920
logger = logging.getLogger('pre_commit')
@@ -114,9 +115,11 @@ def clone(self, url, sha):
114115

115116
dir = tempfile.mkdtemp(prefix='repo', dir=self.directory)
116117
with clean_path_on_failure(dir):
117-
cmd_output('git', 'clone', '--no-checkout', url, dir)
118+
cmd_output(
119+
'git', 'clone', '--no-checkout', url, dir, env=no_git_env(),
120+
)
118121
with cwd(dir):
119-
cmd_output('git', 'reset', sha, '--hard')
122+
cmd_output('git', 'reset', sha, '--hard', env=no_git_env())
120123

121124
# Update our db with the created repo
122125
with sqlite3.connect(self.db_path) as db:

pre_commit/util.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ def shell_escape(arg):
7171
return "'" + arg.replace("'", "'\"'\"'".strip()) + "'"
7272

7373

74+
def no_git_env():
75+
# Too many bugs dealing with environment variables and GIT:
76+
# https://github.com/pre-commit/pre-commit/issues/300
77+
# In git 2.6.3 (maybe others), git exports GIT_WORK_TREE while running
78+
# pre-commit hooks
79+
# In git 1.9.1 (maybe others), git exports GIT_DIR and GIT_INDEX_FILE
80+
# while running pre-commit hooks in submodules.
81+
# GIT_DIR: Causes git clone to clone wrong thing
82+
# GIT_INDEX_FILE: Causes 'error invalid object ...' during commit
83+
return dict(
84+
(k, v) for k, v in os.environ.items() if not k.startswith('GIT_')
85+
)
86+
87+
7488
@contextlib.contextmanager
7589
def tarfile_open(*args, **kwargs):
7690
"""Compatibility layer because python2.6"""

tests/commands/install_uninstall_test.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def _get_commit_output(
133133
home = home or tempdir_factory.get()
134134
env = dict(env_base, PRE_COMMIT_HOME=home)
135135
return cmd_output(
136-
'git', 'commit', '-m', 'Commit!', '--allow-empty',
136+
'git', 'commit', '-am', 'Commit!', '--allow-empty',
137137
# git commit puts pre-commit to stderr
138138
stderr=subprocess.STDOUT,
139139
env=env,
@@ -175,7 +175,7 @@ def test_install_in_submodule_and_run(tempdir_factory):
175175
parent_path = git_dir(tempdir_factory)
176176
with cwd(parent_path):
177177
cmd_output('git', 'submodule', 'add', src_path, 'sub')
178-
cmd_output('git', 'commit', '-am', 'foo')
178+
cmd_output('git', 'commit', '-m', 'foo')
179179

180180
sub_pth = os.path.join(parent_path, 'sub')
181181
with cwd(sub_pth):
@@ -185,6 +185,23 @@ def test_install_in_submodule_and_run(tempdir_factory):
185185
assert NORMAL_PRE_COMMIT_RUN.match(output)
186186

187187

188+
def test_commit_am(tempdir_factory):
189+
"""Regression test for #322."""
190+
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
191+
with cwd(path):
192+
# Make an unstaged change
193+
open('unstaged', 'w').close()
194+
cmd_output('git', 'add', '.')
195+
cmd_output('git', 'commit', '-m', 'foo')
196+
with io.open('unstaged', 'w') as foo_file:
197+
foo_file.write('Oh hai')
198+
199+
assert install(Runner(path)) == 0
200+
201+
ret, output = _get_commit_output(tempdir_factory)
202+
assert ret == 0
203+
204+
188205
def test_install_idempotent(tempdir_factory):
189206
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
190207
with cwd(path):

tests/repository_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ def test_additional_dependencies_roll_forward(tempdir_factory, store):
377377
assert 'mccabe' in output
378378

379379

380+
@skipif_slowtests_false
380381
@xfailif_windows_no_ruby
381382
@pytest.mark.integration
382383
def test_additional_ruby_dependencies_installed(
@@ -392,6 +393,7 @@ def test_additional_ruby_dependencies_installed(
392393
assert 'thread_safe' in output
393394

394395

396+
@skipif_slowtests_false
395397
@xfailif_windows_no_node
396398
@pytest.mark.integration
397399
def test_additional_node_dependencies_installed(

0 commit comments

Comments
 (0)