Skip to content

Instantly share code, notes, and snippets.

@taniomi
Created May 22, 2025 21:37
Show Gist options
  • Select an option

  • Save taniomi/89c5b212a83482bedde173ea7694268a to your computer and use it in GitHub Desktop.

Select an option

Save taniomi/89c5b212a83482bedde173ea7694268a to your computer and use it in GitHub Desktop.
Bump version from project in `pyproject.toml`, commit the bump and `git tag` it
import sys
import re
from pathlib import Path
import subprocess
def bump_version(file_path, version):
"""Replace the version string in the given file."""
content = Path(file_path).read_text(encoding="utf-8")
new_content = re.sub(r'version\s*=\s*"[v]?[0-9]+\.[0-9]+\.[0-9]+"', f'version = "{version}"', content)
Path(file_path).write_text(new_content, encoding="utf-8")
def main():
if len(sys.argv) != 2:
print("Usage: python bump_version.py vX.Y.Z")
sys.exit(1)
raw_version = sys.argv[1]
if not raw_version.startswith("v"):
print("❌ Version must start with 'v', e.g., v0.2.2")
sys.exit(1)
plain_version = raw_version.lstrip("v")
print(f"Bumping version to {plain_version} in pyproject.toml...")
bump_version("pyproject.toml", plain_version)
subprocess.run(["git", "add", "pyproject.toml"])
subprocess.run(["git", "commit", "-m", f"chore: bump version to {plain_version}"])
subprocess.run(["git", "tag", raw_version])
print("✅ Version bump complete.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment