summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntonio Cardace <acardace@redhat.com>2020-09-02 12:55:55 +0200
committerAntonio Cardace <acardace@redhat.com>2020-09-02 17:31:42 +0200
commit715392a45e22791d54496916c8b01079776aa513 (patch)
tree44c8b9fdbd9b40e4d67636a31a46007da7948660
parent9c732c12bdfe83731a889a4d3acc65b95d7ca89c (diff)
downloadNetworkManager-715392a45e22791d54496916c8b01079776aa513.tar.gz
scripts: add script to format codebase using clang-format
Signed-off-by: Antonio Cardace <acardace@redhat.com>
-rwxr-xr-xcontrib/scripts/nm-code-format.sh72
1 files changed, 72 insertions, 0 deletions
diff --git a/contrib/scripts/nm-code-format.sh b/contrib/scripts/nm-code-format.sh
new file mode 100755
index 0000000000..660309a73b
--- /dev/null
+++ b/contrib/scripts/nm-code-format.sh
@@ -0,0 +1,72 @@
+#!/bin/bash
+
+set -e
+
+while (( $# )); do
+ case $1 in
+ -i)
+ FLAGS="-i"
+ shift
+ ;;
+ -h)
+ printf "Usage: %s [OPTION]... [FILE]...\n" $(basename $0)
+ printf "Reformat source files using NetworkManager's code-style.\n\n"
+ printf "If no file is given the script runs on the whole codebase.\n"
+ printf "If no flag is given no file is touch but errors are reported.\n\n"
+ printf "OPTIONS:\n"
+ printf " -i Reformat files\n"
+ printf " -h Print this help message\n"
+ exit 0
+ ;;
+ *)
+ FILES+=($1)
+ shift
+ ;;
+ esac
+done
+
+NM_ROOT=$(git rev-parse --show-toplevel)
+EXCLUDE=":(exclude)shared/systemd
+ :(exclude)src/systemd
+ :(exclude)shared/n-dhcp4
+ :(exclude)shared/c-list
+ :(exclude)shared/c-list
+ :(exclude)shared/c-list
+ :(exclude)shared/c-rbtree
+ :(exclude)shared/c-siphash
+ :(exclude)shared/c-stdaux
+ :(exclude)shared/n-acd"
+
+if ! which clang-format &> /dev/null; then
+ echo -n "Error: clang-format is not installed, "
+ echo "on RHEL/Fedora/CentOS run 'dnf install clang-tools-extra'"
+ exit 1
+fi
+
+if [ ! -f ${NM_ROOT}/.clang-format ]; then
+ echo "Error: the clang-format file does not exist"
+ exit 1
+fi
+
+
+FLAGS=${FLAGS:-"--Werror -n --ferror-limit=1"}
+
+if [ -z "${FILES[@]}" ]; then
+ cd $NM_ROOT
+ FILES=($(git ls-files --full-name -- '*.[ch]' ${EXCLUDE}))
+fi
+
+for f in "${FILES[@]}"; do
+ if [ -f $f ]; then
+ if ! clang-format $FLAGS $f &> /dev/null; then
+ TMP_FILE=$(mktemp)
+ clang-format $f > $TMP_FILE
+ git --no-pager diff $f $TMP_FILE || true
+ rm $TMP_FILE
+ echo "Error: $f code-style is wrong, fix it by running '$(basename $0) -i'"
+ exit 1
+ fi
+ else
+ echo "Error: $f No such file"
+ fi
+done