Skip to content

fix: display a clear message when there are no commits #244

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 5 commits into from
Jul 6, 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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ repos:
# - id: check-branch # uncomment if you need.
- id: check-author-name # uncomment if you need.
- id: check-author-email # uncomment if you need.
# - id: commit-signoff # uncomment if you need.
# - id: check-merge-base # requires download all git history
# - id: check-commit-signoff # uncomment if you need.
# - id: check-merge-base # requires download all git history
17 changes: 17 additions & 0 deletions commit_check/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ def get_branch_name() -> str:
return branch_name.strip()


def has_commits() -> bool:
"""Check if there are any commits in the current branch.
:returns: `True` if there are commits, `False` otherwise.
"""
try:
subprocess.run(
["git", "rev-parse", "--verify", "HEAD"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True
)
return True
except subprocess.CalledProcessError:
return False

def get_commit_info(format_string: str, sha: str = "HEAD") -> str:
"""Get latest commits information
:param format_string: could be
Expand All @@ -41,6 +56,8 @@ 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
66 changes: 65 additions & 1 deletion tests/util_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import subprocess
from commit_check.util import get_branch_name
from commit_check.util import has_commits
from commit_check.util import git_merge_base
from commit_check.util import get_commit_info
from commit_check.util import cmd_output
Expand Down Expand Up @@ -46,6 +47,43 @@ def test_get_branch_name_with_exception(self, mocker):
]
assert retval == ""

class TestHasCommits:
def test_has_commits_true(self, mocker):
# Must return True when git rev-parse HEAD succeeds
m_subprocess_run = mocker.patch(
"subprocess.run",
return_value=None
)
retval = has_commits()
assert m_subprocess_run.call_count == 1
assert m_subprocess_run.call_args[0][0] == [
"git", "rev-parse", "--verify", "HEAD"
]
assert m_subprocess_run.call_args[1] == {
'stdout': subprocess.DEVNULL,
'stderr': subprocess.DEVNULL,
'check': True
}
assert retval is True

def test_has_commits_false(self, mocker):
# Must return False when git rev-parse HEAD fails
m_subprocess_run = mocker.patch(
"subprocess.run",
side_effect=subprocess.CalledProcessError(128, "git rev-parse")
)
retval = has_commits()
assert m_subprocess_run.call_count == 1
assert m_subprocess_run.call_args[0][0] == [
"git", "rev-parse", "--verify", "HEAD"
]
assert m_subprocess_run.call_args[1] == {
'stdout': subprocess.DEVNULL,
'stderr': subprocess.DEVNULL,
'check': True
}
assert retval is False

class TestGitMergeBase:
@pytest.mark.parametrize("returncode,expected", [
(0, 0), # ancestor exists
Expand Down Expand Up @@ -78,20 +116,45 @@ class TestGetCommitInfo:
]
)
def test_get_commit_info(self, mocker, format_string):
# Must call get_commit_info with given argument.
# Must call get_commit_info with given argument when there are commits.
m_has_commits = mocker.patch(
"commit_check.util.has_commits",
return_value=True
)
m_cmd_output = mocker.patch(
"commit_check.util.cmd_output",
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"
]
assert retval == " fake commit message "

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(
"commit_check.util.has_commits",
return_value=False
)
m_cmd_output = 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."

def test_get_commit_info_with_exception(self, mocker):
# Must return empty string when exception raises in cmd_output.
m_has_commits = mocker.patch(
"commit_check.util.has_commits",
return_value=True
)
m_cmd_output = mocker.patch(
"commit_check.util.cmd_output",
return_value=" fake commit message "
Expand All @@ -104,6 +167,7 @@ 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
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