19
19
from typing import List
20
20
21
21
from .__version__ import __version__
22
- from .commitlint import check_commit_message , remove_comments
23
22
from .exceptions import CommitlintException
24
23
from .git_helpers import get_commit_message_of_hash , get_commit_messages_of_hash_range
25
- from .messages import VALIDATION_SUCCESSFUL
24
+ from .linter import lint_commit_message
25
+ from .linter .utils import remove_comments
26
+ from .messages import VALIDATION_FAILED , VALIDATION_SUCCESSFUL
26
27
27
28
28
29
def get_args () -> argparse .Namespace :
@@ -57,28 +58,43 @@ def get_args() -> argparse.Namespace:
57
58
# --to-hash is optional
58
59
parser .add_argument ("--to-hash" , type = str , help = "To commit hash" , default = "HEAD" )
59
60
61
+ # feature options
62
+ parser .add_argument (
63
+ "--quick" , action = "store_true" , help = "Check the commit message quickly."
64
+ )
65
+
60
66
# parsing args
61
67
args = parser .parse_args ()
62
68
63
69
return args
64
70
65
71
66
- def _show_errors (commit_message : str , errors : List [str ]) -> None :
72
+ def _show_errors (
73
+ commit_message : str ,
74
+ errors : List [str ],
75
+ short : bool = False ,
76
+ ) -> None :
67
77
"""
68
78
Display a formatted error message for a list of errors.
69
79
70
80
Args:
81
+ commit_message (str): The commit message to display.
71
82
errors (List[str]): A list of error messages to be displayed.
83
+ short (bool): Display the error message in short.
84
+
72
85
"""
73
86
error_count = len (errors )
74
87
commit_message = remove_comments (commit_message )
75
88
76
- sys .stderr .write (
77
- f"⧗ Input:\n { commit_message } \n \n ✖ Found { error_count } error(s).\n \n "
78
- )
79
- for index , error in enumerate (errors ):
80
- end_char = "" if index == error_count - 1 else "\n "
81
- sys .stderr .write (f"- { error } \n { end_char } " )
89
+ sys .stderr .write (f"⧗ Input:\n { commit_message } \n \n " )
90
+
91
+ if short :
92
+ sys .stderr .write (f"{ VALIDATION_FAILED } \n " )
93
+ return
94
+
95
+ sys .stderr .write (f"✖ Found { error_count } error(s).\n " )
96
+ for error in errors :
97
+ sys .stderr .write (f"- { error } \n " )
82
98
83
99
84
100
def _get_commit_message_from_file (filepath : str ) -> str :
@@ -101,26 +117,27 @@ def _get_commit_message_from_file(filepath: str) -> str:
101
117
return commit_message
102
118
103
119
104
- def _handle_commit_message (commit_message : str ) -> None :
120
+ def _handle_commit_message (commit_message : str , quick : bool ) -> None :
105
121
"""
106
122
Handles a single commit message, checks its validity, and prints the result.
107
123
108
124
Args:
109
125
commit_message (str): The commit message to be handled.
126
+ quick (bool): True for quick linting.
110
127
111
128
Raises:
112
129
SystemExit: If the commit message is invalid.
113
130
"""
114
- success , errors = check_commit_message (commit_message )
131
+ success , errors = lint_commit_message (commit_message , quick = quick )
115
132
116
133
if success :
117
134
sys .stdout .write (f"{ VALIDATION_SUCCESSFUL } \n " )
118
135
else :
119
- _show_errors (commit_message , errors )
136
+ _show_errors (commit_message , errors , short = quick )
120
137
sys .exit (1 )
121
138
122
139
123
- def _handle_multiple_commit_messages (commit_messages : List [str ]) -> None :
140
+ def _handle_multiple_commit_messages (commit_messages : List [str ], quick : bool ) -> None :
124
141
"""
125
142
Handles multiple commit messages, checks their validity, and prints the result.
126
143
@@ -132,10 +149,10 @@ def _handle_multiple_commit_messages(commit_messages: List[str]) -> None:
132
149
"""
133
150
has_error = False
134
151
for commit_message in commit_messages :
135
- success , errors = check_commit_message (commit_message )
152
+ success , errors = lint_commit_message (commit_message , quick = quick )
136
153
if not success :
137
154
has_error = True
138
- _show_errors (commit_message , errors )
155
+ _show_errors (commit_message , errors , short = quick )
139
156
sys .stderr .write ("\n " )
140
157
141
158
if has_error :
@@ -153,18 +170,18 @@ def main() -> None:
153
170
try :
154
171
if args .file :
155
172
commit_message = _get_commit_message_from_file (args .file )
156
- _handle_commit_message (commit_message )
173
+ _handle_commit_message (commit_message , quick = args . quick )
157
174
elif args .hash :
158
175
commit_message = get_commit_message_of_hash (args .hash )
159
- _handle_commit_message (commit_message )
176
+ _handle_commit_message (commit_message , quick = args . quick )
160
177
elif args .from_hash :
161
178
commit_messages = get_commit_messages_of_hash_range (
162
179
args .from_hash , args .to_hash
163
180
)
164
- _handle_multiple_commit_messages (commit_messages )
181
+ _handle_multiple_commit_messages (commit_messages , quick = args . quick )
165
182
else :
166
183
commit_message = args .commit_message .strip ()
167
- _handle_commit_message (commit_message )
184
+ _handle_commit_message (commit_message , quick = args . quick )
168
185
except CommitlintException as ex :
169
186
sys .stderr .write (f"{ ex } \n " )
170
187
sys .exit (1 )
0 commit comments