aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Meier <lorenz@px4.io>2015-03-02 19:06:55 +0100
committerLorenz Meier <lorenz@px4.io>2015-03-02 19:06:55 +0100
commit8424325f7393d92dc65f39d03457ad7216cea88c (patch)
tree243885efc081e23217b8e3496a9985fb06944ce7
parent0cd5fa2d1d6f388569ccb290cc895459430d7e75 (diff)
parent265147ce7f64d40124c615623afdde86fb2bb8f9 (diff)
downloadpx4-firmware-8424325f7393d92dc65f39d03457ad7216cea88c.tar.gz
px4-firmware-8424325f7393d92dc65f39d03457ad7216cea88c.tar.bz2
px4-firmware-8424325f7393d92dc65f39d03457ad7216cea88c.zip
Merge pull request #1593 from dagar/format
add 'make check_format' to enforce astyle
-rw-r--r--.gitignore2
-rw-r--r--Makefile4
-rwxr-xr-xTools/check_code_style.sh31
-rwxr-xr-xTools/fix_code_style.sh1
-rwxr-xr-xTools/pre-commit26
5 files changed, 64 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 611325444..0e553fa36 100644
--- a/.gitignore
+++ b/.gitignore
@@ -46,3 +46,5 @@ Firmware.zip
unittests/build
*.generated.h
.vagrant
+*.pretty
+
diff --git a/Makefile b/Makefile
index 7620099f9..201187e02 100644
--- a/Makefile
+++ b/Makefile
@@ -262,6 +262,10 @@ testbuild:
tests: generateuorbtopicheaders
$(Q) (mkdir -p $(PX4_BASE)/unittests/build && cd $(PX4_BASE)/unittests/build && cmake .. && $(MAKE) unittests)
+.PHONY: format check_format
+check_format:
+ $(Q) (./Tools/check_code_style.sh | sort -n)
+
#
# Cleanup targets. 'clean' should remove all built products and force
# a complete re-compilation, 'distclean' should remove everything
diff --git a/Tools/check_code_style.sh b/Tools/check_code_style.sh
new file mode 100755
index 000000000..8d1ab6363
--- /dev/null
+++ b/Tools/check_code_style.sh
@@ -0,0 +1,31 @@
+#!/usr/bin/env bash
+set -eu
+failed=0
+for fn in $(find . -path './src/lib/uavcan' -prune -o \
+ -path './src/lib/mathlib/CMSIS' -prune -o \
+ -path './src/modules/attitude_estimator_ekf/codegen/' -prune -o \
+ -path './NuttX' -prune -o \
+ -path './Build' -prune -o \
+ -path './mavlink' -prune -o \
+ -path './unittests/gtest' -prune -o \
+ -path './unittests/build' -prune -o \
+ -name '*.c' -o -name '*.cpp' -o -name '*.hpp' -o -name '*.h' -type f); do
+ if [ -f "$fn" ];
+ then
+ ./Tools/fix_code_style.sh --quiet < $fn > $fn.pretty
+ diffsize=$(diff -y --suppress-common-lines $fn $fn.pretty | wc -l)
+ rm -f $fn.pretty
+ if [ $diffsize -ne 0 ]; then
+ failed=1
+ echo $diffsize $fn
+ fi
+ fi
+done
+
+if [ $failed -eq 0 ]; then
+ echo "Format checks passed"
+ exit 0
+else
+ echo "Format checks failed; please run ./Tools/fix_code_style.sh on each file"
+ exit 1
+fi
diff --git a/Tools/fix_code_style.sh b/Tools/fix_code_style.sh
index 5995d428e..e73a5a8af 100755
--- a/Tools/fix_code_style.sh
+++ b/Tools/fix_code_style.sh
@@ -18,4 +18,5 @@ astyle \
--exclude=EASTL \
--add-brackets \
--max-code-length=120 \
+ --preserve-date \
$*
diff --git a/Tools/pre-commit b/Tools/pre-commit
new file mode 100755
index 000000000..13cd4aadd
--- /dev/null
+++ b/Tools/pre-commit
@@ -0,0 +1,26 @@
+#!/bin/sh
+if git rev-parse --verify HEAD >/dev/null 2>&1
+then
+ against=HEAD
+else
+ # Initial commit: diff against an empty tree object
+ against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
+fi
+
+# Redirect output to stderr.
+exec 1>&2
+
+CHANGED_FILES=`git diff --cached --name-only --diff-filter=ACM $against | grep '\.c\|\.cpp\|\.h\|\.hpp'`
+FAILED=0
+if [ ! -z "$CHANGED_FILES" -a "$CHANGED_FILES" != " " ]; then
+ for FILE in $CHANGED_FILES; do
+ ./Tools/fix_code_style.sh --quiet < $FILE > $FILE.pretty
+ diff -u $FILE $FILE.pretty || FAILED=1
+ rm -f $FILE.pretty
+ if [ $FAILED -ne 0 ]; then
+ echo "There are code formatting errors. Please fix them by running ./Tools/fix_code_style.sh $FILE"
+ exit $FAILED
+ fi
+ done
+fi
+exit 0