Skip to content

Commit e76ea66

Browse files
committed
Updating to support adding messages to clarify what failed
1 parent 9fc2c99 commit e76ea66

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

commit_msg_regex_hook/commit_msg_regex_hook.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,32 @@ def main():
4444
Parse passed in regex and verify message matches
4545
"""
4646
parser = argparse.ArgumentParser()
47-
parser.add_argument("message", nargs="?", type=process_file, default=COMMIT_EDITMSG,
48-
help="File path for commit message")
49-
parser.add_argument("--pattern", type=process_pattern)
5047
parser.add_argument(
51-
'--debug',
52-
action='store_true',
53-
help='print debug messages to stdout'
54-
)
48+
"message",
49+
nargs="?",
50+
type=process_file,
51+
default=COMMIT_EDITMSG,
52+
help="File path for commit message")
53+
54+
55+
parser.add_argument(
56+
"--failure_message",
57+
type=str,
58+
default="Commit Message does not match pattern",
59+
help="The message to display if the commit message doesn't match the Regex")
60+
61+
62+
parser.add_argument(
63+
"--pattern",
64+
type=process_pattern,
65+
help="Pattern to check the commit message against")
66+
67+
68+
parser.add_argument(
69+
"--debug",
70+
action="store_true",
71+
help="Flag to get debugging messages")
72+
5573

5674
args = parser.parse_args()
5775

@@ -60,7 +78,7 @@ def main():
6078

6179
checks = [
6280
message_not_empty(args.message),
63-
message_pattern_match(args.message, args.pattern)
81+
message_pattern_match(args.message, args.pattern, args.failure_message)
6482
]
6583

6684
run_checks(checks)
@@ -130,15 +148,17 @@ def check():
130148
return check
131149

132150

133-
def message_pattern_match(message: str, pattern: Pattern) -> Result:
151+
def message_pattern_match(message: str, pattern: Pattern, failure_message: str) -> Result:
134152
"""Verify the commit message matches the pattern
135153
"""
136154
def check():
137155
logger.debug("Pattern: {regex}\nMessage: {message}", regex=pattern, message=message)
138156

139157
if not pattern.match(message):
140158
# Fail the commit message
141-
return Result(f"Commit Message does not match pattern\n\t{pattern}\n\t{message}", FAIL)
159+
return Result(f"""{failure_message}\n\t
160+
Pattern: {pattern}\n\t
161+
Message: {message}""", FAIL)
142162

143163
return Result("The commit message matches the regex", PASS)
144164
return check

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = commit-msg-regex-hook
3-
version = v0.0.8
3+
version = v0.2.0
44
author = David Tippett
55
description = Checks if commit message matches supplied regex
66
long_description = file: README.md

test/test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ class TestMessagePatternMatch(unittest.TestCase):
2020
def test_pattern_not_match(self):
2121
pattern = re.compile(r'[A-Z]{3,4}-[0-9]{3,6} \| [\w\s]* \| .+')
2222
test_str = "asefagrragadsrgasr"
23-
result = commit_msg_regex_hook.message_pattern_match(test_str, pattern)()
23+
result = commit_msg_regex_hook.message_pattern_match(test_str, pattern, "Testing")()
2424
self.assertEqual(result.is_passing(), False)
2525

2626
def test_pattern_match(self):
2727
pattern = re.compile(r'[A-Z]{3,4}-[0-9]{3,6} \| [\w\s]* \| .+')
2828
test_str = "ABC-123 | David | Commit message!"
29-
result = commit_msg_regex_hook.message_pattern_match(test_str, pattern)()
29+
result = commit_msg_regex_hook.message_pattern_match(test_str, pattern, "Testing")()
3030
self.assertEqual(result.is_passing(), True)

0 commit comments

Comments
 (0)