build: write commit hash into version.txt

This commit is contained in:
Sören Weber 2024-09-25 23:28:37 +02:00
parent d6186c0b6d
commit 1bf4613d26
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
4 changed files with 44 additions and 15 deletions

40
.githooks/pre-commit.py Normal file
View file

@ -0,0 +1,40 @@
#!/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()

View file

@ -33,10 +33,10 @@ def main():
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}: 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'Branch "{local_branch}" was not pushed because its name starts with a "#" which marks it as work in progress')
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')
exit(1)
print(f'{time}: Branch "{local_branch}" was pushed', file=open(".githooks/hooks.log", "a"))
print(f'{time}: PRE-PUSH - Branch "{local_branch}" was pushed', file=open(".githooks/hooks.log", "a"))
exit(0)
if __name__ == "__main__":

View file

@ -213,17 +213,6 @@ runs:
${{ steps.changelog_github.outputs.value }}
tag: ${{ env.MILESTONE }}
- name: Update version number to mark non-release version
shell: bash
env:
MILESTONE: ${{ inputs.milestone }}
GITHUB_TOKEN: ${{ inputs.github_token }}
run: |
echo -n "$MILESTONE+tip" > layouts/partials/version.txt
git add *
git commit --message "Mark non-release version"
git push origin main
- name: Create next patch milestone
uses: WyriHaximus/github-action-create-milestone@v1
continue-on-error: true

View file

@ -1 +1 @@
6.4.1+tip
6.4.0+c484ba60edc0cc42cb36c0f8c7f34c1b10b44f89