dncurrency/ci/detect-clang-format.sh
theohax 8ecbb1d251
Improve clang-format detection & enforcement (#3462)
* Improve the detection of non-clang-format-compliant commits/diffs

* Fix formatting

* Add .clang-format to version control; have it consistent and fixed across environments
2021-09-22 16:55:47 +03:00

43 lines
1 KiB
Bash

#!/usr/bin/env bash
set -e
is_clang_format_usable()
{
if [[ $(builtin type -p $1) ]]; then
local output=$($1 --version)
if [[ $output =~ ^(.)*clang-format\ version\ $2(.)*$ ]]; then
echo "0"
else
echo $output
fi
else
echo "1"
fi
}
CLANG_FORMAT=""
CLANG_FORMAT_VERSION="12"
clang_format_attempts=("clang-format"
"clang-format-$CLANG_FORMAT_VERSION")
for itr in ${clang_format_attempts[@]}; do
result=$(is_clang_format_usable $itr $CLANG_FORMAT_VERSION)
if [[ $result == "0" ]]; then
CLANG_FORMAT=$itr
break
elif [[ $result == "1" ]]; then
continue
else
echo "Detected '$itr' with version '$result' " \
"(different than '$CLANG_FORMAT_VERSION'), skipping it."
fi
done
if [[ -z $CLANG_FORMAT ]]; then
echo "No 'clang-format' of version '$CLANG_FORMAT_VERSION' could be detected in your PATH."
exit 1
fi
echo "Using '$CLANG_FORMAT' version '$CLANG_FORMAT_VERSION'"