Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # SPDX-FileCopyrightText: Copyright 2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # Called by "git push" with no arguments. The hook should |
| 18 | # exit with non-zero status after issuing an appropriate message if |
| 19 | # it wants to stop the push. |
| 20 | |
| 21 | # shellcheck disable=SC2034,SC2162 |
| 22 | while read local_ref local_sha remote_ref remote_sha; do |
| 23 | # We should pass only added or modified C/C++ source files to cppcheck. |
| 24 | changed_files=$(git diff --name-only HEAD~1 HEAD | grep -iE "\.(c|cpp|cxx|cc|h|hpp|hxx)$" | cut -f 2) |
| 25 | if [ -n "$changed_files" ]; then |
| 26 | # shellcheck disable=SC2086 |
| 27 | clang-format -style=file --dry-run --Werror $changed_files |
| 28 | |
| 29 | exitcode1=$? |
| 30 | if [ $exitcode1 -ne 0 ]; then |
| 31 | echo "Formatting errors found in file: $changed_files. \ |
| 32 | Please run: |
| 33 | \"clang-format -style=file -i $changed_files\" |
| 34 | to correct these errors" |
| 35 | exit $exitcode1 |
| 36 | fi |
| 37 | |
| 38 | # shellcheck disable=SC2086 |
| 39 | cppcheck --enable=performance,portability --error-exitcode=1 --suppress=*:tests* $changed_files |
| 40 | exitcode2=$? |
| 41 | if [ $exitcode2 -ne 0 ]; then |
| 42 | exit $exitcode2 |
| 43 | fi |
| 44 | fi |
| 45 | exit 0 |
| 46 | done |
| 47 | |
| 48 | exit 0 |