fix(check): expand env vars in rev_range arg by ilitygergo · Pull Request #1993 · commitizen-tools/commitizen · GitHub
Skip to content

fix(check): expand env vars in rev_range arg - #1993

Open
ilitygergo wants to merge 1 commit into
commitizen-tools:masterfrom
ilitygergo:fix/1923-read-from-env-vars
Open

fix(check): expand env vars in rev_range arg#1993
ilitygergo wants to merge 1 commit into
commitizen-tools:masterfrom
ilitygergo:fix/1923-read-from-env-vars

Conversation

@ilitygergo

Copy link
Copy Markdown

Description

Checklist

Was generative AI tooling used to co-author this PR?

  • Yes (please specify the tool below)

Code Changes

  • Add test cases to all the changes you introduce
  • Run uv run poe all locally to ensure this change passes linter check and tests
  • Manually test the changes:
    • Verify the feature/bug fix works as expected in real-world scenarios
    • Test edge cases and error conditions
    • Ensure backward compatibility is maintained
    • Document any manual testing steps performed
  • Update the documentation for the changes

Documentation Changes

  • Run uv run poe doc locally to ensure the documentation pages renders correctly
  • Check and fix any broken links (internal or external)

Expected Behavior

The commitizen-branch pre-push hook should successfully validate commit messages
using the ref range provided by pre-commit via the PRE_COMMIT_FROM_REF and
PRE_COMMIT_TO_REF environment variables.

Steps to Test This Pull Request

  1. Add the following to your .pre-commit-config.yaml:
   repos:
     - repo: https://github.com/commitizen-tools/commitizen
       rev: master
       hooks:
         - id: commitizen
           stages: [commit-msg]
         - id: commitizen-branch
           stages: [pre-push]
  1. Install the pre-commit hooks:
    pre-commit install --hook-type pre-commit --hook-type commit-msg --hook-type pre-push
  2. Make a commit with a valid conventional commit message:
    git commit -m "feat: my new feature"
  3. Run git push — the commitizen-branch hook should pass instead of failing with:
    fatal: ambiguous argument '$PRE_COMMIT_FROM_REF..$PRE_COMMIT_TO_REF': unknown revision or path not in the working tree

Environment used for testing:

  • Python 3.14.3
  • commitizen 4.15.1
  • pre-commit 4.6.0

Additional Context

The root cause is that pre-commit passes args directly to the process as a list
without shell interpolation, so $PRE_COMMIT_FROM_REF..$PRE_COMMIT_TO_REF arrives
as a literal string to cz check rather than being expanded to actual git SHAs.
Pre-commit does set these as environment variables in the hook process, so
os.path.expandvars() correctly resolves them at runtime.

Closes #1923
Closes #1912
Related to #704

@codecov

codecov Bot commented May 14, 2026

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates cz check --rev-range so pre-commit hook arguments containing environment variables can be resolved before Git commit validation runs.

Changes:

  • Expands environment variables in the rev_range argument.
  • Adds parameterized tests covering literal and $VAR/${VAR} rev-range forms.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
commitizen/commands/check.py Expands rev_range values before validation.
tests/commands/test_check_command.py Adds tests for env-var expansion in rev ranges.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

self.commit_msg = arguments.get("message")
self.rev_range = arguments.get("rev_range")
if self.rev_range is not None:
self.rev_range = os.path.expandvars(self.rev_range)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest limiting the expansion to the two pre-commit ref variables to reduce the attack surface from arbitrary environment variable expansion in branch names, as discussed in the issues around the rev-range argument.

if self.rev_range is not None:
    for var in ("PRE_COMMIT_FROM_REF", "PRE_COMMIT_TO_REF"):
        value = os.environ.get(var)
        if value is None:
            continue

        self.rev_range = self.rev_range.replace(f"${{{var}}}", value)
        self.rev_range = self.rev_range.replace(f"${var}", value)
        self.rev_range = self.rev_range.replace(f"%{var}%", value)

Comment on lines +45 to +46
if self.rev_range is not None:
self.rev_range = os.path.expandvars(self.rev_range)
@mcsitter

mcsitter commented Jul 28, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

3 participants