feat(astyle): add option to only format diff with respect to HEAD (#27122)

* feat(astyle): add make format_changed for diff only style fixes

`make format` is pretty slow because it always considers the entire
source tree. For developing on top of a clean state, considering only
files that differ from HEAD should be sufficient.

* docs(astyle): document new format_changed target
This commit is contained in:
Balduin
2026-04-20 04:30:21 +02:00
committed by GitHub
parent 3169dc6b1b
commit 385828fcb3
4 changed files with 32 additions and 7 deletions
+1 -1
View File
@@ -20,7 +20,7 @@ The [developer guide](https://docs.px4.io/main/en/development/development.html)
### Coding standards
All C/C++ code must follow the [PX4 coding style](https://docs.px4.io/main/en/contribute/code.html). Formatting is enforced by [astyle](http://astyle.sourceforge.net/) in CI (`make check_format`). Code quality checks run via [clang-tidy](https://clang.llvm.org/extra/clang-tidy/). Pull requests that fail either check will not be merged.
All C/C++ code must follow the [PX4 coding style](https://docs.px4.io/main/en/contribute/code.html). Formatting is enforced by [astyle](http://astyle.sourceforge.net/) in CI (`make check_format`, ``make format`, `make format_changed`). Code quality checks run via [clang-tidy](https://clang.llvm.org/extra/clang-tidy/). Pull requests that fail either check will not be merged.
Python code is checked with [mypy](https://mypy-lang.org/) and [flake8](https://flake8.pycqa.org/).
+5 -1
View File
@@ -404,7 +404,7 @@ px4_metadata: parameters_metadata airframe_metadata module_documentation extract
# Style
# --------------------------------------------------------------------
.PHONY: check_format format check_newlines
.PHONY: check_format format format_changed check_newlines
check_format:
$(call colorecho,'Checking formatting with astyle')
@@ -415,6 +415,10 @@ format:
$(call colorecho,'Formatting with astyle')
@"$(SRC_DIR)"/Tools/astyle/check_code_style_all.sh --fix
format_changed:
$(call colorecho,'Formatting changed files with astyle')
@"$(SRC_DIR)"/Tools/astyle/check_code_style_all.sh --fix --diff-only
check_newlines:
$(call colorecho,'Checking for missing or duplicate newlines at the end of files')
@"$(SRC_DIR)"/Tools/astyle/check_newlines.sh
+1 -1
View File
@@ -16,7 +16,7 @@ if [ -n "$CHECK_FAILED" ]; then
if [[ $PX4_ASTYLE_FIX -eq 1 ]]; then
${DIR}/fix_code_style.sh $FILE
else
echo 'to fix automatically run "make format" or "./Tools/astyle/fix_code_style.sh' $FILE'"'
echo 'to fix automatically run "make format", "make format_changed" or "./Tools/astyle/fix_code_style.sh' $FILE'"'
exit 1
fi
fi
+25 -4
View File
@@ -36,9 +36,13 @@ fi
CI="${CI:-false}"
DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
if [[ "$@" == "--fix" ]]; then
export PX4_ASTYLE_FIX=1
fi
DIFF_ONLY=false
for arg in "$@"; do
case "$arg" in
--fix) export PX4_ASTYLE_FIX=1 ;;
--diff-only) DIFF_ONLY=true ;;
esac
done
# install git pre-commit hook
@@ -59,7 +63,24 @@ if [[ ! -f "$HOOK_FILE" && "$CI" != "true" && $- == *i* ]]; then
fi
fi
${DIR}/files_to_check_code_style.sh | xargs -P 8 -I % ${DIR}/check_code_style.sh %
FILE_LIST=$(${DIR}/files_to_check_code_style.sh)
if [ "$DIFF_ONLY" = true ]; then
# --diff-filter=d: do not list deleted files (no need to format)
CHANGED=$(git -C "$DIR/../.." diff --name-only --diff-filter=d HEAD -- '*.c' '*.h' '*.cpp' '*.hpp')
if [ -z "$CHANGED" ]; then
FILE_LIST=""
else
FILE_LIST=$(echo "$FILE_LIST" | grep -Fx -f <(echo "$CHANGED") || true)
fi
fi
if [ -z "$FILE_LIST" ]; then
echo "No files to check"
exit 0
fi
echo "$FILE_LIST" | xargs -P 8 -I % ${DIR}/check_code_style.sh %
if [ $? -eq 0 ]; then
echo "Format checks passed"