mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-06-28 21:50:46 +00:00
use auto detected newline instead of hardcoded \n for line splitting
This commit is contained in:
parent
fa6b006f0e
commit
56201ac0ed
2 changed files with 11 additions and 3 deletions
|
|
@ -29,18 +29,21 @@ def sort_file_contents(
|
|||
unique: bool = False,
|
||||
) -> int:
|
||||
before = list(f)
|
||||
# detect the line ending style of the file, based on the first line
|
||||
new_line = b'\r\n' if before and before[0].endswith(b'\r\n') else b'\n'
|
||||
|
||||
lines: Iterable[bytes] = (
|
||||
line.rstrip(b'\n\r') for line in before if line.strip()
|
||||
line.rstrip(new_line) for line in before if line.strip()
|
||||
)
|
||||
if unique:
|
||||
lines = set(lines)
|
||||
after = sorted(lines, key=key)
|
||||
|
||||
before_string = b''.join(before)
|
||||
after_string = b'\n'.join(after)
|
||||
after_string = new_line.join(after)
|
||||
|
||||
if after_string:
|
||||
after_string += b'\n'
|
||||
after_string += new_line
|
||||
|
||||
if before_string == after_string:
|
||||
return PASS
|
||||
|
|
|
|||
|
|
@ -12,11 +12,14 @@ from pre_commit_hooks.file_contents_sorter import PASS
|
|||
(
|
||||
(b'', [], PASS, b''),
|
||||
(b'\n', [], FAIL, b''),
|
||||
(b'\r\n', [], FAIL, b''),
|
||||
(b'\n\n', [], FAIL, b''),
|
||||
(b'lonesome\n', [], PASS, b'lonesome\n'),
|
||||
(b'missing_newline', [], FAIL, b'missing_newline\n'),
|
||||
(b'newline\nmissing', [], FAIL, b'missing\nnewline\n'),
|
||||
(b'missing\nnewline', [], FAIL, b'missing\nnewline\n'),
|
||||
(b'newline\r\nmissing', [], FAIL, b'missing\r\nnewline\r\n'),
|
||||
(b'missing\r\nnewline', [], FAIL, b'missing\r\nnewline\r\n'),
|
||||
(b'alpha\nbeta\n', [], PASS, b'alpha\nbeta\n'),
|
||||
(b'beta\nalpha\n', [], FAIL, b'alpha\nbeta\n'),
|
||||
(b'C\nc\n', [], PASS, b'C\nc\n'),
|
||||
|
|
@ -25,6 +28,8 @@ from pre_commit_hooks.file_contents_sorter import PASS
|
|||
(b'@\n-\n_\n#\n', [], FAIL, b'#\n-\n@\n_\n'),
|
||||
(b'extra\n\n\nwhitespace\n', [], FAIL, b'extra\nwhitespace\n'),
|
||||
(b'whitespace\n\n\nextra\n', [], FAIL, b'extra\nwhitespace\n'),
|
||||
(b'on\r\nwindows\r\n', [], PASS, b'on\r\nwindows\r\n'),
|
||||
(b'extra\r\n\r\n\r\nwindows\r\n', [], FAIL, b'extra\r\nwindows\r\n'),
|
||||
(
|
||||
b'fee\nFie\nFoe\nfum\n',
|
||||
[],
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue