diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index d57006f15..21e44c156 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -4,8 +4,8 @@ Check whether the current commit message follows committing rules. Allow empty commit messages by default, because they typically indicate to Git that the commit should be aborted. - entry: cz check - args: [--allow-abort, --commit-msg-file] + entry: cz + args: [check, --allow-abort, --commit-msg-file] stages: [commit-msg] language: python language_version: python3 @@ -18,8 +18,8 @@ default branch on the origin repository. Useful for checking messages after the fact (e.g., pre-push or in CI) without an expensive check of the entire repository history. - entry: cz check - args: [--rev-range, "$PRE_COMMIT_FROM_REF..$PRE_COMMIT_TO_REF"] + entry: cz + args: [--no-raise, "3", check, --rev-range, "$PRE_COMMIT_FROM_REF..$PRE_COMMIT_TO_REF"] always_run: true pass_filenames: false language: python diff --git a/commitizen/commands/check.py b/commitizen/commands/check.py index fbf707341..86ff0c282 100644 --- a/commitizen/commands/check.py +++ b/commitizen/commands/check.py @@ -42,6 +42,13 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N self.commit_msg_file = arguments.get("commit_msg_file") self.commit_msg = arguments.get("message") rev_range = arguments.get("rev_range") + if ( + rev_range == "$PRE_COMMIT_FROM_REF..$PRE_COMMIT_TO_REF" + and "PRE_COMMIT_FROM_REF" not in os.environ + and "PRE_COMMIT_LOCAL_BRANCH" in os.environ + ): + # pre-commit omits the refs when the push includes the root commit + rev_range = "$PRE_COMMIT_LOCAL_BRANCH" # Expand env vars so the packaged ``commitizen-branch`` pre-push hook # (which passes the literal ``$PRE_COMMIT_FROM_REF..$PRE_COMMIT_TO_REF``) # keeps working after the ``shell=False`` switch in #1941. See #2003. diff --git a/tests/commands/test_check_command.py b/tests/commands/test_check_command.py index 227fa5fa5..4de961a1d 100644 --- a/tests/commands/test_check_command.py +++ b/tests/commands/test_check_command.py @@ -204,6 +204,7 @@ def test_check_rev_range_leaves_unset_env_vars_literal( rewritten to an empty range.""" monkeypatch.delenv("PRE_COMMIT_FROM_REF", raising=False) monkeypatch.delenv("PRE_COMMIT_TO_REF", raising=False) + monkeypatch.delenv("PRE_COMMIT_LOCAL_BRANCH", raising=False) get_commits = mocker.patch( "commitizen.git.get_commits", return_value=_build_fake_git_commits(COMMIT_LOG), @@ -219,6 +220,26 @@ def test_check_rev_range_leaves_unset_env_vars_literal( ) +def test_check_rev_range_falls_back_to_pushed_branch_on_root_push( + config, mocker: MockFixture, monkeypatch: pytest.MonkeyPatch +): + """pre-commit omits the from/to refs when a push includes the root commit.""" + monkeypatch.delenv("PRE_COMMIT_FROM_REF", raising=False) + monkeypatch.delenv("PRE_COMMIT_TO_REF", raising=False) + monkeypatch.setenv("PRE_COMMIT_LOCAL_BRANCH", "refs/heads/main") + get_commits = mocker.patch( + "commitizen.git.get_commits", + return_value=_build_fake_git_commits(COMMIT_LOG), + ) + + commands.Check( + config=config, + arguments={"rev_range": "$PRE_COMMIT_FROM_REF..$PRE_COMMIT_TO_REF"}, + )() + + get_commits.assert_called_once_with(None, "refs/heads/main") + + def test_check_command_with_invalid_argument(config): with pytest.raises( InvalidCommandArgumentError,