From 0e12cdec487e1c7c55d6212c82e7534bb5f2199e Mon Sep 17 00:00:00 2001 From: Maks Litskevich Date: Fri, 18 Aug 2023 03:40:05 +0100 Subject: [PATCH] Add sample pre-commit hook (#2470) Using pre-commit hooks helps us to decrease CI load and improve developers' experience. --- ci/pre_commit_hook_sample | 32 ++++++++++++++++++++++++++++++++ ci/setup.sh | 15 +++++++++++++++ 2 files changed, 47 insertions(+) create mode 100755 ci/pre_commit_hook_sample create mode 100755 ci/setup.sh diff --git a/ci/pre_commit_hook_sample b/ci/pre_commit_hook_sample new file mode 100755 index 000000000..682e78946 --- /dev/null +++ b/ci/pre_commit_hook_sample @@ -0,0 +1,32 @@ +#!/bin/bash + +# Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +# This is a sample of pre-commit hook that can be used to make your code fit the WAMR CI code style requirements. +# You need to have clang-format-12 installed to use this hook. +# To add this pre-commit hook, copy it to /.git/hooks/pre-commit +# (you don't need any extensions here) + +# Function to check if a file has a C or C++ extension + +is_c_or_cpp_file() { + file="$1" + if [[ "$filename" =~ \.(h|c|cpp)$ ]]; then + return 0 + else + return 1 + fi +} + +# Loop through staged files and apply command "abc" to C and C++ files +for staged_file in $(git diff --cached --name-only); do + if is_c_or_cpp_file "$staged_file"; then + clang-format-12 -Werror --style file --dry-run "$staged_file" 2>/dev/null + if [ $? -ne 0 ]; then + echo "Issues are found in $staged_file. Applying the fix" + clang-format-12 --style file -i "$staged_file" + fi + git add "$staged_file" # Add the modified file back to staging + fi +done diff --git a/ci/setup.sh b/ci/setup.sh new file mode 100755 index 000000000..ebdf73af5 --- /dev/null +++ b/ci/setup.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +# This script executes some commands to make your onboarding with WAMR easier. +# For example, setting pre-commit hook that will make your code complaint with the +# code style requirements checked in WAMR CI + +echo "Copy the pre-commit hook to your hooks folder" +cp pre_commit_hook_sample ../.git/hooks/pre-commit + +# Feel free to propose your commands to this script to make developing WAMR easier + +echo "Setup is done"