Skip to content

Commit

Permalink
Finished changes to fix multiline matching
Browse files Browse the repository at this point in the history
  • Loading branch information
dtaivpp committed Mar 30, 2022
1 parent e76ea66 commit 83102ff
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 19 deletions.
35 changes: 24 additions & 11 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
repos:
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
#- repo: git://github.com/pre-commit/pre-commit-hooks
# rev: v3.2.0
# hooks:
# - id: trailing-whitespace
# - id: end-of-file-fixer
# - id: check-yaml
# - id: check-added-large-files
#
#- repo: git://github.com/pycqa/pylint
# rev: pylint-2.6.0
# hooks:
# - id: pylint
# args: ["--fail-under=8"]

- repo: git://github.com/pycqa/pylint
rev: pylint-2.6.0
- repo: local
hooks:
- id: pylint
args: ["--fail-under=8"]
- id: commit-msg-regex-hook
name: commit-msg-regex-hook
entry: commit-msg-regex-hook
language: python
args: ["--pattern='Signed-off-by: .* <(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|'(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*')@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])>'",
"--debug"]
#args: ["--pattern='[A-Z]{3,4}-[0-9]{3,6} \\| [\\w\\s]* \\| .+'",
# "--debug"]
stages: [commit-msg]

20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,28 @@ Finally you can add this to your .pre-commit-config.yaml:

```
- repo: https://github.com/dtaivpp/commit-msg-regex-hook
rev: v0.1.0
rev: v0.2.0
hooks:
- id: commit-msg-hook
args: ["--pattern='[A-Z]{3,4}-[0-9]{3,6} \\| [\\w\\s]* \\| .+'"]
- id: commit-msg-regex-hook
args: ["--pattern='[A-Z]{3,4}-[0-9]{3,6} \\| [\\w\\s]* \\| .+'",
"--failure_message='Commits should match the pattern: Card-ID | Name | Message'"]
stages: [commit-msg]
```

**note: the backslashes in regex need to be escaped need to be escaped
**double note: if you are having issues you can run with the --debug argument as well for additional logging.
**double note: if you are having issues you can run with the --debug argument as well for additional logging.

With this you can achieve so many things. A good example is verifing deveopers have signed the DCO before allowing a commit.

```
- repo: https://github.com/dtaivpp/commit-msg-regex-hook
rev: v0.2.0
hooks:
- id: commit-msg-regex-hook
args: ["--pattern='Signed-off-by: .* <(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|'(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*')@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])>'",
"--failure_message='Ensure you are signing the DCO'"]
stages: [commit-msg]
```

### Developing this project

Expand Down
8 changes: 4 additions & 4 deletions commit_msg_regex_hook/commit_msg_regex_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def run_checks(checks: list):
result = check()

if not result.is_passing():
logger.error("Check Failed:\n {failure}", failure=result.message)
logger.error(f"Check Failed:\n {result.message}")
sys.exit(1)

logger.debug(result.message)
Expand Down Expand Up @@ -139,7 +139,7 @@ def message_not_empty(message: str) -> Result:
"""Verify the commit message is not empty
"""
def check():
logger.debug("Current Message: {message}", message=message)
logger.debug(f"Current Message: {message}")

if len(message.strip()) == 0:
return Result("ֿError: commit message cannot be empty", FAIL)
Expand All @@ -152,9 +152,9 @@ def message_pattern_match(message: str, pattern: Pattern, failure_message: str)
"""Verify the commit message matches the pattern
"""
def check():
logger.debug("Pattern: {regex}\nMessage: {message}", regex=pattern, message=message)
logger.debug(f"Pattern: {pattern}\nMessage: {message}")

if not pattern.match(message):
if not pattern.search(message):
# Fail the commit message
return Result(f"""{failure_message}\n\t
Pattern: {pattern}\n\t
Expand Down

0 comments on commit 83102ff

Please sign in to comment.