Skip to content

fix(bump): don't fail if an invalid version tag is present (fix #1410) #1418

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

Merged
merged 1 commit into from
May 16, 2025
Merged
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
12 changes: 7 additions & 5 deletions commitizen/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,20 @@ def _format_regex(self, tag_pattern: str, star: bool = False) -> str:
format_regex = format_regex.replace(pattern, regex)
return format_regex

def _version_tag_error(self, tag: str) -> str:
"""Format the error message for an invalid version tag"""
return f"Invalid version tag: '{tag}' does not match any configured tag format"

def is_version_tag(self, tag: str | GitTag, warn: bool = False) -> bool:
"""
True if a given tag is a legit version tag.

if `warn` is `True`, it will print a warning message if the tag is not a version tag.
"""
tag = tag.name if isinstance(tag, GitTag) else tag
is_legit = any(regex.match(tag) for regex in self.version_regexes)
is_legit = any(regex.fullmatch(tag) for regex in self.version_regexes)
if warn and not is_legit and not self.is_ignored_tag(tag):
out.warn(f"InvalidVersion {tag} doesn't match any configured tag format")
out.warn(self._version_tag_error(tag))
return is_legit

def is_ignored_tag(self, tag: str | GitTag) -> bool:
Expand All @@ -146,9 +150,7 @@ def extract_version(self, tag: GitTag) -> Version:
m for regex in self.version_regexes if (m := regex.fullmatch(tag.name))
)
if not (m := next(candidates, None)):
raise InvalidVersion(
f"Invalid version tag: '{tag.name}' does not match any configured tag format"
)
raise InvalidVersion(self._version_tag_error(tag.name))
if "version" in m.groupdict():
return self.scheme(m.group("version"))

Expand Down
32 changes: 32 additions & 0 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -1656,3 +1656,35 @@ def test_bump_detect_legacy_tags_from_scm(
cli.main()

assert git.tag_exist("v0.4.3")


def test_bump_warn_but_dont_fail_on_invalid_tags(
tmp_commitizen_project: py.path.local,
mocker: MockFixture,
capsys: pytest.CaptureFixture,
):
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"',
'version_scheme = "pep440"',
]
),
)
create_file_and_commit("feat: new file")
create_tag("0.4.2")
create_file_and_commit("feat: new file")
create_tag("0.4.3.deadbeaf")
create_file_and_commit("feat: new file")

testargs = ["cz", "bump", "--increment", "patch", "--changelog"]
mocker.patch.object(sys, "argv", testargs)
cli.main()

_, err = capsys.readouterr()

assert err.count("Invalid version tag: '0.4.3.deadbeaf'") == 1
assert git.tag_exist("0.4.3")
8 changes: 4 additions & 4 deletions tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -1788,13 +1788,13 @@ def test_changelog_ignored_tags(
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 "Invalid version tag: 'ignore-0.1.0'" not in err
assert "## ignored" not in out
assert "InvalidVersion ignored" not in err
assert "Invalid version tag: 'ignored'" not in err
assert "## not-ignored" not in out
assert "InvalidVersion not-ignored" in err
assert "Invalid version tag: 'not-ignored'" in err
assert "## v0.3.0" in out
assert "InvalidVersion v0.3.0" not in err
assert "Invalid version tag: 'v0.3.0'" not in err


def test_changelog_template_extra_quotes(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,7 @@ def test_tags_rules_get_version_tags(capsys: pytest.CaptureFixture):
}

captured = capsys.readouterr()
assert captured.err.count("InvalidVersion") == 2
assert captured.err.count("Invalid version tag:") == 2
assert captured.err.count("not-a-version") == 2


Expand Down