Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7316c2f

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 7316c2f

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

commitizen/changelog.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from collections.abc import Generator, Iterable, Mapping, Sequence
3333
from dataclasses import dataclass
3434
from datetime import date
35+
from itertools import chain, tee
3536
from typing import TYPE_CHECKING, Any
3637

3738
from jinja2 import (
@@ -281,6 +282,16 @@ def incremental_build(
281282
return output_lines
282283

283284

285+
def get_next_tag_name_after_version(tags: Iterable[GitTag], version: str) -> str | None:
286+
a, b = tee(chain((tag.name for tag in tags), [None]))
287+
next(b, None)
288+
try:
289+
return next(y for x, y in zip(a, b) if x == version)
290+
except StopIteration:
291+
raise NoCommitsFoundError(f"Could not find a valid revision range. {version=}")
292+
293+
294+
# TODO: unused, deprecate this?
284295
def get_smart_tag_range(
285296
tags: Sequence[GitTag], newest: str, oldest: str | None = None
286297
) -> list[GitTag]:
@@ -331,15 +342,13 @@ def get_tag_name(v: str) -> str:
331342
newest_tag_name = get_tag_name(newest_version)
332343
oldest_tag_name = get_tag_name(oldest_version) if oldest_version else None
333344

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
345+
oldest_rev = get_next_tag_name_after_version(
346+
tags, oldest_tag_name or newest_tag_name
347+
)
339348

340349
# Return None for oldest_rev if:
341350
# 1. The oldest tag is the last tag in the list and matches the requested oldest tag
342351
# 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
352+
if oldest_rev == newest_tag_name:
353+
return None, newest_tag_name
345354
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)