mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
Workaround git apply with autocrlf=true bug
This commit is contained in:
parent
ce7481f75b
commit
0548b0b521
2 changed files with 26 additions and 8 deletions
|
|
@ -11,6 +11,16 @@ from pre_commit.util import CalledProcessError
|
||||||
logger = logging.getLogger('pre_commit')
|
logger = logging.getLogger('pre_commit')
|
||||||
|
|
||||||
|
|
||||||
|
def _git_apply(cmd_runner, patch):
|
||||||
|
args = ('apply', '--whitespace=nowarn', patch)
|
||||||
|
try:
|
||||||
|
cmd_runner.run(('git',) + args, encoding=None)
|
||||||
|
except CalledProcessError:
|
||||||
|
# Retry with autocrlf=false -- see #570
|
||||||
|
cmd = ('git', '-c', 'core.autocrlf=false') + args
|
||||||
|
cmd_runner.run(cmd, encoding=None)
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def staged_files_only(cmd_runner):
|
def staged_files_only(cmd_runner):
|
||||||
"""Clear any unstaged changes from the git working directory inside this
|
"""Clear any unstaged changes from the git working directory inside this
|
||||||
|
|
@ -46,10 +56,7 @@ def staged_files_only(cmd_runner):
|
||||||
finally:
|
finally:
|
||||||
# Try to apply the patch we saved
|
# Try to apply the patch we saved
|
||||||
try:
|
try:
|
||||||
cmd_runner.run(
|
_git_apply(cmd_runner, patch_filename)
|
||||||
('git', 'apply', '--whitespace=nowarn', patch_filename),
|
|
||||||
encoding=None,
|
|
||||||
)
|
|
||||||
except CalledProcessError:
|
except CalledProcessError:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
'Stashed changes conflicted with hook auto-fixes... '
|
'Stashed changes conflicted with hook auto-fixes... '
|
||||||
|
|
@ -59,10 +66,7 @@ def staged_files_only(cmd_runner):
|
||||||
# by hooks.
|
# by hooks.
|
||||||
# Roll back the changes made by hooks.
|
# Roll back the changes made by hooks.
|
||||||
cmd_runner.run(('git', 'checkout', '--', '.'))
|
cmd_runner.run(('git', 'checkout', '--', '.'))
|
||||||
cmd_runner.run(
|
_git_apply(cmd_runner, patch_filename)
|
||||||
('git', 'apply', patch_filename, '--whitespace=nowarn'),
|
|
||||||
encoding=None,
|
|
||||||
)
|
|
||||||
logger.info('Restored changes from {}.'.format(patch_filename))
|
logger.info('Restored changes from {}.'.format(patch_filename))
|
||||||
else:
|
else:
|
||||||
# There weren't any staged files so we don't need to do anything
|
# There weren't any staged files so we don't need to do anything
|
||||||
|
|
|
||||||
|
|
@ -354,3 +354,17 @@ def test_crlf(in_git_dir, cmd_runner, crlf_before, crlf_after, autocrlf):
|
||||||
def test_whitespace_errors(in_git_dir, cmd_runner):
|
def test_whitespace_errors(in_git_dir, cmd_runner):
|
||||||
cmd_output('git', 'config', '--local', 'apply.whitespace', 'error')
|
cmd_output('git', 'config', '--local', 'apply.whitespace', 'error')
|
||||||
test_crlf(in_git_dir, cmd_runner, True, True, 'true')
|
test_crlf(in_git_dir, cmd_runner, True, True, 'true')
|
||||||
|
|
||||||
|
|
||||||
|
def test_autocrlf_commited_crlf(in_git_dir, cmd_runner):
|
||||||
|
"""Regression test for #570"""
|
||||||
|
cmd_output('git', 'config', '--local', 'core.autocrlf', 'false')
|
||||||
|
_write(b'1\r\n2\r\n')
|
||||||
|
cmd_output('git', 'add', 'foo')
|
||||||
|
cmd_output('git', 'commit', '-m', 'Check in crlf')
|
||||||
|
|
||||||
|
cmd_output('git', 'config', '--local', 'core.autocrlf', 'true')
|
||||||
|
_write(b'1\r\n2\r\n\r\n\r\n\r\n')
|
||||||
|
|
||||||
|
with staged_files_only(cmd_runner):
|
||||||
|
assert_no_diff()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue