Skip to content

feat(commit): add '--allow-empty' flag to commit command #1217

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

Merged
merged 1 commit into from
Dec 6, 2024
Merged
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
8 changes: 5 additions & 3 deletions commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ def manual_edit(self, message: str) -> str:
return message

def __call__(self):
extra_args: str = self.arguments.get("extra_cli_args", "")

allow_empty: bool = "--allow-empty" in extra_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")
Expand All @@ -101,7 +105,7 @@ def __call__(self):
if is_all:
c = git.add("-u")

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

if write_message_to_file is not None and write_message_to_file.is_dir():
Expand Down Expand Up @@ -137,8 +141,6 @@ def __call__(self):
always_signoff: bool = self.config.settings["always_signoff"]
signoff: bool = self.arguments.get("signoff")

extra_args = self.arguments.get("extra_cli_args", "")

if signoff:
out.warn(
"signoff mechanic is deprecated, please use `cz commit -- -s` instead."
Expand Down
49 changes: 49 additions & 0 deletions tests/commands/test_commit_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,55 @@ def test_commit_when_nothing_to_commit(config, mocker: MockFixture):
assert "No files added to staging!" in str(excinfo.value)


@pytest.mark.usefixtures("staging_is_clean")
def test_commit_with_allow_empty(config, mocker: MockFixture):
prompt_mock = mocker.patch("questionary.prompt")
prompt_mock.return_value = {
"prefix": "feat",
"subject": "user created",
"scope": "",
"is_breaking_change": False,
"body": "closes #21",
"footer": "",
}

commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command("success", "", b"", b"", 0)
success_mock = mocker.patch("commitizen.out.success")

commands.Commit(config, {"extra_cli_args": "--allow-empty"})()

commit_mock.assert_called_with(
"feat: user created\n\ncloses #21", args="--allow-empty"
)
success_mock.assert_called_once()


@pytest.mark.usefixtures("staging_is_clean")
def test_commit_with_signoff_and_allow_empty(config, mocker: MockFixture):
prompt_mock = mocker.patch("questionary.prompt")
prompt_mock.return_value = {
"prefix": "feat",
"subject": "user created",
"scope": "",
"is_breaking_change": False,
"body": "closes #21",
"footer": "",
}

commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command("success", "", b"", b"", 0)
success_mock = mocker.patch("commitizen.out.success")

config.settings["always_signoff"] = True
commands.Commit(config, {"extra_cli_args": "--allow-empty"})()

commit_mock.assert_called_with(
"feat: user created\n\ncloses #21", args="--allow-empty -s"
)
success_mock.assert_called_once()


@pytest.mark.usefixtures("staging_is_clean")
def test_commit_when_customized_expected_raised(config, mocker: MockFixture, capsys):
_err = ValueError()
Expand Down
Loading