Merge pull request #400 from pre-commit/fix_trailing_whitespace_non_utf8_diff

Fix staged-files-only with a non-utf8-trailing-whitespace diff.  Resolves #397
This commit is contained in:
Anthony Sottile
2016-08-18 17:29:15 +02:00
committed by GitHub
2 changed files with 27 additions and 2 deletions

View File

@@ -45,7 +45,7 @@ def staged_files_only(cmd_runner):
finally:
# Try to apply the patch we saved
try:
cmd_runner.run(['git', 'apply', patch_filename])
cmd_runner.run(('git', 'apply', patch_filename), encoding=None)
except CalledProcessError:
logger.warning(
'Stashed changes conflicted with hook auto-fixes... '
@@ -55,7 +55,7 @@ def staged_files_only(cmd_runner):
# by hooks.
# Roll back the changes made by hooks.
cmd_runner.run(['git', 'checkout', '--', '.'])
cmd_runner.run(['git', 'apply', patch_filename])
cmd_runner.run(('git', 'apply', patch_filename), encoding=None)
logger.info('Restored changes from {0}.'.format(patch_filename))
else:
# There weren't any staged files so we don't need to do anything

View File

@@ -280,3 +280,28 @@ def test_stage_non_utf8_changes(foo_staged, cmd_runner):
with staged_files_only(cmd_runner):
_test_foo_state(foo_staged)
_test_foo_state(foo_staged, contents, 'AM', encoding='latin-1')
def test_non_utf8_conflicting_diff(foo_staged, cmd_runner):
"""Regression test for #397"""
# The trailing whitespace is important here, this triggers git to produce
# an error message which looks like:
#
# ...patch1471530032:14: trailing whitespace.
# [[unprintable character]][[space character]]
# error: patch failed: foo:1
# error: foo: patch does not apply
#
# Previously, the error message (though discarded immediately) was being
# decoded with the UTF-8 codec (causing a crash)
contents = 'ú \n'
with io.open('foo', 'w', encoding='latin-1') as foo_file:
foo_file.write(contents)
_test_foo_state(foo_staged, contents, 'AM', encoding='latin-1')
with staged_files_only(cmd_runner):
_test_foo_state(foo_staged)
# Create a conflicting diff that will need to be rolled back
with io.open('foo', 'w') as foo_file:
foo_file.write('')
_test_foo_state(foo_staged, contents, 'AM', encoding='latin-1')