@@ -249,6 +249,32 @@ func (self *RefreshHelper) refreshCommits() {
249249 wg .Wait ()
250250}
251251
252+ func (self * RefreshHelper ) determineCheckoutBranchName () string {
253+ if rebasedBranch := self .c .Git ().Status .BranchBeingRebased (); rebasedBranch != "" {
254+ // During a rebase we're on a detached head, so cannot determine the
255+ // branch name in the usual way. We need to read it from the
256+ // ".git/rebase-merge/head-name" file instead.
257+ return strings .TrimPrefix (rebasedBranch , "refs/heads/" )
258+ }
259+
260+ if bisectedBranch := self .c .Git ().Bisect .GetInfo ().GetStartSha (); bisectedBranch != "" {
261+ // Likewise, when we're bisecting we're on a detached head as well. In
262+ // this case we read the branch name from the ".git/BISECT_START" file.
263+ return bisectedBranch
264+ }
265+
266+ // In all other cases, get the branch name by asking git what branch is
267+ // checked out. Note that if we're on a detached head (for reasons other
268+ // than rebasing or bisecting, i.e. it was explicitly checked out), then
269+ // this will return its sha.
270+ if branchInfo , err := self .c .Git ().Branch .CurrentBranchInfo (); err == nil {
271+ return branchInfo .RefName
272+ }
273+
274+ // Should never get here unless the working copy is corrupt
275+ return ""
276+ }
277+
252278func (self * RefreshHelper ) refreshCommitsWithLimit () error {
253279 self .c .Mutexes ().LocalCommitsMutex .Lock ()
254280 defer self .c .Mutexes ().LocalCommitsMutex .Unlock ()
@@ -267,7 +293,7 @@ func (self *RefreshHelper) refreshCommitsWithLimit() error {
267293 }
268294 self .c .Model ().Commits = commits
269295 self .c .Model ().WorkingTreeStateAtLastCommitRefresh = self .c .Git ().Status .WorkingTreeState ()
270- self .c .Model ().BranchBeingRebased = self .c . Git (). Status . BranchBeingRebased ()
296+ self .c .Model ().CheckedOutBranch = self .determineCheckoutBranchName ()
271297
272298 return self .c .PostRefreshUpdate (self .c .Contexts ().LocalCommits )
273299}
0 commit comments