From d5e2af7de58a0478160785cc9b1100b1fcfc4687 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 31 Jul 2017 20:21:30 -0700 Subject: [PATCH] Fix patch applying when apply.whitespace=error --- pre_commit/staged_files_only.py | 10 ++++++++-- tests/staged_files_only_test.py | 7 ++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pre_commit/staged_files_only.py b/pre_commit/staged_files_only.py index 862c6bd1..151e924a 100644 --- a/pre_commit/staged_files_only.py +++ b/pre_commit/staged_files_only.py @@ -45,7 +45,10 @@ def staged_files_only(cmd_runner): finally: # Try to apply the patch we saved try: - cmd_runner.run(('git', 'apply', patch_filename), encoding=None) + cmd_runner.run( + ('git', 'apply', '--whitespace=nowarn', patch_filename), + encoding=None, + ) except CalledProcessError: logger.warning( 'Stashed changes conflicted with hook auto-fixes... ' @@ -55,7 +58,10 @@ 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), encoding=None) + cmd_runner.run( + ('git', 'apply', patch_filename, '--whitespace=nowarn'), + encoding=None, + ) logger.info('Restored changes from {}.'.format(patch_filename)) else: # There weren't any staged files so we don't need to do anything diff --git a/tests/staged_files_only_test.py b/tests/staged_files_only_test.py index e066c27c..ecaee814 100644 --- a/tests/staged_files_only_test.py +++ b/tests/staged_files_only_test.py @@ -340,7 +340,7 @@ bool_product = tuple(itertools.product((True, False), repeat=2)) def test_crlf(in_git_dir, cmd_runner, crlf_before, crlf_after, autocrlf): cmd_output('git', 'config', '--local', 'core.autocrlf', autocrlf) - before, after = b'1\n2\n', b'3\n4\n' + before, after = b'1\n2\n', b'3\n4\n\n' before = before.replace(b'\n', b'\r\n') if crlf_before else before after = after.replace(b'\n', b'\r\n') if crlf_after else after @@ -349,3 +349,8 @@ def test_crlf(in_git_dir, cmd_runner, crlf_before, crlf_after, autocrlf): _write(after) with staged_files_only(cmd_runner): assert_no_diff() + + +def test_whitespace_errors(in_git_dir, cmd_runner): + cmd_output('git', 'config', '--local', 'apply.whitespace', 'error') + test_crlf(in_git_dir, cmd_runner, True, True, 'true')