build: make githooks reusable

This commit is contained in:
Sören Weber 2024-10-14 20:18:36 +02:00
parent 79aeddcea3
commit 007cc20686
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
3 changed files with 25 additions and 13 deletions

View file

@ -7,15 +7,21 @@
# Linux, Windows and MacOS) # Linux, Windows and MacOS)
# #!/bin/sh # #!/bin/sh
# echo 'execute .githooks/post-commit.py' >> .githooks/hooks.log
# python3 .githooks/post-commit.py # python3 .githooks/post-commit.py
from datetime import datetime from datetime import datetime
import os
import re import re
import subprocess import subprocess
def main(): 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") 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' file_path = 'layouts/partials/version.txt'
with open(file_path, 'r+') as f: with open(file_path, 'r+') as f:
version = f.read().strip() version = f.read().strip()
@ -25,8 +31,8 @@ def main():
semver = match.group(1) semver = match.group(1)
old_hash = match.group(2) old_hash = match.group(2)
new_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD~1']).decode('utf-8').strip() 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'{time}: {repo_name} - {script_name} - old hash {old_hash} - new hash {new_hash}', file=open(log_file, "a"))
print(f'post-commit - old hash {old_hash} - new hash {new_hash}') print(f'{script_name} - old hash {old_hash} - new hash {new_hash}')
if old_hash != new_hash: if old_hash != new_hash:
new_version = f'{semver}+{new_hash}' new_version = f'{semver}+{new_hash}'
f.seek(0) f.seek(0)
@ -36,14 +42,14 @@ def main():
subprocess.check_call(['git', 'add', file_path]) subprocess.check_call(['git', 'add', file_path])
subprocess.check_call(['git', 'commit', '--amend', '--no-edit']) subprocess.check_call(['git', 'commit', '--amend', '--no-edit'])
else: else:
print(f'{time}: post-commit - No change in hash, file {file_path} not updated', file=open(".githooks/hooks.log", "a")) print(f'{time}: {repo_name} - {script_name} - No change in hash, file {file_path} not updated', file=open(log_file, "a"))
print(f'post-commit - No change in hash, file {file_path} not updated') print(f'{script_name} - No change in hash, file {file_path} not updated')
exit(0) exit(0)
else: else:
print(f'{time}: post-commit - Invalid version format in {file_path}', file=open(".githooks/hooks.log", "a")) print(f'{time}: {repo_name} - {script_name} - Invalid version format in {file_path}', file=open(log_file, "a"))
print(f'post-commit - Invalid version format in {file_path}') print(f'{script_name} - Invalid version format in {file_path}')
exit(1) 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) exit(0)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -10,10 +10,10 @@
# Linux, Windows and MacOS) # Linux, Windows and MacOS)
# #!/bin/sh # #!/bin/sh
# echo 'execute .githooks/pre-push.py' >> .githooks/hooks.log
# python3 .githooks/pre-push.py # python3 .githooks/pre-push.py
from datetime import datetime from datetime import datetime
import os
import re import re
import subprocess import subprocess
@ -29,14 +29,20 @@ import subprocess
# an "#" (which are work in progress). # an "#" (which are work in progress).
def main(): 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") 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() local_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], universal_newlines=True).strip()
wip_prefix = '^#\\d+(?:\\b.*)$' wip_prefix = '^#\\d+(?:\\b.*)$'
if re.match(wip_prefix, local_branch): 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'{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'PRE-PUSH - Branch "{local_branch}" was not pushed because its name starts with a "#" which marks it as work in progress') 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) 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) exit(0)
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -1 +1 @@
7.0.0+179369e4b4527c0a34abf2783070181d2a202b3b 7.0.0+79aeddcea3a22e8d8b2ce9cd162adaf36653fc5f