@@ -412,15 +412,14 @@ def branch_heads(self, current_path):
412412 if is_current_branch :
413413 current_branch = branch
414414
415- # Remote branch is seleted use 'git branch -a' as fallback machanism
416- # to get add detached head on remote branch to preserve older functionality
417- # TODO : Revisit this to checkout new local branch with same name as remote
418- # when the remote branch is seleted, VS Code git does the same thing.
419- if not current_branch and self .get_current_branch (current_path ) == "HEAD" :
415+ # Above can fail in certain cases, such as an empty repo with
416+ # no commits. In that case, just fall back to determining
417+ # current branch
418+ if not current_branch :
420419 branch = {
421420 "is_current_branch" : True ,
422421 "is_remote_branch" : False ,
423- "name" : self ._get_detached_head_name (current_path ),
422+ "name" : self .get_current_branch (current_path ),
424423 "upstream" : None ,
425424 "top_commit" : None ,
426425 "tag" : None ,
@@ -771,18 +770,6 @@ def init(self, current_path):
771770 )
772771 return my_output
773772
774- def _is_branch (self , reference_name ):
775- """Check if the given reference is a branch
776- """
777- return reference_name .startswith ("refs/heads/" ) or reference_name .startswith (
778- "refs/remotes/"
779- )
780-
781- def _is_current_branch (self , branch_name , current_branch_name ):
782- """Check if given branch is current branch
783- """
784- return branch_name == current_branch_name
785-
786773 def _is_remote_branch (self , branch_reference ):
787774 """Check if given branch is remote branch by comparing with 'remotes/',
788775 TODO : Consider a better way to check remote branch
@@ -800,10 +787,12 @@ def _get_branch_name(self, branch_reference):
800787 raise ValueError ("Reference [{}] is not a valid branch." , branch_reference )
801788
802789 def get_current_branch (self , current_path ):
803- """Execute 'git rev-parse --abbrev-ref HEAD' to
804- check if given branch is current branch
790+ """Use `symbolic-ref` to get the current branch name. In case of
791+ failure, assume that the HEAD is currently detached, and fall back
792+ to the `branch` command to get the name.
793+ See https://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html
805794 """
806- command = ["git" , "rev-parse" , "--abbrev -ref" , "HEAD" ]
795+ command = ["git" , "symbolic -ref" , "HEAD" ]
807796 p = subprocess .Popen (
808797 command ,
809798 stdout = PIPE ,
@@ -812,15 +801,17 @@ def get_current_branch(self, current_path):
812801 )
813802 output , error = p .communicate ()
814803 if p .returncode == 0 :
815- return output .decode ("utf-8" ).strip ()
804+ return output .decode ("utf-8" ).split ('/' )[- 1 ].strip ()
805+ elif "not a symbolic ref" in error .decode ("utf-8" ):
806+ return self ._get_current_branch_detached (current_path )
816807 else :
817808 raise Exception (
818809 "Error [{}] occurred while executing [{}] command to get current branch." .format (
819810 error .decode ("utf-8" ), " " .join (command )
820811 )
821812 )
822813
823- def _get_detached_head_name (self , current_path ):
814+ def _get_current_branch_detached (self , current_path ):
824815 """Execute 'git branch -a' to get current branch details in case of detached HEAD
825816 """
826817 command = ["git" , "branch" , "-a" ]
0 commit comments