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
@@ -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"