From 007cc20686f04ca1f911975f20f097175dd72a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Mon, 14 Oct 2024 20:18:36 +0200 Subject: [PATCH] build: make githooks reusable --- .githooks/post-commit.py | 22 ++++++++++++++-------- .githooks/pre-push.py | 14 ++++++++++---- layouts/partials/version.txt | 2 +- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/.githooks/post-commit.py b/.githooks/post-commit.py index 4ca8a48c72..dc81097e3e 100644 --- a/.githooks/post-commit.py +++ b/.githooks/post-commit.py @@ -7,15 +7,21 @@ # Linux, Windows and MacOS) # #!/bin/sh -# echo 'execute .githooks/post-commit.py' >> .githooks/hooks.log # python3 .githooks/post-commit.py from datetime import datetime +import os import re import subprocess def main(): + script_name = "POST-COMMIT" + script_dir = os.path.dirname(os.path.abspath(__file__)) + log_file = os.path.join(script_dir, "hooks.log") time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + repo_root = subprocess.check_output(['git', 'rev-parse', '--show-toplevel'], universal_newlines=True).strip() + repo_name = os.path.basename(repo_root) + file_path = 'layouts/partials/version.txt' with open(file_path, 'r+') as f: version = f.read().strip() @@ -25,8 +31,8 @@ def main(): semver = match.group(1) old_hash = match.group(2) new_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD~1']).decode('utf-8').strip() - print(f'{time}: post-commit - old hash {old_hash} - new hash {new_hash}', file=open(".githooks/hooks.log", "a")) - print(f'post-commit - old hash {old_hash} - new hash {new_hash}') + print(f'{time}: {repo_name} - {script_name} - old hash {old_hash} - new hash {new_hash}', file=open(log_file, "a")) + print(f'{script_name} - old hash {old_hash} - new hash {new_hash}') if old_hash != new_hash: new_version = f'{semver}+{new_hash}' f.seek(0) @@ -36,14 +42,14 @@ def main(): subprocess.check_call(['git', 'add', file_path]) subprocess.check_call(['git', 'commit', '--amend', '--no-edit']) else: - print(f'{time}: post-commit - No change in hash, file {file_path} not updated', file=open(".githooks/hooks.log", "a")) - print(f'post-commit - No change in hash, file {file_path} not updated') + print(f'{time}: {repo_name} - {script_name} - No change in hash, file {file_path} not updated', file=open(log_file, "a")) + print(f'{script_name} - No change in hash, file {file_path} not updated') exit(0) else: - print(f'{time}: post-commit - Invalid version format in {file_path}', file=open(".githooks/hooks.log", "a")) - print(f'post-commit - Invalid version format in {file_path}') + print(f'{time}: {repo_name} - {script_name} - Invalid version format in {file_path}', file=open(log_file, "a")) + print(f'{script_name} - Invalid version format in {file_path}') exit(1) - print(f'{time}: post-commit - New version {new_version} was written to {file_path}', file=open(".githooks/hooks.log", "a")) + print(f'{time}: {repo_name} - {script_name} - New version {new_version} was written to {file_path}', file=open(log_file, "a")) exit(0) if __name__ == '__main__': diff --git a/.githooks/pre-push.py b/.githooks/pre-push.py index c5d3726017..0016900f2f 100644 --- a/.githooks/pre-push.py +++ b/.githooks/pre-push.py @@ -10,10 +10,10 @@ # Linux, Windows and MacOS) # #!/bin/sh -# echo 'execute .githooks/pre-push.py' >> .githooks/hooks.log # python3 .githooks/pre-push.py from datetime import datetime +import os import re import subprocess @@ -29,14 +29,20 @@ import subprocess # an "#" (which are work in progress). def main(): + script_name = "PRE-PUSH" + script_dir = os.path.dirname(os.path.abspath(__file__)) + log_file = os.path.join(script_dir, "hooks.log") time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + repo_root = subprocess.check_output(['git', 'rev-parse', '--show-toplevel'], universal_newlines=True).strip() + repo_name = os.path.basename(repo_root) + local_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], universal_newlines=True).strip() wip_prefix = '^#\\d+(?:\\b.*)$' if re.match(wip_prefix, local_branch): - print(f'{time}: PRE-PUSH - Branch "{local_branch}" was not pushed because its name starts with a "#" which marks it as work in progress', file=open(".githooks/hooks.log", "a")) - print(f'PRE-PUSH - Branch "{local_branch}" was not pushed because its name starts with a "#" which marks it as work in progress') + print(f'{time}: {repo_name} - {script_name} - Branch "{local_branch}" was not pushed because its name starts with a "#" which marks it as work in progress', file=open(log_file, "a")) + print(f'{script_name} - Branch "{local_branch}" was not pushed because its name starts with a "#" which marks it as work in progress') exit(1) - print(f'{time}: PRE-PUSH - Branch "{local_branch}" was pushed', file=open(".githooks/hooks.log", "a")) + print(f'{time}: {repo_name} - {script_name} - Branch "{local_branch}" was pushed', file=open(log_file, "a")) exit(0) if __name__ == "__main__": diff --git a/layouts/partials/version.txt b/layouts/partials/version.txt index 2f40e13722..e29c7c2e68 100644 --- a/layouts/partials/version.txt +++ b/layouts/partials/version.txt @@ -1 +1 @@ -7.0.0+179369e4b4527c0a34abf2783070181d2a202b3b \ No newline at end of file +7.0.0+79aeddcea3a22e8d8b2ce9cd162adaf36653fc5f \ No newline at end of file