summaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli/format/text.py
blob: 6dd4511896521e35e6902390a1299fe2ca324bab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Ensure text files have the proper line endings.
"""
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.
    """
    # 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)