mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 03:56:54 +00:00
Refactor mixed_line_ending.py
This commit is contained in:
parent
f9915cbbe2
commit
0e223bcb15
1 changed files with 25 additions and 24 deletions
|
|
@ -94,35 +94,38 @@ def _detect_line_ending(filename):
|
||||||
buf = f.read()
|
buf = f.read()
|
||||||
|
|
||||||
le_counts = {}
|
le_counts = {}
|
||||||
for le_enum in LineEnding:
|
|
||||||
le_counts[le_enum] = len(le_enum.regex.findall(buf))
|
|
||||||
|
|
||||||
mixed = False
|
for le_enum in LineEnding:
|
||||||
le_found_previously = False
|
le_counts[le_enum] = len(le_enum.regex.findall(buf))
|
||||||
most_le = None
|
|
||||||
max_le_count = 0
|
|
||||||
|
|
||||||
for le, le_count in le_counts.items():
|
mixed = False
|
||||||
le_found_cur = le_count > 0
|
le_found_previously = False
|
||||||
|
most_le = None
|
||||||
|
max_le_count = 0
|
||||||
|
|
||||||
mixed |= le_found_previously and le_found_cur
|
for le, le_count in le_counts.items():
|
||||||
le_found_previously |= le_found_cur
|
le_found_cur = le_count > 0
|
||||||
|
|
||||||
if le_count == max_le_count:
|
mixed |= le_found_previously and le_found_cur
|
||||||
most_le = None
|
le_found_previously |= le_found_cur
|
||||||
elif le_count > max_le_count:
|
|
||||||
max_le_count = le_count
|
|
||||||
most_le = le
|
|
||||||
|
|
||||||
if not mixed:
|
if le_count == max_le_count:
|
||||||
return MixedLineDetection.NOT_MIXED
|
most_le = None
|
||||||
|
elif le_count > max_le_count:
|
||||||
|
max_le_count = le_count
|
||||||
|
most_le = le
|
||||||
|
|
||||||
for mld in MixedLineDetection:
|
if not mixed:
|
||||||
if mld.line_ending_enum is not None \
|
return MixedLineDetection.NOT_MIXED
|
||||||
and mld.line_ending_enum == most_le:
|
|
||||||
return mld
|
|
||||||
|
|
||||||
return MixedLineDetection.UNKNOWN
|
for mld in MixedLineDetection:
|
||||||
|
if (
|
||||||
|
mld.line_ending_enum is not None and
|
||||||
|
mld.line_ending_enum == most_le
|
||||||
|
):
|
||||||
|
return mld
|
||||||
|
|
||||||
|
return MixedLineDetection.UNKNOWN
|
||||||
|
|
||||||
|
|
||||||
def _process_no_fix(filenames):
|
def _process_no_fix(filenames):
|
||||||
|
|
@ -154,8 +157,6 @@ def _process_fix_auto(filenames):
|
||||||
|
|
||||||
if detect_result == MixedLineDetection.NOT_MIXED:
|
if detect_result == MixedLineDetection.NOT_MIXED:
|
||||||
print('The file %s has no mixed line ending', filename)
|
print('The file %s has no mixed line ending', filename)
|
||||||
|
|
||||||
mle_found |= False
|
|
||||||
elif detect_result == MixedLineDetection.UNKNOWN:
|
elif detect_result == MixedLineDetection.UNKNOWN:
|
||||||
print(
|
print(
|
||||||
'Could not define most frequent line ending in '
|
'Could not define most frequent line ending in '
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue