1
1
import argparse
2
2
import sys
3
3
4
- from conventional_pre_commit import format
4
+ from conventional_pre_commit import format , output
5
5
6
6
RESULT_SUCCESS = 0
7
7
RESULT_FAIL = 1
8
8
9
9
10
- class Colors :
11
- LBLUE = "\033 [00;34m"
12
- LRED = "\033 [01;31m"
13
- RESTORE = "\033 [0m"
14
- YELLOW = "\033 [00;33m"
15
-
16
-
17
10
def main (argv = []):
18
11
parser = argparse .ArgumentParser (
19
12
prog = "conventional-pre-commit" , description = "Check a git commit message for Conventional Commits formatting."
20
13
)
21
14
parser .add_argument ("types" , type = str , nargs = "*" , default = format .DEFAULT_TYPES , help = "Optional list of types to support" )
22
15
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." )
23
17
parser .add_argument (
24
18
"--force-scope" , action = "store_false" , default = True , dest = "optional_scope" , help = "Force commit to have scope defined."
25
19
)
@@ -34,6 +28,13 @@ def main(argv=[]):
34
28
action = "store_true" ,
35
29
help = "Force commit to strictly follow Conventional Commits formatting. Disallows fixup! style commits." ,
36
30
)
31
+ parser .add_argument (
32
+ "--verbose" ,
33
+ action = "store_true" ,
34
+ dest = "verbose" ,
35
+ default = False ,
36
+ help = "Print more verbose error output." ,
37
+ )
37
38
38
39
if len (argv ) < 1 :
39
40
argv = sys .argv [1 :]
@@ -45,61 +46,34 @@ def main(argv=[]):
45
46
46
47
try :
47
48
with open (args .input , encoding = "utf-8" ) as f :
48
- message = f .read ()
49
+ commit_msg = f .read ()
49
50
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 ))
59
52
return RESULT_FAIL
60
53
if args .scopes :
61
54
scopes = args .scopes .split ("," )
62
55
else :
63
56
scopes = args .scopes
64
57
65
58
if not args .strict :
66
- if format .has_autosquash_prefix (message ):
59
+ if format .has_autosquash_prefix (commit_msg ):
67
60
return RESULT_SUCCESS
68
61
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 ):
70
63
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 ))}
82
64
83
- { Colors . YELLOW } Example commit message adding a feature: { Colors . RESTORE }
65
+ print ( output . fail ( commit_msg , use_color = args . color ))
84
66
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
+ )
101
74
)
102
- return RESULT_FAIL
75
+
76
+ return RESULT_FAIL
103
77
104
78
105
79
if __name__ == "__main__" :
0 commit comments