Eanna O Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Richard Burton | f32a86a | 2022-11-15 11:46:11 +0000 | [diff] [blame] | 2 | # SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Eanna O Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 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 | import os |
| 18 | import sys |
| 19 | import argparse |
| 20 | import subprocess |
| 21 | import stat |
| 22 | |
| 23 | def set_hooks_dir(hooks_dir): |
| 24 | command = 'git config core.hooksPath {}'.format(hooks_dir) |
| 25 | subprocess.Popen(command.split(), stdout=subprocess.PIPE) |
| 26 | |
| 27 | def add_pre_push_hooks(hooks_dir): |
| 28 | pre_push = "pre-push" |
| 29 | file_path = os.path.join(hooks_dir, pre_push) |
| 30 | file_exists = os.path.exists(file_path) |
| 31 | if file_exists: |
| 32 | os.remove(file_path) |
| 33 | f = open(file_path, "a") |
| 34 | f.write( |
| 35 | '''#!/bin/sh |
Richard Burton | f32a86a | 2022-11-15 11:46:11 +0000 | [diff] [blame] | 36 | # SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Eanna O Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 37 | # SPDX-License-Identifier: Apache-2.0 |
| 38 | # |
| 39 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 40 | # you may not use this file except in compliance with the License. |
| 41 | # You may obtain a copy of the License at |
| 42 | # |
| 43 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 44 | # |
| 45 | # Unless required by applicable law or agreed to in writing, software |
| 46 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 47 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 48 | # See the License for the specific language governing permissions and |
| 49 | # limitations under the License. |
| 50 | |
| 51 | # Called by "git push" with no arguments. The hook should |
| 52 | # exit with non-zero status after issuing an appropriate message if |
| 53 | # it wants to stop the push. |
| 54 | |
| 55 | while read local_ref local_sha remote_ref remote_sha |
| 56 | do |
| 57 | # We should pass only added or modified C/C++ source files to cppcheck. |
Kshitij Sisodia | 47406fe | 2022-12-05 17:18:50 +0000 | [diff] [blame^] | 58 | changed_files=$(git diff --name-only HEAD~1 HEAD | grep -iE "\.(c|cpp|cxx|cc|h|hpp|hxx)$" | cut -f 2) |
| 59 | if [ -n "$changed_files" ]; then |
| 60 | clang-format -style=file --dry-run --Werror $changed_files |
Eanna O Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 61 | |
Kshitij Sisodia | 47406fe | 2022-12-05 17:18:50 +0000 | [diff] [blame^] | 62 | exitcode1=$? |
| 63 | if [ $exitcode1 -ne 0 ]; then |
| 64 | echo "Formatting errors found in file: $changed_files. |
| 65 | \nPlease run:\n\ \"clang-format -style=file -i $changed_files\" |
| 66 | \nto correct these errors" |
| 67 | exit $exitcode1 |
| 68 | fi |
Eanna O Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 69 | |
Kshitij Sisodia | 47406fe | 2022-12-05 17:18:50 +0000 | [diff] [blame^] | 70 | cppcheck --enable=performance,portability --error-exitcode=1 $changed_files |
| 71 | exitcode2=$? |
| 72 | if [ $exitcode2 -ne 0 ]; then |
| 73 | exit $exitcode2 |
| 74 | fi |
| 75 | fi |
| 76 | exit 0 |
Eanna O Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 77 | done |
| 78 | |
| 79 | exit 0''' |
| 80 | ) |
| 81 | |
| 82 | f.close() |
| 83 | s = os.stat(file_path) |
| 84 | os.chmod(file_path, s.st_mode | stat.S_IEXEC) |
| 85 | |
| 86 | parser = argparse.ArgumentParser() |
| 87 | parser.add_argument("git_hooks_path") |
| 88 | args = parser.parse_args() |
| 89 | |
| 90 | dir_exists = os.path.exists(args.git_hooks_path) |
| 91 | if not dir_exists: |
| 92 | print('Error! The Git hooks directory you supplied does not exist.') |
| 93 | sys.exit() |
| 94 | |
| 95 | add_pre_push_hooks(args.git_hooks_path) |
| 96 | set_hooks_dir(args.git_hooks_path) |