diff --git a/.commit-check.yml b/.commit-check.yml index 8c25742..853c91b 100644 --- a/.commit-check.yml +++ b/.commit-check.yml @@ -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` diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 58687d8..e7c5825 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 90d4277..7327dbd 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -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 diff --git a/commit_check/__init__.py b/commit_check/__init__.py index c24c029..6abc60a 100644 --- a/commit_check/__init__.py +++ b/commit_check/__init__.py @@ -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`', }, ], } diff --git a/commit_check/commit.py b/commit_check/commit.py index 2699879..3ca2193 100644 --- a/commit_check/commit.py +++ b/commit_check/commit.py @@ -1,10 +1,12 @@ """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'] == "": @@ -12,12 +14,19 @@ def check_commit(checks: list) -> int: 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']) diff --git a/commit_check/main.py b/commit_check/main.py index d21ecde..8c4ec84 100644 --- a/commit_check/main.py +++ b/commit_check/main.py @@ -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 @@ -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: diff --git a/commit_check/util.py b/commit_check/util.py index ef0012e..4dfb0ad 100644 --- a/commit_check/util.py +++ b/commit_check/util.py @@ -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.") diff --git a/tests/commit_test.py b/tests/commit_test.py index 12437d2..c92d7c9 100644 --- a/tests/commit_test.py +++ b/tests/commit_test.py @@ -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" @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/tests/main_test.py b/tests/main_test.py index d237703..ed9b78c 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -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" ) @@ -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" ) @@ -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" ) @@ -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" ) diff --git a/tests/util_test.py b/tests/util_test.py index 719baf9..4e5f417 100644 --- a/tests/util_test.py +++ b/tests/util_test.py @@ -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