Skip to content
Advertisement

The regx in pygrep always returns True

I’m using the pygrep in pre-commit and here is my settings:

  - repo: local
    hooks:
      - id: jira-ticket
        name: check for jira ticket
        entry: '(ERP-[0-9]+ #(feat|fix|docs|style|refactor|perf|test|chore) .+)'
        args: [--multiline]
        language: pygrep
        stages: [commit-msg]

I want to check the commit message format but no mater what I entered, it always returns true.

$ git commit -m "why"
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to C:Usersoliver.chiu.cachepre-commitpatch1657183664-20160.
[INFO] Restored changes from C:Usersoliver.chiu.cachepre-commitpatch1657183664-20160.
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to C:Usersoliver.chiu.cachepre-commitpatch1657183666-11436.
check for jira ticket....................................................Passed
[INFO] Restored changes from C:Usersoliver.chiu.cachepre-commitpatch1657183666-11436.
[purchase_merge_ui dc8bafc9] why
 1 file changed, 1 deletion(-)

Advertisement

Answer

you also commented on github! pick one in the future not both!

your regex doesn’t follow the pattern I outlined in that issue you commented on — here is the example I used:

repos:
-   repo: local
    hooks:
    -   id: jira-ticket
        name: check for jira ticket
        language: pygrep
        entry: 'A(?![A-Z]+-[0-9]+)'
        args: [--multiline]
        stages: [commit-msg]

note that we’re using a negative lookahead — by default pygrep is used to find problems with things and you’re trying to do the opposite

right now your current pattern will only fail on correctly named things rather than only allow correctly named things

adapting your pattern to the one I outlined in the issue you’d want something like:

            entry: 'A(?!ERP-[0-9]+ #(feat|fix|docs|style|refactor|perf|test|chore) .+)'

disclaimer: I created pre-commit

Advertisement