Merge pull request #4415 from jepler/pre-commit-no-run-if-empty
pre-commit: code formatter fixes
This commit is contained in:
commit
bc690d4070
@ -21,6 +21,6 @@ repos:
|
|||||||
language: system
|
language: system
|
||||||
- id: formatting
|
- id: formatting
|
||||||
name: Formatting
|
name: Formatting
|
||||||
entry: sh -c "git diff --staged --name-only | xargs python3 tools/codeformat.py"
|
entry: python3 tools/codeformat.py
|
||||||
types_or: [c, python]
|
types_or: [c, python]
|
||||||
language: system
|
language: system
|
||||||
|
20
BUILDING.md
20
BUILDING.md
@ -85,3 +85,23 @@ Example:
|
|||||||
|
|
||||||
If your port/build includes `arm-none-eabi-gdb-py`, consider using it instead, as it can be used for better register
|
If your port/build includes `arm-none-eabi-gdb-py`, consider using it instead, as it can be used for better register
|
||||||
debugging with https://github.com/bnahill/PyCortexMDebug
|
debugging with https://github.com/bnahill/PyCortexMDebug
|
||||||
|
|
||||||
|
# Code Quality Checks
|
||||||
|
|
||||||
|
We apply code quality checks using pre-commit. Install pre-commit once per system with
|
||||||
|
|
||||||
|
python3 -mpip install pre-commit
|
||||||
|
|
||||||
|
Activate it once per git clone with
|
||||||
|
|
||||||
|
pre-commit --install
|
||||||
|
|
||||||
|
Pre-commit also requires some additional programs to be installed through your package manager:
|
||||||
|
|
||||||
|
* Standard Unix tools such as make, find, etc
|
||||||
|
* The gettext package, any modern version
|
||||||
|
* uncrustify version 0.71 (0.72 is also tested)
|
||||||
|
|
||||||
|
Each time you create a git commit, the pre-commit quality checks will be run. You can also run them e.g., with `pre-commit run foo.c` or `pre-commit run --all` to run on all files whether modified or not.
|
||||||
|
|
||||||
|
Some pre-commit quality checks require your active attention to resolve, others (such as the formatting checks of uncrustify) are made automatically and must simply be incorporated into your code changes by committing them.
|
||||||
|
@ -30,6 +30,9 @@
|
|||||||
#include "supervisor/filesystem.h"
|
#include "supervisor/filesystem.h"
|
||||||
#include "supervisor/shared/stack.h"
|
#include "supervisor/shared/stack.h"
|
||||||
|
|
||||||
void port_background_task(void) {}
|
void port_background_task(void) {
|
||||||
void port_start_background_task(void) {}
|
}
|
||||||
void port_finish_background_task(void) {}
|
void port_start_background_task(void) {
|
||||||
|
}
|
||||||
|
void port_finish_background_task(void) {
|
||||||
|
}
|
||||||
|
@ -27,8 +27,10 @@
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import glob
|
import glob
|
||||||
|
import fnmatch
|
||||||
import itertools
|
import itertools
|
||||||
import os
|
import os
|
||||||
|
import pathlib
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
@ -75,12 +77,13 @@ C_EXTS = (".c", ".h")
|
|||||||
PY_EXTS = (".py",)
|
PY_EXTS = (".py",)
|
||||||
|
|
||||||
|
|
||||||
def list_files(paths, exclusions=None, prefix=""):
|
def list_files(args, paths, exclusions=None, prefix=""):
|
||||||
files = set()
|
files = set()
|
||||||
|
args = [os.path.join(prefix, arg) for arg in args]
|
||||||
for pattern in paths:
|
for pattern in paths:
|
||||||
files.update(glob.glob(os.path.join(prefix, pattern), recursive=True))
|
files.update(arg for arg in args if pathlib.Path(arg).match(pattern))
|
||||||
for pattern in exclusions or []:
|
for pattern in exclusions or []:
|
||||||
files.difference_update(glob.fnmatch.filter(files, os.path.join(prefix, pattern)))
|
files.difference_update(fnmatch.filter(files, os.path.join(prefix, pattern)))
|
||||||
return sorted(files)
|
return sorted(files)
|
||||||
|
|
||||||
|
|
||||||
@ -96,6 +99,10 @@ def fixup_c(filename):
|
|||||||
# Get next line.
|
# Get next line.
|
||||||
l = lines.pop(0)
|
l = lines.pop(0)
|
||||||
|
|
||||||
|
# Revert "// |" back to "//| "
|
||||||
|
if l.startswith("// |"):
|
||||||
|
l = "//|" + l[4:]
|
||||||
|
|
||||||
# Dedent #'s to match indent of following line (not previous line).
|
# Dedent #'s to match indent of following line (not previous line).
|
||||||
m = re.match(r"( +)#(if |ifdef |ifndef |elif |else|endif)", l)
|
m = re.match(r"( +)#(if |ifdef |ifndef |elif |else|endif)", l)
|
||||||
if m:
|
if m:
|
||||||
@ -128,23 +135,19 @@ def fixup_c(filename):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
cmd_parser = argparse.ArgumentParser(description="Auto-format C and Python files.")
|
cmd_parser = argparse.ArgumentParser(description="Auto-format C and Python files -- to be used via pre-commit only.")
|
||||||
cmd_parser.add_argument("-c", action="store_true", help="Format C code only")
|
cmd_parser.add_argument("-c", action="store_true", help="Format C code only")
|
||||||
cmd_parser.add_argument("-p", action="store_true", help="Format Python code only")
|
cmd_parser.add_argument("-p", action="store_true", help="Format Python code only")
|
||||||
cmd_parser.add_argument("-v", action="store_true", help="Enable verbose output")
|
cmd_parser.add_argument("-v", action="store_true", help="Enable verbose output")
|
||||||
cmd_parser.add_argument("files", nargs="*", help="Run on specific globs")
|
cmd_parser.add_argument("files", nargs="+", help="Run on specific globs")
|
||||||
args = cmd_parser.parse_args()
|
args = cmd_parser.parse_args()
|
||||||
|
|
||||||
# Setting only one of -c or -p disables the other. If both or neither are set, then do both.
|
# Setting only one of -c or -p disables the other. If both or neither are set, then do both.
|
||||||
format_c = args.c or not args.p
|
format_c = args.c or not args.p
|
||||||
format_py = args.p or not args.c
|
format_py = args.p or not args.c
|
||||||
|
|
||||||
# Expand the globs passed on the command line, or use the default globs above.
|
# Expand the arguments passed on the command line, subject to the PATHS and EXCLUSIONS
|
||||||
files = []
|
files = list_files(args.files, PATHS, EXCLUSIONS, TOP)
|
||||||
if args.files:
|
|
||||||
files = list_files(args.files)
|
|
||||||
else:
|
|
||||||
files = list_files(PATHS, EXCLUSIONS, TOP)
|
|
||||||
|
|
||||||
# Extract files matching a specific language.
|
# Extract files matching a specific language.
|
||||||
def lang_files(exts):
|
def lang_files(exts):
|
||||||
@ -168,11 +171,6 @@ def main():
|
|||||||
batch(command, lang_files(C_EXTS))
|
batch(command, lang_files(C_EXTS))
|
||||||
for file in lang_files(C_EXTS):
|
for file in lang_files(C_EXTS):
|
||||||
fixup_c(file)
|
fixup_c(file)
|
||||||
# Revert "// |" back to "//|"
|
|
||||||
subprocess.call(
|
|
||||||
"find shared-bindings ports/*/bindings -name '*.c' -exec sed -i 's/\/ |/\/|/' {} \;",
|
|
||||||
shell=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Format Python files with black.
|
# Format Python files with black.
|
||||||
if format_py:
|
if format_py:
|
||||||
|
Loading…
Reference in New Issue
Block a user