Skip to content

Commit 336912b

Browse files
committed
refactor(changelog): code cleanup
1 parent 1ae2d12 commit 336912b

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

commitizen/changelog.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -302,27 +302,37 @@ def incremental_build(
302302
def get_smart_tag_range(
303303
tags: list[GitTag], newest: str, oldest: str | None = None
304304
) -> list[GitTag]:
305-
"""Smart because it finds the N+1 tag.
305+
"""Get a range of tags including the next tag after the oldest tag.
306306
307-
This is because we need to find until the next tag
307+
Args:
308+
tags: List of git tags
309+
newest: Name of the newest tag to include
310+
oldest: Name of the oldest tag to include. If None, same as newest.
311+
312+
Returns:
313+
List of tags from newest to oldest, plus one tag after oldest if it exists.
314+
For nonexistent end tag, returns all tags.
315+
For nonexistent start tag, returns tags starting from second tag.
316+
For nonexistent start and end tags, returns empty list.
308317
"""
309-
accumulator = []
310-
keep = False
311-
if not oldest:
312-
oldest = newest
313-
for index, tag in enumerate(tags):
314-
if tag.name == newest:
315-
keep = True
316-
if keep:
317-
accumulator.append(tag)
318-
if tag.name == oldest:
319-
keep = False
320-
try:
321-
accumulator.append(tags[index + 1])
322-
except IndexError:
323-
pass
324-
break
325-
return accumulator
318+
oldest = oldest or newest
319+
320+
names = set(tag.name for tag in tags)
321+
has_newest = newest in names
322+
has_oldest = oldest in names
323+
if not has_newest and not has_oldest:
324+
return []
325+
326+
if not has_newest:
327+
return tags[1:]
328+
329+
if not has_oldest:
330+
return tags
331+
332+
newest_idx = next(i for i, tag in enumerate(tags) if tag.name == newest)
333+
oldest_idx = next(i for i, tag in enumerate(tags) if tag.name == oldest)
334+
335+
return tags[newest_idx : oldest_idx + 2]
326336

327337

328338
def get_oldest_and_newest_rev(

0 commit comments

Comments
 (0)