Use git repo data (latest tag, current commit hash, etc) for building a version number according to PEP 440.
Features:
- Can be installed & configured through both
setup.pyand PEP 518'spyproject.toml - Does not require to change source code of the project
- Tag-, file-, and callback-based versioning schemas are supported
- Templates for tag, dev and dirty versions are separated
- Templates support a lot of substitutions including git and environment information
- Well-documented
See comparison
between setuptools-git-versioning and other tools.
Limitations:
- Currently the only supported VCS is Git
- Only Git v2 is supported
- Only Setuptools build backend is supported (no Poetry & others)
- Currently does not support automatic exporting of package version to a file for runtime use
(but you can use
setuptools-git-versioning > fileredirect instead)
See https://setuptools-git-versioning.readthedocs.io/en/stable/
Just add setuptools-git-versioning to build-sytem section of your pyproject.toml,
add a section tool.setuptools-git-versioning with config options, and mark the project
version as dynamic.
[build-system]
requires = [ "setuptools>=41", "wheel", "setuptools-git-versioning>=3.0,<4", ]
build-backend = "setuptools.build_meta"
[tool.setuptools-git-versioning]
enabled = true
[project]
dynamic = ["version"]And check the package version generated (see command help):
$ python -m setuptools_git_versioning
0.0.1
# or
$ setuptools-git-versioning
0.0.1When add a git tag:
$ git add .
$ git commit -m "Test tagged"
$ git tag 1.2.3And now version is based on git tag:
$ setuptools-git-versioning
1.2.3
$ echo 1 > uncommitted.change
$ git add .
$ setuptools-git-versioning
1.2.3.post0+git.d2bc6516.dirty
$ git commit -m "Test committed"
$ setuptools-git-versioning
1.2.3.post1+git.d452190bJust add setuptools-git-versioning to setup_requires argument of setuptools.setup function call,
and then add new argument setuptools_git_versioning with config options:
import setuptools
setuptools.setup(
...,
setuptools_git_versioning={
"enabled": True,
},
setup_requires=["setuptools-git-versioning>=3.0,<4"],
)Commands are the same as above, plus python -m setup.py returns the same version.