Skip to content

refactor(Init): remove unnecessary methods from ProjectInfo and refactor _ask_tag, improve test coverage #1526

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(Init): remove unnecessary methods from ProjectInfo and refac…
…tor _ask_tag
  • Loading branch information
bearomorphism committed Jun 14, 2025
commit e1376d369ad0e08128e87b7c8a364186d19bf443
47 changes: 19 additions & 28 deletions commitizen/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,6 @@ def is_npm_package(self) -> bool:
def is_php_composer(self) -> bool:
return os.path.isfile("composer.json")

@property
def latest_tag(self) -> str | None:
return get_latest_tag_name()

def tags(self) -> list | None:
"""Not a property, only use if necessary"""
if self.latest_tag is None:
return None
return get_tag_names()

@property
def is_pre_commit_installed(self) -> bool:
return bool(shutil.which("pre-commit"))
Expand Down Expand Up @@ -181,31 +171,32 @@ def _ask_name(self) -> str:
return name

def _ask_tag(self) -> str:
latest_tag = self.project_info.latest_tag
latest_tag = get_latest_tag_name()
if not latest_tag:
out.error("No Existing Tag. Set tag to v0.0.1")
return "0.0.1"

is_correct_tag = questionary.confirm(
if questionary.confirm(
f"Is {latest_tag} the latest tag?", style=self.cz.style, default=False
).unsafe_ask():
return latest_tag

existing_tags = get_tag_names()
if not existing_tags:
out.error("No Existing Tag. Set tag to v0.0.1")
return "0.0.1"

answer: str = questionary.select(
"Please choose the latest tag: ",
# The latest tag is most likely with the largest number.
# Thus, listing the existing_tags in reverse order makes more sense.
choices=sorted(existing_tags, reverse=True),
style=self.cz.style,
).unsafe_ask()
if not is_correct_tag:
tags = self.project_info.tags()
if not tags:
out.error("No Existing Tag. Set tag to v0.0.1")
return "0.0.1"

# the latest tag is most likely with the largest number. Thus list the tags in reverse order makes more sense
sorted_tags = sorted(tags, reverse=True)
latest_tag = questionary.select(
"Please choose the latest tag: ",
choices=sorted_tags,
style=self.cz.style,
).unsafe_ask()

if not latest_tag:
raise NoAnswersError("Tag is required!")
return latest_tag
if not answer:
raise NoAnswersError("Tag is required!")
return answer

def _ask_tag_format(self, latest_tag: str) -> str:
is_correct_format = False
Expand Down