Skip to content

Commit 834acd6

Browse files
shenxianpengXianpeng Shen
authored and
Xianpeng Shen
committed
fix: check commit msg issue (commit-check#73)
1 parent cf77a39 commit 834acd6

10 files changed

+39
-32
lines changed

.commit-check.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ checks:
66
[optional body]\n
77
[optional footer(s)]\n\n
88
More details please refer to https://www.conventionalcommits.org"
9-
suggest: git commit --amend --no-verify
9+
suggest: please check your commit message whether matches above regex
1010
- check: branch
1111
regex: ^(bugfix|feature|release|hotfix|task)\/.+|(master)|(main)|(HEAD)|(PR-.+)
1212
error: "Branches must begin with these types: bugfix/ feature/ release/ hotfix/ task/"
13-
suggest: git checkout -b type/branch_name
13+
suggest: run command `git checkout -b type/branch_name`
1414
- check: author_name
1515
regex: ^[A-Za-z ,.\'-]+$|.*(\[bot])
1616
error: The committer name seems invalid
17-
suggest: git config user.name "Peter Shen"
17+
suggest: run command `git config user.name "Your Name"`
1818
- check: author_email
1919
regex: ^\S+@\S+\.\S+$
2020
error: The committer email seems invalid
21-
suggest: git config user.email petershen@example.com
21+
suggest: run command `git config user.email yourname@example.com`

.pre-commit-config.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ repos:
3131
hooks:
3232
- id: codespell
3333
- repo: https://github.com/commit-check/commit-check
34-
rev: v0.5.1
34+
rev: v0.5.2
3535
hooks:
3636
- id: check-message
37-
stages: [post-commit]
3837
- id: check-branch
3938
- id: check-author-email
4039
- repo: local

.pre-commit-hooks.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
- id: check-message
22
name: check commit message
33
description: requiring commit message to match regex
4-
entry: commit-check
4+
entry: env IS_PRE_COMMIT=1 commit-check
55
args: [--message]
66
pass_filenames: false
77
language: python
8-
stages: [post-commit]
8+
stages: [commit-msg, commit]
99
- id: check-branch
1010
name: check branch naming
1111
description: requiring branch naming to match regex

commit_check/__init__.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@
2121
'[optional body]\n'
2222
'[optional footer(s)]\n\n'
2323
'More details please refer to https://www.conventionalcommits.org',
24-
'suggest': 'git commit --amend --no-verify'
24+
'suggest': 'please check your commit message whether matches above regex'
2525
},
2626
{
2727
'check': 'branch',
2828
'regex': r'^(bugfix|feature|release|hotfix|task)\/.+|(master)|(main)|(HEAD)|(PR-.+)',
2929
'error': 'Branches must begin with these types: bugfix/ feature/ release/ hotfix/ task/',
30-
'suggest': 'git checkout -b bugfix/branch_name',
30+
'suggest': 'run command `git checkout -b type/branch_name`',
3131
},
3232
{
3333
'check': 'author_name',
3434
'regex': r'^[A-Za-z ,.\'-]+$|.*(\[bot])',
3535
'error': 'The committer name seems invalid',
36-
'suggest': 'git config user.name "Peter Shen"',
36+
'suggest': 'run command `git config user.name "Your Name"`',
3737
},
3838
{
3939
'check': 'author_email',
4040
'regex': r'^\S+@\S+\.\S+$',
4141
'error': 'The committer\'s email seems invalid',
42-
'suggest': 'git config user.email petershen@example.com',
42+
'suggest': 'run command `git config user.email yourname@example.com`',
4343
},
4444
],
4545
}

commit_check/commit.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
"""Check git commit message formatting"""
22
import re
3+
import os
4+
from pathlib import PurePath
35
from commit_check import YELLOW, RESET_COLOR, PASS, FAIL
4-
from commit_check.util import get_commits_info, print_error_message, print_suggestion
6+
from commit_check.util import cmd_output, get_commits_info, print_error_message, print_suggestion
57

68

7-
def check_commit(checks: list) -> int:
9+
def check_commit_msg(checks: list) -> int:
810
for check in checks:
911
if check['check'] == 'message':
1012
if check['regex'] == "":
1113
print(
1214
f"{YELLOW}Not found regex for commit message. skip checking.{RESET_COLOR}",
1315
)
1416
return PASS
15-
commit_message = str(get_commits_info("s"))
16-
result = re.match(check['regex'], commit_message)
17+
# check the message of the current commit
18+
if os.environ.get("IS_PRE_COMMIT") == "1":
19+
git_dir = cmd_output(['git', 'rev-parse', '--git-dir']).strip()
20+
commit_msg_file = PurePath(git_dir, "COMMIT_EDITMSG")
21+
with open(commit_msg_file, 'r') as f:
22+
commit_msg = f.read()
23+
else: # check the message of the last commit
24+
commit_msg = str(get_commits_info("s"))
25+
result = re.match(check['regex'], commit_msg)
1726
if result is None:
1827
print_error_message(
1928
check['check'], check['regex'],
20-
check['error'], commit_message,
29+
check['error'], commit_msg,
2130
)
2231
if check['suggest']:
2332
print_suggestion(check['suggest'])

commit_check/main.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
The module containing main entrypoint function.
66
"""
77
import argparse
8-
98
from commit_check import branch
109
from commit_check import commit
1110
from commit_check import author
@@ -97,7 +96,7 @@ def main() -> int:
9796
) else DEFAULT_CONFIG
9897
checks = config['checks']
9998
if args.message:
100-
retval = commit.check_commit(checks)
99+
retval = commit.check_commit_msg(checks)
101100
if args.author_name:
102101
retval = author.check_author(checks, "author_name")
103102
if args.author_email:

commit_check/util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def print_suggestion(suggest: str) -> None:
144144
"""
145145
if suggest:
146146
print(
147-
f"Suggest to run => {GREEN}{suggest}{RESET_COLOR} ", end='',
147+
f"Suggest: {GREEN}{suggest}{RESET_COLOR} ", end='',
148148
)
149149
else:
150150
print(f"commit-check does not support {suggest} yet.")

tests/commit_test.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from commit_check import PASS, FAIL
2-
from commit_check.commit import check_commit
2+
from commit_check.commit import check_commit_msg
33

44
# used by get_commits_info mock
55
FAKE_BRANCH_NAME = "fake_commits_info"
6-
# The location of check_commit()
6+
# The location of check_commit_msg()
77
LOCATION = "commit_check.commit"
88

99

@@ -22,7 +22,7 @@ def test_check_commit(self, mocker):
2222
"re.match",
2323
return_value="fake_rematch_resp"
2424
)
25-
retval = check_commit(checks)
25+
retval = check_commit_msg(checks)
2626
assert retval == PASS
2727
assert m_get_commits_info.call_count == 1
2828
assert m_re_match.call_count == 1
@@ -38,7 +38,7 @@ def test_check_commit_with_empty_checks(self, mocker):
3838
"re.match",
3939
return_value="fake_commits_info"
4040
)
41-
retval = check_commit(checks)
41+
retval = check_commit_msg(checks)
4242
assert retval == PASS
4343
assert m_get_commits_info.call_count == 0
4444
assert m_re_match.call_count == 0
@@ -57,7 +57,7 @@ def test_check_commit_with_different_check(self, mocker):
5757
"re.match",
5858
return_value="fake_commits_info"
5959
)
60-
retval = check_commit(checks)
60+
retval = check_commit_msg(checks)
6161
assert retval == PASS
6262
assert m_get_commits_info.call_count == 0
6363
assert m_re_match.call_count == 0
@@ -78,7 +78,7 @@ def test_check_commit_with_len0_regex(self, mocker, capfd):
7878
"re.match",
7979
return_value="fake_rematch_resp"
8080
)
81-
retval = check_commit(checks)
81+
retval = check_commit_msg(checks)
8282
assert retval == PASS
8383
assert m_get_commits_info.call_count == 0
8484
assert m_re_match.call_count == 0
@@ -107,7 +107,7 @@ def test_check_commit_with_result_none(self, mocker):
107107
m_print_suggestion = mocker.patch(
108108
f"{LOCATION}.print_suggestion"
109109
)
110-
retval = check_commit(checks)
110+
retval = check_commit_msg(checks)
111111
assert retval == FAIL
112112
assert m_get_commits_info.call_count == 1
113113
assert m_re_match.call_count == 1

tests/main_test.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_main(
4141
]
4242
}
4343
)
44-
m_check_commit = mocker.patch("commit_check.commit.check_commit")
44+
m_check_commit = mocker.patch("commit_check.commit.check_commit_msg")
4545
m_check_branch = mocker.patch(
4646
"commit_check.branch.check_branch"
4747
)
@@ -63,7 +63,7 @@ def test_main_help(self, mocker, capfd):
6363
]
6464
}
6565
)
66-
m_check_commit = mocker.patch("commit_check.commit.check_commit")
66+
m_check_commit = mocker.patch("commit_check.commit.check_commit_msg")
6767
m_check_branch = mocker.patch(
6868
"commit_check.branch.check_branch"
6969
)
@@ -88,7 +88,7 @@ def test_main_version(self, mocker):
8888
]
8989
}
9090
)
91-
m_check_commit = mocker.patch("commit_check.commit.check_commit")
91+
m_check_commit = mocker.patch("commit_check.commit.check_commit_msg")
9292
m_check_branch = mocker.patch(
9393
"commit_check.branch.check_branch"
9494
)
@@ -107,7 +107,7 @@ def test_main_validate_config_ret_none(self, mocker):
107107
"commit_check.main.validate_config",
108108
return_value={}
109109
)
110-
m_check_commit = mocker.patch("commit_check.commit.check_commit")
110+
m_check_commit = mocker.patch("commit_check.commit.check_commit_msg")
111111
mocker.patch(
112112
"commit_check.branch.check_branch"
113113
)

tests/util_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def test_print_suggestion(self, capfd):
245245
# Must print on stdout with given argument.
246246
print_suggestion("dummy suggest")
247247
stdout, _ = capfd.readouterr()
248-
assert "Suggest to run" in stdout
248+
assert "Suggest:" in stdout
249249

250250
def test_print_suggestion_exit1(self, capfd):
251251
# Must exit with 1 when "" passed

0 commit comments

Comments
 (0)