Eanna O Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 2 | # SPDX-FileCopyrightText: Copyright 2022-2023 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 | # |
| 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 Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 15 | """ |
| 16 | Adds the git hooks script into the appropriate location |
| 17 | """ |
Eanna O Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 18 | import argparse |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 19 | import os |
| 20 | import shutil |
Eanna O Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 21 | import subprocess |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 22 | import sys |
| 23 | from pathlib import Path |
Eanna O Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 24 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 25 | HOOKS_SCRIPT = "git_pre_push_hooks.sh" |
Eanna O Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 26 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 27 | |
| 28 | def 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 | |
| 42 | def 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 Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 47 | 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 Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 52 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 53 | script_path = Path(__file__).resolve().parent / HOOKS_SCRIPT |
| 54 | shutil.copy(script_path, hooks_dir) |
Eanna O Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 55 | |
Eanna O Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 56 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 57 | if __name__ == "__main__": |
| 58 | parser = argparse.ArgumentParser() |
| 59 | parser.add_argument("git_hooks_path") |
| 60 | args = parser.parse_args() |
Eanna O Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 61 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 62 | 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 Cathain | 48f526b | 2022-09-28 14:54:58 +0100 | [diff] [blame] | 65 | |
Alex Tawse | daba3cf | 2023-09-29 15:55:38 +0100 | [diff] [blame] | 66 | add_pre_push_hooks(args.git_hooks_path) |
| 67 | set_hooks_dir(args.git_hooks_path) |