Added githook pre-commit checks

This commit is contained in:
Ivan-lis 2024-10-09 11:42:06 +00:00
parent ad36468b84
commit 25adc6512f
5 changed files with 83 additions and 7 deletions

View file

@ -5,12 +5,12 @@ FROM archlinux@sha256:a10e51dd0694d6c4142754e9d06cbce7baf91ace8031a30df37064d109
# Update the package database and install clang
# 1. system tools
# 2. build tools
# 2. build/test tools
# 3. libraries
RUN pacman -Syyu --noconfirm \
&& pacman -S --noconfirm git less vim sudo base-devel python-pip \
&& pacman -S --noconfirm clang cmake make ninja meson \
&& pacman -S --noconfirm gtk4 gtkmm-4.0 boost spdlog fmt libxml++-5.0
&& pacman -Syyu --noconfirm git less vim sudo base-devel python-pip \
&& pacman -Syyu --noconfirm clang cmake make ninja meson shellcheck \
&& pacman -Syyu --noconfirm gtk4 gtkmm-4.0 boost spdlog fmt libxml++-5.0
# Create a non-root user 'dev'
RUN useradd -ms /bin/bash dev \

View file

@ -10,7 +10,7 @@
5280,
5443
],
"postStartCommand": "/workspaces/larra/.devcontainer/post_create_config.sh",
"postStartCommand": "/workspaces/larra/.devcontainer/post_create_config.sh ; git config core.hooksPath .githooks",
"customizations": {
"vscode": {
"extensions": [

75
.githooks/pre-commit Executable file
View file

@ -0,0 +1,75 @@
#!/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" )
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 -DCMAKE_BUILD_TYPE=Release -DENABLE_TESTS=ON .. ; cmake --build . --target larra_xmpp_tests --parallel `nproc` ; ./larra_xmpp_tests --gtest_brief=1
#
GTEST_FOLDER=${PROJECT_FOLDER?}/temp_pre_commit_check
printf "\n\tPrepare GTests to check (takes up to 10 seconds)"
mkdir -p ${GTEST_FOLDER}; cd ${GTEST_FOLDER} || exit 1; cmake -DCMAKE_BUILD_TYPE=Release -DENABLE_TESTS=ON .. > /dev/null 2>&1
printf "\n\tBuild GTests to check (takes up to 30 seconds)"
cmake --build . --target larra_xmpp_tests --parallel "`nproc`" > /dev/null 2>&1
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

1
.gitignore vendored
View file

@ -30,3 +30,4 @@ libxmlplusplus-prefix/
spdlog.pc
build*
larra
temp*

4
.vscode/tasks.json vendored
View file

@ -23,7 +23,7 @@
"command": [
"cd ${workspaceFolder} &&",
"mkdir -p build && cd build &&",
"cmake -DCMAKE_BUILD_TYPE=Debug -DENABLE_EXAMPLES=ON .. &&",
"cmake -DCMAKE_BUILD_TYPE=Debug -DENABLE_EXAMPLES=ON -DENABLE_TESTS=ON .. &&",
"cmake --build . --parallel `nproc`",
"|| ( printf '\n\n\t\\e[31mERROR: Build failed!\\e[0m\n\n\n' && exit 1 )"
],
@ -45,7 +45,7 @@
"command": [
"cd ${workspaceFolder} &&",
"mkdir -p build_clang && cd build_clang &&",
"cmake -DCMAKE_BUILD_TYPE=Debug -DENABLE_EXAMPLES=ON .. &&",
"cmake -DCMAKE_BUILD_TYPE=Debug -DENABLE_EXAMPLES=ON -DENABLE_TESTS=ON .. &&",
"cmake --build . --parallel `nproc`",
"|| ( printf '\n\n\t\\e[31mERROR: Build failed!\\e[0m\n\n\n' && exit 1 )"
],