Skip to content

Commit 0f5fe53

Browse files
pujitha24woile
authored andcommitted
fix(scm-provider): prefer full version over floating tags
Motivation: With tags v1.0.0, v1.0 and v1 on one commit, the scm version provider returned "1.0". The three versions compare equal, and max() kept the first one, which is the newest tag by date. The patch part was dropped from the project version. Approach: Break ties between equal versions by choosing the longest spelling, so 1.0.0 wins over 1.0 and 1. Ordering of unequal versions is unchanged. Validation: `uv run pytest -n0` passed (1331 passed, 2 skipped, 2 xfailed). The new test fails without the change and passes with it. `ruff check`, `ruff format --check` and `mypy commitizen/providers` pass. `uv run poe all` was not run in full. Impact: Only affects `version_provider = "scm"` when equal-valued tags with different lengths exist. Only the version printed or used was wrong; nothing else changed. Report: #1863 Assisted By: AI Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com> Assisted-by: claude-sonnet-5-5 (via Claude Code)
1 parent b78d581 commit 0f5fe53

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎commitizen/providers/scm_provider.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ def get_version(self) -> str:
1818
rules = TagRules.from_settings(self.config.settings)
1919
tags = get_tags(reachable_only=True)
2020
version_tags = rules.get_version_tags(tags)
21-
version = max((rules.extract_version(t) for t in version_tags), default=None)
21+
# Floating tags (e.g. `v1.0` next to `v1.0.0`) compare equal. Among equal
22+
# versions, prefer the most specific (longest) spelling.
23+
version = max(
24+
(rules.extract_version(t) for t in version_tags),
25+
key=lambda v: (v, len(str(v))),
26+
default=None,
27+
)
2228
return str(version) if version is not None else "0.0.0"
2329

2430
def set_version(self, version: str) -> None:

‎tests/providers/test_scm_provider.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,23 @@ def test_scm_provider_detect_legacy_tags(config: BaseConfig, util: UtilFixture):
140140
util.create_file_and_commit("test: fake commit")
141141
util.create_tag("v0.5.0")
142142
assert provider.get_version() == "0.5.0"
143+
144+
145+
@pytest.mark.usefixtures("tmp_git_project")
146+
def test_scm_provider_prefers_full_version_over_floating_tags(
147+
config: BaseConfig, util: UtilFixture
148+
):
149+
"""Floating tags (`v1.0`, `v1`) must not hide the full `v1.0.0` version."""
150+
# Arrange
151+
util.create_file_and_commit("feat: initial commit")
152+
util.create_tag("v1.0.0")
153+
util.create_tag("v1.0")
154+
util.create_tag("v1")
155+
config.settings["version_provider"] = "scm"
156+
config.settings["tag_format"] = "v$version"
157+
158+
# Act
159+
provider = get_provider(config)
160+
161+
# Assert
162+
assert provider.get_version() == "1.0.0"

0 commit comments

Comments
 (0)