Skip to content

Commit

Permalink
style: format codes before commit (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexStocks authored Jan 12, 2025
2 parents 159bbb2 + 8693246 commit 8acd3ce
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 15 deletions.
15 changes: 7 additions & 8 deletions .github/workflows/kiwidb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ "unstable" ]
pull_request:
branches: [ "unstable" ]
branches: [ "*" ]

env:
BUILD_DIR: cmake-build-release
Expand All @@ -22,14 +22,13 @@ jobs:
- name: Add LLVM apt repository
run: |
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-16 main" | sudo tee /etc/apt/sources.list.d/llvm.list
sudo apt-get update
echo "deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-18 main" | sudo tee /etc/apt/sources.list.d/llvm.list
sudo apt update
- name: Install clang-format-16
- name: Install clang-format-18
run: |
sudo apt-get install -y clang-format-16
sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-16 100
sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-14 50
sudo apt install -y clang-format-18
sudo update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-18 200
- name: Verify clang-format version
run: clang-format --version
Expand Down Expand Up @@ -109,7 +108,7 @@ jobs:
- uses: actions/checkout@v4

- name: Install dependencies
run: sudo apt-get install -y ccache
run: sudo apt install -y ccache

- name: Configure ccache
run: |
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ download/
CMakeFiles/
CMakeCache.txt

# clang-format
temp.txt

# Precompiled Headers
*.gch
*.pch
Expand Down
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ ENDIF()
SET(LIB ${LIB} CACHE INTERNAL "libs which should be linked for executable target")

INCLUDE(ExternalProject)
INCLUDE(cmake/precommit.cmake)
INCLUDE(cmake/findTools.cmake)
INCLUDE(cmake/openssl.cmake)
INCLUDE(cmake/gflags.cmake)
Expand Down Expand Up @@ -222,6 +223,15 @@ FOREACH(IGNORE_FILE ${CLANG_FORMAT_IGNORE_FILES})
FILE(APPEND ${BUILD_SUPPORT_DIR}/clang_format_exclusions.txt "${IGNORE_FILE}\n")
ENDFOREACH()

# install git hooks when configure
EXECUTE_PROCESS(
COMMAND ${CMAKE_COMMAND} -E make_directory ${GIT_HOOKS_DIR}
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${HOOKS_SOURCE_DIR}/precommit.sh
${GIT_HOOKS_DIR}/pre-commit
COMMAND ${CMAKE_COMMAND} -E chmod 755 ${GIT_HOOKS_DIR}/pre-commit
)

STRING(CONCAT FORMAT_DIRS "${PROJECT_SOURCE_DIR}/src,")
ADD_CUSTOM_TARGET(format
COMMAND ${BUILD_SUPPORT_DIR}/run_clang_format.py
Expand Down
19 changes: 19 additions & 0 deletions cmake/precommit.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright (c) 2025-present, Arana/Kiwi Community. All rights reserved.
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.


# define hooks source and target path
SET(HOOKS_SOURCE_DIR "${CMAKE_SOURCE_DIR}/etc/script")
set(GIT_HOOKS_DIR "${CMAKE_SOURCE_DIR}/.git/hooks")

# # ensure pre-commit hook is installed
# ADD_CUSTOM_TARGET(install_git_hooks
# COMMAND ${CMAKE_COMMAND} -E make_directory ${GIT_HOOKS_DIR}
# COMMAND ${CMAKE_COMMAND} -E copy_if_different
# ${HOOKS_SOURCE_DIR}/precommit.sh
# ${GIT_HOOKS_DIR}/pre-commit
# COMMAND ${CMAKE_COMMAND} -E chmod 755 ${GIT_HOOKS_DIR}/pre-commit
# COMMENT "Installing git hooks..."
# )
2 changes: 2 additions & 0 deletions etc/script/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ while true; do
--kiwi)
MAKE_FLAGS="${MAKE_FLAGS} kiwi"
;;

-h|--help)
show_help
;;
Expand Down Expand Up @@ -133,3 +134,4 @@ while true; do
done

build

61 changes: 61 additions & 0 deletions etc/script/check_format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

# Check if clang-format is installed
if ! command -v clang-format &> /dev/null; then
echo "Error: clang-format is not installed"
echo "Please install clang-format first"
exit 1
fi

# Set color output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color

# Counters
error_count=0
checked_count=0

# Check file
check_file() {
local file=$1
# Create a temporary file with formatted content
clang-format $file > temp.txt

# Compare the original file with the formatted file
if ! diff -u "$file" temp.txt > /dev/null; then
echo -e "${RED}Needs formatting: $file${NC}"
# Show specific differences
diff -u "$file" temp.txt | grep -E "^[\+\-]" | head -n 10
echo "..."
((error_count++))
else
echo -e "${GREEN}Correctly formatted: $file${NC}"
fi
((checked_count++))

# Clean up temporary file
rm temp.txt
}

# Main function
main() {
echo "Starting code format check..."

# Find all C/C++ source files
while IFS= read -r -d '' file; do
check_file "$file"
done < <(find ./src -type f \( -name "*.cpp" -o -name "*.hpp" -o -name "*.c" -o -name "*.h" -o -name "*.cc" \) -print0)

# Output summary
echo "----------------------------------------"
echo "Check completed!"
echo "Total files checked: $checked_count"
echo "Files needing formatting: $error_count"

# Return non-zero if there are errors
[ "$error_count" -gt 0 ] && exit 1 || exit 0
}

# Run the main function
main
31 changes: 31 additions & 0 deletions etc/script/precommit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

# Path to the check_format.sh script
SCRIPT_PATH="etc/script/check_format.sh"

# Check if the script exists
if [ ! -f "$SCRIPT_PATH" ]; then
echo "Error: $SCRIPT_PATH not found!"
exit 1
fi

# Make sure the script is executable
chmod +x "$SCRIPT_PATH"

# Run the script on all staged files
#STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(cpp|cc|h|hpp|c)$')

# if [ -n "$STAGED_FILES" ]; then
# for file in $STAGED_FILES; do
# "$SCRIPT_PATH" "$file"
# if [ $? -ne 0 ]; then
# echo "Commit aborted due to formatting issues."
# exit 1
# fi
# done
# fi

sh $SCRIPT_PATH

exit 0 # Path to the check_format.sh script

4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.

AUX_SOURCE_DIRECTORY(. kiwi_SRC)
AUX_SOURCE_DIRECTORY(. KIWI_SRC)

ADD_EXECUTABLE(kiwi ${kiwi_SRC})
ADD_EXECUTABLE(kiwi ${KIWI_SRC})

IF (CMAKE_BUILD_TYPE STREQUAL "Release")
# get current date and time and git commit id
Expand Down
2 changes: 1 addition & 1 deletion src/net/base_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class BaseEvent : public std::enable_shared_from_this<BaseEvent> {
const static int EVENT_NULL;

BaseEvent(const std::shared_ptr<NetEvent> &listen, int8_t mode, int8_t type)
: listen_(listen), mode_(mode), type_(type){};
: listen_(listen), mode_(mode), type_(type) {};

virtual ~BaseEvent() = default;

Expand Down
2 changes: 1 addition & 1 deletion src/net/client_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace net {

class ClientSocket : public StreamSocket {
public:
explicit ClientSocket(const SocketAddr& addr) : StreamSocket(0, SOCKET_TCP), addr_(addr){};
explicit ClientSocket(const SocketAddr& addr) : StreamSocket(0, SOCKET_TCP), addr_(addr) {};

~ClientSocket() override = default;

Expand Down
2 changes: 1 addition & 1 deletion src/net/epoll_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace net {
class EpollEvent : public BaseEvent {
public:
explicit EpollEvent(const std::shared_ptr<NetEvent> &listen, int8_t mode)
: BaseEvent(listen, mode, BaseEvent::EVENT_TYPE_EPOLL){};
: BaseEvent(listen, mode, BaseEvent::EVENT_TYPE_EPOLL) {};

~EpollEvent() override { Close(); }

Expand Down
2 changes: 1 addition & 1 deletion src/net/io_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace net {

class IOThread {
public:
explicit IOThread(const std::shared_ptr<BaseEvent> &event) : baseEvent_(event){};
explicit IOThread(const std::shared_ptr<BaseEvent> &event) : baseEvent_(event) {};

~IOThread() = default;

Expand Down
2 changes: 1 addition & 1 deletion src/net/kqueue_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace net {
class KqueueEvent : public BaseEvent {
public:
explicit KqueueEvent(std::shared_ptr<NetEvent> listen, int8_t mode)
: BaseEvent(std::move(listen), mode, BaseEvent::EVENT_TYPE_KQUEUE){};
: BaseEvent(std::move(listen), mode, BaseEvent::EVENT_TYPE_KQUEUE) {};

~KqueueEvent() override { Close(); }

Expand Down
2 changes: 2 additions & 0 deletions src/std/memory_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>

#include <cassert>
#include <cstring>
#include <tuple>

#include "memory_file.h"

Expand Down

0 comments on commit 8acd3ce

Please sign in to comment.