2021-07-20 18:52:14 +00:00
|
|
|
"""Ensure text files have the proper line endings.
|
|
|
|
"""
|
2021-11-14 23:06:22 +00:00
|
|
|
from subprocess import DEVNULL
|
2021-07-20 18:52:14 +00:00
|
|
|
|
|
|
|
from milc import cli
|
|
|
|
|
2021-11-14 23:06:22 +00:00
|
|
|
from qmk.path import normpath
|
2022-03-15 17:59:12 +00:00
|
|
|
from qmk.commands import get_chunks
|
2021-11-14 23:06:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def dos2unix_run(files):
|
|
|
|
"""Spawn multiple dos2unix subprocess avoiding too long commands on formatting everything
|
|
|
|
"""
|
2022-03-15 17:59:12 +00:00
|
|
|
for chunk in get_chunks(files, 10):
|
2021-11-14 23:06:22 +00:00
|
|
|
dos2unix = cli.run(['dos2unix', *chunk])
|
|
|
|
|
|
|
|
if dos2unix.returncode:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
@cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.')
|
|
|
|
@cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all files.')
|
|
|
|
@cli.argument('files', nargs='*', arg_only=True, type=normpath, help='Filename(s) to format.')
|
2021-07-20 18:52:14 +00:00
|
|
|
@cli.subcommand("Ensure text files have the proper line endings.", hidden=True)
|
|
|
|
def format_text(cli):
|
|
|
|
"""Ensure text files have the proper line endings.
|
|
|
|
"""
|
2021-11-14 23:06:22 +00:00
|
|
|
# Find the list of files to format
|
|
|
|
if cli.args.files:
|
|
|
|
files = list(cli.args.files)
|
|
|
|
|
|
|
|
if cli.args.all_files:
|
|
|
|
cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files)))
|
|
|
|
|
|
|
|
elif cli.args.all_files:
|
|
|
|
git_ls_cmd = ['git', 'ls-files']
|
|
|
|
git_ls = cli.run(git_ls_cmd, stdin=DEVNULL)
|
|
|
|
files = list(filter(None, git_ls.stdout.split('\n')))
|
|
|
|
|
|
|
|
else:
|
|
|
|
git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch]
|
|
|
|
git_diff = cli.run(git_diff_cmd, stdin=DEVNULL)
|
|
|
|
files = list(filter(None, git_diff.stdout.split('\n')))
|
|
|
|
|
|
|
|
# Sanity check
|
|
|
|
if not files:
|
|
|
|
cli.log.error('No changed files detected. Use "qmk format-text -a" to format all files')
|
|
|
|
return False
|
|
|
|
|
|
|
|
return dos2unix_run(files)
|