fix: prompt getting git branch (#4607)

fixes #4450
This commit is contained in:
Noorhteen Raja NJ 2021-12-21 17:28:02 +05:30 committed by GitHub
parent 5091c8368e
commit c524028a78
Failed to generate hash of commit
2 changed files with 36 additions and 8 deletions

View file

@ -0,0 +1,23 @@
**Added:**
* <news item>
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* prompt field ``current_branch`` will now work empty git repository.
**Security:**
* <news item>

View file

@ -1,6 +1,6 @@
"""Prompt formatter for simple version control branches"""
# pylint:disable=no-member, invalid-name
import contextlib
import os
import pathlib
import queue
@ -29,13 +29,18 @@ def _run_git_cmd(cmd):
def _get_git_branch(q):
try:
cmd = ["git", "rev-parse", "--abbrev-ref", "HEAD"]
branch = xt.decode_bytes(_run_git_cmd(cmd))
except (subprocess.CalledProcessError, OSError):
q.put(None)
else:
q.put(branch.splitlines()[0] if branch else None)
# from https://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html
for cmds in [
"git symbolic-ref --short HEAD",
"git show-ref --head -s --abbrev", # in detached mode return sha1
]:
with contextlib.suppress(subprocess.CalledProcessError, OSError):
branch = xt.decode_bytes(_run_git_cmd(cmds.split()))
if branch:
q.put(os.path.basename(branch.splitlines()[0]))
return
# all failed
q.put(None)
def get_git_branch():