Skip to content

fix: name the setting behind a regex that will not compile - #574

Merged
shenxianpeng merged 2 commits into
mainfrom
fix/name-the-setting-behind-a-bad-regex
Sep 11, 2026
Merged

shenxianpeng merged 2 commits into
mainfrom
fix/name-the-setting-behind-a-bad-regex

Conversation

@shenxianpeng

Copy link
Copy Markdown
Member

Closes #497.

Why now

#570 made exit code 2 mean "the policy is broken, nothing was judged" and wrote that contract into the README and --help. A regex the user supplied that does not compile is exactly that case, and it was not covered — it came back as exit code 1, the code a rejected commit gets:

$ commit-check --author-name --author-name-pattern='['
Error: unterminated character set at position 0
$ echo $?
1

Two things are wrong there. The message does not say which of the settings holds the pattern, and a CI wrapper (or --format json) reads a typo'd config as a rejected commit. The pattern only fails on its first re.match, deep inside a validator, where main()'s catch-all prints the bare text from re.

What changed

The four settings that really are compiled — [commit] message_pattern, [commit] author_name_pattern, [commit] author_email_pattern, [tag] regex — are compiled while the rule is built, and a failure is a ConfigError, which the CLI already returns as exit code 2:

$ commit-check --author-name --author-name-pattern='['
Error: [commit] author_name_pattern is not a valid regex: '[' (unterminated character set at position 0)
$ echo $?
2

Not blaming the wrong file

These four can arrive from the config file, a --flag, or a CCHK_* variable, and the merged dict no longer remembers which. main() prefixes a ConfigError with the config file it found — correct for warn, which has no flag or env form, but a guess here, and a wrong one for a value passed on the command line. ConfigError now carries the setting name for that case and main() leaves the file out when it is set. The warn error still names the file, unchanged.

Deliberately out of scope

prohibited_patterns is matched with fnmatchcase (a glob, not a regex) and require_rebase_target is stripped of anchors and resolved as a branch name with git rev-parse. Neither is ever compiled, so neither is checked.

allow_branch_types / allow_branch_names are not touched here either: on main they are re.escaped, so any value compiles. #573 restores them to patterns — once that lands, the same check applies to them and I am happy to add it as a follow-up commit to whichever of the two merges second.

Tests

+12, full suite 966 passed.

  • tests/rule_builder_test.py::TestInvalidUserRegexNamesTheSetting — each of the four settings raises ConfigError, naming the setting, echoing the pattern, and keeping re's own reason (so the position survives); valid patterns build; an empty [tag] regex still disables the match rather than compiling "".
  • tests/main_test.py::TestInvalidUserRegexIsAConfigError — exit code 2 end to end; a pattern from a flag does not name an unrelated config file that happens to exist; --tag-regex likewise.

README: the exit-code table now lists "a setting whose regex does not compile" under 2, with the new message shown.

README rev: pins stay at v2.17.0 — still the latest published release on PyPI, per AGENTS.md.

Closes #497.

message_pattern, author_name_pattern, author_email_pattern and the [tag]
regex hold a regular expression the user wrote. An unusable one was only
noticed on its first re.match, deep inside a validator, where main's
catch-all printed the bare text from re and returned exit code 1:

    $ commit-check --author-name --author-name-pattern='['
    Error: unterminated character set at position 0
    $ echo $?
    1

Nothing said which of the four settings held it, and exit code 1 is the
code a rejected commit gets -- so #570, which had just made exit code 2
mean "the policy is broken, nothing was judged", had a hole in it: a CI
wrapper reads a typo'd pattern as a rejected commit.

Compile each of the four while the rule is built and report a failure as
a ConfigError, which the CLI already returns as exit code 2:

    Error: [commit] author_name_pattern is not a valid regex: '[' (unterminated character set at position 0)

These four can come from the file, a --flag or a CCHK_* variable, and the
merged config no longer remembers which. main() prefixes a ConfigError
with the config file it found, which for these would be a guess -- and a
wrong one for a value passed on the command line. ConfigError carries the
setting name for that case, and main() leaves the file out when it is
set; the warn list, which has no flag or env form, still names the file.

A valid pattern is unaffected, and an empty [tag] regex still means "do
not match tags" rather than "compile an empty regex".

prohibited_patterns and require_rebase_target are left alone: the first
is matched with fnmatchcase, the second is stripped of anchors and
resolved as a branch name. Neither is ever compiled.
@shenxianpeng
shenxianpeng requested a review from a team as a code owner September 11, 2026 06:53
@github-actions github-actions Bot added the bug Something isn't working label Sep 11, 2026
@coderabbitai

coderabbitai Bot commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

Next included review available in 38 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: c9b611ff-9ce3-4a1f-aaaa-94424b0fdd08

📥 Commits

Reviewing files that changed from the base of the PR and between c3f502a and 58dae56.

📒 Files selected for processing (6)
  • README.md
  • commit_check/config.py
  • commit_check/main.py
  • commit_check/rule_builder.py
  • tests/main_test.py
  • tests/rule_builder_test.py

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.04%. Comparing base (c3f502a) to head (58dae56).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #574   +/-   ##
=======================================
  Coverage   99.03%   99.04%           
=======================================
  Files          13       13           
  Lines        1866     1881   +15     
=======================================
+ Hits         1848     1863   +15     
  Misses         18       18           

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@shenxianpeng

Copy link
Copy Markdown
Member Author

Heads-up: one expected conflict with the sibling PR. #573 and #574 both touch the module-level helpers at the top of rule_builder.py — #573 deletes _escaped_alternation (and with it the lru_cache import), #574 adds _checked_regex just above it. Whichever lands second gets a conflict there, and the resolution is mechanical: keep _checked_regex, keep import re, drop _escaped_alternation and the functools import. I verified that resolution locally — the merged tree is 967 passed, create-pull-request/.+ passes CC201, and a bad message_pattern exits 2.

#573 removed _escaped_alternation, whose lru_cache import and re import
went with it; this branch had added _checked_regex directly above it.
Keep _checked_regex, drop _escaped_alternation as #573 intended, and put
back the re import that _checked_regex needs.
@sonarqubecloud

Copy link
Copy Markdown

@codspeed

codspeed Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 595 untouched benchmarks
⏩ 122 skipped benchmarks1


Comparing fix/name-the-setting-behind-a-bad-regex (58dae56) with main (c3f502a)

Open in CodSpeed

Footnotes

  1. 122 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@shenxianpeng
shenxianpeng merged commit 41e5a6d into main Sep 11, 2026
29 checks passed
@shenxianpeng
shenxianpeng deleted the fix/name-the-setting-behind-a-bad-regex branch September 11, 2026 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Give a clear error when a user-supplied regex pattern is invalid

1 participant