44import argparse
55from typing import Match
66
7- # Conventional commit pattern
8- CONVENTIONAL_COMMIT_PATTERN : str = r"^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z0-9-]+\))?!?: .{1,100}"
7+ # Conventional commit pattern (including Git revert messages)
8+ CONVENTIONAL_COMMIT_PATTERN : str = (
9+ r"^((build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z0-9-]+\))?!?: .{1,100}|Revert .+)"
10+ )
911
1012
1113def get_commit_message (commit_hash : str ) -> str :
@@ -23,9 +25,7 @@ def get_commit_message(commit_hash: str) -> str:
2325 sys .exit (1 )
2426
2527
26- def check_commit_message (
27- message : str , pattern : str = CONVENTIONAL_COMMIT_PATTERN
28- ) -> bool :
28+ def check_commit_message (message : str , pattern : str = CONVENTIONAL_COMMIT_PATTERN ) -> bool :
2929 """Check if commit message follows conventional commit format."""
3030 first_line : str = message .split ("\n " )[0 ]
3131 match : Match [str ] | None = re .match (pattern , first_line )
@@ -50,9 +50,7 @@ def check_commit_range(base_ref: str, head_ref: str) -> list[dict[str, str]]:
5050 for commit_hash in commit_hashes :
5151 message : str = get_commit_message (commit_hash )
5252 if not check_commit_message (message ):
53- non_compliant .append (
54- {"hash" : commit_hash , "message" : message .split ("\n " )[0 ]}
55- )
53+ non_compliant .append ({"hash" : commit_hash , "message" : message .split ("\n " )[0 ]})
5654
5755 return non_compliant
5856 except subprocess .CalledProcessError as e :
@@ -61,15 +59,9 @@ def check_commit_range(base_ref: str, head_ref: str) -> list[dict[str, str]]:
6159
6260
6361def main () -> None :
64- parser : argparse .ArgumentParser = argparse .ArgumentParser (
65- description = "Check conventional commit compliance"
66- )
67- parser .add_argument (
68- "--base" , required = True , help = "Base ref (starting commit, exclusive)"
69- )
70- parser .add_argument (
71- "--head" , required = True , help = "Head ref (ending commit, inclusive)"
72- )
62+ parser : argparse .ArgumentParser = argparse .ArgumentParser (description = "Check conventional commit compliance" )
63+ parser .add_argument ("--base" , required = True , help = "Base ref (starting commit, exclusive)" )
64+ parser .add_argument ("--head" , required = True , help = "Head ref (ending commit, inclusive)" )
7365 args : argparse .Namespace = parser .parse_args ()
7466
7567 non_compliant : list [dict [str , str ]] = check_commit_range (args .base , args .head )
@@ -80,9 +72,7 @@ def main() -> None:
8072 print (f"- { commit ['hash' ][:8 ]} : { commit ['message' ]} " )
8173 print ("\n Please ensure your commit messages follow the format:" )
8274 print ("type(scope): subject" )
83- print (
84- "\n Where type is one of: build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test"
85- )
75+ print ("\n Where type is one of: build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test" )
8676 sys .exit (1 )
8777 else :
8878 print ("All commits follow the conventional commit format!" )
0 commit comments