mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-09 04:54:16 +00:00
Refactor _detect_line_ending
This commit is contained in:
parent
8bc4af45ba
commit
4fc9624b6a
1 changed files with 27 additions and 15 deletions
|
|
@ -8,7 +8,7 @@ from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
class LineEnding(Enum):
|
class LineEnding(Enum):
|
||||||
CR = b'\r', '\\r', 'cr', re.compile(b'\r', re.DOTALL)
|
CR = b'\r', '\\r', 'cr', re.compile(b'\r(?!\n)', re.DOTALL)
|
||||||
CRLF = b'\r\n', '\\r\\n', 'crlf', re.compile(b'\r\n', re.DOTALL)
|
CRLF = b'\r\n', '\\r\\n', 'crlf', re.compile(b'\r\n', re.DOTALL)
|
||||||
LF = b'\n', '\\n', 'lf', re.compile(b'(?<!\r)\n', re.DOTALL)
|
LF = b'\n', '\\n', 'lf', re.compile(b'(?<!\r)\n', re.DOTALL)
|
||||||
|
|
||||||
|
|
@ -132,26 +132,38 @@ def _detect_line_ending(filename):
|
||||||
with open(filename, 'rb') as f:
|
with open(filename, 'rb') as f:
|
||||||
buf = f.read()
|
buf = f.read()
|
||||||
|
|
||||||
crlf_nb = len(LineEnding.CRLF.regex.findall(buf))
|
le_counts = {}
|
||||||
lf_nb = len(LineEnding.LF.regex.findall(buf))
|
for le_enum in LineEnding:
|
||||||
|
le_counts[le_enum] = len(le_enum.regex.findall(buf))
|
||||||
|
|
||||||
crlf_found = crlf_nb > 0
|
logging.debug('_detect_line_ending: le_counts = ' + str(le_counts))
|
||||||
lf_found = lf_nb > 0
|
|
||||||
|
|
||||||
logging.debug('_detect_line_ending: crlf_nb = %d, lf_nb = %d, '
|
mixed = False
|
||||||
'crlf_found = %s, lf_found = %s',
|
le_found_previously = False
|
||||||
crlf_nb, lf_nb, crlf_found, lf_found)
|
most_le = None
|
||||||
|
max_le_count = 0
|
||||||
|
|
||||||
if crlf_nb == lf_nb:
|
for le, le_count in le_counts.items():
|
||||||
return MixedLineDetection.UNKNOWN
|
le_found_cur = le_count > 0
|
||||||
|
|
||||||
if crlf_found ^ lf_found:
|
mixed |= le_found_previously and le_found_cur
|
||||||
|
le_found_previously |= le_found_cur
|
||||||
|
|
||||||
|
if le_count == max_le_count:
|
||||||
|
most_le = None
|
||||||
|
elif le_count > max_le_count:
|
||||||
|
max_le_count = le_count
|
||||||
|
most_le = le
|
||||||
|
|
||||||
|
if not mixed:
|
||||||
return MixedLineDetection.NOT_MIXED
|
return MixedLineDetection.NOT_MIXED
|
||||||
|
|
||||||
if crlf_nb > lf_nb:
|
for mld in MixedLineDetection:
|
||||||
return MixedLineDetection.MIXED_MOSTLY_CRLF
|
if mld.line_ending_enum is not None \
|
||||||
else:
|
and mld.line_ending_enum == most_le:
|
||||||
return MixedLineDetection.MIXED_MOSTLY_LF
|
return mld
|
||||||
|
|
||||||
|
return MixedLineDetection.UNKNOWN
|
||||||
|
|
||||||
|
|
||||||
def _process_no_fix(filenames):
|
def _process_no_fix(filenames):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue