Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/commands/git_commands/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ func (self *BranchCommands) CurrentBranchName() (string, error) {
return strings.TrimSpace(output), nil
}

// Gets the full ref name of the previously checked out branch. Can return an empty string (but no
// error) e.g. when the previously checked out thing was a detached head.
func (self *BranchCommands) PreviousRef() (string, error) {
cmdArgs := NewGitCmd("rev-parse").
Arg("--symbolic-full-name").
Arg("@{-1}").
ToArgv()

output, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
if err != nil {
return "", err
}

return strings.TrimSpace(output), nil
}

// LocalDelete delete branch locally
func (self *BranchCommands) LocalDelete(branches []string, force bool) error {
cmdArgs := NewGitCmd("branch").
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/controllers/branches_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func (self *BranchesController) forceCheckout() error {

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

func (self *BranchesController) checkoutByName() error {
Expand Down
9 changes: 9 additions & 0 deletions pkg/gui/controllers/helpers/refs_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ func (self *RefsHelper) CheckoutRemoteBranch(fullBranchName string, localBranchN
})
}

func (self *RefsHelper) CheckoutPreviousRef() error {
previousRef, err := self.c.Git().Branch.PreviousRef()
if err == nil && strings.HasPrefix(previousRef, "refs/heads/") {
return self.CheckoutRef(strings.TrimPrefix(previousRef, "refs/heads/"), types.CheckoutRefOptions{})
}

return self.CheckoutRef("-", types.CheckoutRefOptions{})
}

func (self *RefsHelper) GetCheckedOutRef() *models.Branch {
if len(self.c.Model().Branches) == 0 {
return nil
Expand Down