Make sure 'cformat' only runs on core files (#12909)

Co-authored-by: Zach White <skullydazed@drpepper.org>
This commit is contained in:
Erovia 2021-05-18 21:26:17 +02:00 committed by GitHub
parent 1c81e69503
commit 3023015c5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 7 deletions

View file

@ -34,7 +34,7 @@ jobs:
- name: Run qmk cformat and qmk pyformat - name: Run qmk cformat and qmk pyformat
shell: 'bash {0}' shell: 'bash {0}'
run: | run: |
qmk cformat -n $(< ~/files.txt) qmk cformat --core-only -n $(< ~/files.txt)
cformat_exit=$? cformat_exit=$?
qmk pyformat -n qmk pyformat -n
pyformat_exit=$? pyformat_exit=$?

View file

@ -65,11 +65,21 @@ def cformat_run(files):
return False return False
def filter_files(files): def filter_files(files, core_only=False):
"""Yield only files to be formatted and skip the rest """Yield only files to be formatted and skip the rest
""" """
if core_only:
# Filter non-core files
for index, file in enumerate(files):
# The following statement checks each file to see if the file path is
# - in the core directories
# - not in the ignored directories
if not any(i in str(file) for i in core_dirs) or any(i in str(file) for i in ignored):
files[index] = None
cli.log.debug("Skipping non-core file %s, as '--core-only' is used.", file)
for file in files: for file in files:
if file.name.split('.')[-1] in c_file_suffixes: if file and file.name.split('.')[-1] in c_file_suffixes:
yield file yield file
else: else:
cli.log.debug('Skipping file %s', file) cli.log.debug('Skipping file %s', file)
@ -78,6 +88,7 @@ def filter_files(files):
@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Flag only, don't automatically format.") @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Flag only, don't automatically format.")
@cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.') @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 core files.') @cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all core files.')
@cli.argument('--core-only', arg_only=True, action='store_true', help='Format core files only.')
@cli.argument('files', nargs='*', arg_only=True, type=normpath, completer=FilesCompleter('.c'), help='Filename(s) to format.') @cli.argument('files', nargs='*', arg_only=True, type=normpath, completer=FilesCompleter('.c'), help='Filename(s) to format.')
@cli.subcommand("Format C code according to QMK's style.", hidden=False if cli.config.user.developer else True) @cli.subcommand("Format C code according to QMK's style.", hidden=False if cli.config.user.developer else True)
def cformat(cli): def cformat(cli):
@ -85,7 +96,7 @@ def cformat(cli):
""" """
# Find the list of files to format # Find the list of files to format
if cli.args.files: if cli.args.files:
files = list(filter_files(cli.args.files)) files = list(filter_files(cli.args.files, cli.args.core_only))
if not files: if not files:
cli.log.error('No C files in filelist: %s', ', '.join(map(str, cli.args.files))) cli.log.error('No C files in filelist: %s', ', '.join(map(str, cli.args.files)))
@ -96,8 +107,7 @@ def cformat(cli):
elif cli.args.all_files: elif cli.args.all_files:
all_files = c_source_files(core_dirs) all_files = c_source_files(core_dirs)
# The following statement checks each file to see if the file path is in the ignored directories. files = list(filter_files(all_files, True))
files = [file for file in all_files if not any(i in str(file) for i in ignored)]
else: else:
git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch, *core_dirs] git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch, *core_dirs]
@ -117,7 +127,7 @@ def cformat(cli):
# Sanity check # Sanity check
if not files: if not files:
cli.log.error('No changed files detected. Use "qmk cformat -a" to format all files') cli.log.error('No changed files detected. Use "qmk cformat -a" to format all core files')
return False return False
# Run clang-format on the files we've found # Run clang-format on the files we've found