mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-01-15 21:40:19 -06:00
Treat diffs as maybe-not-utf8.
This commit is contained in:
@@ -56,24 +56,24 @@ class PrefixedCommandRunner(object):
|
||||
if not os.path.exists(self.prefix_dir):
|
||||
self.__makedirs(self.prefix_dir)
|
||||
|
||||
def run(self, cmd, retcode=0, stdin=None, **kwargs):
|
||||
def run(self, cmd, retcode=0, stdin=None, encoding='UTF-8', **kwargs):
|
||||
popen_kwargs = {
|
||||
'stdin': subprocess.PIPE,
|
||||
'stdout': subprocess.PIPE,
|
||||
'stderr': subprocess.PIPE,
|
||||
}
|
||||
if stdin is not None:
|
||||
stdin = stdin.encode('utf-8')
|
||||
stdin = stdin.encode('UTF-8')
|
||||
|
||||
popen_kwargs.update(kwargs)
|
||||
self._create_path_if_not_exists()
|
||||
replaced_cmd = _replace_cmd(cmd, prefix=self.prefix_dir)
|
||||
proc = self.__popen(replaced_cmd, **popen_kwargs)
|
||||
stdout, stderr = proc.communicate(stdin)
|
||||
if isinstance(stdout, bytes):
|
||||
stdout = stdout.decode('UTF-8')
|
||||
if isinstance(stderr, bytes):
|
||||
stderr = stderr.decode('UTF-8')
|
||||
if encoding is not None:
|
||||
stdout = stdout.decode(encoding)
|
||||
if encoding is not None:
|
||||
stderr = stderr.decode(encoding)
|
||||
returncode = proc.returncode
|
||||
|
||||
if retcode is not None and retcode != returncode:
|
||||
|
||||
@@ -20,19 +20,20 @@ def staged_files_only(cmd_runner):
|
||||
cmd_runner - PrefixedCommandRunner
|
||||
"""
|
||||
# Determine if there are unstaged files
|
||||
retcode, diff_stdout, _ = cmd_runner.run(
|
||||
retcode, diff_stdout_binary, _ = cmd_runner.run(
|
||||
['git', 'diff', '--ignore-submodules', '--binary', '--exit-code'],
|
||||
retcode=None,
|
||||
encoding=None,
|
||||
)
|
||||
if retcode and diff_stdout.strip():
|
||||
if retcode and diff_stdout_binary.strip():
|
||||
patch_filename = cmd_runner.path('patch{0}'.format(int(time.time())))
|
||||
logger.warning('Unstaged files detected.')
|
||||
logger.info(
|
||||
'Stashing unstaged files to {0}.'.format(patch_filename),
|
||||
)
|
||||
# Save the current unstaged changes as a patch
|
||||
with io.open(patch_filename, 'w', encoding='utf-8') as patch_file:
|
||||
patch_file.write(diff_stdout)
|
||||
with io.open(patch_filename, 'wb') as patch_file:
|
||||
patch_file.write(diff_stdout_binary)
|
||||
|
||||
# Clear the working directory of unstaged changes
|
||||
cmd_runner.run(['git', 'checkout', '--', '.'])
|
||||
|
||||
Reference in New Issue
Block a user