aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli/cformat.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/cli/cformat.py')
-rw-r--r--lib/python/qmk/cli/cformat.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/lib/python/qmk/cli/cformat.py b/lib/python/qmk/cli/cformat.py
index 9333aaec4..15158d9c7 100644
--- a/lib/python/qmk/cli/cformat.py
+++ b/lib/python/qmk/cli/cformat.py
@@ -65,11 +65,21 @@ def cformat_run(files):
65 return False 65 return False
66 66
67 67
68def filter_files(files): 68def filter_files(files, core_only=False):
69 """Yield only files to be formatted and skip the rest 69 """Yield only files to be formatted and skip the rest
70 """ 70 """
71 if core_only:
72 # Filter non-core files
73 for index, file in enumerate(files):
74 # The following statement checks each file to see if the file path is
75 # - in the core directories
76 # - not in the ignored directories
77 if not any(i in str(file) for i in core_dirs) or any(i in str(file) for i in ignored):
78 files[index] = None
79 cli.log.debug("Skipping non-core file %s, as '--core-only' is used.", file)
80
71 for file in files: 81 for file in files:
72 if file.name.split('.')[-1] in c_file_suffixes: 82 if file and file.name.split('.')[-1] in c_file_suffixes:
73 yield file 83 yield file
74 else: 84 else:
75 cli.log.debug('Skipping file %s', file) 85 cli.log.debug('Skipping file %s', file)
@@ -78,6 +88,7 @@ def filter_files(files):
78@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Flag only, don't automatically format.") 88@cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Flag only, don't automatically format.")
79@cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.') 89@cli.argument('-b', '--base-branch', default='origin/master', help='Branch to compare to diffs to.')
80@cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all core files.') 90@cli.argument('-a', '--all-files', arg_only=True, action='store_true', help='Format all core files.')
91@cli.argument('--core-only', arg_only=True, action='store_true', help='Format core files only.')
81@cli.argument('files', nargs='*', arg_only=True, type=normpath, completer=FilesCompleter('.c'), help='Filename(s) to format.') 92@cli.argument('files', nargs='*', arg_only=True, type=normpath, completer=FilesCompleter('.c'), help='Filename(s) to format.')
82@cli.subcommand("Format C code according to QMK's style.", hidden=False if cli.config.user.developer else True) 93@cli.subcommand("Format C code according to QMK's style.", hidden=False if cli.config.user.developer else True)
83def cformat(cli): 94def cformat(cli):
@@ -85,7 +96,7 @@ def cformat(cli):
85 """ 96 """
86 # Find the list of files to format 97 # Find the list of files to format
87 if cli.args.files: 98 if cli.args.files:
88 files = list(filter_files(cli.args.files)) 99 files = list(filter_files(cli.args.files, cli.args.core_only))
89 100
90 if not files: 101 if not files:
91 cli.log.error('No C files in filelist: %s', ', '.join(map(str, cli.args.files))) 102 cli.log.error('No C files in filelist: %s', ', '.join(map(str, cli.args.files)))
@@ -96,8 +107,7 @@ def cformat(cli):
96 107
97 elif cli.args.all_files: 108 elif cli.args.all_files:
98 all_files = c_source_files(core_dirs) 109 all_files = c_source_files(core_dirs)
99 # The following statement checks each file to see if the file path is in the ignored directories. 110 files = list(filter_files(all_files, True))
100 files = [file for file in all_files if not any(i in str(file) for i in ignored)]
101 111
102 else: 112 else:
103 git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch, *core_dirs] 113 git_diff_cmd = ['git', 'diff', '--name-only', cli.args.base_branch, *core_dirs]
@@ -117,7 +127,7 @@ def cformat(cli):
117 127
118 # Sanity check 128 # Sanity check
119 if not files: 129 if not files:
120 cli.log.error('No changed files detected. Use "qmk cformat -a" to format all files') 130 cli.log.error('No changed files detected. Use "qmk cformat -a" to format all core files')
121 return False 131 return False
122 132
123 # Run clang-format on the files we've found 133 # Run clang-format on the files we've found