Use plumbing rev-parse to get the branch name

Git plumbing command rev-parse is able to return the current branch
symbolic name in a machine readable manner and will return the rev
inputted (HEAD in this case) if no corresponding branch can be found
(detached HEAD).

This works on all versions of git since 1.6.3 and alternatively can be
replaced with 'git symbolic-ref --short -q HEAD' on versions 1.7.10 and
newer.

Change-Id: I486dcad4b73f9f06d7196e6888a4da79c7d574ad
This commit is contained in:
Darragh Bailey 2015-05-04 21:22:36 +01:00 committed by Darragh Bailey
parent a13b7a9d77
commit f78f328b17
1 changed files with 4 additions and 10 deletions

View File

@ -811,16 +811,10 @@ def get_branch_name(target_branch):
global _branch_name
if _branch_name is not None:
return _branch_name
_branch_name = None
cmd = "git branch"
has_color = check_color_support()
if has_color:
cmd += " --color=never"
for branch in run_command(cmd).split("\n"):
if branch.startswith('*'):
_branch_name = branch.split()[1].strip()
break
if _branch_name == "(no" or _branch_name == "(detached":
cmd = "git rev-parse --symbolic-full-name --abbrev-ref HEAD"
_branch_name = run_command(cmd)
if _branch_name == "HEAD":
# detached head or no branch found
_branch_name = target_branch
return _branch_name