Skip to content

Commit 636a069

Browse files
AdrianDCLee-W
authored andcommitted
feat(commit): allow '-- --allow-empty' to create empty commits
Signed-off-by: Adrian DC <[email protected]>
1 parent 26152c5 commit 636a069

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

Diff for: commitizen/commands/commit.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ def manual_edit(self, message: str) -> str:
9393
return message
9494

9595
def __call__(self):
96+
extra_args: str = self.arguments.get("extra_cli_args", "")
97+
98+
allow_empty: bool = "--allow-empty" in extra_args
99+
96100
dry_run: bool = self.arguments.get("dry_run")
97101
write_message_to_file: bool = self.arguments.get("write_message_to_file")
98102
manual_edit: bool = self.arguments.get("edit")
@@ -101,7 +105,7 @@ def __call__(self):
101105
if is_all:
102106
c = git.add("-u")
103107

104-
if git.is_staging_clean() and not dry_run:
108+
if git.is_staging_clean() and not (dry_run or allow_empty):
105109
raise NothingToCommitError("No files added to staging!")
106110

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

140-
extra_args = self.arguments.get("extra_cli_args", "")
141-
142144
if signoff:
143145
out.warn(
144146
"signoff mechanic is deprecated, please use `cz commit -- -s` instead."

Diff for: tests/commands/test_commit_command.py

+49
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,55 @@ def test_commit_when_nothing_to_commit(config, mocker: MockFixture):
324324
assert "No files added to staging!" in str(excinfo.value)
325325

326326

327+
@pytest.mark.usefixtures("staging_is_clean")
328+
def test_commit_with_allow_empty(config, mocker: MockFixture):
329+
prompt_mock = mocker.patch("questionary.prompt")
330+
prompt_mock.return_value = {
331+
"prefix": "feat",
332+
"subject": "user created",
333+
"scope": "",
334+
"is_breaking_change": False,
335+
"body": "closes #21",
336+
"footer": "",
337+
}
338+
339+
commit_mock = mocker.patch("commitizen.git.commit")
340+
commit_mock.return_value = cmd.Command("success", "", b"", b"", 0)
341+
success_mock = mocker.patch("commitizen.out.success")
342+
343+
commands.Commit(config, {"extra_cli_args": "--allow-empty"})()
344+
345+
commit_mock.assert_called_with(
346+
"feat: user created\n\ncloses #21", args="--allow-empty"
347+
)
348+
success_mock.assert_called_once()
349+
350+
351+
@pytest.mark.usefixtures("staging_is_clean")
352+
def test_commit_with_signoff_and_allow_empty(config, mocker: MockFixture):
353+
prompt_mock = mocker.patch("questionary.prompt")
354+
prompt_mock.return_value = {
355+
"prefix": "feat",
356+
"subject": "user created",
357+
"scope": "",
358+
"is_breaking_change": False,
359+
"body": "closes #21",
360+
"footer": "",
361+
}
362+
363+
commit_mock = mocker.patch("commitizen.git.commit")
364+
commit_mock.return_value = cmd.Command("success", "", b"", b"", 0)
365+
success_mock = mocker.patch("commitizen.out.success")
366+
367+
config.settings["always_signoff"] = True
368+
commands.Commit(config, {"extra_cli_args": "--allow-empty"})()
369+
370+
commit_mock.assert_called_with(
371+
"feat: user created\n\ncloses #21", args="--allow-empty -s"
372+
)
373+
success_mock.assert_called_once()
374+
375+
327376
@pytest.mark.usefixtures("staging_is_clean")
328377
def test_commit_when_customized_expected_raised(config, mocker: MockFixture, capsys):
329378
_err = ValueError()

0 commit comments

Comments
 (0)