-
-
Notifications
You must be signed in to change notification settings - Fork 289
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
bearomorphism
wants to merge
2
commits into
commitizen-tools:v4-9-0-test
Choose a base branch
from
bearomorphism:get_oldest_and_newest_rev
base: v4-9-0-test
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
from collections.abc import Generator, Iterable, Mapping, Sequence | ||
from dataclasses import dataclass | ||
from datetime import date | ||
from itertools import chain, tee | ||
from typing import TYPE_CHECKING, Any | ||
|
||
from jinja2 import ( | ||
|
@@ -281,6 +282,16 @@ def incremental_build( | |
return output_lines | ||
|
||
|
||
def get_next_tag_name_after_version(tags: Iterable[GitTag], version: str) -> str | None: | ||
a, b = tee(chain((tag.name for tag in tags), [None])) | ||
next(b, None) | ||
try: | ||
return next(y for x, y in zip(a, b) if x == version) | ||
except StopIteration: | ||
raise NoCommitsFoundError(f"Could not find a valid revision range. {version=}") | ||
|
||
|
||
# TODO: unused, deprecate this? | ||
def get_smart_tag_range( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if removing the function is a breaking change. |
||
tags: Sequence[GitTag], newest: str, oldest: str | None = None | ||
) -> list[GitTag]: | ||
|
@@ -308,7 +319,7 @@ def get_smart_tag_range( | |
|
||
|
||
def get_oldest_and_newest_rev( | ||
tags: Sequence[GitTag], | ||
tags: Iterable[GitTag], | ||
version: str, | ||
rules: TagRules, | ||
) -> tuple[str | None, str]: | ||
|
@@ -318,39 +329,26 @@ 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 | ||
oldest_rev = get_next_tag_name_after_version( | ||
tags, oldest_tag_name or newest_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 == newest_tag_name: | ||
return None, newest_tag_name | ||
return oldest_rev, newest_tag_name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @gbaian10 for reminding me here we don't have to use a while loop.