Skip to main content

semver-ratchet (Python)

Python implementation of forward-only versioning for Trunk-Based Development.

Dynamic Versioning

semver-ratchet calculates versions dynamically from your git state. Use it as a build backend to inject versions at build time without maintaining version files.

Feature Branches: 0.{CRC32(branch_name)}.{GitDistance}

Feature branches get ephemeral versions. The minor component is a CRC32 hash of the branch name, and the patch is the commit distance from the trunk branch.

Main Branch: Major.Minor.Patch

The main branch follows Semantic Versioning 2.0.0. Bumps are determined by [MAJOR], [MINOR], or [PATCH] tags in commit messages.

Dynamic Version in pyproject.toml

Use semver-ratchet as your build backend for fully automatic versioning:

[build-system]
requires = ["semver-ratchet", "setuptools>=79.0.1"]
build-backend = "semver_ratchet.build"

[project]
name = "your-project"
dynamic = ["version"]

When setuptools builds your project, it calls semver_ratchet.build.get_version() which reads git state and returns the calculated version.

Alternative: Build-time Script

# setup.py or build script
import subprocess


def get_version():
    result = subprocess.run(["semver-ratchet", "version", "--no-verify"], capture_output=True, text=True, check=True)
    return result.stdout.strip()

Installation

As a CLI Tool

uv pip install semver-ratchet

As a Library Dependency

# pyproject.toml
[project]
dependencies = ["semver-ratchet"]

As a Build Backend

[build-system]
requires = ["semver-ratchet", "setuptools>=79.0.1"]
build-backend = "semver_ratchet.build"

[project]
dynamic = ["version"]

⚠️ Caveat: The build backend resolves config from pyproject.toml by walking up from os.getcwd(). This works when building from the repo root (pip install ., python -m build), but fails silently in isolated build environments (uv build frontends, PEP 517 isolated builds) where the cwd is a temp directory. In those cases, the version falls back to 0.0.0.

For CI with isolated builds, use semver-ratchet as a runtime dependency instead:

[build-system]
requires = ["setuptools>=61", "setuptools_scm[toml]"]
build-backend = "setuptools.build_meta"

[project]
dependencies = ["semver-ratchet"]

See docs/INTEGRATION.md for detailed workarounds.


Usage

CLI

# Get current version
semver-ratchet version

# With custom trunk name
semver-ratchet --trunk-name develop version

# Skip git history verification
semver-ratchet --no-verify version

# Display versioning info
semver-ratchet info

# Create and push tag
semver-ratchet tag --push

Module

python -m semver_ratchet version

Library API

import semver_ratchet

# Get current version
version = semver_ratchet.get_version()
print(version)  # e.g., "1.0.0" or "0.12345678.5"

# With custom trunk name
version = semver_ratchet.get_version(trunk_name="develop")

# Get compatible versions for dependency pinning
compatible = semver_ratchet.get_compatible_versions("1.2.3")
print(compatible)  # ["1.2.3", "1.2", "1"]

# Calculate CRC32 of a branch name
crc32 = semver_ratchet.calculate_crc32_unsigned("feature/login")

# Create a git tag (main branch only)
semver_ratchet.create_git_tag("1.0.0")

# Push tag to remote
semver_ratchet.push_git_tag("1.0.0")

# Verify git history
semver_ratchet.verify_git_history(50)

Configuration

Config Precedence Chain

CLI flag  >  Environment variable  >  pyproject.toml  >  Default

CLI Flags

Flag Description Default
--trunk-name <name> Trunk branch name main
--default-bump {major,minor,patch} Default bump type minor
--no-verify Skip git history verification false

Environment Variables

Variable Description Default
RATCHET_TRUNK_NAME Trunk branch name main
RATCHET_DEFAULT_BUMP Default bump type minor
RATCHET_GIT_PATH Custom git binary path git
RATCHET_VERIFY_MINDEPTH Minimum git history depth 50
RATCHET_MANUAL_VERSION Manual version override (none)
RATCHET_OVERRIDE (deprecated) Same as above (none)

Config File (pyproject.toml)

[tool.semver-ratchet]
trunk-name = "main"
default-bump = "minor"
verify-mindepth = 50
git-path = "/usr/local/bin/git"

Keys accept both kebab-case and snake_case.


API Reference

get_version(default_bump=None, verify_history=True, trunk_name=None, adapter=None)

Get the appropriate version based on current branch.

  • Parameters:
    • default_bump (str, optional): Default bump type
    • verify_history (bool): Verify sufficient git history (default: True)
    • trunk_name (str, optional): Trunk branch name override
    • adapter (GitAdapter, optional): Custom git adapter (for testing)
  • Returns: str — Version string

calculate_feature_version(verify_history=True, trunk_name=None, adapter=None)

Calculate version for a feature branch.

  • Returns: str — Version in format 0.{CRC32}.{Distance}

calculate_main_version(default_bump="patch", verify_history=True, trunk_name=None, adapter=None)

Calculate version for main branch using SemVer.

  • Returns: str — Version in format Major.Minor.Patch

get_compatible_versions(version)

Generate compatible version strings for dependency pinning.

  • Parameters: version (str) — Full version string
  • Returns: list[str] — Array of compatible versions

calculate_crc32_unsigned(s)

Calculate CRC32 checksum as unsigned 32-bit integer.

  • Parameters: s (str) — String to hash
  • Returns: int — Unsigned 32-bit CRC32 value

get_current_branch(adapter=None)

Get the current Git branch name.

  • Returns: str

is_main_branch(trunk_name=None, adapter=None)

Check if current branch is the trunk branch.

  • Returns: bool

determine_version_bump(commit_messages, default_bump="patch")

Determine version bump from commit messages.

  • Returns: str — "major", "minor", or "patch"

create_git_tag(version, force=False)

Create a Git tag for the given version.

  • Returns: bool

push_git_tag(version, remote="origin")

Push a Git tag to remote repository.

  • Returns: bool

verify_git_history(fetch_depth=50)

Verify sufficient git history is available.

  • Raises: RuntimeError if insufficient history

parse_semver(version)

Parse a SemVer string into components.

  • Returns: tuple[int, int, int] — (major, minor, patch)

compare_semver(v1, v2)

Compare two SemVer versions.

  • Returns: int — -1 if v1 < v2, 0 if equal, 1 if v1 > v2

Testing

# Run all tests
uv run pytest

# Run with coverage
uv run pytest --cov=semver_ratchet

# Type checking
uvx ty check

# Linting
uvx ruff check .

License

MIT License. See LICENSE for details.

Metadata

Release files for semver-ratchet 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for semver-ratchet 1.4.0
File Size Uploaded Provenance Actions
semver_ratchet-1.4.0.tar.gz 35.2 kB Without provenance Details for semver_ratchet-1.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for semver-ratchet 1.4.0
File Interpreter ABI Platform Actions
semver_ratchet-1.4.0-py3-none-any.whl Python 3 none any Details for semver_ratchet-1.4.0-py3-none-any.whl

Total release size: 58.2 kB

Release files / semver_ratchet-1.4.0.tar.gz

Download URL semver_ratchet-1.4.0.tar.gz
Size 35.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d1e0e0d5ed56a5bd1050c5693523b9e210903ac5a41da43209cd42443b243349
BLAKE2b-256 checksum
How to use checksums
21d0adfcaac9281f3683adbb2b44ed58c143ff69fa666d608f956216fd050e41
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / semver_ratchet-1.4.0-py3-none-any.whl

Download URL semver_ratchet-1.4.0-py3-none-any.whl
Size 23.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
068598bf2f2766f12969f7c216cea8f48a625fc72000bb2d05f2a3c59226be54
BLAKE2b-256 checksum
How to use checksums
d2d1f175bab7adeb5a59d3896a7931126275b4b3d8d931a997ad42576923e670
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for semver-ratchet 1.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page