From 9b83eb40333ccb5f2b3dbbc8169cacc899856739 Mon Sep 17 00:00:00 2001 From: Ayaz Salikhov Date: Tue, 29 Apr 2025 15:12:38 +0100 Subject: [PATCH] style: Add C++ pre-commit hooks (#2039) --- .githooks/check-format | 76 +---------------------------------- .pre-commit-config.yaml | 21 ++++++++++ docker/ci/Dockerfile | 5 +-- docs/CMakeLists.txt | 2 + src/web/dosguard/DOSGuard.hpp | 2 +- tools/snapshot/CMakeLists.txt | 34 ++++++++-------- 6 files changed, 45 insertions(+), 95 deletions(-) diff --git a/.githooks/check-format b/.githooks/check-format index 3ffd8581..a8a0c87c 100755 --- a/.githooks/check-format +++ b/.githooks/check-format @@ -2,13 +2,13 @@ # Note: This script is intended to be run from the root of the repository. # -# This script checks the format of the code and cmake files. +# 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" =~ ^(benchmark|cmake|src|tests)$ ]]; then + if [[ "$sd" =~ ^(src|tests)$ ]]; then return 1 fi done @@ -23,51 +23,6 @@ echo "+ Checking code format..." # paths to check and re-format sources="src tests" -formatter="clang-format -i" -version=$($formatter --version | grep -o '[0-9\.]*') - -if [[ "19.0.0" > "$version" ]]; then - cat < /dev/null; then - cat <" | xargs sed -i -E "s|#include <(($main_src_dirs)/.*)>|#include \"\1\"|g" fi - -cmake_dirs=$(echo cmake $sources) -cmake_files=$(find $cmake_dirs -type f \( -name "CMakeLists.txt" -o -name "*.cmake" \)) -cmake_files=$(echo $cmake_files ./CMakeLists.txt) - -first=$(git diff $sources $cmake_files) -find $sources -type f \( -name '*.cpp' -o -name '*.hpp' -o -name '*.ipp' \) -print0 | xargs -0 $formatter -cmake-format -i $cmake_files -second=$(git diff $sources $cmake_files) -changes=$(diff <(echo "$first") <(echo "$second")) -changes_number=$(echo -n "$changes" | wc -l | sed -e 's/^[[:space:]]*//') - -if [ "$changes_number" != "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 - if [[ "$1" == "--diff" ]]; then - echo "$changes" - fi - exit 1 -fi diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e3cd7d1f..c0d44410 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -50,3 +50,24 @@ repos: rev: v1.31.2 hooks: - id: typos + + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v19.1.7 + hooks: + - id: clang-format + args: [--style=file] + types: [c++] + + - repo: https://github.com/cheshirekow/cmake-format-precommit + rev: v0.6.13 + hooks: + - id: cmake-format + additional_dependencies: [PyYAML] + + - repo: local + hooks: + - id: check-no-h-files + name: No .h files + entry: There should be no .h files in this repository + language: fail + files: \.h$ diff --git a/docker/ci/Dockerfile b/docker/ci/Dockerfile index f69176ca..2bbf5d96 100644 --- a/docker/ci/Dockerfile +++ b/docker/ci/Dockerfile @@ -20,9 +20,8 @@ RUN apt-get -qq update \ # Install packages RUN apt update -qq \ && apt install -y --no-install-recommends --no-install-suggests python3 python3-pip git git-lfs make ninja-build flex bison jq graphviz \ - clang-format-${LLVM_TOOLS_VERSION} clang-tidy-${LLVM_TOOLS_VERSION} clang-tools-${LLVM_TOOLS_VERSION} \ - && update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-${LLVM_TOOLS_VERSION} 100 \ - && pip3 install -q --upgrade --no-cache-dir pip && pip3 install -q --no-cache-dir conan==1.62 gcovr cmake==3.31.6 cmake-format \ + clang-tidy-${LLVM_TOOLS_VERSION} clang-tools-${LLVM_TOOLS_VERSION} \ + && pip3 install -q --upgrade --no-cache-dir pip && pip3 install -q --no-cache-dir conan==1.62 gcovr cmake==3.31.6 \ && apt-get clean && apt remove -y software-properties-common # Install gcc-12 and make ldconfig aware of the new libstdc++ location (for gcc) diff --git a/docs/CMakeLists.txt b/docs/CMakeLists.txt index 2a6d6fa5..598b4fd4 100644 --- a/docs/CMakeLists.txt +++ b/docs/CMakeLists.txt @@ -3,6 +3,8 @@ project(docs) include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/ClioVersion.cmake) +# cmake-format: off # Generate `docs` target for doxygen documentation # Note: use `cmake --build . --target docs` from your `build` directory to generate the documentation +# cmake-format: on include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/Docs.cmake) diff --git a/src/web/dosguard/DOSGuard.hpp b/src/web/dosguard/DOSGuard.hpp index d74dcf1a..e4ace5d3 100644 --- a/src/web/dosguard/DOSGuard.hpp +++ b/src/web/dosguard/DOSGuard.hpp @@ -49,7 +49,7 @@ class DOSGuard : public DOSGuardInterface { */ struct ClientState { std::uint32_t transferredByte = 0; /**< Accumulated transferred byte */ - std::uint32_t requestsCount = 0; /**< Accumulated served requests count */ + std::uint32_t requestsCount = 0; /**< Accumulated served requests count */ }; struct State { diff --git a/tools/snapshot/CMakeLists.txt b/tools/snapshot/CMakeLists.txt index 97a1aef4..52c2499c 100644 --- a/tools/snapshot/CMakeLists.txt +++ b/tools/snapshot/CMakeLists.txt @@ -1,5 +1,4 @@ -# Build snapshot tool -# Need to be installed before building: go protoc-gen-go protoc-gen-go-grpc +# Build snapshot tool needed to be installed before building: go protoc-gen-go protoc-gen-go-grpc set(GO_EXECUTABLE "go") set(GO_SOURCE_DIR "${CMAKE_SOURCE_DIR}/tools/snapshot") @@ -24,22 +23,22 @@ set(GO_OPTS "") set(GRPC_OPTS "") # Loop through each proto file -foreach(proto ${PROTO_FILES}) - get_filename_component(proto_filename ${proto} NAME) +foreach (proto ${PROTO_FILES}) + get_filename_component(proto_filename ${proto} NAME) - # Build the --go_opt and --go-grpc_opt mappings - set(GO_OPTS ${GO_OPTS} --go_opt=M${GO_IMPORT_PATH}/${proto_filename}=${GO_IMPORT_PATH}) - set(GRPC_OPTS ${GRPC_OPTS} --go-grpc_opt=M${GO_IMPORT_PATH}/${proto_filename}=${GO_IMPORT_PATH}) -endforeach() + # Build the --go_opt and --go-grpc_opt mappings + set(GO_OPTS ${GO_OPTS} --go_opt=M${GO_IMPORT_PATH}/${proto_filename}=${GO_IMPORT_PATH}) + set(GRPC_OPTS ${GRPC_OPTS} --go-grpc_opt=M${GO_IMPORT_PATH}/${proto_filename}=${GO_IMPORT_PATH}) +endforeach () foreach (proto ${PROTO_FILES}) get_filename_component(proto_name ${proto} NAME_WE) add_custom_command( OUTPUT ${GO_SOURCE_DIR}/${GO_IMPORT_PATH}/${proto_name}.pb.go COMMAND - protoc ${GO_OPTS} ${GRPC_OPTS} - --go-grpc_out=${GO_SOURCE_DIR} -I${PROTO_INC_DIR} ${proto} --plugin=${GOPATH_VALUE}/bin/protoc-gen-go - --plugin=${GOPATH_VALUE}/bin/protoc-gen-go-grpc --go_out=${GO_SOURCE_DIR}/ + protoc ${GO_OPTS} ${GRPC_OPTS} --go-grpc_out=${GO_SOURCE_DIR} -I${PROTO_INC_DIR} ${proto} + --plugin=${GOPATH_VALUE}/bin/protoc-gen-go --plugin=${GOPATH_VALUE}/bin/protoc-gen-go-grpc + --go_out=${GO_SOURCE_DIR}/ DEPENDS ${proto} COMMENT "Generating Go code for ${proto}" VERBATIM @@ -50,12 +49,13 @@ endforeach () add_custom_target(build_clio_snapshot ALL DEPENDS run_go_tests ${GO_OUTPUT}) -add_custom_target(run_go_tests - COMMAND go test ./... - WORKING_DIRECTORY ${GO_SOURCE_DIR} - DEPENDS ${GENERATED_GO_FILES} - COMMENT "Running clio_snapshot unittests" - VERBATIM +add_custom_target( + run_go_tests + COMMAND go test ./... + WORKING_DIRECTORY ${GO_SOURCE_DIR} + DEPENDS ${GENERATED_GO_FILES} + COMMENT "Running clio_snapshot unittests" + VERBATIM ) add_custom_command(