From 4fc9624b6a0e3e9d3f9a9ce8a6eff55b4719f8b7 Mon Sep 17 00:00:00 2001 From: Morgan Courbet Date: Mon, 10 Jul 2017 21:27:09 +0200 Subject: [PATCH] Refactor _detect_line_ending --- pre_commit_hooks/mixed_line_ending.py | 42 +++++++++++++++++---------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/pre_commit_hooks/mixed_line_ending.py b/pre_commit_hooks/mixed_line_ending.py index 98b532a..16cac56 100644 --- a/pre_commit_hooks/mixed_line_ending.py +++ b/pre_commit_hooks/mixed_line_ending.py @@ -8,7 +8,7 @@ from enum import 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) LF = b'\n', '\\n', 'lf', re.compile(b'(? 0 - lf_found = lf_nb > 0 + logging.debug('_detect_line_ending: le_counts = ' + str(le_counts)) - logging.debug('_detect_line_ending: crlf_nb = %d, lf_nb = %d, ' - 'crlf_found = %s, lf_found = %s', - crlf_nb, lf_nb, crlf_found, lf_found) + mixed = False + le_found_previously = False + most_le = None + max_le_count = 0 - if crlf_nb == lf_nb: - return MixedLineDetection.UNKNOWN + for le, le_count in le_counts.items(): + 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 - if crlf_nb > lf_nb: - return MixedLineDetection.MIXED_MOSTLY_CRLF - else: - return MixedLineDetection.MIXED_MOSTLY_LF + 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):