Skip to content

Commit beb5c03

Browse files
committed
refactor(hook): get output from helper functions
1 parent 95565c2 commit beb5c03

File tree

1 file changed

+7
-54
lines changed

1 file changed

+7
-54
lines changed

conventional_pre_commit/hook.py

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
import argparse
22
import sys
33

4-
from conventional_pre_commit import format
4+
from conventional_pre_commit import format, output
55

66
RESULT_SUCCESS = 0
77
RESULT_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-
1710
def main(argv=[]):
1811
parser = argparse.ArgumentParser(
1912
prog="conventional-pre-commit", description="Check a git commit message for Conventional Commits formatting."
@@ -45,65 +38,25 @@ def main(argv=[]):
4538

4639
try:
4740
with open(args.input, encoding="utf-8") as f:
48-
message = f.read()
41+
commit_msg = f.read()
4942
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())
5944
return RESULT_FAIL
6045
if args.scopes:
6146
scopes = args.scopes.split(",")
6247
else:
6348
scopes = args.scopes
6449

6550
if not args.strict:
66-
if format.has_autosquash_prefix(message):
51+
if format.has_autosquash_prefix(commit_msg):
6752
return RESULT_SUCCESS
6853

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):
7055
return RESULT_SUCCESS
7156
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))
9058

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
10760

10861

10962
if __name__ == "__main__":

0 commit comments

Comments
 (0)