mirror of
				https://github.com/XRPLF/clio.git
				synced 2025-11-04 03:45:50 +00:00 
			
		
		
		
	style: Add C++ pre-commit hooks (#2039)
This commit is contained in:
		@@ -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 <<EOF
 | 
			
		||||
 | 
			
		||||
                                    ERROR
 | 
			
		||||
-----------------------------------------------------------------------------
 | 
			
		||||
            A minimum of version 19 of `which clang-format` is required.
 | 
			
		||||
            Your version is $version.
 | 
			
		||||
            Please fix paths and run again.
 | 
			
		||||
-----------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
EOF
 | 
			
		||||
    exit 3
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# check there is no .h headers, only .hpp
 | 
			
		||||
wrong_headers=$(find $sources -name "*.h" | sed 's/^/        - /')
 | 
			
		||||
if [[ ! -z "$wrong_headers" ]]; then
 | 
			
		||||
    cat <<EOF
 | 
			
		||||
 | 
			
		||||
                                    ERROR
 | 
			
		||||
-----------------------------------------------------------------------------
 | 
			
		||||
       Found .h headers in the source code. Please rename them to .hpp:
 | 
			
		||||
 | 
			
		||||
$wrong_headers
 | 
			
		||||
-----------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
EOF
 | 
			
		||||
    exit 2
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if ! command -v cmake-format &> /dev/null; then
 | 
			
		||||
    cat <<EOF
 | 
			
		||||
 | 
			
		||||
                                    ERROR
 | 
			
		||||
-----------------------------------------------------------------------------
 | 
			
		||||
        'cmake-format' is required to run this script.
 | 
			
		||||
        Please install it and run again.
 | 
			
		||||
-----------------------------------------------------------------------------
 | 
			
		||||
 | 
			
		||||
EOF
 | 
			
		||||
    exit 3
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
function grep_code {
 | 
			
		||||
    grep -l "${1}" ${sources} -r --include \*.hpp --include \*.cpp
 | 
			
		||||
@@ -90,30 +45,3 @@ else
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
 
 | 
			
		||||
@@ -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$
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
@@ -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(
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user