Skip to content
Draft
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
20 changes: 19 additions & 1 deletion src/semantic_release/cli/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import subprocess
import sys
from collections import defaultdict
from contextlib import suppress
from datetime import datetime, timezone
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -92,11 +93,28 @@ def last_released(
return ts_and_vs[0] if ts_and_vs else None


def _tag_points_into(tag: Tag, commit_hexshas: set[str]) -> bool:
"""Return True if the tag resolves to a commit in ``commit_hexshas``."""
# Tags pointing at a blob or tree (no commit) raise ValueError; those are not in history
with suppress(ValueError):
return tag.commit.hexsha in commit_hexshas
return False


def version_from_forced_level(
repo_dir: Path, forced_level_bump: LevelBump, translator: VersionTranslator
) -> Version:
with Repo(str(repo_dir)) as git_repo:
ts_and_vs = tags_and_versions(git_repo.tags, translator)
# Only consider tags reachable from the current branch's history (HEAD),
# so a forced bump is based on the same history as an unforced one
head_history_hexshas = {
commit.hexsha for commit in git_repo.iter_commits("HEAD")
}
ts_and_vs = [
(tag, version)
for tag, version in tags_and_versions(git_repo.tags, translator)
if _tag_points_into(tag, head_history_hexshas)
]

# If we have no tags, return the default version
if not ts_and_vs:
Expand Down
36 changes: 35 additions & 1 deletion tests/unit/semantic_release/cli/test_version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
from pathlib import Path

import pytest
from git import Repo

from semantic_release.cli.commands.version import is_forced_prerelease
from semantic_release.cli.commands.version import (
is_forced_prerelease,
version_from_forced_level,
)
from semantic_release.enums import LevelBump
from semantic_release.version.translator import VersionTranslator


@pytest.mark.parametrize(
Expand All @@ -21,3 +29,29 @@
)
def test_is_forced_prerelease(force_prerelease, force_level, prerelease, expected):
assert is_forced_prerelease(force_prerelease, force_level, prerelease) == expected


def test_version_from_forced_level_only_considers_head_history(tmp_path: Path):
# A higher tag exists on a side branch that is NOT in HEAD's history.
# A forced bump must be based on the history reachable from HEAD (hotfix),
# not on the highest tag in the whole repository.
repo = Repo.init(tmp_path)
try:
base = repo.index.commit("base")
repo.create_tag("v1.2.0", ref=base)

side = repo.index.commit("side work", parent_commits=(base,), head=False)
repo.create_tag("v1.4.0", ref=side)

hotfix = repo.index.commit("fix: hotfix", parent_commits=(base,))
repo.create_tag("v1.2.1", ref=hotfix)

result = version_from_forced_level(
repo_dir=Path(repo.working_dir),
forced_level_bump=LevelBump.PATCH,
translator=VersionTranslator(),
)
finally:
repo.close()

assert str(result) == "1.2.2"