Skip to content
This repository has been archived by the owner on Oct 12, 2024. It is now read-only.

Commit

Permalink
Added functionality to format the version based on git tags with foll…
Browse files Browse the repository at this point in the history
…owing commits and hashes

Primarily meant for autobuilding commits
  • Loading branch information
MrMEEE committed Dec 22, 2023
1 parent 3066fa3 commit 81ed10f
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
51 changes: 51 additions & 0 deletions poetry_version_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"<b>poetry-version-plugin</b>: 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(
"<b>poetry-version-plugin</b>: Git tag plus hash found, setting "
f"dynamic version to: {tag}+{commits}.{hash}"
)
poetry.package._set_version(tag+"+"+commits+"."+hash)
return
else:
message = (
"<b>poetry-version-plugin</b>: No Git tag found, not "
"extracting dynamic version"
)
io.write_error_line(message)
raise RuntimeError(message)

0 comments on commit 81ed10f

Please sign in to comment.