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"]

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 patch
--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.3.1

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.3.1
File Size Uploaded Provenance Actions
semver_ratchet-1.3.1.tar.gz 31.1 kB Without provenance Details for semver_ratchet-1.3.1.tar.gz

Built distribution (wheel)

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

Total release size: 52.4 kB

Release files / semver_ratchet-1.3.1.tar.gz

Download URL semver_ratchet-1.3.1.tar.gz
Size 31.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7f343db7f8a6e8c81f6182b41cce68094582d2a7eff3d2213a265694618f827d
BLAKE2b-256 checksum
How to use checksums
f37155106eacf799d997af5f980177454e24bd2d76474775a3e06a7827bca7b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","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.3.1-py3-none-any.whl

Download URL semver_ratchet-1.3.1-py3-none-any.whl
Size 21.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
80559ce73cd1ab81f2ec8ad91b90841b770671fcd17c318cd5f463c385ed9b1d
BLAKE2b-256 checksum
How to use checksums
b4c0cd7a00e14792a08f9be4dd1c78cb354f423e8e07f180c8bd643277d6e677
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","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.3.1

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