Skip to content

Commit

Permalink
fix(changelog): handle custom tag_format in changelog generation
Browse files Browse the repository at this point in the history
When the tag_format does not follow the allowed schemas patterns then changlog generation fails.
  • Loading branch information
grahamhar committed Mar 26, 2024
1 parent 8daca3f commit cf69409
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
33 changes: 25 additions & 8 deletions commitizen/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,34 @@ def tag_included_in_changelog(
return True


def get_version_tags(scheme: type[BaseVersion], tags: list[GitTag]) -> list[GitTag]:
def get_version_tags(
scheme: type[BaseVersion], tags: list[GitTag], tag_format: str
) -> list[GitTag]:
valid_tags: list[GitTag] = []
TAG_FORMAT_REGEXS = {
"$version": str(scheme.parser.pattern),
"$major": r"(?P<major>\d+)",
"$minor": r"(?P<minor>\d+)",
"$patch": r"(?P<patch>\d+)",
"$prerelease": r"(?P<prerelease>\w+\d+)?",
"$devrelease": r"(?P<devrelease>\.dev\d+)?",
"${version}": str(scheme.parser.pattern),
"${major}": r"(?P<major>\d+)",
"${minor}": r"(?P<minor>\d+)",
"${patch}": r"(?P<patch>\d+)",
"${prerelease}": r"(?P<prerelease>\w+\d+)?",
"${devrelease}": r"(?P<devrelease>\.dev\d+)?",
}
tag_format_regex = tag_format
for pattern, regex in TAG_FORMAT_REGEXS.items():
tag_format_regex = tag_format_regex.replace(pattern, regex)
for tag in tags:
try:
scheme(tag.name)
except InvalidVersion:
out.warn(f"InvalidVersion {tag}")
else:
if re.match(tag_format_regex, tag.name):
valid_tags.append(tag)

else:
out.warn(
f"InvalidVersion {tag.name} doesn't match configured tag format {tag_format}"
)
return valid_tags


Expand Down Expand Up @@ -351,7 +369,6 @@ def get_oldest_and_newest_rev(
oldest, newest = version.split("..")
except ValueError:
newest = version

newest_tag = normalize_tag(newest, tag_format=tag_format, scheme=scheme)

oldest_tag = None
Expand Down
17 changes: 6 additions & 11 deletions commitizen/commands/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path
from typing import Callable

from commitizen import bump, changelog, defaults, factory, git, out
from commitizen import changelog, defaults, factory, git, out

from commitizen.config import BaseConfig
from commitizen.cz.base import MessageBuilderHook, ChangelogReleaseHook
Expand Down Expand Up @@ -169,21 +169,17 @@ def __call__(self):
# Don't continue if no `file_name` specified.
assert self.file_name

tags = changelog.get_version_tags(self.scheme, git.get_tags()) or []

tags = (
changelog.get_version_tags(self.scheme, git.get_tags(), self.tag_format)
or []
)
end_rev = ""
if self.incremental:
changelog_meta = self.changelog_format.get_metadata(self.file_name)
if changelog_meta.latest_version:
latest_tag_version: str = bump.normalize_tag(
changelog_meta.latest_version,
tag_format=self.tag_format,
scheme=self.scheme,
)
start_rev = self._find_incremental_rev(
strip_local_version(latest_tag_version), tags
strip_local_version(changelog_meta.latest_version), tags
)

if self.rev_range:
start_rev, end_rev = changelog.get_oldest_and_newest_rev(
tags,
Expand All @@ -197,7 +193,6 @@ def __call__(self):
self.current_version is None or not self.current_version.is_prerelease
):
raise NoCommitsFoundError("No commits found")

tree = changelog.generate_tree_from_commits(
commits,
tags,
Expand Down
6 changes: 6 additions & 0 deletions commitizen/providers/scm_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class ScmProvider(VersionProvider):
"$patch": r"(?P<patch>\d+)",
"$prerelease": r"(?P<prerelease>\w+\d+)?",
"$devrelease": r"(?P<devrelease>\.dev\d+)?",
"${version}": r"(?P<version>.+)",
"${major}": r"(?P<major>\d+)",
"${minor}": r"(?P<minor>\d+)",
"${patch}": r"(?P<patch>\d+)",
"${prerelease}": r"(?P<prerelease>\w+\d+)?",
"${devrelease}": r"(?P<devrelease>\.dev\d+)?",
}

def _tag_format_matcher(self) -> Callable[[str], VersionProtocol | None]:
Expand Down

0 comments on commit cf69409

Please sign in to comment.