@@ -99,6 +99,13 @@ def topdir(self) -> Optional[str]:
9999
100100 return path
101101
102+ @property
103+ def git_dir (self ) -> Optional [str ]:
104+ try :
105+ return self ._run_git (["rev-parse" , "--git-dir" ])
106+ except subprocess .CalledProcessError :
107+ return None
108+
102109 def init (self , * , initial_branch : Optional [str ] = None , quiet : bool = True , mute_stderr : bool = False ):
103110 cmd = ["init" ]
104111 if initial_branch :
@@ -131,9 +138,29 @@ def clone(self,
131138 @property
132139 def current_branch (self ) -> Optional [str ]:
133140 try :
134- return self ._run_git (["branch" , "--show-current" ], mute_stderr = True )
141+ result = self ._run_git (["branch" , "--show-current" ], mute_stderr = True )
135142 except subprocess .CalledProcessError :
136- return None
143+ result = None
144+
145+ if not result :
146+ # try to determine the branch during rebase
147+ git_dir = self .git_dir
148+ if git_dir :
149+ paths = [
150+ os .path .join (git_dir , "rebase-apply" , "head-name" ),
151+ os .path .join (git_dir , "rebase-merge" , "head-name" ),
152+ ]
153+ for path in paths :
154+ try :
155+ with open (path , "r" , encoding = "utf-8" ) as f :
156+ line = f .readline ()
157+ # parse "refs/heads/<branch>"
158+ result = line .strip ().split ("/" , 2 )[- 1 ]
159+ break
160+ except FileNotFoundError :
161+ pass
162+
163+ return result
137164
138165 def branch (self , branch : str , set_upstream_to : Optional [str ] = None ):
139166 cmd = ["branch" ]
0 commit comments