Skip to content

Commit 83102ff

Browse files
committed
Finished changes to fix multiline matching
1 parent e76ea66 commit 83102ff

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

.pre-commit-config.yaml

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
repos:
2-
- repo: git://github.com/pre-commit/pre-commit-hooks
3-
rev: v3.2.0
4-
hooks:
5-
- id: trailing-whitespace
6-
- id: end-of-file-fixer
7-
- id: check-yaml
8-
- id: check-added-large-files
2+
#- repo: git://github.com/pre-commit/pre-commit-hooks
3+
# rev: v3.2.0
4+
# hooks:
5+
# - id: trailing-whitespace
6+
# - id: end-of-file-fixer
7+
# - id: check-yaml
8+
# - id: check-added-large-files
9+
#
10+
#- repo: git://github.com/pycqa/pylint
11+
# rev: pylint-2.6.0
12+
# hooks:
13+
# - id: pylint
14+
# args: ["--fail-under=8"]
915

10-
- repo: git://github.com/pycqa/pylint
11-
rev: pylint-2.6.0
16+
- repo: local
1217
hooks:
13-
- id: pylint
14-
args: ["--fail-under=8"]
18+
- id: commit-msg-regex-hook
19+
name: commit-msg-regex-hook
20+
entry: commit-msg-regex-hook
21+
language: python
22+
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])+)\\])>'",
23+
"--debug"]
24+
#args: ["--pattern='[A-Z]{3,4}-[0-9]{3,6} \\| [\\w\\s]* \\| .+'",
25+
# "--debug"]
26+
stages: [commit-msg]
27+

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,28 @@ Finally you can add this to your .pre-commit-config.yaml:
1414

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

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

28+
With this you can achieve so many things. A good example is verifing deveopers have signed the DCO before allowing a commit.
29+
30+
```
31+
- repo: https://github.com/dtaivpp/commit-msg-regex-hook
32+
rev: v0.2.0
33+
hooks:
34+
- id: commit-msg-regex-hook
35+
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])+)\\])>'",
36+
"--failure_message='Ensure you are signing the DCO'"]
37+
stages: [commit-msg]
38+
```
2739

2840
### Developing this project
2941

commit_msg_regex_hook/commit_msg_regex_hook.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def run_checks(checks: list):
9292
result = check()
9393

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

9898
logger.debug(result.message)
@@ -139,7 +139,7 @@ def message_not_empty(message: str) -> Result:
139139
"""Verify the commit message is not empty
140140
"""
141141
def check():
142-
logger.debug("Current Message: {message}", message=message)
142+
logger.debug(f"Current Message: {message}")
143143

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

157-
if not pattern.match(message):
157+
if not pattern.search(message):
158158
# Fail the commit message
159159
return Result(f"""{failure_message}\n\t
160160
Pattern: {pattern}\n\t

0 commit comments

Comments
 (0)