Skip to content

Commit 92678c3

Browse files
authored
Merge pull request #3126 from pre-commit/crlf-only-diff
staged_files_only can handle a crlf-only diff
2 parents 7384838 + 032d8e2 commit 92678c3

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

pre_commit/staged_files_only.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ def _unstaged_changes_cleared(patch_dir: str) -> Generator[None, None, None]:
5959
# There weren't any staged files so we don't need to do anything
6060
# special
6161
yield
62+
elif retcode == 1 and not diff_stdout.strip():
63+
# due to behaviour (probably a bug?) in git with crlf endings and
64+
# autocrlf set to either `true` or `input` sometimes git will refuse
65+
# to show a crlf-only diff to us :(
66+
yield
6267
elif retcode == 1 and diff_stdout.strip():
6368
patch_filename = f'patch{int(time.time())}-{os.getpid()}'
6469
patch_filename = os.path.join(patch_dir, patch_filename)

tests/staged_files_only_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,21 @@ def test_crlf(in_git_dir, patch_dir, crlf_before, crlf_after, autocrlf):
358358
assert_no_diff()
359359

360360

361+
@pytest.mark.parametrize('autocrlf', ('true', 'input'))
362+
def test_crlf_diff_only(in_git_dir, patch_dir, autocrlf):
363+
# due to a quirk (?) in git -- a diff only in crlf does not show but
364+
# still results in an exit code of `1`
365+
# we treat this as "no diff" -- though ideally it would discard the diff
366+
# while committing
367+
cmd_output('git', 'config', '--local', 'core.autocrlf', autocrlf)
368+
369+
_write(b'1\r\n2\r\n3\r\n')
370+
cmd_output('git', 'add', 'foo')
371+
_write(b'1\n2\n3\n')
372+
with staged_files_only(patch_dir):
373+
pass
374+
375+
361376
def test_whitespace_errors(in_git_dir, patch_dir):
362377
cmd_output('git', 'config', '--local', 'apply.whitespace', 'error')
363378
test_crlf(in_git_dir, patch_dir, True, True, 'true')

0 commit comments

Comments
 (0)