From 466f9e173285bbdc90379d8878d50a753582505d Mon Sep 17 00:00:00 2001 From: Morgan Courbet Date: Mon, 26 Jun 2017 23:06:57 +0200 Subject: [PATCH] Add line ending detection --- pre_commit_hooks/mixed_line_ending.py | 53 +++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/pre_commit_hooks/mixed_line_ending.py b/pre_commit_hooks/mixed_line_ending.py index 0710cf8..7b6a4bf 100644 --- a/pre_commit_hooks/mixed_line_ending.py +++ b/pre_commit_hooks/mixed_line_ending.py @@ -1,4 +1,6 @@ import argparse +import os +import re import sys from enum import Enum @@ -10,6 +12,7 @@ class CLIOption(Enum): class LineEnding(CLIOption): + CR = '\r', '\\r', 'cr' CRLF = '\r\n', '\\r\\n', 'crlf' LF = '\n', '\\n', 'lf' @@ -26,11 +29,33 @@ class MixedLineEndingOption(CLIOption): LF = LineEnding.LF.optName +class MixedLineDetection(Enum): + MIXED_MOSTLY_CRLF = True, LineEnding.CRLF.string + MIXED_MOSTLY_LF = True, LineEnding.LF.string + NOT_MIXED = False, None + UNKNOWN = False, None + + def __init__(self, conversion, line_ending_char): + self.conversion = conversion + self.line_ending_char = line_ending_char + + +# Matches CRLF +CRLF_PATTERN = re.compile(LineEnding.CRLF.string, re.DOTALL) +# Matches LF (without preceding CR) +LF_PATTERN = re.compile('(? 0 + lf_found = lf_nb > 0 + + if crlf_nb == lf_nb: + return MixedLineDetection.UNKNOWN + + if crlf_found ^ lf_found: + return MixedLineDetection.NOT_MIXED + + if crlf_nb > lf_nb: + return MixedLineDetection.MIXED_MOSTLY_CRLF + else: + return MixedLineDetection.MIXED_MOSTLY_LF + + if __name__ == '__main__': sys.exit(mixed_line_ending())