scripts: add script to format codebase using clang-format

Signed-off-by: Antonio Cardace <acardace@redhat.com>
This commit is contained in:
Antonio Cardace 2020-09-02 12:55:55 +02:00
parent 9c732c12bd
commit 715392a45e
No known key found for this signature in database
GPG key ID: 6BF80ABD43E377D3

View file

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