Browse Source

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
tags/1.0.0
Darragh Bailey 4 years ago
parent
commit
f78f328b17
1 changed files with 4 additions and 10 deletions
  1. 4
    10
      git_review/cmd.py

+ 4
- 10
git_review/cmd.py View File

@@ -811,16 +811,10 @@ def get_branch_name(target_branch):
811 811
     global _branch_name
812 812
     if _branch_name is not None:
813 813
         return _branch_name
814
-    _branch_name = None
815
-    cmd = "git branch"
816
-    has_color = check_color_support()
817
-    if has_color:
818
-        cmd += " --color=never"
819
-    for branch in run_command(cmd).split("\n"):
820
-        if branch.startswith('*'):
821
-            _branch_name = branch.split()[1].strip()
822
-            break
823
-    if _branch_name == "(no" or _branch_name == "(detached":
814
+    cmd = "git rev-parse --symbolic-full-name --abbrev-ref HEAD"
815
+    _branch_name = run_command(cmd)
816
+    if _branch_name == "HEAD":
817
+        # detached head or no branch found
824 818
         _branch_name = target_branch
825 819
     return _branch_name
826 820
 

Loading…
Cancel
Save