|
@@ -1,16 +1,14 @@
|
|
|
"""Format C code according to QMK's style.
|
|
|
"""
|
|
|
-import os
|
|
|
import subprocess
|
|
|
from shutil import which
|
|
|
|
|
|
from milc import cli
|
|
|
+import qmk.path
|
|
|
|
|
|
|
|
|
-@cli.argument('files', nargs='*', arg_only=True, help='Filename(s) to format.')
|
|
|
-@cli.subcommand("Format C code according to QMK's style.")
|
|
|
-def cformat(cli):
|
|
|
- """Format C code according to QMK's style.
|
|
|
+def cformat_run(files, all_files):
|
|
|
+ """Spawn clang-format subprocess with proper arguments
|
|
|
"""
|
|
|
|
|
|
clang_format = ['clang-format', '-i']
|
|
@@ -19,27 +17,48 @@ def cformat(cli):
|
|
|
if which(binary):
|
|
|
clang_format[0] = binary
|
|
|
break
|
|
|
-
|
|
|
-
|
|
|
- if cli.args.files:
|
|
|
- cli.args.files = [os.path.join(os.environ['ORIG_CWD'], file) for file in cli.args.files]
|
|
|
- else:
|
|
|
- ignores = ['tmk_core/protocol/usb_hid', 'quantum/template']
|
|
|
- for dir in ['drivers', 'quantum', 'tests', 'tmk_core']:
|
|
|
- for dirpath, dirnames, filenames in os.walk(dir):
|
|
|
- if any(i in dirpath for i in ignores):
|
|
|
- dirnames.clear()
|
|
|
- continue
|
|
|
-
|
|
|
- for name in filenames:
|
|
|
- if name.endswith(('.c', '.h', '.cpp')):
|
|
|
- cli.args.files.append(os.path.join(dirpath, name))
|
|
|
-
|
|
|
-
|
|
|
try:
|
|
|
- subprocess.run(clang_format + cli.args.files, check=True)
|
|
|
+ if not files:
|
|
|
+ cli.log.warn('No changes detected. Use "qmk cformat -a" to format all files')
|
|
|
+ return False
|
|
|
+ if files and all_files:
|
|
|
+ cli.log.warning('Filenames passed with -a, only formatting: %s', ','.join(cli.args.files))
|
|
|
+
|
|
|
+ subprocess.run(clang_format + [str(file) for file in files], check=True)
|
|
|
cli.log.info('Successfully formatted the C code.')
|
|
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
cli.log.error('Error formatting C code!')
|
|
|
return False
|
|
|
+
|
|
|
+
|
|
|
+@cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all core files.')
|
|
|
+@cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.')
|
|
|
+@cli.argument('files', nargs='*', arg_only=True, help='Filename(s) to format.')
|
|
|
+@cli.subcommand("Format C code according to QMK's style.")
|
|
|
+def cformat(cli):
|
|
|
+ """Format C code according to QMK's style.
|
|
|
+ """
|
|
|
+
|
|
|
+ files = []
|
|
|
+
|
|
|
+ core_dirs = ['drivers', 'quantum', 'tests', 'tmk_core']
|
|
|
+ ignores = ['tmk_core/protocol/usb_hid', 'quantum/template']
|
|
|
+
|
|
|
+ if cli.args.files:
|
|
|
+ files.extend(qmk.path.normpath(file) for file in cli.args.files)
|
|
|
+
|
|
|
+ elif cli.args.all_files:
|
|
|
+ all_files = qmk.path.c_source_files(core_dirs)
|
|
|
+
|
|
|
+ files.extend(file for file in all_files if not any(i in str(file) for i in ignores))
|
|
|
+
|
|
|
+ else:
|
|
|
+ base_args = ['git', 'diff', '--name-only', cli.args.base_branch]
|
|
|
+ out = subprocess.run(base_args + core_dirs, check=True, stdout=subprocess.PIPE)
|
|
|
+ changed_files = filter(None, out.stdout.decode('UTF-8').split('\n'))
|
|
|
+ filtered_files = [qmk.path.normpath(file) for file in changed_files if not any(i in file for i in ignores)]
|
|
|
+ files.extend(file for file in filtered_files if file.exists() and file.suffix in ['.c', '.h', '.cpp'])
|
|
|
+
|
|
|
+
|
|
|
+ cformat_run(files, cli.args.all_files)
|