From 81ed10f8a36994382a943784f72ebccb74a4f203 Mon Sep 17 00:00:00 2001 From: Martin Juhl Date: Fri, 22 Dec 2023 02:33:54 +0100 Subject: [PATCH] Added functionality to format the version based on git tags with following commits and hashes Primarily meant for autobuilding commits --- README.md | 50 ++++++++++++++++++++++++++++++++ poetry_version_plugin/plugin.py | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) diff --git a/README.md b/README.md index dec8d3a..60bffd2 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,49 @@ Building my-awesome-package (0.1.3) - Built my-awesome-package-0.1.3-py3-none-any.whl ``` +### Set the version in a Git tag and add commits and hash + +Another alternative , to extract the version to use from a Git tag, but also add commits and hashes when there have been commits since the last tag. +Add a section: + +```toml +[tool.poetry-version-plugin] +source = "git-tag-plus-hash" +``` + +Then create a git tag, for example: + +```console +$ git tag 0.1.3 +``` + +In this case, when building your project, it will show an output like: + +```console +$ poetry build +Git tag found, setting dynamic version to: 0.1.3 +Building my-awesome-package (0.1.3) + - Building sdist + - Built my-awesome-package-0.1.3.tar.gz + - Building wheel + - Built my-awesome-package-0.1.3-py3-none-any.whl +``` + +But, if there has been a git commit after the last tag, the output will be like: + +```console +$ poetry build +Git tag plus hash found, setting dynamic version to: 0.1.3+0.76cdb16a +Building my-awesome-package (0.1.3+0.76cdb16a) + - Building sdist + - Built my-awesome-package-0.1.3+0.76cdb16a.tar.gz + - Building wheel + - Built my-awesome-package-0.1.3+0.76cdb16a-py3-none-any.whl +``` + +This can be useful for autobuilding commits. + + ## Version in `pyproject.toml` Currently (2021-05-24) Poetry requires a `version` configuration in the `pyproject.toml`, even if you use this plugin. @@ -349,6 +392,13 @@ and source = "git-tag" ``` +and + +```toml +[tool.poetry-version-plugin] +source = "git-tag-plus-hash" +``` + let me know what alternative configuration would make more sense and be more intuitive to you. 👍 The good news is, assuming you are building packages to then upload them to PyPI for your users to download and use them, the **worst that could happen** if something broke is that you wouldn't be able to build a new version until something is fixed or changed. But your users shouldn't be affected in any way. diff --git a/poetry_version_plugin/plugin.py b/poetry_version_plugin/plugin.py index 1308fa8..a62eedc 100644 --- a/poetry_version_plugin/plugin.py +++ b/poetry_version_plugin/plugin.py @@ -104,3 +104,54 @@ def activate(self, poetry: Poetry, io: IO) -> None: ) io.write_error_line(message) raise RuntimeError(message) + elif version_source == "git-tag-plus-hash": + result = subprocess.run( + ["git", "describe", "--exact-match", "--tags", "HEAD"], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + universal_newlines=True, + ) + if result.returncode == 0: + tag = result.stdout.strip() + io.write_line( + "poetry-version-plugin: Git tag found, setting " + f"dynamic version to: {tag}" + ) + poetry.package._set_version(tag) + return + else: + result = subprocess.run( + ["git", "describe", "--tags", "--abbrev=0"], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + universal_newlines=True, + ) + if result.returncode == 0: + tag = result.stdout.strip() + result = subprocess.run( + ["git", "rev-parse", "--short=8", "HEAD"], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + universal_newlines=True, + ) + hash = result.stdout.strip() + result = subprocess.run( + ["git", "rev-list", hash+"..HEAD", "--count"], + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + universal_newlines=True, + ) + commits = result.stdout.strip() + io.write_line( + "poetry-version-plugin: Git tag plus hash found, setting " + f"dynamic version to: {tag}+{commits}.{hash}" + ) + poetry.package._set_version(tag+"+"+commits+"."+hash) + return + else: + message = ( + "poetry-version-plugin: No Git tag found, not " + "extracting dynamic version" + ) + io.write_error_line(message) + raise RuntimeError(message)