diff options
| author | QMK Bot <hello@qmk.fm> | 2021-11-14 23:06:50 +0000 |
|---|---|---|
| committer | QMK Bot <hello@qmk.fm> | 2021-11-14 23:06:50 +0000 |
| commit | 462c3a615113e84ac3ca837a5caeb928c0ec8505 (patch) | |
| tree | e7e380167da9131d71c367f5e94015687ac2f923 /lib | |
| parent | f7536f3adfa10d2d5a8eaf89004de00a6ad46581 (diff) | |
| parent | ec0bb791f89c1ba53a9dcc3ca02ec6ad9823d2a1 (diff) | |
| download | qmk_firmware-462c3a615113e84ac3ca837a5caeb928c0ec8505.tar.gz qmk_firmware-462c3a615113e84ac3ca837a5caeb928c0ec8505.zip | |
Merge remote-tracking branch 'origin/master' into develop
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/python/qmk/cli/format/text.py | 64 |
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 e7e07b729..6dd451189 100644 --- a/lib/python/qmk/cli/format/text.py +++ b/lib/python/qmk/cli/format/text.py | |||
| @@ -1,27 +1,57 @@ | |||
| 1 | """Ensure text files have the proper line endings. | 1 | """Ensure text files have the proper line endings. |
| 2 | """ | 2 | """ |
| 3 | from subprocess import CalledProcessError | 3 | from itertools import islice |
| 4 | from subprocess import DEVNULL | ||
| 4 | 5 | ||
| 5 | from milc import cli | 6 | from milc import cli |
| 6 | 7 | ||
| 8 | from qmk.path import normpath | ||
| 7 | 9 | ||
| 10 | |||
| 11 | def _get_chunks(it, size): | ||
| 12 | """Break down a collection into smaller parts | ||
| 13 | """ | ||
| 14 | it = iter(it) | ||
| 15 | return iter(lambda: tuple(islice(it, size)), ()) | ||
| 16 | |||
| 17 | |||
| 18 | def dos2unix_run(files): | ||
| 19 | """Spawn multiple dos2unix subprocess avoiding too long commands on formatting everything | ||
| 20 | """ | ||
| 21 | for chunk in _get_chunks(files, 10): | ||
| 22 | dos2unix = cli.run(['dos2unix', *chunk]) | ||
| 23 | |||
| 24 | if dos2unix.returncode: | ||
| 25 | return False | ||
| 26 | |||
| 27 | |||
| 28 | @cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.') | ||
| 29 | @cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all files.') | ||
| 30 | @cli.argument('files', nargs='*', arg_only=True, type=normpath, help='Filename(s) to format.') | ||
| 8 | @cli.subcommand("Ensure text files have the proper line endings.", hidden=True) | 31 | @cli.subcommand("Ensure text files have the proper line endings.", hidden=True) |
| 9 | def format_text(cli): | 32 | def format_text(cli): |
| 10 | """Ensure text files have the proper line endings. | 33 | """Ensure text files have the proper line endings. |
| 11 | """ | 34 | """ |
| 12 | try: | 35 | # Find the list of files to format |
| 13 | file_list_cmd = cli.run(['git', 'ls-files', '-z'], check=True) | 36 | if cli.args.files: |
| 14 | except CalledProcessError as e: | 37 | files = list(cli.args.files) |
| 15 | cli.log.error('Could not get file list: %s', e) | 38 | |
| 16 | exit(1) | 39 | if cli.args.all_files: |
| 17 | except Exception as e: | 40 | cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files))) |
| 18 | cli.log.error('Unhandled exception: %s: %s', e.__class__.__name__, e) | 41 | |
| 19 | cli.log.exception(e) | 42 | elif cli.args.all_files: |
| 20 | exit(1) | 43 | git_ls_cmd = ['git', 'ls-files'] |
| 21 | 44 | git_ls = cli.run(git_ls_cmd, stdin=DEVNULL) | |
| 22 | dos2unix = cli.run(['xargs', '-0', 'dos2unix'], stdin=None, input=file_list_cmd.stdout) | 45 | files = list(filter(None, git_ls.stdout.split('\n'))) |
| 23 | 46 | ||
| 24 | if dos2unix.returncode != 0: | 47 | else: |
| 25 | print(dos2unix.stderr) | 48 | git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch] |
| 26 | 49 | git_diff = cli.run(git_diff_cmd, stdin=DEVNULL) | |
| 27 | return dos2unix.returncode | 50 | files = list(filter(None, git_diff.stdout.split('\n'))) |
| 51 | |||
| 52 | # Sanity check | ||
| 53 | if not files: | ||
| 54 | cli.log.error('No changed files detected. Use "qmk format-text -a" to format all files') | ||
| 55 | return False | ||
| 56 | |||
| 57 | return dos2unix_run(files) | ||
