Skip to content

Commit 836b32a

Browse files
committed
fix(check): support first pushes in the 'commitizen-branch' hook
Details: When a push includes the root commit (first push to an empty remote, or a new unrelated history), pre-commit does not set 'PRE_COMMIT_FROM_REF' and 'PRE_COMMIT_TO_REF'. The 'commitizen-branch' hook then fails with: > fatal: ambiguous argument '$PRE_COMMIT_FROM_REF..$PRE_COMMIT_TO_REF' In this case, check all commits of 'PRE_COMMIT_LOCAL_BRANCH', since they are all new to the remote. --- Signed-off-by: Adrian DC <radian.dc@gmail.com>
1 parent 16a1b9e commit 836b32a

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎commitizen/commands/check.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N
4242
self.commit_msg_file = arguments.get("commit_msg_file")
4343
self.commit_msg = arguments.get("message")
4444
rev_range = arguments.get("rev_range")
45+
if (
46+
rev_range == "$PRE_COMMIT_FROM_REF..$PRE_COMMIT_TO_REF"
47+
and "PRE_COMMIT_FROM_REF" not in os.environ
48+
and "PRE_COMMIT_LOCAL_BRANCH" in os.environ
49+
):
50+
# pre-commit omits the refs when the push includes the root commit
51+
rev_range = "$PRE_COMMIT_LOCAL_BRANCH"
4552
# Expand env vars so the packaged ``commitizen-branch`` pre-push hook
4653
# (which passes the literal ``$PRE_COMMIT_FROM_REF..$PRE_COMMIT_TO_REF``)
4754
# keeps working after the ``shell=False`` switch in #1941. See #2003.

‎tests/commands/test_check_command.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ def test_check_rev_range_leaves_unset_env_vars_literal(
204204
rewritten to an empty range."""
205205
monkeypatch.delenv("PRE_COMMIT_FROM_REF", raising=False)
206206
monkeypatch.delenv("PRE_COMMIT_TO_REF", raising=False)
207+
monkeypatch.delenv("PRE_COMMIT_LOCAL_BRANCH", raising=False)
207208
get_commits = mocker.patch(
208209
"commitizen.git.get_commits",
209210
return_value=_build_fake_git_commits(COMMIT_LOG),
@@ -219,6 +220,26 @@ def test_check_rev_range_leaves_unset_env_vars_literal(
219220
)
220221

221222

223+
def test_check_rev_range_falls_back_to_pushed_branch_on_root_push(
224+
config, mocker: MockFixture, monkeypatch: pytest.MonkeyPatch
225+
):
226+
"""pre-commit omits the from/to refs when a push includes the root commit."""
227+
monkeypatch.delenv("PRE_COMMIT_FROM_REF", raising=False)
228+
monkeypatch.delenv("PRE_COMMIT_TO_REF", raising=False)
229+
monkeypatch.setenv("PRE_COMMIT_LOCAL_BRANCH", "refs/heads/main")
230+
get_commits = mocker.patch(
231+
"commitizen.git.get_commits",
232+
return_value=_build_fake_git_commits(COMMIT_LOG),
233+
)
234+
235+
commands.Check(
236+
config=config,
237+
arguments={"rev_range": "$PRE_COMMIT_FROM_REF..$PRE_COMMIT_TO_REF"},
238+
)()
239+
240+
get_commits.assert_called_once_with(None, "refs/heads/main")
241+
242+
222243
def test_check_command_with_invalid_argument(config):
223244
with pytest.raises(
224245
InvalidCommandArgumentError,

0 commit comments

Comments
 (0)