Skip to content

Commit cdd15fd

Browse files
committed
refactor(changelog): code cleanup
1 parent 3d2c316 commit cdd15fd

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

commitizen/changelog.py

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

330340

331341
def get_oldest_and_newest_rev(

commitizen/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class GitObject:
4949
def __eq__(self, other: object) -> bool:
5050
return hasattr(other, "rev") and self.rev == other.rev
5151

52-
def __hash__(self):
52+
def __hash__(self) -> int:
5353
return hash(self.rev)
5454

5555

0 commit comments

Comments
 (0)