Skip to content

Commit d5e2af7

Browse files
committed
Fix patch applying when apply.whitespace=error
1 parent ec102a8 commit d5e2af7

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

pre_commit/staged_files_only.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ 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), encoding=None)
48+
cmd_runner.run(
49+
('git', 'apply', '--whitespace=nowarn', patch_filename),
50+
encoding=None,
51+
)
4952
except CalledProcessError:
5053
logger.warning(
5154
'Stashed changes conflicted with hook auto-fixes... '
@@ -55,7 +58,10 @@ def staged_files_only(cmd_runner):
5558
# by hooks.
5659
# Roll back the changes made by hooks.
5760
cmd_runner.run(['git', 'checkout', '--', '.'])
58-
cmd_runner.run(('git', 'apply', patch_filename), encoding=None)
61+
cmd_runner.run(
62+
('git', 'apply', patch_filename, '--whitespace=nowarn'),
63+
encoding=None,
64+
)
5965
logger.info('Restored changes from {}.'.format(patch_filename))
6066
else:
6167
# There weren't any staged files so we don't need to do anything

tests/staged_files_only_test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def assert_no_diff():
340340
def test_crlf(in_git_dir, cmd_runner, crlf_before, crlf_after, autocrlf):
341341
cmd_output('git', 'config', '--local', 'core.autocrlf', autocrlf)
342342

343-
before, after = b'1\n2\n', b'3\n4\n'
343+
before, after = b'1\n2\n', b'3\n4\n\n'
344344
before = before.replace(b'\n', b'\r\n') if crlf_before else before
345345
after = after.replace(b'\n', b'\r\n') if crlf_after else after
346346

@@ -349,3 +349,8 @@ def test_crlf(in_git_dir, cmd_runner, crlf_before, crlf_after, autocrlf):
349349
_write(after)
350350
with staged_files_only(cmd_runner):
351351
assert_no_diff()
352+
353+
354+
def test_whitespace_errors(in_git_dir, cmd_runner):
355+
cmd_output('git', 'config', '--local', 'apply.whitespace', 'error')
356+
test_crlf(in_git_dir, cmd_runner, True, True, 'true')

0 commit comments

Comments
 (0)