Files
PX4-Autopilot/Tools/astyle/check_code_style.sh
T
Matthias Grob 3d820c5e80 build(astyle): pin astyle 3.1 via astyle-py
Distros ship newer astyle versions (Ubuntu 26.04: 3.6) which are
incompatible with our options and format differently, so make
check_format failed out of the box and required manually installing
an old .deb. astyle-py bundles astyle 3.1 as WebAssembly and installs
via pip on any OS, giving byte-identical results to native 3.1.

Native astyle 2.06-3.1 is still accepted as fallback, so existing
setups and the current CI container keep working.

Files are now formatted in batches on a temporary copy instead of one
astyle call per file, which keeps make check_format fast despite the
interpreter startup.
2026-11-18 08:23:56 -08:00

43 lines
1.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Check the formatting of the given files, print a diff for each badly formatted
# one and fail. With PX4_ASTYLE_FIX=1 the files get fixed instead.
#
# All files are formatted in one batch on a temporary copy to keep the
# formatter startup cost independent of the number of files.
if [[ $# -eq 0 ]]; then
exit 0
fi
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
TMP_DIR=$(mktemp -d)
trap 'rm -rf "$TMP_DIR"' EXIT
FORMATTED=()
for FILE in "$@"; do
mkdir -p "$TMP_DIR/$(dirname "$FILE")"
cp "$FILE" "$TMP_DIR/$FILE"
FORMATTED+=("$TMP_DIR/$FILE")
done
"${DIR}/fix_code_style.sh" "${FORMATTED[@]}" || exit 1
CHECK_FAILED=0
for FILE in "$@"; do
if ! cmp -s "$FILE" "$TMP_DIR/$FILE"; then
echo -e 'Formatting issue found in' "$FILE"
echo
git --no-pager diff --no-index --minimal --histogram --color=always "$FILE" "$TMP_DIR/$FILE" | tail -n +5
echo
if [[ $PX4_ASTYLE_FIX -eq 1 ]]; then
cp "$TMP_DIR/$FILE" "$FILE"
else
echo 'to fix automatically run "make format", "make format_changed" or "./Tools/astyle/fix_code_style.sh' "$FILE"'"'
CHECK_FAILED=1
fi
fi
done
exit $CHECK_FAILED