Skip to content

Commit 174d446

Browse files
committed
feat(hook): arg for more verbose output
1 parent beb5c03 commit 174d446

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

conventional_pre_commit/hook.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ def main(argv=[]):
2727
action="store_true",
2828
help="Force commit to strictly follow Conventional Commits formatting. Disallows fixup! style commits.",
2929
)
30+
parser.add_argument(
31+
"--verbose",
32+
action="store_true",
33+
dest="verbose",
34+
default=False,
35+
help="Print more verbose error output.",
36+
)
3037

3138
if len(argv) < 1:
3239
argv = sys.argv[1:]
@@ -53,8 +60,13 @@ def main(argv=[]):
5360

5461
if format.is_conventional(commit_msg, args.types, args.optional_scope, scopes):
5562
return RESULT_SUCCESS
63+
64+
print(output.fail(commit_msg))
65+
66+
if not args.verbose:
67+
print(output.verbose_arg())
5668
else:
57-
print(output.fail(commit_msg))
69+
print(output.fail_verbose(commit_msg, args.types, args.optional_scope, scopes))
5870

5971
return RESULT_FAIL
6072

conventional_pre_commit/output.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ def fail(commit_msg):
1313
f"{Colors.LRED}[Bad commit message] >>{Colors.RESTORE} {commit_msg}"
1414
f"{Colors.YELLOW}Your commit message does not follow Conventional Commits formatting{Colors.RESTORE}",
1515
f"{Colors.LBLUE}https://www.conventionalcommits.org/{Colors.RESTORE}",
16+
]
17+
return os.linesep.join(lines)
18+
19+
20+
def verbose_arg():
21+
lines = [
1622
"",
1723
f"{Colors.YELLOW}Use the {Colors.RESTORE}--verbose{Colors.YELLOW} arg for more information{Colors.RESTORE}",
1824
]

tests/messages/bad_commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
bad: message
1+
bad message

tests/test_hook.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import subprocess
23

34
import pytest
@@ -94,6 +95,24 @@ def test_main_fail__conventional_commit_bad_multi_line(conventional_commit_bad_m
9495
assert result == RESULT_FAIL
9596

9697

98+
def test_main_fail__verbose(bad_commit_path, capsys):
99+
result = main(["--verbose", "--force-scope", bad_commit_path])
100+
101+
assert result == RESULT_FAIL
102+
103+
captured = capsys.readouterr()
104+
output = captured.out
105+
106+
assert "Conventional Commit messages follow a pattern like" in output
107+
assert f"type(scope): subject{os.linesep}{os.linesep} extended body" in output
108+
assert "Expected value for 'type' but found none." in output
109+
assert "Expected value for 'delim' but found none." in output
110+
assert "Expected value for 'scope' but found none." in output
111+
assert "Expected value for 'subject' but found none." in output
112+
assert "git commit --edit --file=.git/COMMIT_EDITMSG" in output
113+
assert "edit the commit message and retry the commit" in output
114+
115+
97116
def test_subprocess_fail__missing_args(cmd):
98117
result = subprocess.call(cmd)
99118

0 commit comments

Comments
 (0)