diff --git a/commitizen/cli.py b/commitizen/cli.py index 472ef2b270..7f4d4893c0 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -75,6 +75,11 @@ "action": "store_true", "help": "sign off the commit", }, + { + "name": ["-a", "--all"], + "action": "store_true", + "help": "Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.", + }, ], }, { diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index 913526f893..39cab33b5c 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -67,6 +67,10 @@ def __call__(self): dry_run: bool = self.arguments.get("dry_run") write_message_to_file = self.arguments.get("write_message_to_file") + is_all: bool = self.arguments.get("all") + if is_all: + c = git.add("-u") + if git.is_staging_clean() and not dry_run: raise NothingToCommitError("No files added to staging!") diff --git a/commitizen/git.py b/commitizen/git.py index adfe494383..67652b9516 100644 --- a/commitizen/git.py +++ b/commitizen/git.py @@ -92,6 +92,11 @@ def tag(tag: str, annotated: bool = False, signed: bool = False) -> cmd.Command: return c +def add(args: str = "") -> cmd.Command: + c = cmd.run(f"git add {args}") + return c + + def commit( message: str, args: str = "", committer_date: str | None = None ) -> cmd.Command: diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index 29840bb6a1..b48ac9d0ed 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -255,3 +255,24 @@ def test_commit_in_non_git_project(tmpdir, config): with tmpdir.as_cwd(): with pytest.raises(NotAGitProjectError): commands.Commit(config, {}) + + +@pytest.mark.usefixtures("staging_is_clean") +def test_commit_command_with_all_option(config, mocker: MockFixture): + 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("success", "", b"", b"", 0) + success_mock = mocker.patch("commitizen.out.success") + add_mock = mocker.patch("commitizen.git.add") + commands.Commit(config, {"all": True})() + add_mock.assert_called() + success_mock.assert_called_once()