mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
78 lines
2.6 KiB
Bash
Executable File
78 lines
2.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
exec 1>&2
|
|
|
|
# paths to check and re-format
|
|
sources="src unittests"
|
|
formatter="clang-format -i"
|
|
version=$($formatter --version | grep -o '[0-9\.]*')
|
|
|
|
if [[ "17.0.0" > "$version" ]]; then
|
|
cat <<EOF
|
|
|
|
ERROR
|
|
-----------------------------------------------------------------------------
|
|
A minimum of version 17 of `which clang-format` is required.
|
|
Your version is $version.
|
|
Please fix paths and run again.
|
|
-----------------------------------------------------------------------------
|
|
|
|
EOF
|
|
exit 3
|
|
fi
|
|
|
|
# check there is no .h headers, only .hpp
|
|
wrong_headers=$(find $sources -name "*.h" | sed 's/^/ - /')
|
|
if [[ ! -z "$wrong_headers" ]]; then
|
|
cat <<EOF
|
|
|
|
ERROR
|
|
-----------------------------------------------------------------------------
|
|
Found .h headers in the source code. Please rename them to .hpp:
|
|
|
|
$wrong_headers
|
|
-----------------------------------------------------------------------------
|
|
|
|
EOF
|
|
exit 2
|
|
fi
|
|
|
|
function grep_code {
|
|
grep -l "${1}" ${sources} -r --include \*.hpp --include \*.cpp
|
|
}
|
|
|
|
if [[ "$OSTYPE" == "darwin"* ]]; then
|
|
# make all includes to be <...> style
|
|
grep_code '#include ".*"' | xargs sed -i '' -E 's|#include "(.*)"|#include <\1>|g'
|
|
|
|
# make local includes to be "..." style
|
|
main_src_dirs=$(find ./src -maxdepth 1 -type d -exec basename {} \; | tr '\n' '|' | sed 's/|$//' | sed 's/|/\\|/g')
|
|
grep_code "#include <\($main_src_dirs\)/.*>" | xargs sed -i '' -E "s|#include <(($main_src_dirs)/.*)>|#include \"\1\"|g"
|
|
else
|
|
# make all includes to be <...> style
|
|
grep_code '#include ".*"' | xargs sed -i -E 's|#include "(.*)"|#include <\1>|g'
|
|
|
|
# make local includes to be "..." style
|
|
main_src_dirs=$(find ./src -type d -maxdepth 1 -exec basename {} \; | paste -sd '|' | sed 's/|/\\|/g')
|
|
grep_code "#include <\($main_src_dirs\)/.*>" | xargs sed -i -E "s|#include <(($main_src_dirs)/.*)>|#include \"\1\"|g"
|
|
fi
|
|
|
|
first=$(git diff $sources)
|
|
find $sources -type f \( -name '*.cpp' -o -name '*.hpp' -o -name '*.ipp' \) -print0 | xargs -0 $formatter
|
|
second=$(git diff $sources)
|
|
changes=$(diff <(echo "$first") <(echo "$second") | wc -l | sed -e 's/^[[:space:]]*//')
|
|
|
|
if [ "$changes" != "0" ]; then
|
|
cat <<\EOF
|
|
|
|
WARNING
|
|
-----------------------------------------------------------------------------
|
|
Automatically re-formatted code with `clang-format` - commit was aborted.
|
|
Please manually add any updated files and commit again.
|
|
-----------------------------------------------------------------------------
|
|
|
|
EOF
|
|
exit 1
|
|
fi
|
|
|