mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
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:
commit
60cc4158ed
2 changed files with 27 additions and 2 deletions
|
|
@ -45,7 +45,7 @@ def staged_files_only(cmd_runner):
|
||||||
finally:
|
finally:
|
||||||
# Try to apply the patch we saved
|
# Try to apply the patch we saved
|
||||||
try:
|
try:
|
||||||
cmd_runner.run(['git', 'apply', patch_filename])
|
cmd_runner.run(('git', 'apply', patch_filename), encoding=None)
|
||||||
except CalledProcessError:
|
except CalledProcessError:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
'Stashed changes conflicted with hook auto-fixes... '
|
'Stashed changes conflicted with hook auto-fixes... '
|
||||||
|
|
@ -55,7 +55,7 @@ def staged_files_only(cmd_runner):
|
||||||
# by hooks.
|
# by hooks.
|
||||||
# Roll back the changes made by hooks.
|
# Roll back the changes made by hooks.
|
||||||
cmd_runner.run(['git', 'checkout', '--', '.'])
|
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))
|
logger.info('Restored changes from {0}.'.format(patch_filename))
|
||||||
else:
|
else:
|
||||||
# There weren't any staged files so we don't need to do anything
|
# There weren't any staged files so we don't need to do anything
|
||||||
|
|
|
||||||
|
|
@ -280,3 +280,28 @@ def test_stage_non_utf8_changes(foo_staged, cmd_runner):
|
||||||
with staged_files_only(cmd_runner):
|
with staged_files_only(cmd_runner):
|
||||||
_test_foo_state(foo_staged)
|
_test_foo_state(foo_staged)
|
||||||
_test_foo_state(foo_staged, contents, 'AM', encoding='latin-1')
|
_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')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue