mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-06 20:16:53 +00:00
Change files according to --fix option
This commit is contained in:
parent
aaf134c2bc
commit
0a8b929f07
1 changed files with 24 additions and 9 deletions
|
|
@ -24,10 +24,14 @@ class LineEnding(CLIOption):
|
||||||
|
|
||||||
|
|
||||||
class MixedLineEndingOption(CLIOption):
|
class MixedLineEndingOption(CLIOption):
|
||||||
AUTO = 'auto'
|
AUTO = 'auto', None
|
||||||
NO = 'no'
|
NO = 'no', None
|
||||||
CRLF = LineEnding.CRLF.optName
|
CRLF = LineEnding.CRLF.optName, LineEnding.CRLF
|
||||||
LF = LineEnding.LF.optName
|
LF = LineEnding.LF.optName, LineEnding.LF
|
||||||
|
|
||||||
|
def __init__(self, opt_name, line_ending_enum):
|
||||||
|
self.opt_name = opt_name
|
||||||
|
self.line_ending_enum = line_ending_enum
|
||||||
|
|
||||||
|
|
||||||
class MixedLineDetection(Enum):
|
class MixedLineDetection(Enum):
|
||||||
|
|
@ -61,11 +65,22 @@ def mixed_line_ending(argv=None):
|
||||||
|
|
||||||
_check_filenames(options['filenames'])
|
_check_filenames(options['filenames'])
|
||||||
|
|
||||||
for filename in options['filenames']:
|
fix_option = options['fix']
|
||||||
detect_result = _detect_line_ending(filename)
|
|
||||||
|
|
||||||
if detect_result.conversion:
|
if fix_option == MixedLineEndingOption.NO:
|
||||||
_convert_line_ending(filename, detect_result.line_ending_char)
|
pass
|
||||||
|
elif fix_option == MixedLineEndingOption.AUTO:
|
||||||
|
for filename in options['filenames']:
|
||||||
|
detect_result = _detect_line_ending(filename)
|
||||||
|
|
||||||
|
if detect_result.conversion:
|
||||||
|
_convert_line_ending(filename, detect_result.line_ending_char)
|
||||||
|
# when a line ending character is forced with --fix option
|
||||||
|
else:
|
||||||
|
line_ending_enum = fix_option.line_ending_enum
|
||||||
|
|
||||||
|
for filename in options['filenames']:
|
||||||
|
_convert_line_ending(filename, line_ending_enum.string)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
@ -75,7 +90,7 @@ def _parse_arguments(argv=None):
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-f',
|
'-f',
|
||||||
'--fix',
|
'--fix',
|
||||||
choices=[m.optName for m in MixedLineEndingOption],
|
choices=[m.opt_name for m in MixedLineEndingOption],
|
||||||
default='auto',
|
default='auto',
|
||||||
help='Replace line ending with the specified. Default is "auto"')
|
help='Replace line ending with the specified. Default is "auto"')
|
||||||
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue