Skip to content

fix: make sure git repo has commit before checking #253

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 4 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion commit_check/author.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Check git author name and email"""
import re
from commit_check import YELLOW, RESET_COLOR, PASS, FAIL
from commit_check.util import get_commit_info, print_error_header, print_error_message, print_suggestion
from commit_check.util import get_commit_info, has_commits, print_error_header, print_error_message, print_suggestion


def check_author(checks: list, check_type: str) -> int:
if has_commits() is False:
return PASS # pragma: no cover

for check in checks:
if check['check'] == check_type:
if check['regex'] == "":
Expand Down
5 changes: 4 additions & 1 deletion commit_check/branch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Check git branch naming convention."""
import re
from commit_check import YELLOW, RESET_COLOR, PASS, FAIL
from commit_check.util import get_branch_name, git_merge_base, print_error_header, print_error_message, print_suggestion
from commit_check.util import get_branch_name, git_merge_base, print_error_header, print_error_message, print_suggestion, has_commits


def check_branch(checks: list) -> int:
Expand Down Expand Up @@ -33,6 +33,9 @@ def check_merge_base(checks: list) -> int:

:returns PASS(0) if merge base check succeeds, FAIL(1) otherwise
"""
if has_commits() is False:
return PASS # pragma: no cover

for check in checks:
if check['check'] == 'merge_base':
if check['regex'] == "":
Expand Down
9 changes: 8 additions & 1 deletion commit_check/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
from pathlib import PurePath
from commit_check import YELLOW, RESET_COLOR, PASS, FAIL
from commit_check.util import cmd_output, get_commit_info, print_error_header, print_error_message, print_suggestion
from commit_check.util import cmd_output, get_commit_info, print_error_header, print_error_message, print_suggestion, has_commits


def get_default_commit_msg_file() -> str:
Expand All @@ -22,6 +22,10 @@ def read_commit_msg(commit_msg_file) -> str:


def check_commit_msg(checks: list, commit_msg_file: str = "") -> int:
"""Check commit message against the provided checks."""
if has_commits() is False:
return PASS # pragma: no cover

if commit_msg_file is None or commit_msg_file == "":
commit_msg_file = get_default_commit_msg_file()

Expand Down Expand Up @@ -51,6 +55,9 @@ def check_commit_msg(checks: list, commit_msg_file: str = "") -> int:


def check_commit_signoff(checks: list, commit_msg_file: str = "") -> int:
if has_commits() is False:
return PASS # pragma: no cover

if commit_msg_file is None or commit_msg_file == "":
commit_msg_file = get_default_commit_msg_file()

Expand Down
2 changes: 0 additions & 2 deletions commit_check/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ def get_commit_info(format_string: str, sha: str = "HEAD") -> str:

:returns: A `str`.
"""
if has_commits() is False:
return 'Repo has no commits yet.'
try:
commands = [
'git', 'log', '-n', '1', f"--pretty=format:%{format_string}", f"{sha}",
Expand Down
15 changes: 6 additions & 9 deletions tests/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class TestGetCommitInfo:
)
def test_get_commit_info(self, mocker, format_string):
# Must call get_commit_info with given argument when there are commits.
m_has_commits = mocker.patch(
mocker.patch(
"commit_check.util.has_commits",
return_value=True
)
Expand All @@ -132,7 +132,6 @@ def test_get_commit_info(self, mocker, format_string):
return_value=" fake commit message "
)
retval = get_commit_info(format_string)
assert m_has_commits.call_count == 1
assert m_cmd_output.call_count == 1
assert m_cmd_output.call_args[0][0] == [
"git", "log", "-n", "1", f"--pretty=format:%{format_string}", "HEAD"
Expand All @@ -142,24 +141,23 @@ def test_get_commit_info(self, mocker, format_string):
@pytest.mark.benchmark
def test_get_commit_info_no_commits(self, mocker):
# Must return 'Repo has no commits yet.' when there are no commits.
m_has_commits = mocker.patch(
mocker.patch(
"commit_check.util.has_commits",
return_value=False
)
m_cmd_output = mocker.patch(
mocker.patch(
"commit_check.util.cmd_output",
return_value=" fake commit message "
)
format_string = "s"
retval = get_commit_info(format_string)
assert m_has_commits.call_count == 1
assert m_cmd_output.call_count == 0 # Should not call cmd_output
assert retval == "Repo has no commits yet."
assert retval == " fake commit message "


@pytest.mark.benchmark
def test_get_commit_info_with_exception(self, mocker):
# Must return empty string when exception raises in cmd_output.
m_has_commits = mocker.patch(
mocker.patch(
"commit_check.util.has_commits",
return_value=True
)
Expand All @@ -175,7 +173,6 @@ def test_get_commit_info_with_exception(self, mocker):
)
format_string = "s"
retval = get_commit_info(format_string)
assert m_has_commits.call_count == 1
assert m_cmd_output.call_count == 1
assert m_cmd_output.call_args[0][0] == [
"git", "log", "-n", "1", f"--pretty=format:%{format_string}", "HEAD"
Expand Down
Loading