diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2019-11-17 12:07:36 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2019-11-21 22:07:14 +1100 |
commit | 55ff0c0dee09b505ecd123f3f893e143651947bf (patch) | |
tree | e9df2f62c6ce3e49a80517d65cd07207c7f8edb4 | |
parent | 6b7aaeca45847ebc41aa0fd9b773362d4a79f1ab (diff) |
support detached heads when showing the selected branch
-rw-r--r-- | pkg/commands/git.go | 15 | ||||
-rw-r--r-- | pkg/commands/loading_remotes.go | 7 | ||||
-rw-r--r-- | pkg/commands/remote_branch.go | 5 | ||||
-rw-r--r-- | pkg/gui/keybindings.go | 8 | ||||
-rw-r--r-- | pkg/gui/remote_branches_panel.go | 11 |
5 files changed, 39 insertions, 7 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 26f8aae32..65e17a3a1 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -6,6 +6,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "strings" "time" @@ -21,6 +22,13 @@ import ( gogit "gopkg.in/src-d/go-git.v4" ) +// this takes something like: +// * (HEAD detached at 264fc6f5) +// remotes +// and returns '264fc6f5' as the second match + +const CurrentBranchNameRegex = `(?m)^\*.*?([^ ]*?)\)?$` + func verifyInGitRepo(runCmd func(string) error) error { return runCmd("git status") } @@ -325,8 +333,11 @@ func (c *GitCommand) NewBranch(name string) error { // CurrentBranchName is a function. func (c *GitCommand) CurrentBranchName() (string, error) { branchName, err := c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD") - if err != nil { - branchName, err = c.OSCommand.RunCommandWithOutput("git rev-parse --short HEAD") + if err != nil || branchName == "HEAD\n" { + output, err := c.OSCommand.RunCommandWithOutput("git branch --contains") + re := regexp.MustCompile(CurrentBranchNameRegex) + match := re.FindStringSubmatch(output) + branchName = match[1] if err != nil { return "", err } diff --git a/pkg/commands/loading_remotes.go b/pkg/commands/loading_remotes.go index dc5dcac44..c33eb1d37 100644 --- a/pkg/commands/loading_remotes.go +++ b/pkg/commands/loading_remotes.go @@ -22,14 +22,15 @@ func (c *GitCommand) GetRemotes() ([]*Remote, error) { // first step is to get our remotes from go-git remotes := make([]*Remote, len(goGitRemotes)) for i, goGitRemote := range goGitRemotes { - name := goGitRemote.Config().Name + remoteName := goGitRemote.Config().Name - re := regexp.MustCompile(fmt.Sprintf("%s\\/(.*)", name)) + re := regexp.MustCompile(fmt.Sprintf("%s\\/(.*)", remoteName)) matches := re.FindAllStringSubmatch(remoteBranchesStr, -1) branches := make([]*RemoteBranch, len(matches)) for j, match := range matches { branches[j] = &RemoteBranch{ - Name: match[1], + Name: match[1], + RemoteName: remoteName, } } diff --git a/pkg/commands/remote_branch.go b/pkg/commands/remote_branch.go index 18e58fa85..5016dc5bd 100644 --- a/pkg/commands/remote_branch.go +++ b/pkg/commands/remote_branch.go @@ -6,8 +6,9 @@ import ( // Remote Branch : A git remote branch type RemoteBranch struct { - Name string - Selected bool + Name string + Selected bool + RemoteName string } // GetDisplayStrings returns the display string of branch diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index eb817e76d..b74281df7 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -1058,6 +1058,14 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Description: gui.Tr.SLocalize("removeRemote"), }, { + ViewName: "branches", + Contexts: []string{"remote-branches"}, + Key: gocui.KeySpace, + Modifier: gocui.ModNone, + Handler: gui.handleCheckoutRemoteBranch, + Description: gui.Tr.SLocalize("checkout"), + }, + { ViewName: "commits", Key: gocui.MouseLeft, Modifier: gocui.ModNone, diff --git a/pkg/gui/remote_branches_panel.go b/pkg/gui/remote_branches_panel.go index f47ea9f63..771a7a9ad 100644 --- a/pkg/gui/remote_branches_panel.go +++ b/pkg/gui/remote_branches_panel.go @@ -81,3 +81,14 @@ func (gui *Gui) renderRemoteBranchesWithSelection() error { return nil } + +func (gui *Gui) handleCheckoutRemoteBranch(g *gocui.Gui, v *gocui.View) error { + remoteBranch := gui.getSelectedRemoteBranch() + if remoteBranch == nil { + return nil + } + if err := gui.handleCheckoutBranch(remoteBranch.RemoteName + "/" + remoteBranch.Name); err != nil { + return err + } + return gui.switchBranchesPanelContext("local-branches") +} |