diff --git a/README.md b/README.md index 344facf..b856ee0 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,17 @@ vcs = "git" style = "semver" ``` +## Environment variables +In addition to the project-specific configuration above, +you can apply some global overrides via environment variables. + +* `UV_DYNAMIC_VERSIONING_BYPASS`: + Use this to bypass the VCS mechanisms and use a static version instead. + The value of the environment variable will be used as the version + for the active project and any path/SSH dependencies that also use the plugin. + This is mainly for distro package maintainers who need to patch existing releases, + without needing access to the original repository. + ## Alternatives - [hatch-vcs](https://github.com/ofek/hatch-vcs) diff --git a/src/uv_dynamic_versioning/main.py b/src/uv_dynamic_versioning/main.py index aae3c47..307a719 100644 --- a/src/uv_dynamic_versioning/main.py +++ b/src/uv_dynamic_versioning/main.py @@ -1,3 +1,4 @@ +import os from pathlib import Path import tomlkit @@ -25,6 +26,9 @@ def validate(project: tomlkit.TOMLDocument): @safe def get_version(config: schemas.UvDynamicVersioning) -> str: + if "UV_DYNAMIC_VERSIONING_BYPASS" in os.environ: + return os.environ["UV_DYNAMIC_VERSIONING_BYPASS"] + version = Version.from_vcs( config.vcs, latest_tag=config.latest_tag, @@ -36,6 +40,7 @@ def get_version(config: schemas.UvDynamicVersioning) -> str: pattern=config.pattern, pattern_prefix=config.pattern_prefix, ) + return version.serialize( metadata=config.metadata, style=config.style, diff --git a/tests/test_env_bypass.py b/tests/test_env_bypass.py new file mode 100644 index 0000000..d9f8bdc --- /dev/null +++ b/tests/test_env_bypass.py @@ -0,0 +1,27 @@ +import os + +import pytest + +from uv_dynamic_versioning import schemas +from uv_dynamic_versioning.main import get_version + + +@pytest.fixture +def version(): + return "1.1.1" + + +@pytest.fixture +def set_uv_dynamic_versioning_bypass(version: str): + os.environ["UV_DYNAMIC_VERSIONING_BYPASS"] = version + + try: + yield version + finally: + del os.environ["UV_DYNAMIC_VERSIONING_BYPASS"] + + +@pytest.mark.usefixtures("set_uv_dynamic_versioning_bypass") +def test_get_version(version: str): + got = get_version(schemas.UvDynamicVersioning()).unwrap() + assert got == version