Skip to content

Commit 1299d8f

Browse files
committed
refactor(changelog): get_smart_tag_range and more test cases
1 parent 9bb9ed9 commit 1299d8f

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

commitizen/changelog.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -283,27 +283,37 @@ def incremental_build(
283283
def get_smart_tag_range(
284284
tags: list[GitTag], newest: str, oldest: str | None = None
285285
) -> list[GitTag]:
286-
"""Smart because it finds the N+1 tag.
286+
"""Get a range of tags including the next tag after the oldest tag.
287287
288-
This is because we need to find until the next tag
288+
Args:
289+
tags: List of git tags
290+
newest: Name of the newest tag to include
291+
oldest: Name of the oldest tag to include. If None, same as newest.
292+
293+
Returns:
294+
List of tags from newest to oldest, plus one tag after oldest if it exists.
295+
For nonexistent end tag, returns all tags.
296+
For nonexistent start tag, returns tags starting from second tag.
297+
For nonexistent start and end tags, returns empty list.
289298
"""
290-
accumulator = []
291-
keep = False
292-
if not oldest:
293-
oldest = newest
294-
for index, tag in enumerate(tags):
295-
if tag.name == newest:
296-
keep = True
297-
if keep:
298-
accumulator.append(tag)
299-
if tag.name == oldest:
300-
keep = False
301-
try:
302-
accumulator.append(tags[index + 1])
303-
except IndexError:
304-
pass
305-
break
306-
return accumulator
299+
oldest = oldest or newest
300+
301+
names = [tag.name for tag in tags]
302+
has_newest = newest in names
303+
has_oldest = oldest in names
304+
if not has_newest and not has_oldest:
305+
return []
306+
307+
if not has_newest:
308+
return tags[1:]
309+
310+
if not has_oldest:
311+
return tags
312+
313+
newest_idx = next(i for i, tag in enumerate(tags) if tag.name == newest)
314+
oldest_idx = next(i for i, tag in enumerate(tags) if tag.name == oldest)
315+
316+
return tags[newest_idx : oldest_idx + 2]
307317

308318

309319
def get_oldest_and_newest_rev(

tests/test_changelog.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,22 @@ def test_get_smart_tag_range_returns_an_extra_for_a_range(tags):
15311531
assert 4 == len(res)
15321532

15331533

1534+
def test_get_smart_tag_range_returns_all_tags_for_a_range(tags):
1535+
start = tags[0]
1536+
1537+
end = tags[-1]
1538+
res = changelog.get_smart_tag_range(tags, start.name, end.name)
1539+
assert len(tags) == len(res)
1540+
1541+
end = tags[-2]
1542+
res = changelog.get_smart_tag_range(tags, start.name, end.name)
1543+
assert len(tags) == len(res)
1544+
1545+
end = tags[-3]
1546+
res = changelog.get_smart_tag_range(tags, start.name, end.name)
1547+
assert len(tags) - 1 == len(res)
1548+
1549+
15341550
def test_get_smart_tag_range_returns_an_extra_for_a_single_tag(tags):
15351551
start = tags[0] # len here is 1, but we expect one more tag as designed
15361552
res = changelog.get_smart_tag_range(tags, start.name)

0 commit comments

Comments
 (0)