mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-28 16:02:01 +03:00
support detached heads when showing the selected branch
This commit is contained in:
@ -6,6 +6,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -21,6 +22,13 @@ import (
|
|||||||
gogit "gopkg.in/src-d/go-git.v4"
|
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 {
|
func verifyInGitRepo(runCmd func(string) error) error {
|
||||||
return runCmd("git status")
|
return runCmd("git status")
|
||||||
}
|
}
|
||||||
@ -325,8 +333,11 @@ func (c *GitCommand) NewBranch(name string) error {
|
|||||||
// CurrentBranchName is a function.
|
// CurrentBranchName is a function.
|
||||||
func (c *GitCommand) CurrentBranchName() (string, error) {
|
func (c *GitCommand) CurrentBranchName() (string, error) {
|
||||||
branchName, err := c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD")
|
branchName, err := c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD")
|
||||||
if err != nil {
|
if err != nil || branchName == "HEAD\n" {
|
||||||
branchName, err = c.OSCommand.RunCommandWithOutput("git rev-parse --short HEAD")
|
output, err := c.OSCommand.RunCommandWithOutput("git branch --contains")
|
||||||
|
re := regexp.MustCompile(CurrentBranchNameRegex)
|
||||||
|
match := re.FindStringSubmatch(output)
|
||||||
|
branchName = match[1]
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -22,14 +22,15 @@ func (c *GitCommand) GetRemotes() ([]*Remote, error) {
|
|||||||
// first step is to get our remotes from go-git
|
// first step is to get our remotes from go-git
|
||||||
remotes := make([]*Remote, len(goGitRemotes))
|
remotes := make([]*Remote, len(goGitRemotes))
|
||||||
for i, goGitRemote := range 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)
|
matches := re.FindAllStringSubmatch(remoteBranchesStr, -1)
|
||||||
branches := make([]*RemoteBranch, len(matches))
|
branches := make([]*RemoteBranch, len(matches))
|
||||||
for j, match := range matches {
|
for j, match := range matches {
|
||||||
branches[j] = &RemoteBranch{
|
branches[j] = &RemoteBranch{
|
||||||
Name: match[1],
|
Name: match[1],
|
||||||
|
RemoteName: remoteName,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,8 +6,9 @@ import (
|
|||||||
|
|
||||||
// Remote Branch : A git remote branch
|
// Remote Branch : A git remote branch
|
||||||
type RemoteBranch struct {
|
type RemoteBranch struct {
|
||||||
Name string
|
Name string
|
||||||
Selected bool
|
Selected bool
|
||||||
|
RemoteName string
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDisplayStrings returns the display string of branch
|
// GetDisplayStrings returns the display string of branch
|
||||||
|
@ -1057,6 +1057,14 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
|||||||
Handler: gui.handleRemoveRemote,
|
Handler: gui.handleRemoveRemote,
|
||||||
Description: gui.Tr.SLocalize("removeRemote"),
|
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",
|
ViewName: "commits",
|
||||||
Key: gocui.MouseLeft,
|
Key: gocui.MouseLeft,
|
||||||
|
@ -81,3 +81,14 @@ func (gui *Gui) renderRemoteBranchesWithSelection() error {
|
|||||||
|
|
||||||
return nil
|
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")
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user