Skip to content

Commit 55b000f

Browse files
authored
fix: make sure git repo has commit before checking (#253)
* fix: make sure git repo has commit before checking * chore: remove print * chore: add # pragma: no cover * chore: add # pragma: no cover
1 parent cf9d245 commit 55b000f

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

commit_check/author.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
"""Check git author name and email"""
22
import re
33
from commit_check import YELLOW, RESET_COLOR, PASS, FAIL
4-
from commit_check.util import get_commit_info, print_error_header, print_error_message, print_suggestion
4+
from commit_check.util import get_commit_info, has_commits, print_error_header, print_error_message, print_suggestion
55

66

77
def check_author(checks: list, check_type: str) -> int:
8+
if has_commits() is False:
9+
return PASS # pragma: no cover
10+
811
for check in checks:
912
if check['check'] == check_type:
1013
if check['regex'] == "":

commit_check/branch.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Check git branch naming convention."""
22
import re
33
from commit_check import YELLOW, RESET_COLOR, PASS, FAIL
4-
from commit_check.util import get_branch_name, git_merge_base, print_error_header, print_error_message, print_suggestion
4+
from commit_check.util import get_branch_name, git_merge_base, print_error_header, print_error_message, print_suggestion, has_commits
55

66

77
def check_branch(checks: list) -> int:
@@ -33,6 +33,9 @@ def check_merge_base(checks: list) -> int:
3333
3434
:returns PASS(0) if merge base check succeeds, FAIL(1) otherwise
3535
"""
36+
if has_commits() is False:
37+
return PASS # pragma: no cover
38+
3639
for check in checks:
3740
if check['check'] == 'merge_base':
3841
if check['regex'] == "":

commit_check/commit.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import re
33
from pathlib import PurePath
44
from commit_check import YELLOW, RESET_COLOR, PASS, FAIL
5-
from commit_check.util import cmd_output, get_commit_info, print_error_header, print_error_message, print_suggestion
5+
from commit_check.util import cmd_output, get_commit_info, print_error_header, print_error_message, print_suggestion, has_commits
66

77

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

2323

2424
def check_commit_msg(checks: list, commit_msg_file: str = "") -> int:
25+
"""Check commit message against the provided checks."""
26+
if has_commits() is False:
27+
return PASS # pragma: no cover
28+
2529
if commit_msg_file is None or commit_msg_file == "":
2630
commit_msg_file = get_default_commit_msg_file()
2731

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

5256

5357
def check_commit_signoff(checks: list, commit_msg_file: str = "") -> int:
58+
if has_commits() is False:
59+
return PASS # pragma: no cover
60+
5461
if commit_msg_file is None or commit_msg_file == "":
5562
commit_msg_file = get_default_commit_msg_file()
5663

commit_check/util.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ def get_commit_info(format_string: str, sha: str = "HEAD") -> str:
5757
5858
:returns: A `str`.
5959
"""
60-
if has_commits() is False:
61-
return 'Repo has no commits yet.'
6260
try:
6361
commands = [
6462
'git', 'log', '-n', '1', f"--pretty=format:%{format_string}", f"{sha}",

tests/util_test.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class TestGetCommitInfo:
123123
)
124124
def test_get_commit_info(self, mocker, format_string):
125125
# Must call get_commit_info with given argument when there are commits.
126-
m_has_commits = mocker.patch(
126+
mocker.patch(
127127
"commit_check.util.has_commits",
128128
return_value=True
129129
)
@@ -132,7 +132,6 @@ def test_get_commit_info(self, mocker, format_string):
132132
return_value=" fake commit message "
133133
)
134134
retval = get_commit_info(format_string)
135-
assert m_has_commits.call_count == 1
136135
assert m_cmd_output.call_count == 1
137136
assert m_cmd_output.call_args[0][0] == [
138137
"git", "log", "-n", "1", f"--pretty=format:%{format_string}", "HEAD"
@@ -142,24 +141,23 @@ def test_get_commit_info(self, mocker, format_string):
142141
@pytest.mark.benchmark
143142
def test_get_commit_info_no_commits(self, mocker):
144143
# Must return 'Repo has no commits yet.' when there are no commits.
145-
m_has_commits = mocker.patch(
144+
mocker.patch(
146145
"commit_check.util.has_commits",
147146
return_value=False
148147
)
149-
m_cmd_output = mocker.patch(
148+
mocker.patch(
150149
"commit_check.util.cmd_output",
151150
return_value=" fake commit message "
152151
)
153152
format_string = "s"
154153
retval = get_commit_info(format_string)
155-
assert m_has_commits.call_count == 1
156-
assert m_cmd_output.call_count == 0 # Should not call cmd_output
157-
assert retval == "Repo has no commits yet."
154+
assert retval == " fake commit message "
155+
158156

159157
@pytest.mark.benchmark
160158
def test_get_commit_info_with_exception(self, mocker):
161159
# Must return empty string when exception raises in cmd_output.
162-
m_has_commits = mocker.patch(
160+
mocker.patch(
163161
"commit_check.util.has_commits",
164162
return_value=True
165163
)
@@ -175,7 +173,6 @@ def test_get_commit_info_with_exception(self, mocker):
175173
)
176174
format_string = "s"
177175
retval = get_commit_info(format_string)
178-
assert m_has_commits.call_count == 1
179176
assert m_cmd_output.call_count == 1
180177
assert m_cmd_output.call_args[0][0] == [
181178
"git", "log", "-n", "1", f"--pretty=format:%{format_string}", "HEAD"

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy