Skip to content

Commit 7544de0

Browse files
authored
Give better visual feedback when checking out the previous branch (#4929)
I find the command "Checkout previous branch" quite useful, and I use it a lot. However, I'm unhappy with the visual feedback, so this PR improves that a bit. Previously, the feedback you got when pressing "-" was just a "Checking out..." status in the bottom line. This was both easy to miss if you are used to looking for an inline status in the branches panel, and it didn't provide information about which branch was being checked out, which can be annoying in very large repos where checking out takes a while, and you only see at the end if you are now on the right branch. Improve this by trying to figure out which branch was the previously checked out one, and then checking it out normally so that you get an inline status next to it (as if you had pressed space on it). There are cases where this fails, e.g. when the previously checked out ref was a detached head, in which case we fall back to the previous behavior.
2 parents ba0e2d1 + e585acc commit 7544de0

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

pkg/commands/git_commands/branch.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ func (self *BranchCommands) CurrentBranchName() (string, error) {
116116
return strings.TrimSpace(output), nil
117117
}
118118

119+
// Gets the full ref name of the previously checked out branch. Can return an empty string (but no
120+
// error) e.g. when the previously checked out thing was a detached head.
121+
func (self *BranchCommands) PreviousRef() (string, error) {
122+
cmdArgs := NewGitCmd("rev-parse").
123+
Arg("--symbolic-full-name").
124+
Arg("@{-1}").
125+
ToArgv()
126+
127+
output, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
128+
if err != nil {
129+
return "", err
130+
}
131+
132+
return strings.TrimSpace(output), nil
133+
}
134+
119135
// LocalDelete delete branch locally
120136
func (self *BranchCommands) LocalDelete(branches []string, force bool) error {
121137
cmdArgs := NewGitCmd("branch").

pkg/gui/controllers/branches_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ func (self *BranchesController) forceCheckout() error {
490490

491491
func (self *BranchesController) checkoutPreviousBranch() error {
492492
self.c.LogAction(self.c.Tr.Actions.CheckoutBranch)
493-
return self.c.Helpers().Refs.CheckoutRef("-", types.CheckoutRefOptions{})
493+
return self.c.Helpers().Refs.CheckoutPreviousRef()
494494
}
495495

496496
func (self *BranchesController) checkoutByName() error {

pkg/gui/controllers/helpers/refs_helper.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ func (self *RefsHelper) CheckoutRemoteBranch(fullBranchName string, localBranchN
163163
})
164164
}
165165

166+
func (self *RefsHelper) CheckoutPreviousRef() error {
167+
previousRef, err := self.c.Git().Branch.PreviousRef()
168+
if err == nil && strings.HasPrefix(previousRef, "refs/heads/") {
169+
return self.CheckoutRef(strings.TrimPrefix(previousRef, "refs/heads/"), types.CheckoutRefOptions{})
170+
}
171+
172+
return self.CheckoutRef("-", types.CheckoutRefOptions{})
173+
}
174+
166175
func (self *RefsHelper) GetCheckedOutRef() *models.Branch {
167176
if len(self.c.Model().Branches) == 0 {
168177
return nil

0 commit comments

Comments
 (0)