I am hoping to have git create a version.py file that looks like this (where the version is incremented each commit):
# This file is created by the pre-push script
class Version:
comment = "git commit comment"
hash = "some git hash"
version = "0.8.9"
I have tried this:
#!/usr/bin/env /usr/bin/python
import os
import subprocess
import re
import sys
commit_msg_file = sys.argv[1]
with open(commit_msg_file, 'r') as file:
commit_msg = file.read().strip()
version_file = os.path.abspath('version.py')
hashed_code = subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip().decode('utf-8')
if os.path.exists(version_file):
print(f'Reading previous {version_file}')
with open(version_file, 'r') as f:
content = f.read()
major, minor, patch = map(int, re.search(r'version = "(d+).(d+).(d+)"', content).groups())
patch += 1
else:
print(f'Creating new {version_file}')
major, minor, patch = 0, 0, 1
print(f'Writing contents of {version_file} with "{commit_msg}"')
with open(version_file, 'w') as f:
f.write(f'''# This file is created by the pre-push script
class Version:
comment = "{commit_msg}"
hash = "{hashed_code}"
version = "{major}.{minor}.{patch}"
if __name__ == "__main__":
print(Version.version)
''')
f.close()
print(f'adding {version_file}')
subprocess.call(['git', 'add', version_file])
# subprocess.call(['git', 'commit', '-m', comment])
print(f'adding {version_file}')
subprocess.call(['git', 'add', version_file])
# subprocess.call(['git', 'commit', '-m', comment])
I haved tried this in pre-push and repare-commit-msg to no avail… Is there a better way or a way to fix what I have?
thanks in advance