|
|
|
@ -1,6 +1,5 @@ |
|
|
|
|
"""Format C code according to QMK's style. |
|
|
|
|
""" |
|
|
|
|
from os import path |
|
|
|
|
from shutil import which |
|
|
|
|
from subprocess import CalledProcessError, DEVNULL, Popen, PIPE |
|
|
|
|
|
|
|
|
@ -15,6 +14,12 @@ core_dirs = ('drivers', 'quantum', 'tests', 'tmk_core', 'platforms') |
|
|
|
|
ignored = ('tmk_core/protocol/usb_hid', 'platforms/chibios/boards') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_relative_to(file, other): |
|
|
|
|
"""Provide similar behavior to PurePath.is_relative_to in Python > 3.9 |
|
|
|
|
""" |
|
|
|
|
return str(normpath(file).resolve()).startswith(str(normpath(other).resolve())) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def find_clang_format(): |
|
|
|
|
"""Returns the path to clang-format. |
|
|
|
|
""" |
|
|
|
@ -68,18 +73,19 @@ def cformat_run(files): |
|
|
|
|
def filter_files(files, core_only=False): |
|
|
|
|
"""Yield only files to be formatted and skip the rest |
|
|
|
|
""" |
|
|
|
|
files = list(map(normpath, filter(None, files))) |
|
|
|
|
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(str(file).startswith(i) for i in core_dirs) or any(str(file).startswith(i) for i in ignored): |
|
|
|
|
files[index] = None |
|
|
|
|
if not any(is_relative_to(file, i) for i in core_dirs) or any(is_relative_to(file, i) for i in ignored): |
|
|
|
|
del files[index] |
|
|
|
|
cli.log.debug("Skipping non-core file %s, as '--core-only' is used.", file) |
|
|
|
|
|
|
|
|
|
for file in files: |
|
|
|
|
if file and file.name.split('.')[-1] in c_file_suffixes: |
|
|
|
|
if file.suffix[1:] in c_file_suffixes: |
|
|
|
|
yield file |
|
|
|
|
else: |
|
|
|
|
cli.log.debug('Skipping file %s', file) |
|
|
|
@ -118,12 +124,8 @@ def format_c(cli): |
|
|
|
|
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) |
|
|
|
|
changed_files = git_diff.stdout.strip().split('\n') |
|
|
|
|
files = list(filter_files(changed_files, True)) |
|
|
|
|
|
|
|
|
|
# Sanity check |
|
|
|
|
if not files: |
|
|
|
|