From 25adc6512fb62b019887f29d7e79051bf9727b54 Mon Sep 17 00:00:00 2001 From: Ivan-lis Date: Wed, 9 Oct 2024 11:42:06 +0000 Subject: [PATCH] Added githook pre-commit checks --- .devcontainer/Dockerfile | 8 ++-- .devcontainer/devcontainer.json | 2 +- .githooks/pre-commit | 75 +++++++++++++++++++++++++++++++++ .gitignore | 1 + .vscode/tasks.json | 4 +- 5 files changed, 83 insertions(+), 7 deletions(-) create mode 100755 .githooks/pre-commit diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 88118e7..50b253e 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -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 \ diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 4e6c412..2c84710 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -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": [ diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 0000000..a24e546 --- /dev/null +++ b/.githooks/pre-commit @@ -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 diff --git a/.gitignore b/.gitignore index d52f86a..da9f39d 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,4 @@ libxmlplusplus-prefix/ spdlog.pc build* larra +temp* diff --git a/.vscode/tasks.json b/.vscode/tasks.json index eaea877..b0f68e3 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -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 )" ],