11import argparse
22import sys
33
4- from conventional_pre_commit import format
4+ from conventional_pre_commit import format , output
55
66RESULT_SUCCESS = 0
77RESULT_FAIL = 1
88
99
10- class Colors :
11- LBLUE = "\033 [00;34m"
12- LRED = "\033 [01;31m"
13- RESTORE = "\033 [0m"
14- YELLOW = "\033 [00;33m"
15-
16-
1710def main (argv = []):
1811 parser = argparse .ArgumentParser (
1912 prog = "conventional-pre-commit" , description = "Check a git commit message for Conventional Commits formatting."
2013 )
2114 parser .add_argument ("types" , type = str , nargs = "*" , default = format .DEFAULT_TYPES , help = "Optional list of types to support" )
2215 parser .add_argument ("input" , type = str , help = "A file containing a git commit message" )
16+ parser .add_argument ("--no-color" , action = "store_false" , default = True , dest = "color" , help = "Disable color in output." )
2317 parser .add_argument (
2418 "--force-scope" , action = "store_false" , default = True , dest = "optional_scope" , help = "Force commit to have scope defined."
2519 )
@@ -34,6 +28,13 @@ def main(argv=[]):
3428 action = "store_true" ,
3529 help = "Force commit to strictly follow Conventional Commits formatting. Disallows fixup! style commits." ,
3630 )
31+ parser .add_argument (
32+ "--verbose" ,
33+ action = "store_true" ,
34+ dest = "verbose" ,
35+ default = False ,
36+ help = "Print more verbose error output." ,
37+ )
3738
3839 if len (argv ) < 1 :
3940 argv = sys .argv [1 :]
@@ -45,61 +46,34 @@ def main(argv=[]):
4546
4647 try :
4748 with open (args .input , encoding = "utf-8" ) as f :
48- message = f .read ()
49+ commit_msg = f .read ()
4950 except UnicodeDecodeError :
50- print (
51- f"""
52- { Colors .LRED } [Bad Commit message encoding] { Colors .RESTORE }
53-
54- { Colors .YELLOW } conventional-pre-commit couldn't decode your commit message.{ Colors .RESTORE }
55- { Colors .YELLOW } UTF-8{ Colors .RESTORE } encoding is assumed, please configure git to write commit messages in UTF-8.
56- See { Colors .LBLUE } https://git-scm.com/docs/git-commit/#_discussion{ Colors .RESTORE } for more.
57- """
58- )
51+ print (output .unicode_decode_error (args .color ))
5952 return RESULT_FAIL
6053 if args .scopes :
6154 scopes = args .scopes .split ("," )
6255 else :
6356 scopes = args .scopes
6457
6558 if not args .strict :
66- if format .has_autosquash_prefix (message ):
59+ if format .has_autosquash_prefix (commit_msg ):
6760 return RESULT_SUCCESS
6861
69- if format .is_conventional (message , args .types , args .optional_scope , scopes ):
62+ if format .is_conventional (commit_msg , args .types , args .optional_scope , scopes ):
7063 return RESULT_SUCCESS
71- else :
72- print (
73- f"""
74- { Colors .LRED } [Bad Commit message] >>{ Colors .RESTORE } { message }
75- { Colors .YELLOW } Your commit message does not follow Conventional Commits formatting
76- { Colors .LBLUE } https://www.conventionalcommits.org/{ Colors .YELLOW }
77-
78- Conventional Commits start with one of the below types, followed by a colon,
79- followed by the commit subject and an optional body seperated by a blank line:{ Colors .RESTORE }
80-
81- { " " .join (format .conventional_types (args .types ))}
8264
83- { Colors . YELLOW } Example commit message adding a feature: { Colors . RESTORE }
65+ print ( output . fail ( commit_msg , use_color = args . color ))
8466
85- feat: implement new API
86-
87- { Colors .YELLOW } Example commit message fixing an issue:{ Colors .RESTORE }
88-
89- fix: remove infinite loop
90-
91- { Colors .YELLOW } Example commit with scope in parentheses after the type for more context:{ Colors .RESTORE }
92-
93- fix(account): remove infinite loop
94-
95- { Colors .YELLOW } Example commit with a body:{ Colors .RESTORE }
96-
97- fix: remove infinite loop
98-
99- Additional information on the issue caused by the infinite loop
100- """
67+ if not args .verbose :
68+ print (output .verbose_arg (use_color = args .color ))
69+ else :
70+ print (
71+ output .fail_verbose (
72+ commit_msg , types = args .types , optional_scope = args .optional_scope , scopes = scopes , use_color = args .color
73+ )
10174 )
102- return RESULT_FAIL
75+
76+ return RESULT_FAIL
10377
10478
10579if __name__ == "__main__" :
0 commit comments