-
-
Notifications
You must be signed in to change notification settings - Fork 267
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
feat: Introduce tag_regex option with smart default #692
base: master
Are you sure you want to change the base?
Changes from all commits
c26e5bd
0aa98e1
10c7a9a
c0ff3af
c780f4e
e58e56f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,5 @@ | ||||||
import os | ||||||
import re | ||||||
from enum import Enum | ||||||
from os import linesep | ||||||
from pathlib import Path | ||||||
|
@@ -140,7 +141,7 @@ def get_filenames_in_commit(git_reference: str = ""): | |||||
raise GitCommandError(c.err) | ||||||
|
||||||
|
||||||
def get_tags(dateformat: str = "%Y-%m-%d") -> List[GitTag]: | ||||||
def get_tags(dateformat: str = "%Y-%m-%d", *, pattern: re.Pattern) -> List[GitTag]: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make the pattern filtering optional:
Suggested change
|
||||||
inner_delimiter = "---inner_delimiter---" | ||||||
formatter = ( | ||||||
f'"%(refname:lstrip=2){inner_delimiter}' | ||||||
|
@@ -163,7 +164,9 @@ def get_tags(dateformat: str = "%Y-%m-%d") -> List[GitTag]: | |||||
for line in c.out.split("\n")[:-1] | ||||||
] | ||||||
|
||||||
return git_tags | ||||||
filtered_git_tags = [t for t in git_tags if pattern.fullmatch(t.name)] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we can use git command to filter . but it's not requried. |
||||||
|
||||||
return filtered_git_tags | ||||||
|
||||||
|
||||||
def tag_exist(tag: str) -> bool: | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,65 @@ | ||||||
import re | ||||||
import sys | ||||||
from string import Template | ||||||
from typing import Any, Optional, Type, Union | ||||||
|
||||||
from packaging.version import VERSION_PATTERN, Version | ||||||
|
||||||
if sys.version_info >= (3, 8): | ||||||
from commitizen.version_types import VersionProtocol | ||||||
else: | ||||||
# workaround mypy issue for 3.7 python | ||||||
VersionProtocol = Any | ||||||
|
||||||
|
||||||
def tag_from_version( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
version: Union[VersionProtocol, str], | ||||||
tag_format: str, | ||||||
version_type_cls: Optional[Type[VersionProtocol]] = None, | ||||||
) -> str: | ||||||
"""The tag and the software version might be different. | ||||||
|
||||||
That's why this function exists. | ||||||
|
||||||
Example: | ||||||
| tag | version (PEP 0440) | | ||||||
| --- | ------- | | ||||||
| v0.9.0 | 0.9.0 | | ||||||
| ver1.0.0 | 1.0.0 | | ||||||
| ver1.0.0.a0 | 1.0.0a0 | | ||||||
""" | ||||||
if version_type_cls is None: | ||||||
version_type_cls = Version | ||||||
if isinstance(version, str): | ||||||
version = version_type_cls(version) | ||||||
|
||||||
major, minor, patch = version.release | ||||||
prerelease = "" | ||||||
# version.pre is needed for mypy check | ||||||
if version.is_prerelease and version.pre: | ||||||
prerelease = f"{version.pre[0]}{version.pre[1]}" | ||||||
|
||||||
t = Template(tag_format) | ||||||
return t.safe_substitute( | ||||||
version=version, major=major, minor=minor, patch=patch, prerelease=prerelease | ||||||
) | ||||||
|
||||||
|
||||||
def make_tag_pattern(tag_format: str) -> str: | ||||||
"""Make regex pattern to match all tags created by tag_format.""" | ||||||
escaped_format = re.escape(tag_format) | ||||||
escaped_format = re.sub( | ||||||
r"\\\$(version|major|minor|patch|prerelease)", r"$\1", escaped_format | ||||||
) | ||||||
# pre-release part of VERSION_PATTERN | ||||||
pre_release_pattern = r"([-_\.]?(a|b|c|rc|alpha|beta|pre|preview)([-_\.]?[0-9]+)?)?" | ||||||
filter_regex = Template(escaped_format).safe_substitute( | ||||||
# VERSION_PATTERN allows the v prefix, but we'd rather have users configure it | ||||||
# explicitly. | ||||||
version=VERSION_PATTERN.lstrip("\n v?"), | ||||||
major="[0-9]+", | ||||||
minor="[0-9]+", | ||||||
patch="[0-9]+", | ||||||
prerelease=pre_release_pattern, | ||||||
) | ||||||
return filter_regex |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -38,10 +38,18 @@ Version provider used to read and write version [Read more](#version-providers) | |||||||||
|
||||||||||
Type: `str` | ||||||||||
|
||||||||||
Default: `None` | ||||||||||
Default: `$version` | ||||||||||
|
||||||||||
Format for the git tag, useful for old projects, that use a convention like `"v1.2.1"`. [Read more][tag_format] | ||||||||||
|
||||||||||
### `tag_regex` | ||||||||||
|
||||||||||
Type: `str` | ||||||||||
|
||||||||||
Default: Based on `tag_format` | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
or
Suggested change
|
||||||||||
|
||||||||||
Tags must match this to be included in the changelog (e.g. `"([0-9.])*"` to exclude pre-releases). [Read more][tag_regex] | ||||||||||
|
||||||||||
### `update_changelog_on_bump` | ||||||||||
|
||||||||||
Type: `bool` | ||||||||||
|
@@ -339,6 +347,7 @@ setup( | |||||||||
|
||||||||||
[version_files]: bump.md#version_files | ||||||||||
[tag_format]: bump.md#tag_format | ||||||||||
[tag_regex]: changelog.md#tag_regex | ||||||||||
[bump_message]: bump.md#bump_message | ||||||||||
[major-version-zero]: bump.md#-major-version-zero | ||||||||||
[prerelease-offset]: bump.md#-prerelease_offset | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not related to this PR, can you remove this ? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[virtualenvs] | ||
in-project = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to inject default there, default are already loaded and overwritten by actual settings