Skip to content

Commit cf9d245

Browse files
authored
fix: use git branch --show-current to get branch name (#252)
* fix: use git branch --show-current to get branch name * fix: set branch name to HEAD if empty
1 parent 54738e6 commit cf9d245

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

commit_check/util.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ def get_branch_name() -> str:
2222
:returns: A `str` describing the current branch name.
2323
"""
2424
try:
25-
commands = ['git', 'rev-parse', '--abbrev-ref', 'HEAD']
26-
branch_name = cmd_output(commands)
25+
# Git 2.22 and above supports `git branch --show-current`
26+
commands = ['git', 'branch', '--show-current']
27+
branch_name = cmd_output(commands) or "HEAD"
2728
except CalledProcessError:
2829
branch_name = ''
2930
return branch_name.strip()

tests/util_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_get_branch_name(self, mocker):
2525
retval = get_branch_name()
2626
assert m_cmd_output.call_count == 1
2727
assert m_cmd_output.call_args[0][0] == [
28-
"git", "rev-parse", "--abbrev-ref", "HEAD"
28+
"git", "branch", "--show-current"
2929
]
3030
assert retval == "fake_branch_name"
3131

@@ -45,7 +45,7 @@ def test_get_branch_name_with_exception(self, mocker):
4545
retval = get_branch_name()
4646
assert m_cmd_output.call_count == 1
4747
assert m_cmd_output.call_args[0][0] == [
48-
"git", "rev-parse", "--abbrev-ref", "HEAD"
48+
"git", "branch", "--show-current"
4949
]
5050
assert retval == ""
5151

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