Skip to content

refactor(changelog): simplify logic for get_oldest_and_newest_rev #1539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: v4-9-0-test
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor(changelog): simplify logic for get_oldest_and_newest_rev
  • Loading branch information
bearomorphism committed Jun 16, 2025
commit 4ec52130c929f39a4d90bfdc76bd6c28a44ad8ee
45 changes: 17 additions & 28 deletions commitizen/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,39 +318,28 @@ def get_oldest_and_newest_rev(
- `0.1.0..0.4.0`: as a range
- `0.3.0`: as a single version
"""
oldest: str | None = None
newest: str | None = None
try:
oldest, newest = version.split("..")
except ValueError:
newest = version
if not (newest_tag := rules.find_tag_for(tags, newest)):
oldest_version, sep, newest_version = version.partition("..")
if not sep:
newest_version = version
oldest_version = ""

def get_tag_name(v: str) -> str:
if tag := rules.find_tag_for(tags, v):
return tag.name
raise NoCommitsFoundError("Could not find a valid revision range.")

oldest_tag = None
oldest_tag_name = None
if oldest:
if not (oldest_tag := rules.find_tag_for(tags, oldest)):
raise NoCommitsFoundError("Could not find a valid revision range.")
oldest_tag_name = oldest_tag.name
newest_tag_name = get_tag_name(newest_version)
oldest_tag_name = get_tag_name(oldest_version) if oldest_version else None

tags_range = get_smart_tag_range(
tags, newest=newest_tag.name, oldest=oldest_tag_name
)
tags_range = get_smart_tag_range(tags, newest_tag_name, oldest_tag_name)
if not tags_range:
raise NoCommitsFoundError("Could not find a valid revision range.")

oldest_rev: str | None = tags_range[-1].name
newest_rev = newest_tag.name

# check if it's the first tag created
# and it's also being requested as part of the range
if oldest_rev == tags[-1].name and oldest_rev == oldest_tag_name:
return None, newest_rev

# when they are the same, and it's also the
# first tag created
if oldest_rev == newest_rev:
return None, newest_rev

return oldest_rev, newest_rev
# Return None for oldest_rev if:
# 1. The oldest tag is the last tag in the list and matches the requested oldest tag
# 2. The oldest and the newest tag are the same
if oldest_rev == oldest_tag_name == tags[-1].name or oldest_rev == newest_tag_name:
oldest_rev = None
return oldest_rev, newest_tag_name