From c286dcd73d6eb50a42d033a0b409a9dd679bda92 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Mon, 7 Jul 2025 00:28:26 +0300 Subject: [PATCH 1/5] fix: check if there are commit in the current branch --- commit_check/util.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/commit_check/util.py b/commit_check/util.py index a63d6c1..f41960a 100644 --- a/commit_check/util.py +++ b/commit_check/util.py @@ -29,6 +29,17 @@ 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: + commands = ['git', 'rev-parse', '--verify', 'HEAD'] + cmd_output(commands) + return True + except CalledProcessError: + return False + def get_commit_info(format_string: str, sha: str = "HEAD") -> str: """Get latest commits information :param format_string: could be @@ -41,6 +52,8 @@ def get_commit_info(format_string: str, sha: str = "HEAD") -> str: :returns: A `str`. """ + if has_commits() is False: + return '' try: commands = [ 'git', 'log', '-n', '1', f"--pretty=format:%{format_string}", f"{sha}", From 35abe6630aa49bdb2b6610e98499c2bbe4aa4878 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Mon, 7 Jul 2025 00:48:35 +0300 Subject: [PATCH 2/5] fix: check if there are commit in the current branch --- .pre-commit-config.yaml | 4 ++-- commit_check/util.py | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cf938ad..d289da2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/commit_check/util.py b/commit_check/util.py index f41960a..e7758d5 100644 --- a/commit_check/util.py +++ b/commit_check/util.py @@ -34,10 +34,14 @@ def has_commits() -> bool: :returns: `True` if there are commits, `False` otherwise. """ try: - commands = ['git', 'rev-parse', '--verify', 'HEAD'] - cmd_output(commands) + subprocess.run( + ["git", "rev-parse", "--verify", "HEAD"], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + check=True + ) return True - except CalledProcessError: + except subprocess.CalledProcessError: return False def get_commit_info(format_string: str, sha: str = "HEAD") -> str: From 59f9b6f8e7db354b81fc112b343c24e99753b7aa Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Mon, 7 Jul 2025 00:51:13 +0300 Subject: [PATCH 3/5] add output when no commits found --- commit_check/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commit_check/util.py b/commit_check/util.py index e7758d5..52a8d70 100644 --- a/commit_check/util.py +++ b/commit_check/util.py @@ -57,7 +57,7 @@ def get_commit_info(format_string: str, sha: str = "HEAD") -> str: :returns: A `str`. """ if has_commits() is False: - return '' + return 'No commits found.' try: commands = [ 'git', 'log', '-n', '1', f"--pretty=format:%{format_string}", f"{sha}", From 148dc32e657486f15f5112bb823289adc009bd25 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Mon, 7 Jul 2025 00:53:16 +0300 Subject: [PATCH 4/5] docs: change output when no commits found --- commit_check/util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commit_check/util.py b/commit_check/util.py index 52a8d70..52b4eaa 100644 --- a/commit_check/util.py +++ b/commit_check/util.py @@ -57,7 +57,7 @@ def get_commit_info(format_string: str, sha: str = "HEAD") -> str: :returns: A `str`. """ if has_commits() is False: - return 'No commits found.' + return 'Repo has no commits yet.' try: commands = [ 'git', 'log', '-n', '1', f"--pretty=format:%{format_string}", f"{sha}", From b22b53f5c18b404dd1eb91fe8d1dab972c3fa606 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Mon, 7 Jul 2025 01:01:29 +0300 Subject: [PATCH 5/5] test: add more test for util --- tests/util_test.py | 66 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/tests/util_test.py b/tests/util_test.py index 42870ea..e42ba24 100644 --- a/tests/util_test.py +++ b/tests/util_test.py @@ -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 @@ -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 @@ -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 " @@ -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" 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