1818# limitations under the License.
1919# *******************************************************************************
2020
21+ import re
2122import argparse
2223import subprocess
23- import re
2424
2525# Ensure the scope ends in a colon and that same level scopes are
2626# comma delimited.
3030def __scopeCheck (msg : str ):
3131 status = "Message scope: "
3232
33- if not re .match ('^[a-z0-9_]+(, [a-z0-9_]+)*: ' , msg ):
34- print (f"{ status } FAILED: Commit message must follow the format "
35- "<scope>:[ <scope>:] <short description>" )
33+ if not re .match (r"^[a-z0-9_]+(, [a-z0-9_]+)*: " , msg ):
34+ if re .match (r"^\s+" , msg ):
35+ print (
36+ f"{ status } FAILED: Commit message shouldn't have leading spaces"
37+ )
38+ return False
39+
40+ if re .match (r"^Merge " , msg ):
41+ print (f"{ status } FAILED: Merge commits are not allowed" )
42+ return False
43+
44+ print (
45+ f"{ status } FAILED: Commit message must follow the format "
46+ "<scope>:[ <scope>:] <short description>"
47+ )
3648 return False
3749
3850 print (f"{ status } OK" )
@@ -45,14 +57,20 @@ def __numCharacterCheck(msg: str):
4557 print (f"{ status } OK" )
4658 return True
4759 else :
48- # Fixup commits usually include the full name of the commit they are
49- # fixing, which adds 6 more symbols to the message. Let them in.
50- if re .match ('^fixup: ' , msg ):
60+ # Fixup or revert commits usually include the full name of the commit
61+ # they are fixing, which adds 6 more symbols to the message.
62+ # Let them in.
63+ if re .match (r"^fixup: " , msg ):
5164 print (f"{ status } Fixup message, OK" )
5265 return True
66+ elif re .match (r"^revert: " , msg ):
67+ print (f"{ status } Revert message, OK" )
68+ return True
5369 else :
54- print (f"{ status } FAILED: Commit message summary must not "
55- "exceed 72 characters." )
70+ print (
71+ f"{ status } FAILED: Commit message summary must not "
72+ "exceed 72 characters."
73+ )
5674 return False
5775
5876def main ():
@@ -64,23 +82,28 @@ def main():
6482 head : str = args .head
6583
6684 commit_range = base + ".." + head
67- messages = subprocess .run (["git" , "rev-list" , "--format=oneline" ,
68- commit_range ], capture_output = True , text = True ).stdout
85+ messages = subprocess .run (
86+ ["git" , "rev-list" , "--format=oneline" , commit_range ],
87+ capture_output = True ,
88+ text = True ,
89+ ).stdout
6990
7091 is_ok = True
7192 for i in messages .splitlines ():
72- print (i )
73- commit_msg = i .split (' ' , 1 )[1 ]
74- result = __numCharacterCheck (commit_msg )
75- is_ok = is_ok and result
76- result = __scopeCheck (commit_msg )
77- is_ok = is_ok and result
93+ print (i )
94+ commit_msg = i .split (" " , 1 )[1 ]
95+ result = __numCharacterCheck (commit_msg )
96+ is_ok = is_ok and result
97+ result = __scopeCheck (commit_msg )
98+ is_ok = is_ok and result
7899
79100 if is_ok :
80101 print ("All commmit messages are formatted correctly. " )
81102 else :
82- print ("Some commit message checks failed. Please align commit messages "
83- "with Contributing Guidelines and update the PR." )
103+ print (
104+ "Some commit message checks failed. Please align commit messages "
105+ "with Contributing Guidelines and update the PR."
106+ )
84107 exit (1 )
85108
86109
0 commit comments