|
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." |
@@ -45,65 +38,25 @@ def main(argv=[]): |
45 | 38 |
|
46 | 39 | try: |
47 | 40 | with open(args.input, encoding="utf-8") as f: |
48 | | - message = f.read() |
| 41 | + commit_msg = f.read() |
49 | 42 | 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 | | - ) |
| 43 | + print(output.unicode_decode_error()) |
59 | 44 | return RESULT_FAIL |
60 | 45 | if args.scopes: |
61 | 46 | scopes = args.scopes.split(",") |
62 | 47 | else: |
63 | 48 | scopes = args.scopes |
64 | 49 |
|
65 | 50 | if not args.strict: |
66 | | - if format.has_autosquash_prefix(message): |
| 51 | + if format.has_autosquash_prefix(commit_msg): |
67 | 52 | return RESULT_SUCCESS |
68 | 53 |
|
69 | | - if format.is_conventional(message, args.types, args.optional_scope, scopes): |
| 54 | + if format.is_conventional(commit_msg, args.types, args.optional_scope, scopes): |
70 | 55 | return RESULT_SUCCESS |
71 | 56 | 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 | | - Run |
79 | | - git commit --edit --file=.git/COMMIT_EDITMSG |
80 | | - to reedit the commit message do the commit. |
81 | | -
|
82 | | - Conventional Commits start with one of the below types, followed by a colon, |
83 | | - followed by the commit subject and an optional body seperated by a blank line:{Colors.RESTORE} |
84 | | -
|
85 | | - {" ".join(format.conventional_types(args.types))} |
86 | | -
|
87 | | - {Colors.YELLOW}Example commit message adding a feature:{Colors.RESTORE} |
88 | | -
|
89 | | - feat: implement new API |
| 57 | + print(output.fail(commit_msg)) |
90 | 58 |
|
91 | | - {Colors.YELLOW}Example commit message fixing an issue:{Colors.RESTORE} |
92 | | -
|
93 | | - fix: remove infinite loop |
94 | | -
|
95 | | - {Colors.YELLOW}Example commit with scope in parentheses after the type for more context:{Colors.RESTORE} |
96 | | -
|
97 | | - fix(account): remove infinite loop |
98 | | -
|
99 | | - {Colors.YELLOW}Example commit with a body:{Colors.RESTORE} |
100 | | -
|
101 | | - fix: remove infinite loop |
102 | | -
|
103 | | - Additional information on the issue caused by the infinite loop |
104 | | - """ |
105 | | - ) |
106 | | - return RESULT_FAIL |
| 59 | + return RESULT_FAIL |
107 | 60 |
|
108 | 61 |
|
109 | 62 | if __name__ == "__main__": |
|
0 commit comments