Add sample pre-commit hook (#2470)

Using pre-commit hooks helps us to decrease CI load and improve developers' experience.
This commit is contained in:
Maks Litskevich 2023-08-18 03:40:05 +01:00 committed by GitHub
parent 2cb701f7f3
commit 0e12cdec48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 0 deletions

32
ci/pre_commit_hook_sample Executable file
View File

@ -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 <path_to_wamr>/.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

15
ci/setup.sh Executable file
View File

@ -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"