larra/.githooks/pre-commit

79 lines
3.5 KiB
Text
Raw Permalink Normal View History

2024-10-09 11:42:06 +00:00
#!/bin/bash
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
SCRIPTS_FOLDER=$( dirname "$(realpath "$0")" )
PROJECT_FOLDER=$( dirname "$SCRIPTS_FOLDER" )
cd $PROJECT_FOLDER || exit 1
2024-10-09 11:42:06 +00:00
GIT_STAGED_FILES=$(git diff --cached --name-only --diff-filter=d | grep -E -i "\.(h|hpp|c|cpp)$")
GIT_SCRIPT_FILES="$(git diff --cached --name-only --diff-filter=d | grep -E -i "\.(sh)$") .githooks/pre-commit"
printf "Code files to be checked:\n"
for FILE in $GIT_STAGED_FILES; do printf " \t$FILE \n"; done
#
# Description: clang-format code check
# Test tool: clang-format
# Manual cmd command: for FILE in $(git diff --cached --name-only --diff-filter=d | grep -e ".*[(h|hpp|c|cpp)]$"); do clang-format -i $FILE --Werror --dry-run; done
#
for FILE in $GIT_STAGED_FILES; do clang-format -i $PROJECT_FOLDER\/$FILE --dry-run --Werror; done
CLANG_FORMAT_OUTPUT=$(for FILE in $GIT_STAGED_FILES; do clang-format -i $PROJECT_FOLDER\/$FILE --dry-run --Werror; done 2>&1)
if [[ $CLANG_FORMAT_OUTPUT ]]; then
printf "\n\t ${RED}[ERROR] clang-format check FAILED!${NC} Fix above errors before commiting your changes. (check .githooks/pre-commit for additional info)\n"
printf "\t\t Execute below command for auto-fix:\n\t\t for FILE in \$(git diff --cached --name-only --diff-filter=d | grep -E -i \"\.(h|hpp|c|cpp)$\"); do clang-format -i $PROJECT_FOLDER/\$FILE --Werror; done; git status\n"
exit 1
else
printf "\t ${GREEN}[PASS] No clang-format violations!${NC}\n"
fi
#
# Description: Check shell scripts
# Test tool: shellcheck
# Manual cmd command: for FILE in "$(git diff --cached --name-only --diff-filter=d | grep -E -i "\.(sh)$") .githooks/pre-commit"; do shellcheck -S warning $FILE; done
#
SHELLCHECK_RES=0
for FILE in $GIT_SCRIPT_FILES; do shellcheck -S warning $FILE; RET_CODE=$?; SHELLCHECK_RES=$(( RET_CODE + SHELLCHECK_RES )); done
if [[ $SHELLCHECK_RES != 0 ]]; then
printf "\n\t ${RED}[ERROR] shell scripts check FAILED!${NC} Fix above errors before commiting your changes. (check .githooks/pre-commit for additional info)\n"
exit 1
else
printf "\t ${GREEN}[PASS] No shell scripts violations!${NC}\n"
fi
#
# Description: Temporal solution before CI
# Test tools: default compiler, gtest execution
# Manual cmd command: cd /workspaces/larra ; mkdir temp_pre_commit_check ; cd temp_pre_commit_check ; cmake -Wno-dev -S .. -B . -GNinja -DCMAKE_BUILD_TYPE=Release -DENABLE_TESTS=ON -DCMAKE_CXX_FLAGS="-Wno-non-template-friend" ; cmake --build . --target larra_xmpp_tests --parallel `nproc` ; ./larra_xmpp_tests --gtest_brief=1
2024-10-09 11:42:06 +00:00
#
GTEST_FOLDER=${PROJECT_FOLDER?}/temp_pre_commit_check
rm -rf ${GTEST_FOLDER} || exit 1
2024-10-09 11:42:06 +00:00
printf "\n\tPrepare GTests to check (takes up to 10 seconds)"
mkdir -p ${GTEST_FOLDER}; cd ${GTEST_FOLDER} || exit 1; cmake -Wno-dev -S ${PROJECT_FOLDER} -B ${GTEST_FOLDER} -GNinja -DCMAKE_BUILD_TYPE=Release -DENABLE_TESTS=ON -DCMAKE_CXX_FLAGS="-Wno-non-template-friend" > /dev/null 2>&1
2024-10-09 11:42:06 +00:00
printf "\n\tBuild GTests to check (takes up to 30 seconds)"
cmake --build ${GTEST_FOLDER} --target larra_xmpp_tests --parallel "$(nproc)"
2024-10-09 11:42:06 +00:00
printf "\n\tLaunch GTests to check\n"
./larra_xmpp_tests --gtest_brief=1
GTEST_RES=$?
cd ${PROJECT_FOLDER} && rm -rf ${GTEST_FOLDER?}
if [[ $GTEST_RES != 0 ]]; then
printf "\n\n\t ${RED}[ERROR] gtest check FAILED!${NC} Fix above errors before commiting your changes. (check .githooks/pre-commit for additional info)\n"
exit 1
else
printf "\n\t ${GREEN}[PASS] No gtest violations!${NC}\n"
fi
printf "\n\n"
exit 0