Treat diffs as maybe-not-utf8. · precommit/pre-commit@e40a151 · GitHub
Skip to content

Commit e40a151

Browse files
committed
Treat diffs as maybe-not-utf8.
1 parent 105af6f commit e40a151

3 files changed

Lines changed: 32 additions & 13 deletions

File tree

pre_commit/prefixed_command_runner.py

Lines changed: 6 additions & 6 deletions

pre_commit/staged_files_only.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,20 @@ def staged_files_only(cmd_runner):
2020
cmd_runner - PrefixedCommandRunner
2121
"""
2222
# Determine if there are unstaged files
23-
retcode, diff_stdout, _ = cmd_runner.run(
23+
retcode, diff_stdout_binary, _ = cmd_runner.run(
2424
['git', 'diff', '--ignore-submodules', '--binary', '--exit-code'],
2525
retcode=None,
26+
encoding=None,
2627
)
27-
if retcode and diff_stdout.strip():
28+
if retcode and diff_stdout_binary.strip():
2829
patch_filename = cmd_runner.path('patch{0}'.format(int(time.time())))
2930
logger.warning('Unstaged files detected.')
3031
logger.info(
3132
'Stashing unstaged files to {0}.'.format(patch_filename),
3233
)
3334
# Save the current unstaged changes as a patch
34-
with io.open(patch_filename, 'w', encoding='utf-8') as patch_file:
35-
patch_file.write(diff_stdout)
35+
with io.open(patch_filename, 'wb') as patch_file:
36+
patch_file.write(diff_stdout_binary)
3637

3738
# Clear the working directory of unstaged changes
3839
cmd_runner.run(['git', 'checkout', '--', '.'])

tests/staged_files_only_test.py

Lines changed: 21 additions & 3 deletions

0 commit comments

Comments
 (0)