diff --git a/commitizen/bump.py b/commitizen/bump.py index 2351dbd7ec..dc0b37a55e 100644 --- a/commitizen/bump.py +++ b/commitizen/bump.py @@ -10,7 +10,7 @@ from commitizen.defaults import MAJOR, MINOR, PATCH, bump_message, encoding from commitizen.exceptions import CurrentVersionNotFoundError from commitizen.git import GitCommit, smart_open -from commitizen.version_schemes import DEFAULT_SCHEME, Increment, Version, VersionScheme +from commitizen.version_schemes import Increment, Version VERSION_TYPES = [None, PATCH, MINOR, MAJOR] @@ -131,34 +131,6 @@ def _version_to_regex(version: str) -> str: return version.replace(".", r"\.").replace("+", r"\+") -def normalize_tag( - version: Version | str, - tag_format: str, - scheme: VersionScheme | None = 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 | - """ - scheme = scheme or DEFAULT_SCHEME - version = scheme(version) if isinstance(version, str) else version - - major, minor, patch = version.release - prerelease = version.prerelease or "" - - t = Template(tag_format) - return t.safe_substitute( - version=version, major=major, minor=minor, patch=patch, prerelease=prerelease - ) - - def create_commit_message( current_version: Version | str, new_version: Version | str, diff --git a/commitizen/changelog.py b/commitizen/changelog.py index 32a66c47eb..344b3b5060 100644 --- a/commitizen/changelog.py +++ b/commitizen/changelog.py @@ -41,21 +41,13 @@ Template, ) -from commitizen import out -from commitizen.bump import normalize_tag from commitizen.cz.base import ChangelogReleaseHook -from commitizen.defaults import get_tag_regexes from commitizen.exceptions import InvalidConfigurationError, NoCommitsFoundError from commitizen.git import GitCommit, GitTag -from commitizen.version_schemes import ( - DEFAULT_SCHEME, - BaseVersion, - InvalidVersion, -) +from commitizen.tags import TagRules if TYPE_CHECKING: from commitizen.cz.base import MessageBuilderHook - from commitizen.version_schemes import VersionScheme @dataclass @@ -68,50 +60,19 @@ class Metadata: unreleased_end: int | None = None latest_version: str | None = None latest_version_position: int | None = None + latest_version_tag: str | None = None + + def __post_init__(self): + if self.latest_version and not self.latest_version_tag: + # Test syntactic sugar + # latest version tag is optional if same as latest version + self.latest_version_tag = self.latest_version def get_commit_tag(commit: GitCommit, tags: list[GitTag]) -> GitTag | None: return next((tag for tag in tags if tag.rev == commit.rev), None) -def tag_included_in_changelog( - tag: GitTag, - used_tags: list, - merge_prerelease: bool, - scheme: VersionScheme = DEFAULT_SCHEME, -) -> bool: - if tag in used_tags: - return False - - try: - version = scheme(tag.name) - except InvalidVersion: - return False - - if merge_prerelease and version.is_prerelease: - return False - - return True - - -def get_version_tags( - scheme: type[BaseVersion], tags: list[GitTag], tag_format: str -) -> list[GitTag]: - valid_tags: list[GitTag] = [] - TAG_FORMAT_REGEXS = get_tag_regexes(scheme.parser.pattern) - 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: - 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 - - def generate_tree_from_commits( commits: list[GitCommit], tags: list[GitTag], @@ -121,13 +82,13 @@ def generate_tree_from_commits( change_type_map: dict[str, str] | None = None, changelog_message_builder_hook: MessageBuilderHook | None = None, changelog_release_hook: ChangelogReleaseHook | None = None, - merge_prerelease: bool = False, - scheme: VersionScheme = DEFAULT_SCHEME, + rules: TagRules | None = None, ) -> Iterable[dict]: pat = re.compile(changelog_pattern) map_pat = re.compile(commit_parser, re.MULTILINE) body_map_pat = re.compile(commit_parser, re.MULTILINE | re.DOTALL) current_tag: GitTag | None = None + rules = rules or TagRules() # Check if the latest commit is not tagged if commits: @@ -147,8 +108,10 @@ def generate_tree_from_commits( for commit in commits: commit_tag = get_commit_tag(commit, tags) - if commit_tag is not None and tag_included_in_changelog( - commit_tag, used_tags, merge_prerelease, scheme=scheme + if ( + commit_tag + and commit_tag not in used_tags + and rules.include_in_changelog(commit_tag) ): used_tags.append(commit_tag) release = { @@ -342,8 +305,7 @@ def get_smart_tag_range( def get_oldest_and_newest_rev( tags: list[GitTag], version: str, - tag_format: str, - scheme: VersionScheme | None = None, + rules: TagRules, ) -> tuple[str | None, str | None]: """Find the tags for the given version. @@ -357,22 +319,28 @@ 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) + if not (newest_tag := rules.find_tag_for(tags, newest)): + raise NoCommitsFoundError("Could not find a valid revision range.") oldest_tag = None + oldest_tag_name = None if oldest: - oldest_tag = normalize_tag(oldest, tag_format=tag_format, scheme=scheme) + if not (oldest_tag := rules.find_tag_for(tags, oldest)): + raise NoCommitsFoundError("Could not find a valid revision range.") + oldest_tag_name = oldest_tag.name - tags_range = get_smart_tag_range(tags, newest=newest_tag, oldest=oldest_tag) + tags_range = get_smart_tag_range( + tags, newest=newest_tag.name, oldest=oldest_tag_name + ) if not tags_range: raise NoCommitsFoundError("Could not find a valid revision range.") oldest_rev: str | None = tags_range[-1].name - newest_rev = newest_tag + newest_rev = newest_tag.name # check if it's the first tag created # and it's also being requested as part of the range - if oldest_rev == tags[-1].name and oldest_rev == oldest_tag: + if oldest_rev == tags[-1].name and oldest_rev == oldest_tag_name: return None, newest_rev # when they are the same, and it's also the diff --git a/commitizen/changelog_formats/asciidoc.py b/commitizen/changelog_formats/asciidoc.py index 6007a56d16..eb94505652 100644 --- a/commitizen/changelog_formats/asciidoc.py +++ b/commitizen/changelog_formats/asciidoc.py @@ -10,27 +10,12 @@ class AsciiDoc(BaseFormat): RE_TITLE = re.compile(r"^(?P=+) (?P.*)$") - def parse_version_from_title(self, line: str) -> str | None: + def parse_version_from_title(self, line: str) -> tuple[str, str] | None: m = self.RE_TITLE.match(line) if not m: return None # Capture last match as AsciiDoc use postfixed URL labels - matches = list(re.finditer(self.version_parser, m.group("title"))) - if not matches: - return None - if "version" in matches[-1].groupdict(): - return matches[-1].group("version") - partial_matches = matches[-1].groupdict() - try: - partial_version = f"{partial_matches['major']}.{partial_matches['minor']}.{partial_matches['patch']}" - except KeyError: - return None - - if partial_matches.get("prerelease"): - partial_version = f"{partial_version}-{partial_matches['prerelease']}" - if partial_matches.get("devrelease"): - partial_version = f"{partial_version}{partial_matches['devrelease']}" - return partial_version + return self.tag_rules.search_version(m.group("title"), last=True) def parse_title_level(self, line: str) -> int | None: m = self.RE_TITLE.match(line) diff --git a/commitizen/changelog_formats/base.py b/commitizen/changelog_formats/base.py index 53527a060c..d7177d2e70 100644 --- a/commitizen/changelog_formats/base.py +++ b/commitizen/changelog_formats/base.py @@ -1,14 +1,11 @@ from __future__ import annotations import os -import re from abc import ABCMeta -from re import Pattern from typing import IO, Any, ClassVar -from commitizen.changelog import Metadata +from commitizen.changelog import Metadata, TagRules from commitizen.config.base_config import BaseConfig -from commitizen.defaults import get_tag_regexes from commitizen.version_schemes import get_version_scheme from . import ChangelogFormat @@ -28,15 +25,12 @@ def __init__(self, config: BaseConfig): self.config = config self.encoding = self.config.settings["encoding"] self.tag_format = self.config.settings["tag_format"] - - @property - def version_parser(self) -> Pattern: - tag_regex: str = self.tag_format - version_regex = get_version_scheme(self.config).parser.pattern - TAG_FORMAT_REGEXS = get_tag_regexes(version_regex) - for pattern, regex in TAG_FORMAT_REGEXS.items(): - tag_regex = tag_regex.replace(pattern, regex) - return re.compile(tag_regex) + self.tag_rules = TagRules( + scheme=get_version_scheme(self.config.settings), + tag_format=self.tag_format, + legacy_tag_formats=self.config.settings["legacy_tag_formats"], + ignored_tag_formats=self.config.settings["ignored_tag_formats"], + ) def get_metadata(self, filepath: str) -> Metadata: if not os.path.isfile(filepath): @@ -65,7 +59,8 @@ def get_metadata_from_file(self, file: IO[Any]) -> Metadata: # Try to find the latest release done version = self.parse_version_from_title(line) if version: - meta.latest_version = version + meta.latest_version = version[0] + meta.latest_version_tag = version[1] meta.latest_version_position = index break # there's no need for more info if meta.unreleased_start is not None and meta.unreleased_end is None: @@ -73,7 +68,7 @@ def get_metadata_from_file(self, file: IO[Any]) -> Metadata: return meta - def parse_version_from_title(self, line: str) -> str | None: + def parse_version_from_title(self, line: str) -> tuple[str, str] | None: """ Extract the version from a title line if any """ diff --git a/commitizen/changelog_formats/markdown.py b/commitizen/changelog_formats/markdown.py index 29c1cce54a..46485bda1b 100644 --- a/commitizen/changelog_formats/markdown.py +++ b/commitizen/changelog_formats/markdown.py @@ -12,28 +12,11 @@ class Markdown(BaseFormat): RE_TITLE = re.compile(r"^(?P<level>#+) (?P<title>.*)$") - def parse_version_from_title(self, line: str) -> str | None: + def parse_version_from_title(self, line: str) -> tuple[str, str] | None: m = self.RE_TITLE.match(line) if not m: return None - m = re.search(self.version_parser, m.group("title")) - if not m: - return None - if "version" in m.groupdict(): - return m.group("version") - matches = m.groupdict() - try: - partial_version = ( - f"{matches['major']}.{matches['minor']}.{matches['patch']}" - ) - except KeyError: - return None - - if matches.get("prerelease"): - partial_version = f"{partial_version}-{matches['prerelease']}" - if matches.get("devrelease"): - partial_version = f"{partial_version}{matches['devrelease']}" - return partial_version + return self.tag_rules.search_version(m.group("title")) def parse_title_level(self, line: str) -> int | None: m = self.RE_TITLE.match(line) diff --git a/commitizen/changelog_formats/restructuredtext.py b/commitizen/changelog_formats/restructuredtext.py index 09d032400c..fb1bb7c8e2 100644 --- a/commitizen/changelog_formats/restructuredtext.py +++ b/commitizen/changelog_formats/restructuredtext.py @@ -1,6 +1,5 @@ from __future__ import annotations -import re import sys from itertools import zip_longest from typing import IO, TYPE_CHECKING, Any, Tuple, Union @@ -64,31 +63,11 @@ def get_metadata_from_file(self, file: IO[Any]) -> Metadata: elif unreleased_title_kind and unreleased_title_kind == kind: meta.unreleased_end = index # Try to find the latest release done - m = re.search(self.version_parser, title) - if m: - matches = m.groupdict() - if "version" in matches: - version = m.group("version") - meta.latest_version = version - meta.latest_version_position = index - break # there's no need for more info - try: - partial_version = ( - f"{matches['major']}.{matches['minor']}.{matches['patch']}" - ) - if matches.get("prerelease"): - partial_version = ( - f"{partial_version}-{matches['prerelease']}" - ) - if matches.get("devrelease"): - partial_version = ( - f"{partial_version}{matches['devrelease']}" - ) - meta.latest_version = partial_version - meta.latest_version_position = index - break - except KeyError: - pass + if version := self.tag_rules.search_version(title): + meta.latest_version = version[0] + meta.latest_version_tag = version[1] + meta.latest_version_position = index + break if meta.unreleased_start is not None and meta.unreleased_end is None: meta.unreleased_end = ( meta.latest_version_position if meta.latest_version else index + 1 diff --git a/commitizen/changelog_formats/textile.py b/commitizen/changelog_formats/textile.py index 8750f0056c..d1ea9c5b00 100644 --- a/commitizen/changelog_formats/textile.py +++ b/commitizen/changelog_formats/textile.py @@ -10,30 +10,10 @@ class Textile(BaseFormat): RE_TITLE = re.compile(r"^h(?P<level>\d)\. (?P<title>.*)$") - def parse_version_from_title(self, line: str) -> str | None: + def parse_version_from_title(self, line: str) -> tuple[str, str] | None: if not self.RE_TITLE.match(line): return None - m = re.search(self.version_parser, line) - if not m: - return None - if "version" in m.groupdict(): - return m.group("version") - matches = m.groupdict() - if not all( - [ - version_segment in matches - for version_segment in ("major", "minor", "patch") - ] - ): - return None - - partial_version = f"{matches['major']}.{matches['minor']}.{matches['patch']}" - - if matches.get("prerelease"): - partial_version = f"{partial_version}-{matches['prerelease']}" - if matches.get("devrelease"): - partial_version = f"{partial_version}{matches['devrelease']}" - return partial_version + return self.tag_rules.search_version(line) def parse_title_level(self, line: str) -> int | None: m = self.RE_TITLE.match(line) diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index a3682df8f3..c666f48884 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -2,6 +2,7 @@ import warnings from logging import getLogger +from typing import cast import questionary @@ -9,6 +10,7 @@ from commitizen.changelog_formats import get_changelog_format from commitizen.commands.changelog import Changelog from commitizen.config import BaseConfig +from commitizen.defaults import Settings from commitizen.exceptions import ( BumpCommitFailedError, BumpTagFailedError, @@ -24,6 +26,7 @@ NoVersionSpecifiedError, ) from commitizen.providers import get_provider +from commitizen.tags import TagRules from commitizen.version_schemes import ( Increment, InvalidVersion, @@ -85,7 +88,7 @@ def __init__(self, config: BaseConfig, arguments: dict): ) ) self.scheme = get_version_scheme( - self.config, arguments["version_scheme"] or deprecated_version_type + self.config.settings, arguments["version_scheme"] or deprecated_version_type ) self.file_name = arguments["file_name"] or self.config.settings.get( "changelog_file" @@ -99,18 +102,20 @@ def __init__(self, config: BaseConfig, arguments: dict): ) self.extras = arguments["extras"] - def is_initial_tag(self, current_tag_version: str, is_yes: bool = False) -> bool: + def is_initial_tag( + self, current_tag: git.GitTag | None, is_yes: bool = False + ) -> bool: """Check if reading the whole git tree up to HEAD is needed.""" is_initial = False - if not git.tag_exist(current_tag_version): + if not current_tag: if is_yes: is_initial = True else: - out.info(f"Tag {current_tag_version} could not be found. ") + out.info("No tag matching configuration could not be found.") out.info( "Possible causes:\n" "- version in configuration is not the current version\n" - "- tag_format is missing, check them using 'git tag --list'\n" + "- tag_format or legacy_tag_formats is missing, check them using 'git tag --list'\n" ) is_initial = questionary.confirm("Is this the first tag created?").ask() return is_initial @@ -144,7 +149,6 @@ def __call__(self) -> None: # noqa: C901 except TypeError: raise NoVersionSpecifiedError() - tag_format: str = self.bump_settings["tag_format"] bump_commit_message: str = self.bump_settings["bump_message"] version_files: list[str] = self.bump_settings["version_files"] major_version_zero: bool = self.bump_settings["major_version_zero"] @@ -219,13 +223,13 @@ def __call__(self) -> None: # noqa: C901 # Setting dry_run to prevent any unwanted changes to the repo or files self.dry_run = True - current_tag_version: str = bump.normalize_tag( - current_version, - tag_format=tag_format, - scheme=self.scheme, + rules = TagRules.from_settings(cast(Settings, self.bump_settings)) + current_tag = rules.find_tag_for(git.get_tags(), current_version) + current_tag_version = getattr( + current_tag, "name", rules.normalize_tag(current_version) ) - is_initial = self.is_initial_tag(current_tag_version, is_yes) + is_initial = self.is_initial_tag(current_tag, is_yes) if manual_version: try: @@ -237,10 +241,10 @@ def __call__(self) -> None: # noqa: C901 ) from exc else: if increment is None: - if is_initial: - commits = git.get_commits() + if current_tag: + commits = git.get_commits(current_tag.name) else: - commits = git.get_commits(current_tag_version) + commits = git.get_commits() # No commits, there is no need to create an empty tag. # Unless we previously had a prerelease. @@ -270,11 +274,7 @@ def __call__(self) -> None: # noqa: C901 exact_increment=increment_mode == "exact", ) - new_tag_version = bump.normalize_tag( - new_version, - tag_format=tag_format, - scheme=self.scheme, - ) + new_tag_version = rules.normalize_tag(new_version) message = bump.create_commit_message( current_version, new_version, bump_commit_message ) diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index 25e644aaef..80a72651e4 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -6,7 +6,7 @@ from pathlib import Path from typing import Callable, cast -from commitizen import bump, changelog, defaults, factory, git, out +from commitizen import changelog, defaults, factory, git, out from commitizen.changelog_formats import get_changelog_format from commitizen.config import BaseConfig from commitizen.cz.base import ChangelogReleaseHook, MessageBuilderHook @@ -20,6 +20,7 @@ NotAllowed, ) from commitizen.git import GitTag, smart_open +from commitizen.tags import TagRules from commitizen.version_schemes import get_version_scheme @@ -53,7 +54,9 @@ def __init__(self, config: BaseConfig, args): ) self.dry_run = args["dry_run"] - self.scheme = get_version_scheme(self.config, args.get("version_scheme")) + self.scheme = get_version_scheme( + self.config.settings, args.get("version_scheme") + ) current_version = ( args.get("current_version", config.settings.get("version")) or "" @@ -73,9 +76,14 @@ def __init__(self, config: BaseConfig, args): self.tag_format: str = ( args.get("tag_format") or self.config.settings["tag_format"] ) - self.merge_prerelease = args.get( - "merge_prerelease" - ) or self.config.settings.get("changelog_merge_prerelease") + self.tag_rules = TagRules( + scheme=self.scheme, + tag_format=self.tag_format, + legacy_tag_formats=self.config.settings["legacy_tag_formats"], + ignored_tag_formats=self.config.settings["ignored_tag_formats"], + merge_prereleases=args.get("merge_prerelease") + or self.config.settings["changelog_merge_prerelease"], + ) self.template = ( args.get("template") @@ -152,7 +160,6 @@ def __call__(self): changelog_release_hook: ChangelogReleaseHook | None = ( self.cz.changelog_release_hook ) - merge_prerelease = self.merge_prerelease if self.export_template_to: return self.export_template() @@ -168,28 +175,19 @@ 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(), self.tag_format) - or [] - ) + tags = self.tag_rules.get_version_tags(git.get_tags(), warn=True) 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_tag), tags ) if self.rev_range: start_rev, end_rev = changelog.get_oldest_and_newest_rev( tags, - version=self.rev_range, - tag_format=self.tag_format, - scheme=self.scheme, + self.rev_range, + self.tag_rules, ) commits = git.get_commits(start=start_rev, end=end_rev, args="--topo-order") if not commits and ( @@ -205,8 +203,7 @@ def __call__(self): change_type_map=change_type_map, changelog_message_builder_hook=changelog_message_builder_hook, changelog_release_hook=changelog_release_hook, - merge_prerelease=merge_prerelease, - scheme=self.scheme, + rules=self.tag_rules, ) if self.change_type_order: tree = changelog.order_changelog_tree(tree, self.change_type_order) diff --git a/commitizen/commands/init.py b/commitizen/commands/init.py index ffc5e3eb3b..e39dfbe291 100644 --- a/commitizen/commands/init.py +++ b/commitizen/commands/init.py @@ -98,7 +98,7 @@ def __call__(self): version_provider = self._ask_version_provider() # select tag = self._ask_tag() # confirm & select version_scheme = self._ask_version_scheme() # select - version = get_version_scheme(self.config, version_scheme)(tag) + version = get_version_scheme(self.config.settings, version_scheme)(tag) tag_format = self._ask_tag_format(tag) # confirm & text update_changelog_on_bump = self._ask_update_changelog_on_bump() # confirm major_version_zero = self._ask_major_version_zero(version) # confirm diff --git a/commitizen/defaults.py b/commitizen/defaults.py index 6675f23acf..802de57afc 100644 --- a/commitizen/defaults.py +++ b/commitizen/defaults.py @@ -2,6 +2,7 @@ import pathlib from collections import OrderedDict +from collections.abc import Sequence from typing import Any, Iterable, MutableMapping, TypedDict # Type @@ -34,6 +35,8 @@ class Settings(TypedDict, total=False): version_scheme: str | None version_type: str | None tag_format: str + legacy_tag_formats: Sequence[str] + ignored_tag_formats: Sequence[str] bump_message: str | None retry_after_failure: bool allow_abort: bool @@ -76,6 +79,8 @@ class Settings(TypedDict, total=False): "version_provider": "commitizen", "version_scheme": None, "tag_format": "$version", # example v$version + "legacy_tag_formats": [], + "ignored_tag_formats": [], "bump_message": None, # bumped v$current_version to $new_version "retry_after_failure": False, "allow_abort": False, diff --git a/commitizen/providers/scm_provider.py b/commitizen/providers/scm_provider.py index 33e470cfc6..cb575148cb 100644 --- a/commitizen/providers/scm_provider.py +++ b/commitizen/providers/scm_provider.py @@ -1,17 +1,8 @@ from __future__ import annotations -import re -from typing import Callable - -from commitizen.defaults import get_tag_regexes from commitizen.git import get_tags from commitizen.providers.base_provider import VersionProvider -from commitizen.version_schemes import ( - InvalidVersion, - Version, - VersionProtocol, - get_version_scheme, -) +from commitizen.tags import TagRules class ScmProvider(VersionProvider): @@ -23,57 +14,14 @@ class ScmProvider(VersionProvider): It is meant for `setuptools-scm` or any package manager `*-scm` provider. """ - TAG_FORMAT_REGEXS = get_tag_regexes(r"(?P<version>.+)") - - def _tag_format_matcher(self) -> Callable[[str], VersionProtocol | None]: - version_scheme = get_version_scheme(self.config) - pattern = self.config.settings["tag_format"] - if pattern == "$version": - pattern = version_scheme.parser.pattern - for var, tag_pattern in self.TAG_FORMAT_REGEXS.items(): - pattern = pattern.replace(var, tag_pattern) - - regex = re.compile(f"^{pattern}$", re.VERBOSE) - - def matcher(tag: str) -> Version | None: - match = regex.match(tag) - if not match: - return None - groups = match.groupdict() - if "version" in groups: - ver = groups["version"] - elif "major" in groups: - ver = "".join( - ( - groups["major"], - f".{groups['minor']}" if groups.get("minor") else "", - f".{groups['patch']}" if groups.get("patch") else "", - groups["prerelease"] if groups.get("prerelease") else "", - groups["devrelease"] if groups.get("devrelease") else "", - ) - ) - elif pattern == version_scheme.parser.pattern: - ver = tag - else: - return None - - try: - return version_scheme(ver) - except InvalidVersion: - return None - - return matcher - def get_version(self) -> str: - matcher = self._tag_format_matcher() - matches = sorted( - version - for t in get_tags(reachable_only=True) - if (version := matcher(t.name)) - ) - if not matches: + rules = TagRules.from_settings(self.config.settings) + tags = get_tags(reachable_only=True) + version_tags = rules.get_version_tags(tags) + versions = sorted(rules.extract_version(t) for t in version_tags) + if not versions: return "0.0.0" - return str(matches[-1]) + return str(versions[-1]) def set_version(self, version: str): # Not necessary diff --git a/commitizen/tags.py b/commitizen/tags.py new file mode 100644 index 0000000000..2fe0176c99 --- /dev/null +++ b/commitizen/tags.py @@ -0,0 +1,204 @@ +from __future__ import annotations + +import re +from dataclasses import dataclass, field +from functools import cached_property +from string import Template +from typing import TYPE_CHECKING, Sequence + +from typing_extensions import Self + +from commitizen import out +from commitizen.defaults import DEFAULT_SETTINGS, Settings, get_tag_regexes +from commitizen.git import GitTag +from commitizen.version_schemes import ( + DEFAULT_SCHEME, + InvalidVersion, + Version, + VersionScheme, + get_version_scheme, +) + +if TYPE_CHECKING: + from commitizen.version_schemes import VersionScheme + + +@dataclass +class TagRules: + """ + Encapsulate tag-related rules + """ + + scheme: VersionScheme = DEFAULT_SCHEME + tag_format: str = DEFAULT_SETTINGS["tag_format"] + legacy_tag_formats: Sequence[str] = field(default_factory=list) + ignored_tag_formats: Sequence[str] = field(default_factory=list) + merge_prereleases: bool = False + + @cached_property + def version_regexes(self) -> Sequence[re.Pattern]: + tag_formats = [self.tag_format, *self.legacy_tag_formats] + regexes = (self._format_regex(p) for p in tag_formats) + return [re.compile(r) for r in regexes] + + @cached_property + def ignored_regexes(self) -> Sequence[re.Pattern]: + regexes = (self._format_regex(p, star=True) for p in self.ignored_tag_formats) + return [re.compile(r) for r in regexes] + + def _format_regex(self, tag_pattern: str, star: bool = False) -> str: + tag_regexes = get_tag_regexes(self.scheme.parser.pattern) + format_regex = tag_pattern.replace("*", "(?:.*?)") if star else tag_pattern + for pattern, regex in tag_regexes.items(): + format_regex = format_regex.replace(pattern, regex) + return format_regex + + def is_version_tag(self, tag: str | GitTag) -> bool: + """True if a given tag is a legit version tag""" + tag = tag.name if isinstance(tag, GitTag) else tag + return any(regex.match(tag) for regex in self.version_regexes) + + def is_ignored_tag(self, tag: str | GitTag) -> bool: + """True if a given tag can be ignored""" + tag = tag.name if isinstance(tag, GitTag) else tag + return any(regex.match(tag) for regex in self.ignored_regexes) + + def _select_tag(self, tag: GitTag, warn: bool = False) -> bool: + if self.is_version_tag(tag.name): + return True + if warn and not self.is_ignored_tag(tag.name): + out.warn( + f"InvalidVersion {tag.name} doesn't match any configured tag format" + ) + return False + + def get_version_tags( + self, tags: Sequence[GitTag], warn: bool = False + ) -> Sequence[GitTag]: + """Filter in version tags and warn on unexpected tags""" + return [tag for tag in tags if self._select_tag(tag, warn)] + + def extract_version(self, tag: GitTag) -> Version: + candidates = ( + match + for regex in self.version_regexes + if (match := regex.fullmatch(tag.name)) + ) + if not (m := next(candidates, None)): + raise InvalidVersion() + if not (version := self._version_string(m)): + raise InvalidVersion() + return self.scheme(version) + + def _version_string(self, match: re.Match[str]) -> str | None: + if "version" in match.groupdict(): + return match.group("version") + + parts = match.groupdict() + try: + version = parts["major"] + except KeyError: + return None + + if minor := parts.get("minor"): + version = f"{version}.{minor}" + if patch := parts.get("patch"): + version = f"{version}.{patch}" + + if parts.get("prerelease"): + version = f"{version}-{parts['prerelease']}" + if parts.get("devrelease"): + version = f"{version}{parts['devrelease']}" + + return version + + def include_in_changelog(self, tag: GitTag) -> bool: + """Check if a tag should be included in the changelog""" + try: + version = self.extract_version(tag) + except InvalidVersion: + return False + + if self.merge_prereleases and version.is_prerelease: + return False + + return True + + def search_version(self, txt: str, last: bool = False) -> tuple[str, str] | None: + """ + Search the first or last version tag occurrence in txt. + + Returns a 2-tuple (version, version_tag) if found + """ + candidates = ( + m for regex in self.version_regexes if len(m := list(regex.finditer(txt))) + ) + if not (matches := next(candidates, [])): + return None + + match = matches[-1 if last else 0] + + if "version" in match.groupdict(): + return match.group("version"), match.group(0) + + parts = match.groupdict() + try: + version = f"{parts['major']}.{parts['minor']}.{parts['patch']}" + except KeyError: + return None + + if parts.get("prerelease"): + version = f"{version}-{parts['prerelease']}" + if parts.get("devrelease"): + version = f"{version}{parts['devrelease']}" + return version, match.group(0) + + def normalize_tag( + self, version: Version | str, tag_format: str | None = 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 | + """ + version = self.scheme(version) if isinstance(version, str) else version + tag_format = tag_format or self.tag_format + + major, minor, patch = version.release + prerelease = version.prerelease or "" + + t = Template(tag_format) + return t.safe_substitute( + version=version, + major=major, + minor=minor, + patch=patch, + prerelease=prerelease, + ) + + def find_tag_for( + self, tags: Sequence[GitTag], version: Version | str + ) -> GitTag | None: + version = self.scheme(version) if isinstance(version, str) else version + possible_tags = [ + self.normalize_tag(version, f) + for f in (self.tag_format, *self.legacy_tag_formats) + ] + candidates = (t for t in tags if any(t.name == p for p in possible_tags)) + return next(candidates, None) + + @classmethod + def from_settings(cls, settings: Settings) -> Self: + return cls( + scheme=get_version_scheme(settings), + tag_format=settings["tag_format"], + legacy_tag_formats=settings["legacy_tag_formats"], + ignored_tag_formats=settings["ignored_tag_formats"], + merge_prereleases=settings["changelog_merge_prerelease"], + ) diff --git a/commitizen/version_schemes.py b/commitizen/version_schemes.py index 346287a065..fd37a8abc5 100644 --- a/commitizen/version_schemes.py +++ b/commitizen/version_schemes.py @@ -23,8 +23,7 @@ from packaging.version import InvalidVersion # noqa: F401: Rexpose the common exception from packaging.version import Version as _BaseVersion -from commitizen.config.base_config import BaseConfig -from commitizen.defaults import MAJOR, MINOR, PATCH +from commitizen.defaults import MAJOR, MINOR, PATCH, Settings from commitizen.exceptions import VersionSchemeUnknown if TYPE_CHECKING: @@ -43,7 +42,7 @@ Increment: TypeAlias = Literal["MAJOR", "MINOR", "PATCH"] Prerelease: TypeAlias = Literal["alpha", "beta", "rc"] -DEFAULT_VERSION_PARSER = r"v?(?P<version>([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?(\w+)?)" +DEFAULT_VERSION_PARSER = r"v?(?P<version>([0-9]+)\.([0-9]+)(?:\.([0-9]+))?(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z.]+)?(\w+)?)" @runtime_checkable @@ -409,14 +408,14 @@ def __str__(self) -> str: """All known registered version schemes""" -def get_version_scheme(config: BaseConfig, name: str | None = None) -> VersionScheme: +def get_version_scheme(settings: Settings, name: str | None = None) -> VersionScheme: """ Get the version scheme as defined in the configuration or from an overridden `name` :raises VersionSchemeUnknown: if the version scheme is not found. """ - deprecated_setting: str | None = config.settings.get("version_type") + deprecated_setting: str | None = settings.get("version_type") if deprecated_setting: warnings.warn( DeprecationWarning( @@ -424,7 +423,7 @@ def get_version_scheme(config: BaseConfig, name: str | None = None) -> VersionSc "Please use `version_scheme` instead" ) ) - name = name or config.settings.get("version_scheme") or deprecated_setting + name = name or settings.get("version_scheme") or deprecated_setting if not name: return DEFAULT_SCHEME diff --git a/docs/config.md b/docs/config.md index 210b5d7ff8..5ec1894872 100644 --- a/docs/config.md +++ b/docs/config.md @@ -51,6 +51,26 @@ Default: `$version` Format for the git tag, useful for old projects, that use a convention like `"v1.2.1"`. [Read more][tag_format] +### `legacy_tag_formats` + +Type: `list` + +Default: `[ ]` + +Legacy git tag formats, useful for old projects that changed tag format. +Tags matching those formats will be recognized as version tags and be included in the changelog. +Each entry use the the syntax as [`tag_format`](#tag_format). [Read more][tag_format] + +### `ignored_tag_formats` + +Type: `list` + +Default: `[ ]` + +Tags matching those formats will be totally ignored and won't raise a warning. +Each entry use the the syntax as [`tag_format`](#tag_format) with the addition of `*` +that will match everything (non-greedy). [Read more][tag_format] + ### `update_changelog_on_bump` Type: `bool` diff --git a/docs/faq.md b/docs/faq.md index 4bcb2bc7cf..2711fcf49a 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -107,3 +107,36 @@ If you would like to learn more about both schemes, there are plenty of good res [#173]: https://github.com/commitizen-tools/commitizen/issues/173 [#385]: https://github.com/commitizen-tools/commitizen/pull/385 + +## How to change the tag format ? + +You can use the [`legacy_tag_formats`](config.md#legacy_tag_formats) to list old tag formats. +New bumpped tags will be in the new format but old ones will still work for: +- changelog generation (full, incremental and version range) +- bump new version computation (automatically guessed or increment given) + + +So given if you change from `myproject-$version` to `${version}` and then `v${version}`, +your commitizen configuration will look like this: + +```toml +tag_format = "v${version}" +legacy_tag_formats = [ + "${version}", + "myproject-$version", +] +``` + +## How to avoid warnings for expected non-version tags + +You can explicitly ignore them with [`ignored_tag_formats`](config.md#ignored_tag_formats). + +```toml +tag_format = "v${version}" +ignored_tag_formats = [ + "stable", + "component-*", + "env/*", + "v${major}.${minor}", +] +``` diff --git a/docs/tutorials/monorepo_guidance.md b/docs/tutorials/monorepo_guidance.md index ba6d70fd82..817f92321d 100644 --- a/docs/tutorials/monorepo_guidance.md +++ b/docs/tutorials/monorepo_guidance.md @@ -27,6 +27,7 @@ Sample `.cz.toml` for each component: name = "cz_customize" version = "0.0.0" tag_format = "${version}-library-b" # the component name can be a prefix or suffix with or without a separator +ignored_tag_formats = ["${version}-library-*"] # Avoid noise from other tags update_changelog_on_bump = true ``` @@ -36,6 +37,7 @@ update_changelog_on_bump = true name = "cz_customize" version = "0.0.0" tag_format = "${version}-library-z" +ignored_tag_formats = ["${version}-library-*"] # Avoid noise from other tags update_changelog_on_bump = true ``` diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py index 81273764dd..72003404ae 100644 --- a/tests/commands/test_bump_command.py +++ b/tests/commands/test_bump_command.py @@ -7,6 +7,7 @@ from textwrap import dedent from unittest.mock import MagicMock, call +import py import pytest from pytest_mock import MockFixture @@ -1523,3 +1524,31 @@ def test_bump_get_next__no_eligible_commits_raises(mocker: MockFixture): with pytest.raises(NoneIncrementExit): cli.main() + + +def test_bump_detect_legacy_tags_from_scm( + tmp_commitizen_project: py.path.local, mocker: MockFixture +): + project_root = Path(tmp_commitizen_project) + tmp_commitizen_cfg_file = project_root / "pyproject.toml" + tmp_commitizen_cfg_file.write_text( + "\n".join( + [ + "[tool.commitizen]", + 'version_provider = "scm"', + 'tag_format = "v$version"', + "legacy_tag_formats = [", + ' "legacy-${version}"', + "]", + ] + ), + ) + create_file_and_commit("feat: new file") + create_tag("legacy-0.4.2") + create_file_and_commit("feat: new file") + + testargs = ["cz", "bump", "--increment", "patch", "--changelog"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + + assert git.tag_exist("v0.4.3") diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index bc0d6c6a28..3b6dd393e6 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -8,6 +8,7 @@ from dateutil import relativedelta from jinja2 import FileSystemLoader from pytest_mock import MockFixture +from pytest_regressions.file_regression import FileRegressionFixture from commitizen import __file__ as commitizen_init from commitizen import cli, git @@ -1116,6 +1117,41 @@ def test_changelog_from_rev_version_range_from_arg( file_regression.check(out, extension=".md") +@pytest.mark.usefixtures("tmp_commitizen_project") +@pytest.mark.freeze_time("2022-02-13") +def test_changelog_from_rev_version_range_with_legacy_tags( + mocker: MockFixture, config_path, changelog_path, file_regression +): + mocker.patch("commitizen.git.GitTag.date", "2022-02-13") + + changelog = Path(changelog_path) + Path(config_path).write_text( + "\n".join( + [ + "[tool.commitizen]", + 'version_provider = "scm"', + 'tag_format = "v$version"', + "legacy_tag_formats = [", + ' "legacy-${version}",', + ' "old-${version}",', + "]", + ] + ), + ) + + create_file_and_commit("feat: new file") + create_tag("old-0.2.0") + create_file_and_commit("feat: new file") + create_tag("legacy-0.3.0") + create_file_and_commit("feat: new file") + create_tag("legacy-0.4.0") + + testargs = ["cz", "changelog", "0.2.0..0.4.0"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + file_regression.check(changelog.read_text(), extension=".md") + + @pytest.mark.usefixtures("tmp_commitizen_project") @pytest.mark.freeze_time("2022-02-13") def test_changelog_from_rev_version_with_big_range_from_arg( @@ -1639,6 +1675,127 @@ def test_changelog_only_tag_matching_tag_format_included_suffix_sep( assert "## 0.2.0 (2021-06-11)" not in out +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_changelog_legacy_tags( + mocker: MockFixture, + changelog_path: Path, + config_path: Path, +): + with open(config_path, "a", encoding="utf-8") as f: + f.writelines( + [ + 'tag_format = "v${version}"\n', + "legacy_tag_formats = [\n", + ' "older-${version}",\n', + ' "oldest-${version}",\n', + "]\n", + ] + ) + create_file_and_commit("feat: new file") + git.tag("oldest-0.1.0") + create_file_and_commit("feat: new file") + git.tag("older-0.2.0") + create_file_and_commit("feat: another new file") + git.tag("v0.3.0") + create_file_and_commit("feat: another new file") + git.tag("not-0.3.1") + testargs = ["cz", "bump", "--changelog", "--yes"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + out = open(changelog_path).read() + assert "## v0.3.0" in out + assert "## older-0.2.0" in out + assert "## oldest-0.1.0" in out + assert "## v0.3.0" in out + assert "## not-0.3.1" not in out + + +@pytest.mark.usefixtures("tmp_commitizen_project") +@pytest.mark.freeze_time("2024-11-18") +def test_changelog_incremental_change_tag_format( + mocker: MockFixture, + changelog_path: Path, + config_path: Path, + file_regression: FileRegressionFixture, +): + mocker.patch("commitizen.git.GitTag.date", "2024-11-18") + config = Path(config_path) + base_config = config.read_text() + config.write_text( + "\n".join( + ( + base_config, + 'tag_format = "older-${version}"', + ) + ) + ) + create_file_and_commit("feat: new file") + git.tag("older-0.1.0") + create_file_and_commit("feat: new file") + git.tag("older-0.2.0") + mocker.patch.object(sys, "argv", ["cz", "changelog"]) + cli.main() + + config.write_text( + "\n".join( + ( + base_config, + 'tag_format = "v${version}"', + 'legacy_tag_formats = ["older-${version}"]', + ) + ) + ) + create_file_and_commit("feat: another new file") + git.tag("v0.3.0") + mocker.patch.object(sys, "argv", ["cz", "changelog", "--incremental"]) + cli.main() + out = open(changelog_path).read() + assert "## v0.3.0" in out + assert "## older-0.2.0" in out + assert "## older-0.1.0" in out + file_regression.check(out, extension=".md") + + +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_changelog_ignored_tags( + mocker: MockFixture, + changelog_path: Path, + config_path: Path, + capsys: pytest.CaptureFixture, +): + with open(config_path, "a", encoding="utf-8") as f: + f.writelines( + [ + 'tag_format = "v${version}"\n', + "ignored_tag_formats = [\n", + ' "ignored",\n', + ' "ignore-${version}",\n', + "]\n", + ] + ) + create_file_and_commit("feat: new file") + git.tag("ignore-0.1.0") + create_file_and_commit("feat: new file") + git.tag("ignored") + create_file_and_commit("feat: another new file") + git.tag("v0.3.0") + create_file_and_commit("feat: another new file") + git.tag("not-ignored") + testargs = ["cz", "bump", "--changelog", "--yes"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + out = open(changelog_path).read() + _, err = capsys.readouterr() + assert "## ignore-0.1.0" not in out + assert "InvalidVersion ignore-0.1.0" not in err + assert "## ignored" not in out + assert "InvalidVersion ignored" not in err + assert "## not-ignored" not in out + assert "InvalidVersion not-ignored" in err + assert "## v0.3.0" in out + assert "InvalidVersion v0.3.0" not in err + + def test_changelog_template_extra_quotes( mocker: MockFixture, tmp_commitizen_project: Path, diff --git a/tests/commands/test_changelog_command/test_changelog_from_rev_version_range_with_legacy_tags.md b/tests/commands/test_changelog_command/test_changelog_from_rev_version_range_with_legacy_tags.md new file mode 100644 index 0000000000..5d37333aa5 --- /dev/null +++ b/tests/commands/test_changelog_command/test_changelog_from_rev_version_range_with_legacy_tags.md @@ -0,0 +1,17 @@ +## legacy-0.4.0 (2022-02-13) + +### Feat + +- new file + +## legacy-0.3.0 (2022-02-13) + +### Feat + +- new file + +## old-0.2.0 (2022-02-13) + +### Feat + +- new file diff --git a/tests/commands/test_changelog_command/test_changelog_incremental_change_tag_format.md b/tests/commands/test_changelog_command/test_changelog_incremental_change_tag_format.md new file mode 100644 index 0000000000..2f0cc2909e --- /dev/null +++ b/tests/commands/test_changelog_command/test_changelog_incremental_change_tag_format.md @@ -0,0 +1,17 @@ +## v0.3.0 (2024-11-18) + +### Feat + +- another new file + +## older-0.2.0 (2024-11-18) + +### Feat + +- new file + +## older-0.1.0 (2024-11-18) + +### Feat + +- new file diff --git a/tests/providers/test_scm_provider.py b/tests/providers/test_scm_provider.py index 01e7ab9943..9d955b2323 100644 --- a/tests/providers/test_scm_provider.py +++ b/tests/providers/test_scm_provider.py @@ -113,3 +113,26 @@ def test_scm_provider_default_with_commits_and_tags(config: BaseConfig): merge_branch("master") assert provider.get_version() == "1.1.0rc0" + + +@pytest.mark.usefixtures("tmp_git_project") +def test_scm_provider_detect_legacy_tags(config: BaseConfig): + config.settings["version_provider"] = "scm" + config.settings["tag_format"] = "v${version}" + config.settings["legacy_tag_formats"] = [ + "legacy-${version}", + "old-${version}", + ] + provider = get_provider(config) + + create_file_and_commit("test: fake commit") + create_tag("old-0.4.1") + assert provider.get_version() == "0.4.1" + + create_file_and_commit("test: fake commit") + create_tag("legacy-0.4.2") + assert provider.get_version() == "0.4.2" + + create_file_and_commit("test: fake commit") + create_tag("v0.5.0") + assert provider.get_version() == "0.5.0" diff --git a/tests/test_bump_normalize_tag.py b/tests/test_bump_normalize_tag.py index c1eb696afd..895acbd71a 100644 --- a/tests/test_bump_normalize_tag.py +++ b/tests/test_bump_normalize_tag.py @@ -1,6 +1,6 @@ import pytest -from commitizen import bump +from commitizen.tags import TagRules conversion = [ (("1.2.3", "v$version"), "v1.2.3"), @@ -18,5 +18,6 @@ @pytest.mark.parametrize("test_input,expected", conversion) def test_create_tag(test_input, expected): version, format = test_input - new_tag = bump.normalize_tag(version, format) + rules = TagRules() + new_tag = rules.normalize_tag(version, format) assert new_tag == expected diff --git a/tests/test_changelog.py b/tests/test_changelog.py index 20d59488d3..657e93f1a1 100644 --- a/tests/test_changelog.py +++ b/tests/test_changelog.py @@ -1,4 +1,5 @@ import re +from dataclasses import dataclass from pathlib import Path from typing import Optional @@ -11,6 +12,7 @@ ConventionalCommitsCz, ) from commitizen.exceptions import InvalidConfigurationError +from commitizen.version_schemes import Pep440 COMMITS_DATA = [ { @@ -522,12 +524,14 @@ def test_get_commit_tag_is_None(gitcommits, tags): @pytest.mark.parametrize("test_input", TAGS) def test_valid_tag_included_in_changelog(test_input): tag = git.GitTag(*test_input) - assert changelog.tag_included_in_changelog(tag, [], False) + rules = changelog.TagRules() + assert rules.include_in_changelog(tag) def test_invalid_tag_included_in_changelog(): tag = git.GitTag("not_a_version", "rev", "date") - assert not changelog.tag_included_in_changelog(tag, [], False) + rules = changelog.TagRules() + assert not rules.include_in_changelog(tag) COMMITS_TREE = ( @@ -1080,8 +1084,11 @@ def test_invalid_tag_included_in_changelog(): def test_generate_tree_from_commits(gitcommits, tags, merge_prereleases): parser = ConventionalCommitsCz.commit_parser changelog_pattern = ConventionalCommitsCz.bump_pattern + rules = changelog.TagRules( + merge_prereleases=merge_prereleases, + ) tree = changelog.generate_tree_from_commits( - gitcommits, tags, parser, changelog_pattern, merge_prerelease=merge_prereleases + gitcommits, tags, parser, changelog_pattern, rules=rules ) expected = ( COMMITS_TREE_AFTER_MERGED_PRERELEASES if merge_prereleases else COMMITS_TREE @@ -1451,3 +1458,105 @@ def test_get_smart_tag_range_returns_an_extra_for_a_single_tag(tags): start = tags[0] # len here is 1, but we expect one more tag as designed res = changelog.get_smart_tag_range(tags, start.name) assert 2 == len(res) + + +@dataclass +class TagDef: + name: str + is_version: bool + is_legacy: bool + is_ignored: bool + + +TAGS_PARAMS = ( + pytest.param(TagDef("1.2.3", True, False, False), id="version"), + # We test with `v-` prefix as `v` prefix is a special case kept for backward compatibility + pytest.param(TagDef("v-1.2.3", False, True, False), id="v-prefix"), + pytest.param(TagDef("project-1.2.3", False, True, False), id="project-prefix"), + pytest.param(TagDef("ignored", False, False, True), id="ignored"), + pytest.param(TagDef("unknown", False, False, False), id="unknown"), +) + + +@pytest.mark.parametrize("tag", TAGS_PARAMS) +def test_tag_rules_tag_format_only(tag: TagDef): + rules = changelog.TagRules(Pep440, "$version") + assert rules.is_version_tag(tag.name) is tag.is_version + + +@pytest.mark.parametrize("tag", TAGS_PARAMS) +def test_tag_rules_with_legacy_tags(tag: TagDef): + rules = changelog.TagRules( + scheme=Pep440, + tag_format="$version", + legacy_tag_formats=["v-$version", "project-${version}"], + ) + assert rules.is_version_tag(tag.name) is tag.is_version or tag.is_legacy + + +@pytest.mark.parametrize("tag", TAGS_PARAMS) +def test_tag_rules_with_ignored_tags(tag: TagDef): + rules = changelog.TagRules( + scheme=Pep440, tag_format="$version", ignored_tag_formats=["ignored"] + ) + assert rules.is_ignored_tag(tag.name) is tag.is_ignored + + +def test_tags_rules_get_version_tags(capsys: pytest.CaptureFixture): + tags = [ + git.GitTag("v1.1.0", "17efb44d2cd16f6621413691a543e467c7d2dda6", "2019-04-14"), + git.GitTag("v1.0.0", "aa44a92d68014d0da98965c0c2cb8c07957d4362", "2019-03-01"), + git.GitTag("1.0.0b2", "aab33d13110f26604fb786878856ec0b9e5fc32b", "2019-01-18"), + git.GitTag( + "project-not-a-version", + "7c7e96b723c2aaa1aec3a52561f680adf0b60e97", + "2019-01-17", + ), + git.GitTag( + "not-a-version", "c52eca6f74f844ab3ffbde61d98ef96071e132b7", "2018-12-17" + ), + git.GitTag( + "star-something", "c52eca6f74f844ab3ffbde61d98fe96071e132b2", "2018-11-12" + ), + git.GitTag("known", "b3f89892222340150e32631ae6b7aab65230036f", "2018-09-22"), + git.GitTag( + "ignored-0.9.3", "684e0259cc95c7c5e94854608cd3dcebbd53219e", "2018-09-22" + ), + git.GitTag( + "project-0.9.3", "dacc86159b260ee98eb5f57941c99ba731a01399", "2018-07-28" + ), + git.GitTag( + "anything-0.9", "5141f54503d2e1cf39bd666c0ca5ab5eb78772ab", "2018-01-10" + ), + git.GitTag( + "project-0.9.2", "1541f54503d2e1cf39bd777c0ca5ab5eb78772ba", "2017-11-11" + ), + git.GitTag( + "ignored-0.9.1", "46e9032e18a819e466618c7a014bcb0e9981af9e", "2017-11-11" + ), + ] + + rules = changelog.TagRules( + scheme=Pep440, + tag_format="v$version", + legacy_tag_formats=["$version", "project-${version}"], + ignored_tag_formats=[ + "known", + "ignored-${version}", + "star-*", + "*-${major}.${minor}", + ], + ) + + version_tags = rules.get_version_tags(tags, warn=True) + assert {t.name for t in version_tags} == { + "v1.1.0", + "v1.0.0", + "1.0.0b2", + "project-0.9.3", + "project-0.9.2", + } + + captured = capsys.readouterr() + assert captured.err.count("InvalidVersion") == 2 + assert captured.err.count("not-a-version") == 2 diff --git a/tests/test_changelog_format_asciidoc.py b/tests/test_changelog_format_asciidoc.py index 0c5930df46..59ca56191e 100644 --- a/tests/test_changelog_format_asciidoc.py +++ b/tests/test_changelog_format_asciidoc.py @@ -55,6 +55,7 @@ """ EXPECTED_C = Metadata( latest_version="1.0.0", + latest_version_tag="v1.0.0", latest_version_position=3, unreleased_end=3, unreleased_start=1, @@ -105,20 +106,21 @@ def format(config: BaseConfig) -> AsciiDoc: @pytest.fixture def format_with_tags(config: BaseConfig, request) -> AsciiDoc: config.settings["tag_format"] = request.param + config.settings["legacy_tag_formats"] = ["legacy-${version}"] return AsciiDoc(config) VERSIONS_EXAMPLES = [ - ("== [1.0.0] - 2017-06-20", "1.0.0"), + ("== [1.0.0] - 2017-06-20", ("1.0.0", "1.0.0")), ( "= https://github.com/angular/angular/compare/10.0.0-next.2...10.0.0-next.3[10.0.0-next.3] (2020-04-22)", - "10.0.0-next.3", + ("10.0.0-next.3", "10.0.0-next.3"), ), - ("=== 0.19.1 (Jan 7, 2020)", "0.19.1"), - ("== 1.0.0", "1.0.0"), - ("== v1.0.0", "1.0.0"), - ("== v1.0.0 - (2012-24-32)", "1.0.0"), - ("= version 2020.03.24", "2020.03.24"), + ("=== 0.19.1 (Jan 7, 2020)", ("0.19.1", "0.19.1")), + ("== 1.0.0", ("1.0.0", "1.0.0")), + ("== v1.0.0", ("1.0.0", "v1.0.0")), + ("== v1.0.0 - (2012-24-32)", ("1.0.0", "v1.0.0")), + ("= version 2020.03.24", ("2020.03.24", "2020.03.24")), ("== [Unreleased]", None), ("All notable changes to this project will be documented in this file.", None), ("= Changelog", None), @@ -128,7 +130,7 @@ def format_with_tags(config: BaseConfig, request) -> AsciiDoc: @pytest.mark.parametrize("line_from_changelog,output_version", VERSIONS_EXAMPLES) def test_changelog_detect_version( - line_from_changelog: str, output_version: str, format: AsciiDoc + line_from_changelog: str, output_version: tuple[str, str] | None, format: AsciiDoc ): version = format.parse_version_from_title(line_from_changelog) assert version == output_version @@ -186,6 +188,7 @@ def test_get_matadata( "1-0-0-a1.dev1-example", "1.0.0-a1.dev1", ), + pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), ), indirect=["format_with_tags"], ) diff --git a/tests/test_changelog_format_markdown.py b/tests/test_changelog_format_markdown.py index 52612b8e2b..e1f0d67311 100644 --- a/tests/test_changelog_format_markdown.py +++ b/tests/test_changelog_format_markdown.py @@ -55,6 +55,7 @@ """ EXPECTED_C = Metadata( latest_version="1.0.0", + latest_version_tag="v1.0.0", latest_version_position=3, unreleased_end=3, unreleased_start=1, @@ -105,20 +106,21 @@ def format(config: BaseConfig) -> Markdown: @pytest.fixture def format_with_tags(config: BaseConfig, request) -> Markdown: config.settings["tag_format"] = request.param + config.settings["legacy_tag_formats"] = ["legacy-${version}"] return Markdown(config) VERSIONS_EXAMPLES = [ - ("## [1.0.0] - 2017-06-20", "1.0.0"), + ("## [1.0.0] - 2017-06-20", ("1.0.0", "1.0.0")), ( "# [10.0.0-next.3](https://github.com/angular/angular/compare/10.0.0-next.2...10.0.0-next.3) (2020-04-22)", - "10.0.0-next.3", + ("10.0.0-next.3", "10.0.0-next.3"), ), - ("### 0.19.1 (Jan 7, 2020)", "0.19.1"), - ("## 1.0.0", "1.0.0"), - ("## v1.0.0", "1.0.0"), - ("## v1.0.0 - (2012-24-32)", "1.0.0"), - ("# version 2020.03.24", "2020.03.24"), + ("### 0.19.1 (Jan 7, 2020)", ("0.19.1", "0.19.1")), + ("## 1.0.0", ("1.0.0", "1.0.0")), + ("## v1.0.0", ("1.0.0", "v1.0.0")), + ("## v1.0.0 - (2012-24-32)", ("1.0.0", "v1.0.0")), + ("# version 2020.03.24", ("2020.03.24", "2020.03.24")), ("## [Unreleased]", None), ("All notable changes to this project will be documented in this file.", None), ("# Changelog", None), @@ -128,7 +130,7 @@ def format_with_tags(config: BaseConfig, request) -> Markdown: @pytest.mark.parametrize("line_from_changelog,output_version", VERSIONS_EXAMPLES) def test_changelog_detect_version( - line_from_changelog: str, output_version: str, format: Markdown + line_from_changelog: str, output_version: tuple[str, str] | None, format: Markdown ): version = format.parse_version_from_title(line_from_changelog) assert version == output_version @@ -191,6 +193,7 @@ def test_get_matadata( "1-0-0-a1.dev1-example", "1.0.0-a1.dev1", ), + pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), ), indirect=["format_with_tags"], ) diff --git a/tests/test_changelog_format_restructuredtext.py b/tests/test_changelog_format_restructuredtext.py index 11356ae28f..74b6b736f9 100644 --- a/tests/test_changelog_format_restructuredtext.py +++ b/tests/test_changelog_format_restructuredtext.py @@ -22,6 +22,7 @@ def case( content: str, latest_version: str | None = None, latest_version_position: int | None = None, + latest_version_tag: str | None = None, unreleased_start: int | None = None, unreleased_end: int | None = None, ): @@ -30,6 +31,7 @@ def case( dedent(content).strip(), Metadata( latest_version=latest_version, + latest_version_tag=latest_version_tag, latest_version_position=latest_version_position, unreleased_start=unreleased_start, unreleased_end=unreleased_end, @@ -93,6 +95,7 @@ def case( ====== """, latest_version="1.0.0", + latest_version_tag="v1.0.0", latest_version_position=3, unreleased_start=0, unreleased_end=3, @@ -303,6 +306,7 @@ def format(config: BaseConfig) -> RestructuredText: @pytest.fixture def format_with_tags(config: BaseConfig, request) -> RestructuredText: config.settings["tag_format"] = request.param + config.settings["legacy_tag_formats"] = ["legacy-${version}"] return RestructuredText(config) @@ -357,6 +361,7 @@ def test_is_overlined_title(format: RestructuredText, text: str, expected: bool) "1-0-0-a1.dev1-example", "1.0.0-a1.dev1", ), + pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), ), indirect=["format_with_tags"], ) diff --git a/tests/test_changelog_format_textile.py b/tests/test_changelog_format_textile.py index 3fac5c1756..eb03484ad5 100644 --- a/tests/test_changelog_format_textile.py +++ b/tests/test_changelog_format_textile.py @@ -55,6 +55,7 @@ """ EXPECTED_C = Metadata( latest_version="1.0.0", + latest_version_tag="v1.0.0", latest_version_position=3, unreleased_end=3, unreleased_start=1, @@ -98,20 +99,21 @@ def format(config: BaseConfig) -> Textile: @pytest.fixture def format_with_tags(config: BaseConfig, request) -> Textile: config.settings["tag_format"] = request.param + config.settings["legacy_tag_formats"] = ["legacy-${version}"] return Textile(config) VERSIONS_EXAMPLES = [ - ("h2. [1.0.0] - 2017-06-20", "1.0.0"), + ("h2. [1.0.0] - 2017-06-20", ("1.0.0", "1.0.0")), ( 'h1. "10.0.0-next.3":https://github.com/angular/angular/compare/10.0.0-next.2...10.0.0-next.3 (2020-04-22)', - "10.0.0-next.3", + ("10.0.0-next.3", "10.0.0-next.3"), ), - ("h3. 0.19.1 (Jan 7, 2020)", "0.19.1"), - ("h2. 1.0.0", "1.0.0"), - ("h2. v1.0.0", "1.0.0"), - ("h2. v1.0.0 - (2012-24-32)", "1.0.0"), - ("h1. version 2020.03.24", "2020.03.24"), + ("h3. 0.19.1 (Jan 7, 2020)", ("0.19.1", "0.19.1")), + ("h2. 1.0.0", ("1.0.0", "1.0.0")), + ("h2. v1.0.0", ("1.0.0", "v1.0.0")), + ("h2. v1.0.0 - (2012-24-32)", ("1.0.0", "v1.0.0")), + ("h1. version 2020.03.24", ("2020.03.24", "2020.03.24")), ("h2. [Unreleased]", None), ("All notable changes to this project will be documented in this file.", None), ("h1. Changelog", None), @@ -121,7 +123,7 @@ def format_with_tags(config: BaseConfig, request) -> Textile: @pytest.mark.parametrize("line_from_changelog,output_version", VERSIONS_EXAMPLES) def test_changelog_detect_version( - line_from_changelog: str, output_version: str, format: Textile + line_from_changelog: str, output_version: tuple[str, str] | None, format: Textile ): version = format.parse_version_from_title(line_from_changelog) assert version == output_version @@ -179,6 +181,7 @@ def test_get_matadata( "1-0-0-a1.dev1-example", "1.0.0-a1.dev1", ), + pytest.param("new-${version}", "legacy-1.0.0", "1.0.0"), ), indirect=["format_with_tags"], ) diff --git a/tests/test_conf.py b/tests/test_conf.py index 1cbbc57aca..4e0ff4968f 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -72,6 +72,8 @@ "version_provider": "commitizen", "version_scheme": None, "tag_format": "$version", + "legacy_tag_formats": [], + "ignored_tag_formats": [], "bump_message": None, "retry_after_failure": False, "allow_abort": False, @@ -101,6 +103,8 @@ "version_provider": "commitizen", "version_scheme": None, "tag_format": "$version", + "legacy_tag_formats": [], + "ignored_tag_formats": [], "bump_message": None, "retry_after_failure": False, "allow_abort": False, diff --git a/tests/test_version_schemes.py b/tests/test_version_schemes.py index 686c0bfde1..8e2dae9027 100644 --- a/tests/test_version_schemes.py +++ b/tests/test_version_schemes.py @@ -16,31 +16,31 @@ def test_default_version_scheme_is_pep440(config: BaseConfig): - scheme = get_version_scheme(config) + scheme = get_version_scheme(config.settings) assert scheme is Pep440 def test_version_scheme_from_config(config: BaseConfig): config.settings["version_scheme"] = "semver" - scheme = get_version_scheme(config) + scheme = get_version_scheme(config.settings) assert scheme is SemVer def test_version_scheme_from_name(config: BaseConfig): config.settings["version_scheme"] = "pep440" - scheme = get_version_scheme(config, "semver") + scheme = get_version_scheme(config.settings, "semver") assert scheme is SemVer def test_raise_for_unknown_version_scheme(config: BaseConfig): with pytest.raises(VersionSchemeUnknown): - get_version_scheme(config, "unknown") + get_version_scheme(config.settings, "unknown") def test_version_scheme_from_deprecated_config(config: BaseConfig): config.settings["version_type"] = "semver" with pytest.warns(DeprecationWarning): - scheme = get_version_scheme(config) + scheme = get_version_scheme(config.settings) assert scheme is SemVer @@ -48,7 +48,7 @@ def test_version_scheme_from_config_priority(config: BaseConfig): config.settings["version_scheme"] = "pep440" config.settings["version_type"] = "semver" with pytest.warns(DeprecationWarning): - scheme = get_version_scheme(config) + scheme = get_version_scheme(config.settings) assert scheme is Pep440 @@ -63,4 +63,4 @@ class NotVersionProtocol: mocker.patch.object(metadata, "entry_points", return_value=(ep,)) with pytest.warns(match="VersionProtocol"): - get_version_scheme(config, "any") + get_version_scheme(config.settings, "any")