Skip to content

Commit bb8384f

Browse files
authored
Merge pull request #240 from pre-commit/mixed_line_ending_force
Fix mixed-line-endings --fix=... when whole file is a different ending
2 parents efdceb4 + 76047f6 commit bb8384f

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pre_commit_hooks/mixed_line_ending.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def fix_filename(filename, fix):
5555
else:
5656
target_ending = FIX_TO_LINE_ENDING[fix]
5757
# find if there are lines with *other* endings
58-
del counts[target_ending]
58+
# It's possible there's no line endings of the target type
59+
counts.pop(target_ending, None)
5960
other_endings = bool(sum(counts.values()))
6061
if other_endings:
6162
_fix(filename, contents, target_ending)

tests/mixed_line_ending_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,13 @@ def test_fix_crlf(tmpdir):
101101

102102
assert ret == 1
103103
assert path.read_binary() == b'foo\r\nbar\r\nbaz\r\n'
104+
105+
106+
def test_fix_lf_all_crlf(tmpdir):
107+
"""Regression test for #239"""
108+
path = tmpdir.join('input.txt')
109+
path.write_binary(b'foo\r\nbar\r\n')
110+
ret = main(('--fix=lf', path.strpath))
111+
112+
assert ret == 1
113+
assert path.read_binary() == b'foo\nbar\n'

0 commit comments

Comments
 (0)