Skip to content

Commit 0b7774c

Browse files
committed
refactor(changelog): add get_next_tag_name_after_version and test, mark unused for get_smart_tag_range
1 parent a7f6406 commit 0b7774c

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

commitizen/changelog.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,18 @@ def incremental_build(
281281
return output_lines
282282

283283

284+
def get_next_tag_name_after_version(tags: Sequence[GitTag], version: str) -> str | None:
285+
idx = len(tags) - 1
286+
while idx >= 0:
287+
if tags[idx].name == version:
288+
if idx == len(tags) - 1:
289+
return None
290+
return tags[idx + 1].name
291+
idx -= 1
292+
raise NoCommitsFoundError(f"Could not find a valid revision range. {version=}")
293+
294+
295+
# TODO: unused, deprecate this?
284296
def get_smart_tag_range(
285297
tags: Sequence[GitTag], newest: str, oldest: str | None = None
286298
) -> list[GitTag]:
@@ -331,15 +343,13 @@ def get_tag_name(v: str) -> str:
331343
newest_tag_name = get_tag_name(newest_version)
332344
oldest_tag_name = get_tag_name(oldest_version) if oldest_version else None
333345

334-
tags_range = get_smart_tag_range(tags, newest_tag_name, oldest_tag_name)
335-
if not tags_range:
336-
raise NoCommitsFoundError("Could not find a valid revision range.")
337-
338-
oldest_rev: str | None = tags_range[-1].name
346+
oldest_rev = get_next_tag_name_after_version(
347+
tags, oldest_tag_name or newest_tag_name
348+
)
339349

340350
# Return None for oldest_rev if:
341351
# 1. The oldest tag is the last tag in the list and matches the requested oldest tag
342352
# 2. The oldest and the newest tag are the same
343-
if oldest_rev == oldest_tag_name == tags[-1].name or oldest_rev == newest_tag_name:
344-
oldest_rev = None
353+
if oldest_rev == newest_tag_name:
354+
return None, newest_tag_name
345355
return oldest_rev, newest_tag_name

tests/test_changelog.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,24 @@ def test_get_smart_tag_range_returns_an_extra_for_a_single_tag(tags):
15351535
assert 2 == len(res)
15361536

15371537

1538+
def test_get_next_tag_name_after_version(tags):
1539+
# Test finding next tag after a version
1540+
next_tag_name = changelog.get_next_tag_name_after_version(tags, "v1.2.0")
1541+
assert next_tag_name == "v1.1.1"
1542+
1543+
next_tag_name = changelog.get_next_tag_name_after_version(tags, "v1.1.0")
1544+
assert next_tag_name == "v1.0.0"
1545+
1546+
# Test finding last tag when given version is last
1547+
last_tag_name = changelog.get_next_tag_name_after_version(tags, "v0.9.1")
1548+
assert last_tag_name is None
1549+
1550+
# Test error when version not found
1551+
with pytest.raises(changelog.NoCommitsFoundError) as exc_info:
1552+
changelog.get_next_tag_name_after_version(tags, "nonexistent")
1553+
assert "Could not find a valid revision range" in str(exc_info.value)
1554+
1555+
15381556
@dataclass
15391557
class TagDef:
15401558
name: str

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy