Files

34 lines
1.2 KiB
Python

"""Stage the next version, based on HEAD so an aborted commit can be retried."""
from pathlib import Path
import re
import subprocess
def git(*args, check=True):
return subprocess.run(['git', *args], check=check, capture_output=True, text=True)
def main():
root = Path(git('rev-parse', '--show-toplevel').stdout.strip())
previous = git('show', 'HEAD:VERSION', check=False)
if previous.returncode:
# Allow adoption in an existing repository or the first commit.
tracked = git('ls-tree', '--name-only', 'HEAD', '--', 'VERSION', check=False)
if tracked.stdout.strip():
raise SystemExit('Cannot read HEAD:VERSION; commit cancelled.')
version = '0.0.0'
else:
version = previous.stdout.strip()
match = re.fullmatch(r'(\d+)\.(\d+)\.(\d+)', version)
if not match:
raise SystemExit('Invalid HEAD:VERSION; expected major.minor.patch.')
major, minor, patch = map(int, match.groups())
next_version = f'{major}.{minor}.{patch + 1}'
(root / 'VERSION').write_text(next_version + '\n', encoding='utf-8')
git('add', '--', 'VERSION')
print(f'Documentation version: {next_version}')
if __name__ == '__main__':
main()