text.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """Ensure text files have the proper line endings.
  2. """
  3. from itertools import islice
  4. from subprocess import DEVNULL
  5. from milc import cli
  6. from qmk.path import normpath
  7. def _get_chunks(it, size):
  8. """Break down a collection into smaller parts
  9. """
  10. it = iter(it)
  11. return iter(lambda: tuple(islice(it, size)), ())
  12. def dos2unix_run(files):
  13. """Spawn multiple dos2unix subprocess avoiding too long commands on formatting everything
  14. """
  15. for chunk in _get_chunks(files, 10):
  16. dos2unix = cli.run(['dos2unix', *chunk])
  17. if dos2unix.returncode:
  18. return False
  19. @cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.')
  20. @cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all files.')
  21. @cli.argument('files', nargs='*', arg_only=True, type=normpath, help='Filename(s) to format.')
  22. @cli.subcommand("Ensure text files have the proper line endings.", hidden=True)
  23. def format_text(cli):
  24. """Ensure text files have the proper line endings.
  25. """
  26. # Find the list of files to format
  27. if cli.args.files:
  28. files = list(cli.args.files)
  29. if cli.args.all_files:
  30. cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files)))
  31. elif cli.args.all_files:
  32. git_ls_cmd = ['git', 'ls-files']
  33. git_ls = cli.run(git_ls_cmd, stdin=DEVNULL)
  34. files = list(filter(None, git_ls.stdout.split('\n')))
  35. else:
  36. git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch]
  37. git_diff = cli.run(git_diff_cmd, stdin=DEVNULL)
  38. files = list(filter(None, git_diff.stdout.split('\n')))
  39. # Sanity check
  40. if not files:
  41. cli.log.error('No changed files detected. Use "qmk format-text -a" to format all files')
  42. return False
  43. return dos2unix_run(files)