Skip to content

Commit a2d5ac9

Browse files
authored
Encapsulate logic into check() (#3)
Encapsulate logic into separate function so the core business logic can be used when the script is imported as a module too.
1 parent 9e7c9c4 commit a2d5ac9

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

bad_commit_message_blocker.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import sys
2020
from textblob import TextBlob
2121

22+
DEFAULT_SUBJECT_LIMIT = 50
23+
DEFAULT_BODY_LIMIT = 72
24+
2225

2326
class CliColors:
2427
HEADER = '\033[95m'
@@ -137,6 +140,22 @@ def print_result(check_passed, rule):
137140
"PASSED" if check_passed else CliColors.FAIL + "FAILED") + CliColors.ENDC + "] " + rule)
138141

139142

143+
def check(commit_message, subject_limit=DEFAULT_SUBJECT_LIMIT, body_limit=DEFAULT_BODY_LIMIT):
144+
all_rules_verified = check_subject_is_separated_from_body(
145+
commit_message)
146+
all_rules_verified &= check_subject_is_not_too_long(
147+
commit_message, subject_limit)
148+
all_rules_verified &= check_subject_is_capitalized(commit_message)
149+
all_rules_verified &= check_subject_does_not_end_with_period(
150+
commit_message)
151+
all_rules_verified &= check_subject_uses_imperative(commit_message)
152+
all_rules_verified &= check_body_lines_are_not_too_long(
153+
commit_message, body_limit)
154+
all_rules_verified &= check_body_explains_what_and_why(commit_message)
155+
156+
return all_rules_verified
157+
158+
140159
def main():
141160
parser_description = "Bad commit message blocker: Avoid bad commit messages in your repository"
142161
parser = argparse.ArgumentParser(description=parser_description)
@@ -145,10 +164,10 @@ def main():
145164
required=True)
146165
parser.add_argument("--subject-limit",
147166
help="The maximum allowed length for a commit subject",
148-
default=50)
167+
default=DEFAULT_SUBJECT_LIMIT)
149168
parser.add_argument("--body-limit",
150169
help="The maximum allowed length for a line in the commit body",
151-
default=72)
170+
default=DEFAULT_BODY_LIMIT)
152171
args = parser.parse_args()
153172

154173
commit_message = args.message.strip()
@@ -160,16 +179,8 @@ def main():
160179
print(CliColors.HEADER + CliColors.BOLD +
161180
"Conformance to the 7 rules of a great Git commit message:" + CliColors.ENDC)
162181

163-
all_rules_verified = check_subject_is_separated_from_body(commit_message)
164-
all_rules_verified &= check_subject_is_not_too_long(
165-
commit_message, int(args.subject_limit))
166-
all_rules_verified &= check_subject_is_capitalized(commit_message)
167-
all_rules_verified &= check_subject_does_not_end_with_period(
168-
commit_message)
169-
all_rules_verified &= check_subject_uses_imperative(commit_message)
170-
all_rules_verified &= check_body_lines_are_not_too_long(
171-
commit_message, int(args.body_limit))
172-
all_rules_verified &= check_body_explains_what_and_why(commit_message)
182+
all_rules_verified = check(commit_message, int(
183+
args.subject_limit), int(args.body_limit))
173184

174185
sys.exit(0 if all_rules_verified else 1)
175186

0 commit comments

Comments
 (0)