@@ -48,6 +48,9 @@ class GitBase:
4848 # overview of the naming scheme, see man git check-ref-format
4949 _branch_pattern = re .compile (r"^\s*([^\s~\^\:\?\*\[]+[^.])\s*$" )
5050
51+ # Text returned if in a detached head
52+ detached_head_reference = "detched_head_state"
53+
5154 def __init__ (self , parent = None , repo = None ):
5255 if repo is None :
5356 self ._repo = None
@@ -64,7 +67,13 @@ def get_branch_name(self):
6467 result = m .group (1 )
6568 break
6669 else :
67- raise GitBDiffError ("unable to get branch name" )
70+ # Check for being in a Detached Head state
71+ for line in self .run_git (["branch" ]):
72+ if "HEAD detached" in line :
73+ result = self .detached_head_reference
74+ break
75+ else :
76+ raise GitBDiffError ("unable to get branch name" )
6877 return result
6978
7079 def run_git (self , args ):
@@ -111,6 +120,8 @@ def __init__(self, parent=None, repo=None):
111120 self .ancestor = self .get_branch_point ()
112121 self .current = self .get_latest_commit ()
113122 self .branch = self .get_branch_name ()
123+ if self .branch == self .detached_head_reference :
124+ raise GitBDiffError ("Can't get a diff for a repo in detached head state" )
114125
115126 def get_branch_point (self ):
116127 """Get the branch point from the parent repo.
@@ -175,9 +186,10 @@ def __init__(self, repo=None):
175186 def is_main (self ):
176187 """
177188 Returns true if branch matches a main-like branch name as defined below
189+ Count detached head as main-like as we cannot get a diff for this
178190 """
179191
180- main_like = ("main" , "stable" , "trunk" , "master" )
192+ main_like = ("main" , "stable" , "trunk" , "master" , self . detached_head_reference )
181193 if self .branch in main_like :
182194 return True
183195 return False
0 commit comments