summaryrefslogtreecommitdiffstats
path: root/lib/python
diff options
context:
space:
mode:
authorQMK Bot <hello@qmk.fm>2021-11-14 23:06:50 +0000
committerQMK Bot <hello@qmk.fm>2021-11-14 23:06:50 +0000
commit462c3a615113e84ac3ca837a5caeb928c0ec8505 (patch)
treee7e380167da9131d71c367f5e94015687ac2f923 /lib/python
parentf7536f3adfa10d2d5a8eaf89004de00a6ad46581 (diff)
parentec0bb791f89c1ba53a9dcc3ca02ec6ad9823d2a1 (diff)
Merge remote-tracking branch 'origin/master' into develop
Diffstat (limited to 'lib/python')
-rw-r--r--lib/python/qmk/cli/format/text.py64
1 files changed, 47 insertions, 17 deletions
diff --git a/lib/python/qmk/cli/format/text.py b/lib/python/qmk/cli/format/text.py
index e7e07b7297..6dd4511896 100644
--- a/lib/python/qmk/cli/format/text.py
+++ b/lib/python/qmk/cli/format/text.py
@@ -1,27 +1,57 @@
"""Ensure text files have the proper line endings.
"""
-from subprocess import CalledProcessError
+from itertools import islice
+from subprocess import DEVNULL
from milc import cli
+from qmk.path import normpath
+
+def _get_chunks(it, size):
+ """Break down a collection into smaller parts
+ """
+ it = iter(it)
+ return iter(lambda: tuple(islice(it, size)), ())
+
+
+def dos2unix_run(files):
+ """Spawn multiple dos2unix subprocess avoiding too long commands on formatting everything
+ """
+ for chunk in _get_chunks(files, 10):
+ 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.')
@cli.subcommand("Ensure text files have the proper line endings.", hidden=True)
def format_text(cli):
"""Ensure text files have the proper line endings.
"""
- try:
- file_list_cmd = cli.run(['git', 'ls-files', '-z'], check=True)
- except CalledProcessError as e:
- cli.log.error('Could not get file list: %s', e)
- exit(1)
- except Exception as e:
- cli.log.error('Unhandled exception: %s: %s', e.__class__.__name__, e)
- cli.log.exception(e)
- exit(1)
-
- dos2unix = cli.run(['xargs', '-0', 'dos2unix'], stdin=None, input=file_list_cmd.stdout)
-
- if dos2unix.returncode != 0:
- print(dos2unix.stderr)
-
- return dos2unix.returncode
+ # 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)