Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

refactor(commit): simplify call #1426

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: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 28 additions & 36 deletions commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,41 +92,36 @@ def manual_edit(self, message: str) -> str:
os.unlink(file.name)
return message

def __call__(self):
extra_args: str = self.arguments.get("extra_cli_args", "")
def _get_message(self) -> str:
if self.arguments.get("retry"):
m = self.read_backup_message()
if m is None:
raise NoCommitBackupError()
return m

allow_empty: bool = "--allow-empty" in extra_args
if self.config.settings.get("retry_after_failure") and not self.arguments.get(
"no_retry"
):
return self.read_backup_message() or self.prompt_commit_questions()
return self.prompt_commit_questions()

def __call__(self):
extra_args: str = self.arguments.get("extra_cli_args", "")
dry_run: bool = self.arguments.get("dry_run")
write_message_to_file: bool = self.arguments.get("write_message_to_file")
manual_edit: bool = self.arguments.get("edit")
signoff: bool = self.arguments.get("signoff")

is_all: bool = self.arguments.get("all")
if is_all:
c = git.add("-u")
if self.arguments.get("all"):
git.add("-u")

if git.is_staging_clean() and not (dry_run or allow_empty):
if git.is_staging_clean() and not (dry_run or "--allow-empty" in extra_args):
raise NothingToCommitError("No files added to staging!")

if write_message_to_file is not None and write_message_to_file.is_dir():
raise NotAllowed(f"{write_message_to_file} is a directory")

retry: bool = self.arguments.get("retry")
no_retry: bool = self.arguments.get("no_retry")
retry_after_failure: bool = self.config.settings.get("retry_after_failure")

if retry:
m = self.read_backup_message()
if m is None:
raise NoCommitBackupError()
elif retry_after_failure and not no_retry:
m = self.read_backup_message()
if m is None:
m = self.prompt_commit_questions()
else:
m = self.prompt_commit_questions()

if manual_edit:
m = self._get_message()
if self.arguments.get("edit"):
m = self.manual_edit(m)

out.info(f"\n{m}\n")
Expand All @@ -138,19 +133,15 @@ def __call__(self):
if dry_run:
raise DryRunExit()

always_signoff: bool = self.config.settings["always_signoff"]
signoff: bool = self.arguments.get("signoff")

if signoff:
out.warn(
"signoff mechanic is deprecated, please use `cz commit -- -s` instead."
)

if always_signoff or signoff:
if self.config.settings["always_signoff"] or signoff:
extra_args = f"{extra_args} -s".strip()

c = git.commit(m, args=extra_args)

if c.return_code != 0:
out.error(c.err)

Expand All @@ -160,11 +151,12 @@ def __call__(self):

raise CommitError()

if "nothing added" in c.out or "no changes added to commit" in c.out:
if any(s in c.out for s in ("nothing added", "no changes added to commit")):
out.error(c.out)
else:
with contextlib.suppress(FileNotFoundError):
os.remove(self.temp_file)
out.write(c.err)
out.write(c.out)
out.success("Commit successful!")
return

with contextlib.suppress(FileNotFoundError):
os.remove(self.temp_file)
out.write(c.err)
out.write(c.out)
out.success("Commit successful!")
31 changes: 31 additions & 0 deletions tests/commands/test_commit_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,34 @@ def test_commit_command_shows_description_when_use_help_option(

out, _ = capsys.readouterr()
file_regression.check(out, extension=".txt")


@pytest.mark.usefixtures("staging_is_clean")
@pytest.mark.parametrize(
"out", ["no changes added to commit", "nothing added to commit"]
)
def test_commit_when_nothing_added_to_commit(config, mocker: MockFixture, out):
prompt_mock = mocker.patch("questionary.prompt")
prompt_mock.return_value = {
"prefix": "feat",
"subject": "user created",
"scope": "",
"is_breaking_change": False,
"body": "",
"footer": "",
}

commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command(
out=out,
err="",
stdout=out.encode(),
stderr=b"",
return_code=0,
)
error_mock = mocker.patch("commitizen.out.error")

commands.Commit(config, {})()

commit_mock.assert_called_once()
error_mock.assert_called_once_with(out)