From ee95d53a1d499252ddd9841fc42a39a43c64c115 Mon Sep 17 00:00:00 2001 From: zvecr Date: Sun, 14 Nov 2021 21:57:14 +0000 Subject: [PATCH 1/3] Partially reinstate CI formatting process --- .github/workflows/format.yaml | 21 ++++++---- lib/python/qmk/cli/format/text.py | 64 +++++++++++++++++++++++-------- 2 files changed, 61 insertions(+), 24 deletions(-) diff --git a/.github/workflows/format.yaml b/.github/workflows/format.yaml index 0bb87010684f..3c3253c24cc1 100644 --- a/.github/workflows/format.yaml +++ b/.github/workflows/format.yaml @@ -19,7 +19,9 @@ jobs: container: qmkfm/qmk_cli steps: - - uses: rlespinasse/github-slug-action@v3.x + - name: Install dependencies + run: | + apt-get update && apt-get install -y dos2unix - uses: actions/checkout@v2 with: @@ -31,12 +33,17 @@ jobs: output: ' ' fileOutput: ' ' - - name: Run qmk format-c and qmk format-python + - name: Run qmk formatters shell: 'bash {0}' run: | - qmk format-c --core-only -n $(< ~/files.txt) - format_c_exit=$? - qmk format-python -n - format_python_exit=$? + qmk format-c --core-only $(< ~/files.txt) + qmk format-python + qmk format-text $(< ~/files.txt) + git diff - exit $((format_c_exit + format_python_exit)) + - name: Fail when formatting required + run: | + for file in $(git diff --name-only); do + echo "::error file=${file}::::File Requires Formatting" + done + test -n "$(git status --porcelain)" && exit 1 diff --git a/lib/python/qmk/cli/format/text.py b/lib/python/qmk/cli/format/text.py index e7e07b7297e2..6dd451189652 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) From 86b78b8865477ce394b2e2087dece7c360b77057 Mon Sep 17 00:00:00 2001 From: zvecr Date: Sun, 14 Nov 2021 22:21:50 +0000 Subject: [PATCH 2/3] Fix exit code on clean run --- .github/workflows/format.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/format.yaml b/.github/workflows/format.yaml index 3c3253c24cc1..4de829b71b24 100644 --- a/.github/workflows/format.yaml +++ b/.github/workflows/format.yaml @@ -46,4 +46,4 @@ jobs: for file in $(git diff --name-only); do echo "::error file=${file}::::File Requires Formatting" done - test -n "$(git status --porcelain)" && exit 1 + test -n "$(git diff --name-only)" && exit 1 From db67f41d43da705d9cd8eb1a8182e051f5809d2b Mon Sep 17 00:00:00 2001 From: zvecr Date: Sun, 14 Nov 2021 22:26:48 +0000 Subject: [PATCH 3/3] Fix exit code on clean run --- .github/workflows/format.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/format.yaml b/.github/workflows/format.yaml index 4de829b71b24..9e165e0e8df6 100644 --- a/.github/workflows/format.yaml +++ b/.github/workflows/format.yaml @@ -46,4 +46,4 @@ jobs: for file in $(git diff --name-only); do echo "::error file=${file}::::File Requires Formatting" done - test -n "$(git diff --name-only)" && exit 1 + test -z "$(git diff --name-only)"