2019-08-22 19:18:52 +02:00
|
|
|
"""Format C code according to QMK's style.
|
|
|
|
"""
|
2021-05-10 20:18:44 +02:00
|
|
|
from os import path
|
2019-11-13 02:17:12 +01:00
|
|
|
from shutil import which
|
2021-05-20 00:24:46 +02:00
|
|
|
from subprocess import CalledProcessError, DEVNULL, Popen, PIPE
|
2019-08-22 19:18:52 +02:00
|
|
|
|
2021-04-15 04:00:22 +02:00
|
|
|
from argcomplete.completers import FilesCompleter
|
2019-08-22 19:18:52 +02:00
|
|
|
from milc import cli
|
2020-05-26 22:05:41 +02:00
|
|
|
|
|
|
|
from qmk.path import normpath
|
|
|
|
from qmk.c_parse import c_source_files
|
2019-08-22 19:18:52 +02:00
|
|
|
|
2021-05-10 20:18:44 +02:00
|
|
|
c_file_suffixes = ('c', 'h', 'cpp')
|
|
|
|
core_dirs = ('drivers', 'quantum', 'tests', 'tmk_core', 'platforms')
|
|
|
|
ignored = ('tmk_core/protocol/usb_hid', 'quantum/template', 'platforms/chibios')
|
2019-08-22 19:18:52 +02:00
|
|
|
|
2021-05-10 20:18:44 +02:00
|
|
|
|
|
|
|
def find_clang_format():
|
|
|
|
"""Returns the path to clang-format.
|
2019-08-22 19:18:52 +02:00
|
|
|
"""
|
2021-03-31 01:16:14 +02:00
|
|
|
for clang_version in range(20, 6, -1):
|
2021-05-10 20:18:44 +02:00
|
|
|
binary = f'clang-format-{clang_version}'
|
|
|
|
|
2019-11-13 02:17:12 +01:00
|
|
|
if which(binary):
|
2021-05-10 20:18:44 +02:00
|
|
|
return binary
|
|
|
|
|
|
|
|
return 'clang-format'
|
|
|
|
|
|
|
|
|
|
|
|
def find_diffs(files):
|
|
|
|
"""Run clang-format and diff it against a file.
|
|
|
|
"""
|
|
|
|
found_diffs = False
|
|
|
|
|
|
|
|
for file in files:
|
|
|
|
cli.log.debug('Checking for changes in %s', file)
|
2021-05-20 00:24:46 +02:00
|
|
|
clang_format = Popen([find_clang_format(), file], stdout=PIPE, stderr=PIPE, universal_newlines=True)
|
2021-05-10 20:18:44 +02:00
|
|
|
diff = cli.run(['diff', '-u', f'--label=a/{file}', f'--label=b/{file}', str(file), '-'], stdin=clang_format.stdout, capture_output=True)
|
|
|
|
|
|
|
|
if diff.returncode != 0:
|
|
|
|
print(diff.stdout)
|
|
|
|
found_diffs = True
|
|
|
|
|
|
|
|
return found_diffs
|
|
|
|
|
|
|
|
|
|
|
|
def cformat_run(files):
|
|
|
|
"""Spawn clang-format subprocess with proper arguments
|
|
|
|
"""
|
|
|
|
# Determine which version of clang-format to use
|
|
|
|
clang_format = [find_clang_format(), '-i']
|
|
|
|
|
2019-08-22 19:18:52 +02:00
|
|
|
try:
|
2021-05-20 00:24:46 +02:00
|
|
|
cli.run([*clang_format, *map(str, files)], check=True, capture_output=False, stdin=DEVNULL)
|
2019-08-22 19:18:52 +02:00
|
|
|
cli.log.info('Successfully formatted the C code.')
|
2021-05-10 20:18:44 +02:00
|
|
|
return True
|
2019-08-22 22:33:34 +02:00
|
|
|
|
2021-05-20 00:24:46 +02:00
|
|
|
except CalledProcessError as e:
|
2019-08-22 19:18:52 +02:00
|
|
|
cli.log.error('Error formatting C code!')
|
2021-05-10 20:18:44 +02:00
|
|
|
cli.log.debug('%s exited with returncode %s', e.cmd, e.returncode)
|
|
|
|
cli.log.debug('STDOUT:')
|
|
|
|
cli.log.debug(e.stdout)
|
|
|
|
cli.log.debug('STDERR:')
|
|
|
|
cli.log.debug(e.stderr)
|
2019-08-22 22:30:50 +02:00
|
|
|
return False
|
2020-02-23 05:57:19 +01:00
|
|
|
|
|
|
|
|
2021-05-18 21:26:17 +02:00
|
|
|
def filter_files(files, core_only=False):
|
2021-05-10 20:18:44 +02:00
|
|
|
"""Yield only files to be formatted and skip the rest
|
|
|
|
"""
|
2021-05-18 21:26:17 +02:00
|
|
|
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)
|
|
|
|
|
2021-05-10 20:18:44 +02:00
|
|
|
for file in files:
|
2021-05-18 21:26:17 +02:00
|
|
|
if file and file.name.split('.')[-1] in c_file_suffixes:
|
2021-05-10 20:18:44 +02:00
|
|
|
yield file
|
|
|
|
else:
|
|
|
|
cli.log.debug('Skipping file %s', file)
|
|
|
|
|
|
|
|
|
|
|
|
@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Flag only, don't automatically format.")
|
2020-02-23 05:57:19 +01:00
|
|
|
@cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.')
|
2021-05-10 20:18:44 +02:00
|
|
|
@cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all core files.')
|
2021-05-18 21:26:17 +02:00
|
|
|
@cli.argument('--core-only', arg_only=True, action='store_true', help='Format core files only.')
|
2021-05-10 20:18:44 +02:00
|
|
|
@cli.argument('files', nargs='*', arg_only=True, type=normpath, completer=FilesCompleter('.c'), help='Filename(s) to format.')
|
2020-03-23 21:59:44 +01:00
|
|
|
@cli.subcommand("Format C code according to QMK's style.", hidden=False if cli.config.user.developer else True)
|
2020-02-23 05:57:19 +01:00
|
|
|
def cformat(cli):
|
|
|
|
"""Format C code according to QMK's style.
|
|
|
|
"""
|
|
|
|
# Find the list of files to format
|
|
|
|
if cli.args.files:
|
2021-05-18 21:26:17 +02:00
|
|
|
files = list(filter_files(cli.args.files, cli.args.core_only))
|
2021-05-10 20:18:44 +02:00
|
|
|
|
|
|
|
if not files:
|
|
|
|
cli.log.error('No C files in filelist: %s', ', '.join(map(str, cli.args.files)))
|
|
|
|
exit(0)
|
|
|
|
|
2020-12-10 20:02:41 +01:00
|
|
|
if cli.args.all_files:
|
|
|
|
cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(map(str, files)))
|
2021-05-10 20:18:44 +02:00
|
|
|
|
2020-02-23 05:57:19 +01:00
|
|
|
elif cli.args.all_files:
|
2020-05-26 22:05:41 +02:00
|
|
|
all_files = c_source_files(core_dirs)
|
2021-05-18 21:26:17 +02:00
|
|
|
files = list(filter_files(all_files, True))
|
2021-05-10 20:18:44 +02:00
|
|
|
|
2020-02-23 05:57:19 +01:00
|
|
|
else:
|
2021-05-10 20:18:44 +02:00
|
|
|
git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch, *core_dirs]
|
2021-05-20 00:24:46 +02:00
|
|
|
git_diff = cli.run(git_diff_cmd, stdin=DEVNULL)
|
2021-05-10 20:18:44 +02:00
|
|
|
|
|
|
|
if git_diff.returncode != 0:
|
|
|
|
cli.log.error("Error running %s", git_diff_cmd)
|
|
|
|
print(git_diff.stderr)
|
|
|
|
return git_diff.returncode
|
|
|
|
|
|
|
|
files = []
|
|
|
|
|
|
|
|
for file in git_diff.stdout.strip().split('\n'):
|
|
|
|
if not any([file.startswith(ignore) for ignore in ignored]):
|
|
|
|
if path.exists(file) and file.split('.')[-1] in c_file_suffixes:
|
|
|
|
files.append(file)
|
|
|
|
|
|
|
|
# Sanity check
|
|
|
|
if not files:
|
2021-05-18 21:26:17 +02:00
|
|
|
cli.log.error('No changed files detected. Use "qmk cformat -a" to format all core files')
|
2021-05-10 20:18:44 +02:00
|
|
|
return False
|
2020-02-23 05:57:19 +01:00
|
|
|
|
|
|
|
# Run clang-format on the files we've found
|
2021-05-10 20:18:44 +02:00
|
|
|
if cli.args.dry_run:
|
|
|
|
return not find_diffs(files)
|
|
|
|
else:
|
|
|
|
return cformat_run(files)
|