Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 60cc415

Browse files
authored
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
2 parents f11338c + b05cc40 commit 60cc415

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

pre_commit/staged_files_only.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def staged_files_only(cmd_runner):
4545
finally:
4646
# Try to apply the patch we saved
4747
try:
48-
cmd_runner.run(['git', 'apply', patch_filename])
48+
cmd_runner.run(('git', 'apply', patch_filename), encoding=None)
4949
except CalledProcessError:
5050
logger.warning(
5151
'Stashed changes conflicted with hook auto-fixes... '
@@ -55,7 +55,7 @@ def staged_files_only(cmd_runner):
5555
# by hooks.
5656
# Roll back the changes made by hooks.
5757
cmd_runner.run(['git', 'checkout', '--', '.'])
58-
cmd_runner.run(['git', 'apply', patch_filename])
58+
cmd_runner.run(('git', 'apply', patch_filename), encoding=None)
5959
logger.info('Restored changes from {0}.'.format(patch_filename))
6060
else:
6161
# There weren't any staged files so we don't need to do anything

tests/staged_files_only_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,28 @@ def test_stage_non_utf8_changes(foo_staged, cmd_runner):
280280
with staged_files_only(cmd_runner):
281281
_test_foo_state(foo_staged)
282282
_test_foo_state(foo_staged, contents, 'AM', encoding='latin-1')
283+
284+
285+
def test_non_utf8_conflicting_diff(foo_staged, cmd_runner):
286+
"""Regression test for #397"""
287+
# The trailing whitespace is important here, this triggers git to produce
288+
# an error message which looks like:
289+
#
290+
# ...patch1471530032:14: trailing whitespace.
291+
# [[unprintable character]][[space character]]
292+
# error: patch failed: foo:1
293+
# error: foo: patch does not apply
294+
#
295+
# Previously, the error message (though discarded immediately) was being
296+
# decoded with the UTF-8 codec (causing a crash)
297+
contents = \n'
298+
with io.open('foo', 'w', encoding='latin-1') as foo_file:
299+
foo_file.write(contents)
300+
301+
_test_foo_state(foo_staged, contents, 'AM', encoding='latin-1')
302+
with staged_files_only(cmd_runner):
303+
_test_foo_state(foo_staged)
304+
# Create a conflicting diff that will need to be rolled back
305+
with io.open('foo', 'w') as foo_file:
306+
foo_file.write('')
307+
_test_foo_state(foo_staged, contents, 'AM', encoding='latin-1')

0 commit comments

Comments
 (0)