From 89249ec38cea1b9a168b69751b94ba45b7766ab7 Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Wed, 9 Jul 2025 23:32:41 +0300 Subject: [PATCH 1/2] fix: use git branch --show-current to get branch name --- commit_check/util.py | 3 ++- tests/util_test.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/commit_check/util.py b/commit_check/util.py index 52b4eaa..5e59456 100644 --- a/commit_check/util.py +++ b/commit_check/util.py @@ -22,7 +22,8 @@ def get_branch_name() -> str: :returns: A `str` describing the current branch name. """ try: - commands = ['git', 'rev-parse', '--abbrev-ref', 'HEAD'] + # Git 2.22 and above supports `git branch --show-current` + commands = ['git', 'branch', '--show-current'] branch_name = cmd_output(commands) except CalledProcessError: branch_name = '' diff --git a/tests/util_test.py b/tests/util_test.py index c426831..a9d9f51 100644 --- a/tests/util_test.py +++ b/tests/util_test.py @@ -25,7 +25,7 @@ def test_get_branch_name(self, mocker): retval = get_branch_name() assert m_cmd_output.call_count == 1 assert m_cmd_output.call_args[0][0] == [ - "git", "rev-parse", "--abbrev-ref", "HEAD" + "git", "branch", "--show-current" ] assert retval == "fake_branch_name" @@ -45,7 +45,7 @@ def test_get_branch_name_with_exception(self, mocker): retval = get_branch_name() assert m_cmd_output.call_count == 1 assert m_cmd_output.call_args[0][0] == [ - "git", "rev-parse", "--abbrev-ref", "HEAD" + "git", "branch", "--show-current" ] assert retval == "" From 88ba1e409f8a0b8c4d2c3870831c07b26154ca2c Mon Sep 17 00:00:00 2001 From: shenxianpeng Date: Wed, 9 Jul 2025 23:48:17 +0300 Subject: [PATCH 2/2] fix: set branch name to HEAD if empty --- 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 5e59456..fe8211f 100644 --- a/commit_check/util.py +++ b/commit_check/util.py @@ -24,7 +24,7 @@ def get_branch_name() -> str: try: # Git 2.22 and above supports `git branch --show-current` commands = ['git', 'branch', '--show-current'] - branch_name = cmd_output(commands) + branch_name = cmd_output(commands) or "HEAD" except CalledProcessError: branch_name = '' return branch_name.strip() 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