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

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

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.23%. Comparing base (9707a58) to head (c05a03f).
⚠️ Report is 106 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1993   +/-   ##
=======================================
  Coverage   98.23%   98.23%           
=======================================
  Files          61       61           
  Lines        2779     2782    +3     
=======================================
+ Hits         2730     2733    +3     
  Misses         49       49           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

I can reproduce this with these commands:

commands
rm -rf commitizen-prek-repro commitizen-prek-repro-remote.git

mkdir commitizen-prek-repro
cd commitizen-prek-repro

uv init

uv add --dev commitizen prek

cat > .pre-commit-config.yaml <<'EOF'
default_install_hook_types:
  - pre-push
  - commit-msg

repos:
  - repo: https://github.com/commitizen-tools/commitizen
    rev: v4.16.5
    hooks:
      - id: commitizen
        stages: [commit-msg]
      - id: commitizen-branch
        stages: [pre-push]
EOF

git init

uv run prek install --hook-type pre-push --hook-type commit-msg

git add .
git commit -m "feat: initial commit"

git branch -M main

cd ..
git init --bare commitizen-prek-repro-remote.git

cd commitizen-prek-repro

git remote add origin ../commitizen-prek-repro-remote.git

git checkout -b test-commitizen-branch

git commit --allow-empty -m "feat: reproduce issue"

git push -u origin test-commitizen-branch

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

commitizen-branch fails with exit code: 23 and message fatal: ..: '..' is outside repository commitizen-branch - fatal: ambiguous argument '$PRE_COMMIT_FROM_REF..$PRE_COMMIT_TO_REF': unknown revision or path not in the working tree.

3 participants