| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
PLATFORM=$(uname) |
| 4 |
TOOLSPATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)" |
| 5 |
CLANGFORMAT="${TOOLSPATH}/${PLATFORM}/clang-format -style=file" |
| 6 |
|
| 7 |
if [ "$1" == "--all" ] |
| 8 |
then |
| 9 |
pushd "$TOOLSPATH/.." > /dev/null |
| 10 |
CHECK_DIRS=() |
| 11 |
for DIR in "include" "src" "tests/src" "tests/unit" "tools"; do [ -d "$TOOLSPATH/../$DIR" ] && CHECK_DIRS+=("$DIR") ; done |
| 12 |
find ${CHECK_DIRS[@]} \ |
| 13 |
\( -name '*.cpp' -o -name '*.h' -o -name '*.cu' -o -name '*.cuh' -o -name '*.hpp' \) \ |
| 14 |
-not -name pugi* -not -name json.hpp -not -path '*/third-party/*' \ |
| 15 |
-print0 \ |
| 16 |
| xargs -n1 -0 ${CLANGFORMAT} -output-replacements-xml \ |
| 17 |
| grep -c "<replacement " > /dev/null |
| 18 |
grepCode=$? |
| 19 |
popd > /dev/null |
| 20 |
elif [ "$1" == "--staged" ] |
| 21 |
then |
| 22 |
pushd "$TOOLSPATH/.." > /dev/null |
| 23 |
git diff --cached --name-only --diff-filter=ACMRT | grep -e '.*\.h$' -e '.*\.cpp$' -e '.*\.hpp$' -e '.*\.cu$' -e '.*\.cuh$' -v '**third-party/*' \ |
| 24 |
| xargs -n1 ${CLANGFORMAT} -output-replacements-xml \ |
| 25 |
| grep -c "<replacement " >/dev/null |
| 26 |
grepCode=$? |
| 27 |
popd > /dev/null |
| 28 |
else |
| 29 |
echo "Please specify --all or --staged" |
| 30 |
exit 1 |
| 31 |
fi |
| 32 |
|
| 33 |
# grep exits 0 => found needed formatting changes |
| 34 |
if [ $grepCode -ne 0 ] |
| 35 |
then |
| 36 |
echo "Formatting looks good!" |
| 37 |
exit 0 |
| 38 |
else |
| 39 |
echo "****************************************************" |
| 40 |
echo "Code needs formatting! Please use 'tools/format-all'" |
| 41 |
echo "****************************************************" |
| 42 |
exit 1 |
| 43 |
fi |