setuptools-git-versioning
setuptools-git-versioning is a Python library that automatically infers package versions from Git repository data, adhering to PEP 440. It allows configuration via `pyproject.toml` or `setup.py`/`setup.cfg` and supports tag-, file-, and callback-based versioning schemas with flexible templating. The library is actively maintained, with frequent patch releases and occasional major version bumps introducing breaking changes.
Warnings
- breaking Version 3.0.0 introduced breaking changes: most arguments in `version_from_git` became keyword-only, `version_from_git` and `infer_version` now return `packaging.version.Version` objects instead of strings, and timestamps in version templates are now timezone-aware.
- breaking Version 2.0.0 removed the deprecated `get_branch_tags` function and the `version_config` keyword argument from `setup.py`. It also changed the precedence for the `version_file` option, which now takes priority over tags in the current branch.
- gotcha Building in environments without `git` installed or from shallow clones (e.g., GitHub tarballs or CI/CD without full `git` history) can cause `OSError` exceptions or incorrect version detection. The library relies on `git` executable and a full `.git` folder.
- gotcha The package can report a 'dirty' version (e.g., `1.2.3.post0+git.d2bc6516.dirty`) if there are untracked or uncommitted changes in the repository during the build. This is common in CI pipelines due to build artifacts.
- gotcha If you define a static `version = "X.Y.Z"` in the `[project]` section of `pyproject.toml`, `setuptools-git-versioning` will be ignored.
- gotcha Directly calling internal functions like `setuptools_git_versioning.get_version()` or `get_tag()` from your application's runtime code is strongly discouraged and unsupported. These functions are not part of the public API and may change without backward compatibility. Furthermore, the `.git` folder is not included in the installed package.
Install
-
pip install setuptools-git-versioning -
pip install "setuptools-git-versioning<4,>=3.0"
Imports
- setuptools-git-versioning
python -m setuptools_git_versioning
Quickstart
[build-system]
requires = ["setuptools>=41", "wheel", "setuptools-git-versioning>=3.0,<4"]
build-backend = "setuptools.build_meta"
[project]
name = "my_package"
dynamic = ["version"]
[tool.setuptools-git-versioning]
enabled = true
# Optional: Customize version format
# template = "{tag}.post{ccount}+{gitsha}"
# dev_template = "{tag}.dev{ccount}+{gitsha}"
# dirty_template = "{tag}.dirty{ccount}+{gitsha}"