Skip to content

fix: check commit msg #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Feb 24, 2023
8 changes: 4 additions & 4 deletions .commit-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ checks:
[optional body]\n
[optional footer(s)]\n\n
More details please refer to https://www.conventionalcommits.org"
suggest: git commit --amend --no-verify
suggest: please check your commit message whether matches above regex
- check: branch
regex: ^(bugfix|feature|release|hotfix|task)\/.+|(master)|(main)|(HEAD)|(PR-.+)
error: "Branches must begin with these types: bugfix/ feature/ release/ hotfix/ task/"
suggest: git checkout -b type/branch_name
suggest: run command `git checkout -b type/branch_name`
- check: author_name
regex: ^[A-Za-z ,.\'-]+$|.*(\[bot])
error: The committer name seems invalid
suggest: git config user.name "Peter Shen"
suggest: run command `git config user.name "Your Name"`
- check: author_email
regex: ^\S+@\S+\.\S+$
error: The committer email seems invalid
suggest: git config user.email petershen@example.com
suggest: run command `git config user.email yourname@example.com`
3 changes: 1 addition & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ repos:
hooks:
- id: codespell
- repo: https://github.com/commit-check/commit-check
rev: v0.5.1
rev: v0.5.2
hooks:
- id: check-message
stages: [post-commit]
- id: check-branch
- id: check-author-email
- repo: local
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
- id: check-message
name: check commit message
description: requiring commit message to match regex
entry: commit-check
entry: env IS_PRE_COMMIT=1 commit-check
args: [--message]
pass_filenames: false
language: python
stages: [post-commit]
stages: [commit-msg, commit]
- id: check-branch
name: check branch naming
description: requiring branch naming to match regex
Expand Down
8 changes: 4 additions & 4 deletions commit_check/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@
'[optional body]\n'
'[optional footer(s)]\n\n'
'More details please refer to https://www.conventionalcommits.org',
'suggest': 'git commit --amend --no-verify'
'suggest': 'please check your commit message whether matches above regex'
},
{
'check': 'branch',
'regex': r'^(bugfix|feature|release|hotfix|task)\/.+|(master)|(main)|(HEAD)|(PR-.+)',
'error': 'Branches must begin with these types: bugfix/ feature/ release/ hotfix/ task/',
'suggest': 'git checkout -b bugfix/branch_name',
'suggest': 'run command `git checkout -b type/branch_name`',
},
{
'check': 'author_name',
'regex': r'^[A-Za-z ,.\'-]+$|.*(\[bot])',
'error': 'The committer name seems invalid',
'suggest': 'git config user.name "Peter Shen"',
'suggest': 'run command `git config user.name "Your Name"`',
},
{
'check': 'author_email',
'regex': r'^\S+@\S+\.\S+$',
'error': 'The committer\'s email seems invalid',
'suggest': 'git config user.email petershen@example.com',
'suggest': 'run command `git config user.email yourname@example.com`',
},
],
}
Expand Down
19 changes: 14 additions & 5 deletions commit_check/commit.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
"""Check git commit message formatting"""
import re
import os
from pathlib import PurePath
from commit_check import YELLOW, RESET_COLOR, PASS, FAIL
from commit_check.util import get_commits_info, print_error_message, print_suggestion
from commit_check.util import cmd_output, get_commits_info, print_error_message, print_suggestion


def check_commit(checks: list) -> int:
def check_commit_msg(checks: list) -> int:
for check in checks:
if check['check'] == 'message':
if check['regex'] == "":
print(
f"{YELLOW}Not found regex for commit message. skip checking.{RESET_COLOR}",
)
return PASS
commit_message = str(get_commits_info("s"))
result = re.match(check['regex'], commit_message)
# check the message of the current commit
if os.environ.get("IS_PRE_COMMIT") == "1":
git_dir = cmd_output(['git', 'rev-parse', '--git-dir']).strip()
commit_msg_file = PurePath(git_dir, "COMMIT_EDITMSG")
with open(commit_msg_file, 'r') as f:
commit_msg = f.read()
else: # check the message of the last commit
commit_msg = str(get_commits_info("s"))
result = re.match(check['regex'], commit_msg)
if result is None:
print_error_message(
check['check'], check['regex'],
check['error'], commit_message,
check['error'], commit_msg,
)
if check['suggest']:
print_suggestion(check['suggest'])
Expand Down
3 changes: 1 addition & 2 deletions commit_check/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
The module containing main entrypoint function.
"""
import argparse

from commit_check import branch
from commit_check import commit
from commit_check import author
Expand Down Expand Up @@ -97,7 +96,7 @@ def main() -> int:
) else DEFAULT_CONFIG
checks = config['checks']
if args.message:
retval = commit.check_commit(checks)
retval = commit.check_commit_msg(checks)
if args.author_name:
retval = author.check_author(checks, "author_name")
if args.author_email:
Expand Down
2 changes: 1 addition & 1 deletion commit_check/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def print_suggestion(suggest: str) -> None:
"""
if suggest:
print(
f"Suggest to run => {GREEN}{suggest}{RESET_COLOR} ", end='',
f"Suggest: {GREEN}{suggest}{RESET_COLOR} ", end='',
)
else:
print(f"commit-check does not support {suggest} yet.")
Expand Down
14 changes: 7 additions & 7 deletions tests/commit_test.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from commit_check import PASS, FAIL
from commit_check.commit import check_commit
from commit_check.commit import check_commit_msg

# used by get_commits_info mock
FAKE_BRANCH_NAME = "fake_commits_info"
# The location of check_commit()
# The location of check_commit_msg()
LOCATION = "commit_check.commit"


Expand All @@ -22,7 +22,7 @@ def test_check_commit(self, mocker):
"re.match",
return_value="fake_rematch_resp"
)
retval = check_commit(checks)
retval = check_commit_msg(checks)
assert retval == PASS
assert m_get_commits_info.call_count == 1
assert m_re_match.call_count == 1
Expand All @@ -38,7 +38,7 @@ def test_check_commit_with_empty_checks(self, mocker):
"re.match",
return_value="fake_commits_info"
)
retval = check_commit(checks)
retval = check_commit_msg(checks)
assert retval == PASS
assert m_get_commits_info.call_count == 0
assert m_re_match.call_count == 0
Expand All @@ -57,7 +57,7 @@ def test_check_commit_with_different_check(self, mocker):
"re.match",
return_value="fake_commits_info"
)
retval = check_commit(checks)
retval = check_commit_msg(checks)
assert retval == PASS
assert m_get_commits_info.call_count == 0
assert m_re_match.call_count == 0
Expand All @@ -78,7 +78,7 @@ def test_check_commit_with_len0_regex(self, mocker, capfd):
"re.match",
return_value="fake_rematch_resp"
)
retval = check_commit(checks)
retval = check_commit_msg(checks)
assert retval == PASS
assert m_get_commits_info.call_count == 0
assert m_re_match.call_count == 0
Expand Down Expand Up @@ -107,7 +107,7 @@ def test_check_commit_with_result_none(self, mocker):
m_print_suggestion = mocker.patch(
f"{LOCATION}.print_suggestion"
)
retval = check_commit(checks)
retval = check_commit_msg(checks)
assert retval == FAIL
assert m_get_commits_info.call_count == 1
assert m_re_match.call_count == 1
Expand Down
8 changes: 4 additions & 4 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_main(
]
}
)
m_check_commit = mocker.patch("commit_check.commit.check_commit")
m_check_commit = mocker.patch("commit_check.commit.check_commit_msg")
m_check_branch = mocker.patch(
"commit_check.branch.check_branch"
)
Expand All @@ -63,7 +63,7 @@ def test_main_help(self, mocker, capfd):
]
}
)
m_check_commit = mocker.patch("commit_check.commit.check_commit")
m_check_commit = mocker.patch("commit_check.commit.check_commit_msg")
m_check_branch = mocker.patch(
"commit_check.branch.check_branch"
)
Expand All @@ -88,7 +88,7 @@ def test_main_version(self, mocker):
]
}
)
m_check_commit = mocker.patch("commit_check.commit.check_commit")
m_check_commit = mocker.patch("commit_check.commit.check_commit_msg")
m_check_branch = mocker.patch(
"commit_check.branch.check_branch"
)
Expand All @@ -107,7 +107,7 @@ def test_main_validate_config_ret_none(self, mocker):
"commit_check.main.validate_config",
return_value={}
)
m_check_commit = mocker.patch("commit_check.commit.check_commit")
m_check_commit = mocker.patch("commit_check.commit.check_commit_msg")
mocker.patch(
"commit_check.branch.check_branch"
)
Expand Down
2 changes: 1 addition & 1 deletion tests/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def test_print_suggestion(self, capfd):
# Must print on stdout with given argument.
print_suggestion("dummy suggest")
stdout, _ = capfd.readouterr()
assert "Suggest to run" in stdout
assert "Suggest:" in stdout

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