diff --git a/.githooks/post-commit.py b/.githooks/post-commit.py new file mode 100644 index 0000000000..4ca8a48c72 --- /dev/null +++ b/.githooks/post-commit.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +# This script appends the current commit hash to the version information in +# `layouts/partials/version.txt` +# +# Call this script from your ".git/hooks/post-commit" file like this (supporting +# 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 re +import subprocess + +def main(): + time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + file_path = 'layouts/partials/version.txt' + with open(file_path, 'r+') as f: + version = f.read().strip() + new_version = '' + match = re.match(r'(\d+\.\d+\.\d+)(?:\+([^+]+))?', version) + if match: + 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}') + if old_hash != new_hash: + new_version = f'{semver}+{new_hash}' + f.seek(0) + f.write(new_version) + f.truncate() + f.close() + 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') + 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}') + exit(1) + print(f'{time}: post-commit - New version {new_version} was written to {file_path}', file=open(".githooks/hooks.log", "a")) + exit(0) + +if __name__ == '__main__': + main() diff --git a/.githooks/pre-commit.py b/.githooks/pre-commit.py deleted file mode 100644 index b51f139263..0000000000 --- a/.githooks/pre-commit.py +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env python3 - -# This script appends the current commit hash to the version information in -# `layouts/partials/version.txt` -# -# Call this script from your ".git/hooks/pre-commit" file like this (supporting -# Linux, Windows and MacOS) - -# #!/bin/sh -# echo 'execute .githooks/pre-commit.py' >> .githooks/hooks.log -# python3 .githooks/pre-commit.py - -from datetime import datetime -import re -import subprocess - -def main(): - time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - file_path = 'layouts/partials/version.txt' - with open(file_path, 'r+') as f: - version = f.read().strip() - new_version = '' - match = re.match(r'(\d+\.\d+\.\d+)(\+[^+]+)?', version) - if match: - semver = match.group(1) - commit_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD~1']).decode('utf-8').strip() - new_version = f'{semver}+{commit_hash}' - f.seek(0) - f.write(new_version) - f.truncate() - subprocess.check_call(['git', 'add', file_path]) - else: - print(f'{time}: PRE-COMMIT - Invalid version format in {file_path}', file=open(".githooks/hooks.log", "a")) - print(f'PRE-COMMIT - Invalid version format in {file_path}') - exit(1) - print(f'{time}: PRE-COMMIT - New version {new_version} was written to {file_path}', file=open(".githooks/hooks.log", "a")) - exit(0) - -if __name__ == '__main__': - main() diff --git a/layouts/partials/version.txt b/layouts/partials/version.txt index 05d0d3c12d..e821feb1a5 100644 --- a/layouts/partials/version.txt +++ b/layouts/partials/version.txt @@ -1 +1 @@ -6.4.0+0294364d6fb83fb48f259fa0235ff177d305d98a \ No newline at end of file +6.4.0+3166e40fd4702b1d134b484ac40ef5b572343876 \ No newline at end of file