mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-04 11:55:51 +00:00
48 lines
1.6 KiB
Bash
Executable File
48 lines
1.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Note: This script is intended to be run from the root of the repository.
|
|
#
|
|
# This script checks the format of the C++ code.
|
|
# In many cases it will automatically fix the issues and abort the commit.
|
|
|
|
no_formatted_directories_staged() {
|
|
staged_directories=$(git diff-index --cached --name-only HEAD | awk -F/ '{print $1}')
|
|
for sd in $staged_directories; do
|
|
if [[ "$sd" =~ ^(src|tests)$ ]]; then
|
|
return 1
|
|
fi
|
|
done
|
|
return 0
|
|
}
|
|
|
|
if no_formatted_directories_staged ; then
|
|
exit 0
|
|
fi
|
|
|
|
echo "+ Checking code format..."
|
|
|
|
# paths to check and re-format
|
|
sources="src tests"
|
|
|
|
function grep_code {
|
|
grep -l "${1}" ${sources} -r --include \*.hpp --include \*.cpp
|
|
}
|
|
|
|
GNU_SED=$(sed --version 2>&1 | grep -q 'GNU' && echo true || echo false)
|
|
|
|
if [[ "$GNU_SED" == "false" ]]; then # macOS sed
|
|
# 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 -maxdepth 1 -type d -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
|