1
1
import os
2
2
import re
3
+ from typing import Dict
3
4
4
- from commitizen import factory , out
5
+ from commitizen import factory , git , out
5
6
from commitizen .config import BaseConfig
6
7
from commitizen .error_codes import INVALID_COMMIT_MSG
7
8
8
9
9
10
class Check :
10
11
"""Check if the current commit msg matches the commitizen format."""
11
12
12
- def __init__ (self , config : BaseConfig , arguments : dict , cwd = os .getcwd ()):
13
+ def __init__ (self , config : BaseConfig , arguments : Dict [ str , str ] , cwd = os .getcwd ()):
13
14
"""Init method.
14
15
15
16
Parameters
@@ -21,31 +22,53 @@ def __init__(self, config: BaseConfig, arguments: dict, cwd=os.getcwd()):
21
22
the flags provided by the user
22
23
23
24
"""
25
+ self .commit_msg_file : str = arguments .get ("commit_msg_file" )
26
+ self .rev_range : str = arguments .get ("rev_range" )
27
+
28
+ self ._valid_command_argument ()
29
+
24
30
self .config : BaseConfig = config
25
31
self .cz = factory .commiter_factory (self .config )
26
- self .arguments : dict = arguments
32
+
33
+ def _valid_command_argument (self ):
34
+ if bool (self .commit_msg_file ) is bool (self .rev_range ):
35
+ out .error ("One and only one argument is required for check command!" )
36
+ raise SystemExit ()
27
37
28
38
def __call__ (self ):
29
- """Validate if a commit message follows the conventional pattern.
39
+ """Validate if commit messages follows the conventional pattern.
30
40
31
41
Raises
32
42
------
33
43
SystemExit
34
44
if the commit provided not follows the conventional pattern
35
45
36
46
"""
37
- commit_msg_content = self ._get_commit_msg ()
47
+ commit_msgs = self ._get_commit_messages ()
38
48
pattern = self .cz .schema_pattern ()
39
- if self ._has_proper_format (pattern , commit_msg_content ) is not None :
40
- out .success ("Commit validation: successful!" )
41
- else :
42
- out .error ("commit validation: failed!" )
43
- out .error ("please enter a commit message in the commitizen format." )
44
- raise SystemExit (INVALID_COMMIT_MSG )
45
-
46
- def _get_commit_msg (self ):
47
- temp_filename : str = self .arguments .get ("commit_msg_file" )
48
- return open (temp_filename , "r" ).read ()
49
-
50
- def _has_proper_format (self , pattern , commit_msg ):
49
+ for commit_msg in commit_msgs :
50
+ if not Check .validate_commit_message (commit_msg , pattern ):
51
+ out .error (
52
+ "commit validation: failed!\n "
53
+ "please enter a commit message in the commitizen format.\n "
54
+ f"commit: { commit_msg } \n "
55
+ f"pattern: { pattern } "
56
+ )
57
+ raise SystemExit (INVALID_COMMIT_MSG )
58
+ out .success ("Commit validation: successful!" )
59
+
60
+ def _get_commit_messages (self ):
61
+ # Get commit message from file (--commit-msg-file)
62
+ if self .commit_msg_file :
63
+ with open (self .commit_msg_file , "r" ) as commit_file :
64
+ commit_msg = commit_file .read ()
65
+ return [commit_msg ]
66
+
67
+ # Get commit messages from git log (--rev-range)
68
+ return [commit .message for commit in git .get_commits (end = self .rev_range )]
69
+
70
+ @staticmethod
71
+ def validate_commit_message (commit_msg : str , pattern : str ) -> bool :
72
+ if commit_msg .startswith ("Merge" ) or commit_msg .startswith ("Revert" ):
73
+ return True
51
74
return re .match (pattern , commit_msg )
0 commit comments