check-formatting 1.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#!/usr/bin/env bash

PLATFORM=$(uname)
TOOLSPATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
CLANGFORMAT="${TOOLSPATH}/${PLATFORM}/clang-format -style=file"

if [ "$1" == "--all" ]
then
    find include src tests/src tests/unit \( -name *.cpp -or -name *.h \) -print0 \
    | xargs -n1 -0 ${CLANGFORMAT} -output-replacements-xml \
    | grep -c "<replacement " > /dev/null
    grepCode=$?
elif [ "$1" == "--staged" ]
then
    git diff --cached --name-only --diff-filter=ACMRT | grep -e '.*\.h$' -e '.*\.cpp' -v '**third-party/*' \
    | xargs -n1 ${CLANGFORMAT} -output-replacements-xml \
    | grep -c "<replacement " >/dev/null
    grepCode=$?
else
    echo "Please specify --all or --staged"
    exit 1
fi

# grep exits 0 => found needed formatting changes
if [ $grepCode -ne 0 ]
then
    echo "Formatting looks good!"
    exit 0
else
    echo "****************************************************"
    echo "Code needs formatting!  Please use 'tools/format-all'"
    echo "****************************************************"
    exit 1
fi