diff --git a/commitizen/cli.py b/commitizen/cli.py index df082467f..1e0b1c627 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -168,6 +168,12 @@ def __call__( "default": 0, "help": "length limit of the commit message; 0 for no limit", }, + { + "name": ["--"], + "action": "store_true", + "dest": "double_dash", + "help": "Positional arguments separator (recommended)", + }, ], }, { diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index c955d02a5..9b13a020b 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -134,17 +134,18 @@ def __call__(self): if dry_run: raise DryRunExit() - signoff: bool = ( - self.arguments.get("signoff") or self.config.settings["always_signoff"] - ) + 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." ) - extra_args = self.arguments.get("extra_cli_args", "--") + " -s" - else: - extra_args = self.arguments.get("extra_cli_args", "") + + if always_signoff or signoff: + extra_args = f"{extra_args} -s".strip() c = git.commit(m, args=extra_args) diff --git a/docs/getting_started.md b/docs/getting_started.md index 81da513e0..378b81919 100644 --- a/docs/getting_started.md +++ b/docs/getting_started.md @@ -51,13 +51,13 @@ cz c Run in the terminal ```bash -cz commit --signoff +cz commit -- --signoff ``` or the shortcut ```bash -cz commit -s +cz commit -- -s ``` ### Get project version diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index 91a75e097..85959abe3 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -260,7 +260,7 @@ def test_commit_command_with_signoff_option(config, mocker: MockFixture): commands.Commit(config, {"signoff": True})() - commit_mock.assert_called_once_with(ANY, args="-- -s") + commit_mock.assert_called_once_with(ANY, args="-s") success_mock.assert_called_once() @@ -283,7 +283,32 @@ def test_commit_command_with_always_signoff_enabled(config, mocker: MockFixture) config.settings["always_signoff"] = True commands.Commit(config, {})() - commit_mock.assert_called_once_with(ANY, args="-- -s") + commit_mock.assert_called_once_with(ANY, args="-s") + success_mock.assert_called_once() + + +@pytest.mark.usefixtures("staging_is_clean") +def test_commit_command_with_gpgsign_and_always_signoff_enabled( + 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") + + config.settings["always_signoff"] = True + commands.Commit(config, {"extra_cli_args": "-S"})() + + commit_mock.assert_called_once_with(ANY, args="-S -s") success_mock.assert_called_once() diff --git a/tests/commands/test_commit_command/test_commit_command_shows_description_when_use_help_option.txt b/tests/commands/test_commit_command/test_commit_command_shows_description_when_use_help_option.txt index 955b3d8fd..dd1f53f3d 100644 --- a/tests/commands/test_commit_command/test_commit_command_shows_description_when_use_help_option.txt +++ b/tests/commands/test_commit_command/test_commit_command_shows_description_when_use_help_option.txt @@ -1,6 +1,6 @@ usage: cz commit [-h] [--retry] [--no-retry] [--dry-run] [--write-message-to-file FILE_PATH] [-s] [-a] [-e] - [-l MESSAGE_LENGTH_LIMIT] + [-l MESSAGE_LENGTH_LIMIT] [--] create new commit @@ -19,3 +19,4 @@ options: -e, --edit edit the commit message before committing -l, --message-length-limit MESSAGE_LENGTH_LIMIT length limit of the commit message; 0 for no limit + -- Positional arguments separator (recommended) diff --git a/tests/conftest.py b/tests/conftest.py index cc306ac6d..95f3df3b2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -35,12 +35,19 @@ def git_sandbox(monkeypatch: pytest.MonkeyPatch, tmp_path: Path): gitconfig = tmp_path / ".git" / "config" if not gitconfig.parent.exists(): gitconfig.parent.mkdir() + monkeypatch.setenv("GIT_CONFIG_GLOBAL", str(gitconfig)) + r = cmd.run(f"git config --file {gitconfig} user.name {SIGNER}") assert r.return_code == 0, r.err r = cmd.run(f"git config --file {gitconfig} user.email {SIGNER_MAIL}") assert r.return_code == 0, r.err - cmd.run("git config --global init.defaultBranch master") + + r = cmd.run(f"git config --file {gitconfig} safe.directory '*'") + assert r.return_code == 0, r.err + + r = cmd.run("git config --global init.defaultBranch master") + assert r.return_code == 0, r.err @pytest.fixture