Skip to content

Commit 9cdecd4

Browse files
committed
feat: refactor print error message
1 parent fd7deb8 commit 9cdecd4

4 files changed

Lines changed: 35 additions & 11 deletions

File tree

‎commit_check/author.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Check git author name and email"""
22
import re
33
from commit_check import YELLOW, RESET_COLOR, PASS, FAIL
4-
from commit_check.util import get_commit_info, print_error_message, print_suggestion
4+
from commit_check.util import get_commit_info, print_error_head, print_error_message, print_suggestion
55

66

77
def check_author(checks: list, check_type: str) -> int:
@@ -19,6 +19,8 @@ def check_author(checks: list, check_type: str) -> int:
1919
config_value = str(get_commit_info(format_str))
2020
result = re.match(check['regex'], config_value)
2121
if result is None:
22+
if not print_error_head.has_been_called:
23+
print_error_head()
2224
print_error_message(
2325
check['check'], check['regex'],
2426
check['error'], config_value,

‎commit_check/branch.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Check git branch naming convention."""
22
import re
33
from commit_check import YELLOW, RESET_COLOR, PASS, FAIL
4-
from commit_check.util import get_branch_name, git_merge_base, print_error_message, print_suggestion
4+
from commit_check.util import get_branch_name, git_merge_base, print_error_head, print_error_message, print_suggestion
55

66

77
def check_branch(checks: list) -> int:
@@ -15,6 +15,8 @@ def check_branch(checks: list) -> int:
1515
branch_name = get_branch_name()
1616
result = re.match(check['regex'], branch_name)
1717
if result is None:
18+
if not print_error_head.has_been_called:
19+
print_error_head()
1820
print_error_message(
1921
check['check'], check['regex'],
2022
check['error'], branch_name,
@@ -40,6 +42,8 @@ def check_merge_base(checks: list) -> int:
4042
result = git_merge_base(check['regex'], 'HEAD')
4143
if result != 0:
4244
branch_name = get_branch_name()
45+
if not print_error_head.has_been_called:
46+
print_error_head()
4347
print_error_message(
4448
check['check'], check['regex'],
4549
check['error'], branch_name,

‎commit_check/commit.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import re
33
from pathlib import PurePath
44
from commit_check import YELLOW, RESET_COLOR, PASS, FAIL
5-
from commit_check.util import cmd_output, get_commit_info, print_error_message, print_suggestion
5+
from commit_check.util import cmd_output, get_commit_info, print_error_head, print_error_message, print_suggestion
66

77

88
def get_default_commit_msg_file() -> str:
@@ -37,6 +37,8 @@ def check_commit_msg(checks: list, commit_msg_file: str = "") -> int:
3737
if check['check'] == 'message':
3838
result = re.match(check['regex'], commit_msg)
3939
if result is None:
40+
if not print_error_head.has_been_called:
41+
print_error_head()
4042
print_error_message(
4143
check['check'], check['regex'],
4244
check['error'], commit_msg,
@@ -64,6 +66,8 @@ def check_commit_signoff(checks: list, commit_msg_file: str = "") -> int:
6466
commit_hash = get_commit_info("H")
6567
result = re.search(check['regex'], commit_msg)
6668
if result is None:
69+
if not print_error_head.has_been_called:
70+
print_error_head()
6771
print_error_message(
6872
check['check'], check['regex'],
6973
check['error'], commit_hash,

‎commit_check/util.py‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,18 @@ def validate_config(path_to_config: str) -> dict:
100100
return configuration
101101

102102

103-
def print_error_message(check_type: str, regex: str, error: str, reason: str):
104-
"""Print error message.
105-
:param check_type:
106-
:param regex:
107-
:param error:
108-
:param reason:
103+
def track_print_call(func):
104+
def wrapper(*args, **kwargs):
105+
wrapper.has_been_called = True
106+
return func(*args, **kwargs)
107+
wrapper.has_been_called = False # Initialize as False
108+
return wrapper
109109

110-
:returns: Give error messages to user
110+
111+
@track_print_call
112+
def print_error_head():
113+
"""Print error message.
114+
:returns: Print error head to user
111115
"""
112116
print("Commit rejected by Commit-Check. ")
113117
print(" ")
@@ -122,10 +126,20 @@ def print_error_message(check_type: str, regex: str, error: str, reason: str):
122126
print(" ")
123127
print("Commit rejected. ")
124128
print(" ")
129+
130+
131+
def print_error_message(check_type: str, regex: str, error: str, reason: str):
132+
"""Print error message.
133+
:param check_type:
134+
:param regex:
135+
:param error:
136+
:param reason:
137+
138+
:returns: Give error messages to user
139+
"""
125140
print(f"Type {YELLOW}{check_type}{RESET_COLOR} check failed => {RED}{reason}{RESET_COLOR} ", end='',)
126141
print("")
127142
print(f"It doesn't match regex: {regex}")
128-
print("")
129143
print(error)
130144

131145

0 commit comments

Comments
 (0)