Versioning It with your Version In Git - automatic package versioning based on VCS tags
—
Main functions for determining and calculating versions from version control systems. These functions provide the primary interface for most use cases, with automatic configuration loading and fallback support.
Get the current version for a project based on VCS tags and configuration.
def get_version(
project_dir: str | Path = os.curdir,
config: Optional[dict] = None,
write: bool = False,
fallback: bool = True,
) -> str:
"""
Determine the version for the project at project_dir.
Parameters:
- project_dir: Path to project root (default: current directory)
- config: Configuration dict or None to read from pyproject.toml/versioningit.toml
- write: Whether to write version to file specified in configuration
- fallback: Whether to read from PKG-INFO if not under version control
Returns:
str: The calculated version string
Raises:
- NotVCSError: if fallback is False and project_dir is not under version control
- NotSdistError: if fallback is True, not under VCS, and no PKG-INFO file
- NoConfigFileError: if config is None and no configuration file exists
- NoConfigSectionError: if configuration file has no versioningit section
- ConfigError: if configuration values are invalid
- MethodError: if a method returns wrong type
"""Calculate the next version after the current VCS-tagged version.
def get_next_version(
project_dir: str | Path = os.curdir,
config: Optional[dict] = None
) -> str:
"""
Determine the next version after the current VCS-tagged version.
Parameters:
- project_dir: Path to project root (default: current directory)
- config: Configuration dict or None to read from configuration file
Returns:
str: The calculated next version string
Raises:
- NotVCSError: if project_dir is not under version control
- NoConfigFileError: if config is None and no configuration file exists
- NoConfigSectionError: if configuration file has no versioningit section
- ConfigError: if configuration values are invalid
- MethodError: if a method returns wrong type
"""Extract version from PKG-INFO file in source distributions.
def get_version_from_pkg_info(project_dir: str | Path) -> str:
"""
Return the Version field from the PKG-INFO file in project_dir.
Parameters:
- project_dir: Path to directory containing PKG-INFO file
Returns:
str: Version string from PKG-INFO file
Raises:
- NotSdistError: if there is no PKG-INFO file
- ValueError: if PKG-INFO file does not contain a Version field
"""from versioningit import get_version
# Get version using default configuration
version = get_version()
print(f"Current version: {version}")
# Get version for specific project directory
version = get_version("/path/to/project")
# Get version and write to configured file
version = get_version(write=True)config = {
"vcs": {
"method": "git",
"match": ["v*.*.*"],
"exclude": ["*rc*", "*dev*"]
},
"tag2version": {
"method": "basic",
"rmprefix": "v"
},
"next-version": {
"method": "minor"
},
"format": {
"distance": "{version}.post{distance}+{vcs}{rev}",
"dirty": "{version}+d{build_date:%Y%m%d}",
"distance-dirty": "{version}.post{distance}+{vcs}{rev}.d{build_date:%Y%m%d}"
},
"write": {
"file": "src/mypackage/_version.py",
"template": '__version__ = "{version}"'
}
}
version = get_version(config=config, write=True)from versioningit import get_next_version
# Calculate next version
current = get_version() # e.g., "1.2.3"
next_ver = get_next_version() # e.g., "1.3.0" (depending on next-version method)
print(f"Current: {current}, Next: {next_ver}")from versioningit import get_version, NotVCSError, NoConfigFileError
try:
version = get_version(fallback=False)
except NotVCSError:
print("Project is not under version control")
except NoConfigFileError:
print("No pyproject.toml or versioningit.toml file found")# With fallback enabled (default), will read from PKG-INFO if not under VCS
version = get_version(fallback=True)
# Disable fallback to ensure version comes from VCS
try:
version = get_version(fallback=False)
except NotVCSError:
print("Must be run from VCS repository")Install with Tessl CLI
npx tessl i tessl/pypi-versioningit