blob: 97a28615c397527951bd46aa65072012f2ef3085 [file] [log] [blame]
Eanna O Cathain48f526b2022-09-28 14:54:58 +01001#!/usr/bin/env python3
Richard Burtonf32a86a2022-11-15 11:46:11 +00002# SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
Eanna O Cathain48f526b2022-09-28 14:54:58 +01003# 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
17import os
18import sys
19import argparse
20import subprocess
21import stat
22
23def set_hooks_dir(hooks_dir):
24 command = 'git config core.hooksPath {}'.format(hooks_dir)
25 subprocess.Popen(command.split(), stdout=subprocess.PIPE)
26
27def 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 Burtonf32a86a2022-11-15 11:46:11 +000036# SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
Eanna O Cathain48f526b2022-09-28 14:54:58 +010037# 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
55while read local_ref local_sha remote_ref remote_sha
56do
57 # We should pass only added or modified C/C++ source files to cppcheck.
58 changed_files=$(git diff --name-only HEAD~1 HEAD | grep -E "*\.(c|cpp|cc|cxx)" | cut -f 2)
59 if [ -n "$changed_files" ]; then
60 clang-format -style=file --dry-run --Werror $changed_files
61
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
69
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
77done
78
79exit 0'''
80)
81
82 f.close()
83 s = os.stat(file_path)
84 os.chmod(file_path, s.st_mode | stat.S_IEXEC)
85
86parser = argparse.ArgumentParser()
87parser.add_argument("git_hooks_path")
88args = parser.parse_args()
89
90dir_exists = os.path.exists(args.git_hooks_path)
91if not dir_exists:
92 print('Error! The Git hooks directory you supplied does not exist.')
93 sys.exit()
94
95add_pre_push_hooks(args.git_hooks_path)
96set_hooks_dir(args.git_hooks_path)