aboutsummaryrefslogtreecommitdiffstats
path: root/extras/scripts/checkstyle.sh
diff options
context:
space:
mode:
authorDamjan Marion <damarion@cisco.com>2020-12-12 19:09:31 +0100
committerDamjan Marion <damarion@cisco.com>2020-12-18 11:06:33 +0100
commit942542f7c1c3aae62a88612b8702a45f0d3b0e35 (patch)
tree6955c8198ccc33eb0e5b24c250853f6b27d29230 /extras/scripts/checkstyle.sh
parent44cae547370cc49fbc65127aad6d4f5211716383 (diff)
misc: migrate from GNU indent to clang-format
Type: make Change-Id: I085dcd6fe826da14d456f84a23355310bdc5d1e9 Signed-off-by: Damjan Marion <damarion@cisco.com>
Diffstat (limited to 'extras/scripts/checkstyle.sh')
-rwxr-xr-xextras/scripts/checkstyle.sh90
1 files changed, 90 insertions, 0 deletions
diff --git a/extras/scripts/checkstyle.sh b/extras/scripts/checkstyle.sh
new file mode 100755
index 00000000000..b40f43fa1e6
--- /dev/null
+++ b/extras/scripts/checkstyle.sh
@@ -0,0 +1,90 @@
+#!/bin/bash
+
+# Copyright (c) 2020 Cisco and/or its affiliates.
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+set -eEo pipefail
+
+CLANG_FORMAT_VER=10
+GIT_DIFF_ARGS="-U0 --no-color --relative HEAD~1"
+CLANG_FORMAT_DIFF_ARGS="-style file -p1"
+SUFFIX="-${CLANG_FORMAT_VER}"
+
+clang-format${SUFFIX} --version
+
+in=$(mktemp)
+git diff ${GIT_DIFF_ARGS} > ${in}
+
+line_count=$(sed -n '/^+.*\*INDENT-O[NF][F]\{0,1\}\*/p' ${in} | wc -l)
+if [ ${line_count} -gt 0 ] ; then
+ echo
+ sed -n '/^+++ /{h}; /^+.*\*INDENT-O[NF][F]\{0,1\}\*/{x;p;x;p;}' ${in}
+ echo
+ echo "*******************************************************************"
+ echo "* CHECKSTYLE FAILED"
+ echo "* Please remove INDENT-ON and INDENT-OFF from modified lines."
+ echo "*******************************************************************"
+ rm ${in}
+ exit 1
+fi
+
+if [ "${1}" == "--fix" ]; then
+ cat ${in} | clang-format-diff${SUFFIX} ${CLANG_FORMAT_DIFF_ARGS} -i
+ filelist=$(sed -n 's/^+++ b\/\(.*\.[ch]\)/\1/p' ${in})
+ git status ${filelist}
+ rm ${in}
+ exit 0
+fi
+
+line_count=$(sed -n '/^+.*\s\+$/p' ${in} | wc -l)
+if [ ${line_count} -gt 0 ] ; then
+ echo
+ sed -n '/^+++/h; /^+.*\s\+$/{x;p;x;p;}' ${in}
+ echo
+ echo "*******************************************************************"
+ echo "* CHECKSTYLE FAILED"
+ echo "* Trailing whitespace detected"
+ echo "*******************************************************************"
+ rm ${in}
+ exit 1
+fi
+
+out=$(mktemp)
+
+cat ${in} | clang-format-diff${SUFFIX} ${CLANG_FORMAT_DIFF_ARGS} > ${out}
+rm ${in}
+
+line_count=$(cat ${out} | wc -l)
+
+if [ -t 1 ] && [ -n $(tput colors) ] && [ $(tput colors) -ge 1 ] && \
+ command -v highlight &> /dev/null ; then
+ highlight --syntax diff -O ansi ${out}
+else
+ cat ${out}
+fi
+
+rm ${out}
+
+if [ ${line_count} -gt 0 ] ; then
+ echo "*******************************************************************"
+ echo "* CHECKSTYLE FAILED"
+ echo "* CONSULT DIFF ABOVE"
+ echo "* NOTE: Running 'extras/scripts/checkstyle.sh --fix' *MAY* fix the issue"
+ echo "*******************************************************************"
+ exit 1
+else
+ echo "*******************************************************************"
+ echo "* CHECKSTYLE SUCCESSFULLY COMPLETED"
+ echo "*******************************************************************"
+ exit 0
+fi