blob: dc3156c815d8daeff5f7a09a8d2b3d5c28137356 [file] [log] [blame]
Eanna O Cathain48f526b2022-09-28 14:54:58 +01001#!/usr/bin/env python3
Alex Tawsedaba3cf2023-09-29 15:55:38 +01002# SPDX-FileCopyrightText: Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
Eanna O Cathain48f526b2022-09-28 14:54:58 +01003#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
Alex Tawsedaba3cf2023-09-29 15:55:38 +010015"""
16Adds the git hooks script into the appropriate location
17"""
Eanna O Cathain48f526b2022-09-28 14:54:58 +010018import argparse
Alex Tawsedaba3cf2023-09-29 15:55:38 +010019import os
20import shutil
Eanna O Cathain48f526b2022-09-28 14:54:58 +010021import subprocess
Alex Tawsedaba3cf2023-09-29 15:55:38 +010022import sys
23from pathlib import Path
Eanna O Cathain48f526b2022-09-28 14:54:58 +010024
Alex Tawsedaba3cf2023-09-29 15:55:38 +010025HOOKS_SCRIPT = "git_pre_push_hooks.sh"
Eanna O Cathain48f526b2022-09-28 14:54:58 +010026
Alex Tawsedaba3cf2023-09-29 15:55:38 +010027
28def set_hooks_dir(hooks_dir: str):
29 """
30 Set the hooks path in the git configuration
31 @param hooks_dir: The hooks directory
32 """
33 command = f'git config core.hooksPath {hooks_dir}'
34 with subprocess.Popen(command.split(), stdout=subprocess.PIPE) as process:
35 process.communicate()
36 return_code = process.returncode
37
38 if return_code != 0:
39 raise RuntimeError(f"Could not configure git hooks path, exited with code {return_code}")
40
41
42def add_pre_push_hooks(hooks_dir: str):
43 """
44 Copies the git hooks scripts into the specified location
45 @param hooks_dir: The specified git hooks directory
46 """
Eanna O Cathain48f526b2022-09-28 14:54:58 +010047 pre_push = "pre-push"
48 file_path = os.path.join(hooks_dir, pre_push)
49 file_exists = os.path.exists(file_path)
50 if file_exists:
51 os.remove(file_path)
Eanna O Cathain48f526b2022-09-28 14:54:58 +010052
Alex Tawsedaba3cf2023-09-29 15:55:38 +010053 script_path = Path(__file__).resolve().parent / HOOKS_SCRIPT
54 shutil.copy(script_path, hooks_dir)
Eanna O Cathain48f526b2022-09-28 14:54:58 +010055
Eanna O Cathain48f526b2022-09-28 14:54:58 +010056
Alex Tawsedaba3cf2023-09-29 15:55:38 +010057if __name__ == "__main__":
58 parser = argparse.ArgumentParser()
59 parser.add_argument("git_hooks_path")
60 args = parser.parse_args()
Eanna O Cathain48f526b2022-09-28 14:54:58 +010061
Alex Tawsedaba3cf2023-09-29 15:55:38 +010062 if not os.path.exists(args.git_hooks_path):
63 print('Error! The Git hooks directory you supplied does not exist.')
64 sys.exit()
Eanna O Cathain48f526b2022-09-28 14:54:58 +010065
Alex Tawsedaba3cf2023-09-29 15:55:38 +010066 add_pre_push_hooks(args.git_hooks_path)
67 set_hooks_dir(args.git_hooks_path)