shows commit SHA as curr_branch if HEAD detached

This behaviour was broken in fixing getting git branch name for
short statuses by using rev-parse instead.

This commit changes the method again, to use `git branch` which
shows detached HEADs in the same format as `git status --long`,
but is faster as discussed in #1823.
This commit is contained in:
Ollie Ford 2016-10-09 23:39:24 +01:00
parent 3159d41fa7
commit 8d99128b98
Failed to generate hash of commit

View file

@ -15,17 +15,25 @@ import xonsh.tools as xt
def _get_git_branch(q):
try:
branch = subprocess.check_output(
['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
branches = xt.decode_bytes(subprocess.check_output(
['git', 'branch'],
stderr=subprocess.DEVNULL
)
)).splitlines()
except (subprocess.CalledProcessError, OSError):
q.put(None)
else:
info = xt.decode_bytes(branch)
branch = info.splitlines()[0].split()[-1]
q.put(branch)
for branch in branches:
if not branch.startswith('* '):
continue
elif branch.endswith(')'):
branch = branch.split()[-1][:-1]
else:
branch = branch.split()[-1]
q.put(branch)
break
else:
q.put(None)
def get_git_branch():
"""Attempts to find the current git branch. If this could not