Skip to content

Commit

Permalink
feat(commit): allow '-- --allow-empty' to create empty commits
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian DC <[email protected]>
  • Loading branch information
AdrianDC authored and Lee-W committed Dec 6, 2024
1 parent 26152c5 commit 636a069
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
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

0 comments on commit 636a069

Please sign in to comment.