mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 20:16:53 +00:00
Refactor mixed_line_ending
This commit is contained in:
parent
a1ffbfaa59
commit
c6c4c4a2fa
1 changed files with 29 additions and 25 deletions
|
|
@ -76,31 +76,7 @@ def mixed_line_ending(argv=None):
|
||||||
if fix_option == MixedLineEndingOption.NO:
|
if fix_option == MixedLineEndingOption.NO:
|
||||||
return _process_no_fix(filenames)
|
return _process_no_fix(filenames)
|
||||||
elif fix_option == MixedLineEndingOption.AUTO:
|
elif fix_option == MixedLineEndingOption.AUTO:
|
||||||
for filename in options['filenames']:
|
return _process_fix_auto(filenames)
|
||||||
detect_result = _detect_line_ending(filename)
|
|
||||||
|
|
||||||
logging.debug('mixed_line_ending: detect_result = %s',
|
|
||||||
detect_result)
|
|
||||||
|
|
||||||
if detect_result.mle_found:
|
|
||||||
le_enum = detect_result.line_ending_enum
|
|
||||||
|
|
||||||
logging.info('The file %s has mixed line ending with a '
|
|
||||||
'majority of "%s". Converting to "%s"', filename,
|
|
||||||
le_enum.str_print, le_enum.str_print)
|
|
||||||
|
|
||||||
_convert_line_ending(filename, le_enum.string)
|
|
||||||
|
|
||||||
return 1
|
|
||||||
elif detect_result == MixedLineDetection.NOT_MIXED:
|
|
||||||
logging.info('The file %s has no mixed line ending', filename)
|
|
||||||
|
|
||||||
return 0
|
|
||||||
elif detect_result == MixedLineDetection.UNKNOWN:
|
|
||||||
logging.info('Could not define most frequent line ending in '
|
|
||||||
'file %s. File skiped.', filename)
|
|
||||||
|
|
||||||
return 0
|
|
||||||
|
|
||||||
# when a line ending character is forced with --fix option
|
# when a line ending character is forced with --fix option
|
||||||
else:
|
else:
|
||||||
|
|
@ -211,6 +187,34 @@ def _process_no_fix(filenames):
|
||||||
return 1 if mle_found else 0
|
return 1 if mle_found else 0
|
||||||
|
|
||||||
|
|
||||||
|
def _process_fix_auto(filenames):
|
||||||
|
for filename in filenames:
|
||||||
|
detect_result = _detect_line_ending(filename)
|
||||||
|
|
||||||
|
logging.debug('mixed_line_ending: detect_result = %s',
|
||||||
|
detect_result)
|
||||||
|
|
||||||
|
if detect_result.mle_found:
|
||||||
|
le_enum = detect_result.line_ending_enum
|
||||||
|
|
||||||
|
logging.info('The file %s has mixed line ending with a '
|
||||||
|
'majority of "%s". Converting to "%s"', filename,
|
||||||
|
le_enum.str_print, le_enum.str_print)
|
||||||
|
|
||||||
|
_convert_line_ending(filename, le_enum.string)
|
||||||
|
|
||||||
|
return 1
|
||||||
|
elif detect_result == MixedLineDetection.NOT_MIXED:
|
||||||
|
logging.info('The file %s has no mixed line ending', filename)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
elif detect_result == MixedLineDetection.UNKNOWN:
|
||||||
|
logging.info('Could not define most frequent line ending in '
|
||||||
|
'file %s. File skiped.', filename)
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def _convert_line_ending(filename, line_ending):
|
def _convert_line_ending(filename, line_ending):
|
||||||
# read the file
|
# read the file
|
||||||
fin = open(filename, 'r')
|
fin = open(filename, 'r')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue