Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended the git tags functionality #49

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", tag+"..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)
Loading