pre_commit_hook_sample 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. #!/bin/bash
  2. # Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. # This is a sample of pre-commit hook that can be used to make your code fit the WAMR CI code style requirements.
  5. # You need to have clang-format-12 installed to use this hook.
  6. # To add this pre-commit hook, copy it to <path_to_wamr>/.git/hooks/pre-commit
  7. # (you don't need any extensions here)
  8. # Function to check if a file has a C or C++ extension
  9. is_c_or_cpp_file() {
  10. file="$1"
  11. if [[ "$filename" =~ \.(h|c|cpp)$ ]]; then
  12. return 0
  13. else
  14. return 1
  15. fi
  16. }
  17. # Loop through staged files and apply command "abc" to C and C++ files
  18. for staged_file in $(git diff --cached --name-only); do
  19. if is_c_or_cpp_file "$staged_file"; then
  20. clang-format-12 -Werror --style file --dry-run "$staged_file" 2>/dev/null
  21. if [ $? -ne 0 ]; then
  22. echo "Issues are found in $staged_file. Applying the fix"
  23. clang-format-12 --style file -i "$staged_file"
  24. fi
  25. git add "$staged_file" # Add the modified file back to staging
  26. fi
  27. done